ua_securitypolicy_basic256sha256.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2018 (c) Mark Giraud, Fraunhofer IOSB
  6. * Copyright 2018 (c) Daniel Feist, Precitec GmbH & Co. KG
  7. */
  8. #include <mbedtls/aes.h>
  9. #include <mbedtls/md.h>
  10. #include <mbedtls/sha256.h>
  11. #include <mbedtls/x509_crt.h>
  12. #include <mbedtls/ctr_drbg.h>
  13. #include <mbedtls/entropy.h>
  14. #include <mbedtls/entropy_poll.h>
  15. #include <mbedtls/error.h>
  16. #include <mbedtls/version.h>
  17. #include <mbedtls/sha1.h>
  18. #include "ua_plugin_pki.h"
  19. #include "ua_plugin_securitypolicy.h"
  20. #include "ua_securitypolicy_basic256sha256.h"
  21. #include "ua_types.h"
  22. #include "ua_types_generated_handling.h"
  23. /* Notes:
  24. * mbedTLS' AES allows in-place encryption and decryption. Sow we don't have to
  25. * allocate temp buffers.
  26. * https://tls.mbed.org/discussions/generic/in-place-decryption-with-aes256-same-input-output-buffer
  27. */
  28. #define UA_SECURITYPOLICY_BASIC256SHA256_RSAPADDING_LEN 42
  29. #define UA_SHA1_LENGTH 20
  30. #define UA_SHA256_LENGTH 32
  31. #define UA_BASIC256SHA256_SYM_SIGNING_KEY_LENGTH 32
  32. #define UA_SECURITYPOLICY_BASIC256SHA256_SYM_KEY_LENGTH 32
  33. #define UA_SECURITYPOLICY_BASIC256SHA256_SYM_ENCRYPTION_BLOCK_SIZE 16
  34. #define UA_SECURITYPOLICY_BASIC256SHA256_SYM_PLAIN_TEXT_BLOCK_SIZE 16
  35. #define UA_SECURITYPOLICY_BASIC256SHA256_MINASYMKEYLENGTH 256
  36. #define UA_SECURITYPOLICY_BASIC256SHA256_MAXASYMKEYLENGTH 512
  37. #define UA_LOG_MBEDERR \
  38. char errBuff[300]; \
  39. mbedtls_strerror(mbedErr, errBuff, 300); \
  40. UA_LOG_WARNING(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY, \
  41. "mbedTLS returned an error: %s", errBuff); \
  42. #define UA_MBEDTLS_ERRORHANDLING(errorcode) \
  43. if(mbedErr) { \
  44. UA_LOG_MBEDERR \
  45. retval = errorcode; \
  46. }
  47. #define UA_MBEDTLS_ERRORHANDLING_RETURN(errorcode) \
  48. if(mbedErr) { \
  49. UA_LOG_MBEDERR \
  50. return errorcode; \
  51. }
  52. typedef struct {
  53. const UA_SecurityPolicy *securityPolicy;
  54. UA_ByteString localCertThumbprint;
  55. mbedtls_ctr_drbg_context drbgContext;
  56. mbedtls_entropy_context entropyContext;
  57. mbedtls_md_context_t sha256MdContext;
  58. mbedtls_pk_context localPrivateKey;
  59. } Basic256Sha256_PolicyContext;
  60. typedef struct {
  61. Basic256Sha256_PolicyContext *policyContext;
  62. UA_ByteString localSymSigningKey;
  63. UA_ByteString localSymEncryptingKey;
  64. UA_ByteString localSymIv;
  65. UA_ByteString remoteSymSigningKey;
  66. UA_ByteString remoteSymEncryptingKey;
  67. UA_ByteString remoteSymIv;
  68. mbedtls_x509_crt remoteCertificate;
  69. } Basic256Sha256_ChannelContext;
  70. /********************/
  71. /* AsymmetricModule */
  72. /********************/
  73. /* VERIFY AsymmetricSignatureAlgorithm_RSA-PKCS15-SHA2-256 */
  74. static UA_StatusCode
  75. asym_verify_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  76. Basic256Sha256_ChannelContext *cc,
  77. const UA_ByteString *message,
  78. const UA_ByteString *signature) {
  79. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  80. return UA_STATUSCODE_BADINTERNALERROR;
  81. unsigned char hash[UA_SHA256_LENGTH];
  82. #if MBEDTLS_VERSION_NUMBER >= 0x02070000
  83. // TODO check return status
  84. mbedtls_sha256_ret(message->data, message->length, hash, 0);
  85. #else
  86. mbedtls_sha256(message->data, message->length, hash, 0);
  87. #endif
  88. /* Set the RSA settings */
  89. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  90. mbedtls_rsa_set_padding(rsaContext, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
  91. /* For RSA keys, the default padding type is PKCS#1 v1.5 in mbedtls_pk_verify() */
  92. /* Alternatively, use more specific function mbedtls_rsa_rsassa_pkcs1_v15_verify(), i.e. */
  93. /* int mbedErr = mbedtls_rsa_rsassa_pkcs1_v15_verify(rsaContext, NULL, NULL,
  94. MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA256,
  95. UA_SHA256_LENGTH, hash,
  96. signature->data); */
  97. int mbedErr = mbedtls_pk_verify(&cc->remoteCertificate.pk,
  98. MBEDTLS_MD_SHA256, hash, UA_SHA256_LENGTH,
  99. signature->data, signature->length);
  100. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  101. return UA_STATUSCODE_GOOD;
  102. }
  103. /* AsymmetricSignatureAlgorithm_RSA-PKCS15-SHA2-256 */
  104. static UA_StatusCode
  105. asym_sign_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  106. Basic256Sha256_ChannelContext *cc,
  107. const UA_ByteString *message,
  108. UA_ByteString *signature) {
  109. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  110. return UA_STATUSCODE_BADINTERNALERROR;
  111. unsigned char hash[UA_SHA256_LENGTH];
  112. #if MBEDTLS_VERSION_NUMBER >= 0x02070000
  113. // TODO check return status
  114. mbedtls_sha256_ret(message->data, message->length, hash, 0);
  115. #else
  116. mbedtls_sha256(message->data, message->length, hash, 0);
  117. #endif
  118. Basic256Sha256_PolicyContext *pc = cc->policyContext;
  119. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(pc->localPrivateKey);
  120. mbedtls_rsa_set_padding(rsaContext, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
  121. size_t sigLen = 0;
  122. /* For RSA keys, the default padding type is PKCS#1 v1.5 in mbedtls_pk_sign */
  123. /* Alternatively use more specific function mbedtls_rsa_rsassa_pkcs1_v15_sign() */
  124. int mbedErr = mbedtls_pk_sign(&pc->localPrivateKey,
  125. MBEDTLS_MD_SHA256, hash,
  126. UA_SHA256_LENGTH, signature->data,
  127. &sigLen, mbedtls_ctr_drbg_random,
  128. &pc->drbgContext);
  129. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADINTERNALERROR);
  130. return UA_STATUSCODE_GOOD;
  131. }
  132. static size_t
  133. asym_getLocalSignatureSize_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  134. const Basic256Sha256_ChannelContext *cc) {
  135. if(securityPolicy == NULL || cc == NULL)
  136. return 0;
  137. return mbedtls_pk_rsa(cc->policyContext->localPrivateKey)->len;
  138. }
  139. static size_t
  140. asym_getRemoteSignatureSize_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  141. const Basic256Sha256_ChannelContext *cc) {
  142. if(securityPolicy == NULL || cc == NULL)
  143. return 0;
  144. return mbedtls_pk_rsa(cc->remoteCertificate.pk)->len;
  145. }
  146. /* AsymmetricEncryptionAlgorithm_RSA-OAEP-SHA1 */
  147. static UA_StatusCode
  148. asym_encrypt_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  149. Basic256Sha256_ChannelContext *cc,
  150. UA_ByteString *data) {
  151. if(securityPolicy == NULL || cc == NULL || data == NULL)
  152. return UA_STATUSCODE_BADINTERNALERROR;
  153. const size_t plainTextBlockSize = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
  154. getRemotePlainTextBlockSize(securityPolicy, cc);
  155. if(data->length % plainTextBlockSize != 0)
  156. return UA_STATUSCODE_BADINTERNALERROR;
  157. mbedtls_rsa_context *remoteRsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  158. mbedtls_rsa_set_padding(remoteRsaContext, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1);
  159. UA_ByteString encrypted;
  160. const size_t bufferOverhead =
  161. UA_SecurityPolicy_getRemoteAsymEncryptionBufferLengthOverhead(securityPolicy, cc, data->length);
  162. UA_StatusCode retval = UA_ByteString_allocBuffer(&encrypted, data->length + bufferOverhead);
  163. if(retval != UA_STATUSCODE_GOOD)
  164. return retval;
  165. size_t lenDataToEncrypt = data->length;
  166. size_t inOffset = 0;
  167. size_t offset = 0;
  168. size_t outLength = 0;
  169. const unsigned char *label = NULL;
  170. Basic256Sha256_PolicyContext *pc = cc->policyContext;
  171. while(lenDataToEncrypt >= plainTextBlockSize) {
  172. int mbedErr = mbedtls_rsa_rsaes_oaep_encrypt(remoteRsaContext, mbedtls_ctr_drbg_random,
  173. &pc->drbgContext, MBEDTLS_RSA_PUBLIC,
  174. label, 0, plainTextBlockSize,
  175. data->data + inOffset, encrypted.data + offset);
  176. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADINTERNALERROR);
  177. if(retval != UA_STATUSCODE_GOOD) {
  178. UA_ByteString_deleteMembers(&encrypted);
  179. return retval;
  180. }
  181. outLength += remoteRsaContext->len;
  182. inOffset += plainTextBlockSize;
  183. offset += outLength;
  184. lenDataToEncrypt -= plainTextBlockSize;
  185. }
  186. memcpy(data->data, encrypted.data, offset);
  187. UA_ByteString_deleteMembers(&encrypted);
  188. return UA_STATUSCODE_GOOD;
  189. }
  190. /* AsymmetricEncryptionAlgorithm_RSA-OAEP-SHA1 */
  191. static UA_StatusCode
  192. asym_decrypt_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  193. Basic256Sha256_ChannelContext *cc,
  194. UA_ByteString *data) {
  195. if(securityPolicy == NULL || cc == NULL || data == NULL)
  196. return UA_STATUSCODE_BADINTERNALERROR;
  197. mbedtls_rsa_context *rsaContext =
  198. mbedtls_pk_rsa(cc->policyContext->localPrivateKey);
  199. mbedtls_rsa_set_padding(rsaContext, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1);
  200. if(data->length % rsaContext->len != 0)
  201. return UA_STATUSCODE_BADINTERNALERROR;
  202. UA_ByteString decrypted;
  203. UA_StatusCode retval = UA_ByteString_allocBuffer(&decrypted, data->length);
  204. if(retval != UA_STATUSCODE_GOOD)
  205. return retval;
  206. size_t lenDataToDecrypt = data->length;
  207. size_t inOffset = 0;
  208. size_t offset = 0;
  209. size_t outLength = 0;
  210. const unsigned char *label = NULL;
  211. Basic256Sha256_PolicyContext *pc = cc->policyContext;
  212. while(lenDataToDecrypt >= rsaContext->len) {
  213. int mbedErr = mbedtls_rsa_rsaes_oaep_decrypt(rsaContext, mbedtls_ctr_drbg_random,
  214. &pc->drbgContext, MBEDTLS_RSA_PRIVATE,
  215. label, 0, &outLength,
  216. data->data + inOffset,
  217. decrypted.data + offset,
  218. decrypted.length - offset);
  219. if(mbedErr)
  220. UA_ByteString_deleteMembers(&decrypted); // TODO: Maybe change error macro to jump to cleanup?
  221. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  222. inOffset += rsaContext->len;
  223. offset += outLength;
  224. lenDataToDecrypt -= rsaContext->len;
  225. }
  226. if(lenDataToDecrypt == 0) {
  227. memcpy(data->data, decrypted.data, offset);
  228. data->length = offset;
  229. } else {
  230. retval = UA_STATUSCODE_BADINTERNALERROR;
  231. }
  232. UA_ByteString_deleteMembers(&decrypted);
  233. return retval;
  234. }
  235. static size_t
  236. asym_getRemoteEncryptionKeyLength_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  237. const Basic256Sha256_ChannelContext *cc) {
  238. return mbedtls_pk_get_len(&cc->remoteCertificate.pk) * 8;
  239. }
  240. static size_t
  241. asym_getRemoteBlockSize_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  242. const Basic256Sha256_ChannelContext *cc) {
  243. mbedtls_rsa_context *const rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  244. return rsaContext->len;
  245. }
  246. static size_t
  247. asym_getRemotePlainTextBlockSize_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  248. const Basic256Sha256_ChannelContext *cc) {
  249. mbedtls_rsa_context *const rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  250. return rsaContext->len - UA_SECURITYPOLICY_BASIC256SHA256_RSAPADDING_LEN;
  251. }
  252. static UA_StatusCode
  253. asym_makeThumbprint_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  254. const UA_ByteString *certificate,
  255. UA_ByteString *thumbprint) {
  256. if(securityPolicy == NULL || certificate == NULL || thumbprint == NULL)
  257. return UA_STATUSCODE_BADINTERNALERROR;
  258. if(UA_ByteString_equal(certificate, &UA_BYTESTRING_NULL))
  259. return UA_STATUSCODE_BADINTERNALERROR;
  260. if(thumbprint->length != UA_SHA1_LENGTH)
  261. return UA_STATUSCODE_BADINTERNALERROR;
  262. /* The certificate thumbprint is always a 20 bit sha1 hash, see Part 4 of the Specification. */
  263. #if MBEDTLS_VERSION_NUMBER >= 0x02070000
  264. mbedtls_sha1_ret(certificate->data, certificate->length, thumbprint->data);
  265. #else
  266. mbedtls_sha1(certificate->data, certificate->length, thumbprint->data);
  267. #endif
  268. return UA_STATUSCODE_GOOD;
  269. }
  270. static UA_StatusCode
  271. asymmetricModule_compareCertificateThumbprint_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  272. const UA_ByteString *certificateThumbprint) {
  273. if(securityPolicy == NULL || certificateThumbprint == NULL)
  274. return UA_STATUSCODE_BADINTERNALERROR;
  275. Basic256Sha256_PolicyContext *pc = (Basic256Sha256_PolicyContext *)securityPolicy->policyContext;
  276. if(!UA_ByteString_equal(certificateThumbprint, &pc->localCertThumbprint))
  277. return UA_STATUSCODE_BADCERTIFICATEINVALID;
  278. return UA_STATUSCODE_GOOD;
  279. }
  280. /*******************/
  281. /* SymmetricModule */
  282. /*******************/
  283. static void
  284. md_hmac_Basic256Sha256(mbedtls_md_context_t *context, const UA_ByteString *key,
  285. const UA_ByteString *in, unsigned char out[32]) {
  286. mbedtls_md_hmac_starts(context, key->data, key->length);
  287. mbedtls_md_hmac_update(context, in->data, in->length);
  288. mbedtls_md_hmac_finish(context, out);
  289. }
  290. static UA_StatusCode
  291. sym_verify_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  292. Basic256Sha256_ChannelContext *cc,
  293. const UA_ByteString *message,
  294. const UA_ByteString *signature) {
  295. if(securityPolicy == NULL || cc == NULL || message == NULL || signature == NULL)
  296. return UA_STATUSCODE_BADINTERNALERROR;
  297. /* Compute MAC */
  298. if(signature->length != UA_SHA256_LENGTH) {
  299. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  300. "Signature size does not have the desired size defined by the security policy");
  301. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  302. }
  303. Basic256Sha256_PolicyContext *pc =
  304. (Basic256Sha256_PolicyContext *)securityPolicy->policyContext;
  305. unsigned char mac[UA_SHA256_LENGTH];
  306. md_hmac_Basic256Sha256(&pc->sha256MdContext, &cc->remoteSymSigningKey, message, mac);
  307. /* Compare with Signature */
  308. if(memcmp(signature->data, mac, UA_SHA256_LENGTH) != 0)
  309. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  310. return UA_STATUSCODE_GOOD;
  311. }
  312. static UA_StatusCode
  313. sym_sign_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  314. const Basic256Sha256_ChannelContext *cc,
  315. const UA_ByteString *message,
  316. UA_ByteString *signature) {
  317. if(signature->length != UA_SHA256_LENGTH)
  318. return UA_STATUSCODE_BADINTERNALERROR;
  319. md_hmac_Basic256Sha256(&cc->policyContext->sha256MdContext, &cc->localSymSigningKey,
  320. message, signature->data);
  321. return UA_STATUSCODE_GOOD;
  322. }
  323. static size_t
  324. sym_getSignatureSize_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  325. const void *channelContext) {
  326. return UA_SHA256_LENGTH;
  327. }
  328. static size_t
  329. sym_getSigningKeyLength_sp_basic256sha256(const UA_SecurityPolicy *const securityPolicy,
  330. const void *const channelContext) {
  331. return UA_BASIC256SHA256_SYM_SIGNING_KEY_LENGTH;
  332. }
  333. static size_t
  334. sym_getEncryptionKeyLength_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  335. const void *channelContext) {
  336. return UA_SECURITYPOLICY_BASIC256SHA256_SYM_KEY_LENGTH;
  337. }
  338. static size_t
  339. sym_getEncryptionBlockSize_sp_basic256sha256(const UA_SecurityPolicy *const securityPolicy,
  340. const void *const channelContext) {
  341. return UA_SECURITYPOLICY_BASIC256SHA256_SYM_ENCRYPTION_BLOCK_SIZE;
  342. }
  343. static size_t
  344. sym_getPlainTextBlockSize_sp_basic256sha256(const UA_SecurityPolicy *const securityPolicy,
  345. const void *const channelContext) {
  346. return UA_SECURITYPOLICY_BASIC256SHA256_SYM_PLAIN_TEXT_BLOCK_SIZE;
  347. }
  348. static UA_StatusCode
  349. sym_encrypt_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  350. const Basic256Sha256_ChannelContext *cc,
  351. UA_ByteString *data) {
  352. if(securityPolicy == NULL || cc == NULL || data == NULL)
  353. return UA_STATUSCODE_BADINTERNALERROR;
  354. if(cc->localSymIv.length !=
  355. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalBlockSize(securityPolicy, cc))
  356. return UA_STATUSCODE_BADINTERNALERROR;
  357. size_t plainTextBlockSize =
  358. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalPlainTextBlockSize(securityPolicy, cc);
  359. if(data->length % plainTextBlockSize != 0) {
  360. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  361. "Length of data to encrypt is not a multiple of the plain text block size."
  362. "Padding might not have been calculated appropriately.");
  363. return UA_STATUSCODE_BADINTERNALERROR;
  364. }
  365. /* Keylength in bits */
  366. unsigned int keylength = (unsigned int)(cc->localSymEncryptingKey.length * 8);
  367. mbedtls_aes_context aesContext;
  368. int mbedErr = mbedtls_aes_setkey_enc(&aesContext, cc->localSymEncryptingKey.data, keylength);
  369. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADINTERNALERROR);
  370. UA_ByteString ivCopy;
  371. UA_StatusCode retval = UA_ByteString_copy(&cc->localSymIv, &ivCopy);
  372. if(retval != UA_STATUSCODE_GOOD)
  373. return retval;
  374. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_ENCRYPT, data->length,
  375. ivCopy.data, data->data, data->data);
  376. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADINTERNALERROR);
  377. UA_ByteString_deleteMembers(&ivCopy);
  378. return retval;
  379. }
  380. static UA_StatusCode
  381. sym_decrypt_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  382. const Basic256Sha256_ChannelContext *cc,
  383. UA_ByteString *data) {
  384. if(securityPolicy == NULL || cc == NULL || data == NULL)
  385. return UA_STATUSCODE_BADINTERNALERROR;
  386. size_t encryptionBlockSize =
  387. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalBlockSize(securityPolicy, cc);
  388. if(cc->remoteSymIv.length != encryptionBlockSize)
  389. return UA_STATUSCODE_BADINTERNALERROR;
  390. if(data->length % encryptionBlockSize != 0) {
  391. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  392. "Length of data to decrypt is not a multiple of the encryptingBlock size.");
  393. return UA_STATUSCODE_BADINTERNALERROR;
  394. }
  395. unsigned int keylength = (unsigned int)(cc->remoteSymEncryptingKey.length * 8);
  396. mbedtls_aes_context aesContext;
  397. int mbedErr = mbedtls_aes_setkey_dec(&aesContext, cc->remoteSymEncryptingKey.data, keylength);
  398. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADINTERNALERROR);
  399. UA_ByteString ivCopy;
  400. UA_StatusCode retval = UA_ByteString_copy(&cc->remoteSymIv, &ivCopy);
  401. if(retval != UA_STATUSCODE_GOOD)
  402. return retval;
  403. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_DECRYPT, data->length,
  404. ivCopy.data, data->data, data->data);
  405. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADINTERNALERROR);
  406. UA_ByteString_deleteMembers(&ivCopy);
  407. return retval;
  408. }
  409. static void
  410. swapBuffers_Basic256Sha256(UA_ByteString *const bufA, UA_ByteString *const bufB) {
  411. UA_ByteString tmp = *bufA;
  412. *bufA = *bufB;
  413. *bufB = tmp;
  414. }
  415. static UA_StatusCode
  416. sym_generateKey_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  417. const UA_ByteString *secret, const UA_ByteString *seed,
  418. UA_ByteString *out) {
  419. if(securityPolicy == NULL || secret == NULL || seed == NULL || out == NULL)
  420. return UA_STATUSCODE_BADINTERNALERROR;
  421. Basic256Sha256_PolicyContext *pc =
  422. (Basic256Sha256_PolicyContext *)securityPolicy->policyContext;
  423. size_t hashLen = 0;
  424. const mbedtls_md_info_t *mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
  425. hashLen = (size_t)mbedtls_md_get_size(mdInfo);
  426. UA_ByteString A_and_seed;
  427. UA_ByteString_allocBuffer(&A_and_seed, hashLen + seed->length);
  428. memcpy(A_and_seed.data + hashLen, seed->data, seed->length);
  429. UA_ByteString ANext_and_seed;
  430. UA_ByteString_allocBuffer(&ANext_and_seed, hashLen + seed->length);
  431. memcpy(ANext_and_seed.data + hashLen, seed->data, seed->length);
  432. UA_ByteString A = {
  433. hashLen,
  434. A_and_seed.data
  435. };
  436. UA_ByteString ANext = {
  437. hashLen,
  438. ANext_and_seed.data
  439. };
  440. md_hmac_Basic256Sha256(&pc->sha256MdContext, secret, seed, A.data);
  441. UA_StatusCode retval = 0;
  442. for(size_t offset = 0; offset < out->length; offset += hashLen) {
  443. UA_ByteString outSegment = {
  444. hashLen,
  445. out->data + offset
  446. };
  447. UA_Boolean bufferAllocated = UA_FALSE;
  448. // Not enough room in out buffer to write the hash.
  449. if(offset + hashLen > out->length) {
  450. outSegment.data = NULL;
  451. outSegment.length = 0;
  452. retval |= UA_ByteString_allocBuffer(&outSegment, hashLen);
  453. if(retval != UA_STATUSCODE_GOOD) {
  454. UA_ByteString_deleteMembers(&A_and_seed);
  455. UA_ByteString_deleteMembers(&ANext_and_seed);
  456. return retval;
  457. }
  458. bufferAllocated = UA_TRUE;
  459. }
  460. md_hmac_Basic256Sha256(&pc->sha256MdContext, secret, &A_and_seed, outSegment.data);
  461. md_hmac_Basic256Sha256(&pc->sha256MdContext, secret, &A, ANext.data);
  462. if(retval != UA_STATUSCODE_GOOD) {
  463. if(bufferAllocated)
  464. UA_ByteString_deleteMembers(&outSegment);
  465. UA_ByteString_deleteMembers(&A_and_seed);
  466. UA_ByteString_deleteMembers(&ANext_and_seed);
  467. return retval;
  468. }
  469. if(bufferAllocated) {
  470. memcpy(out->data + offset, outSegment.data, out->length - offset);
  471. UA_ByteString_deleteMembers(&outSegment);
  472. }
  473. swapBuffers_Basic256Sha256(&ANext_and_seed, &A_and_seed);
  474. swapBuffers_Basic256Sha256(&ANext, &A);
  475. }
  476. UA_ByteString_deleteMembers(&A_and_seed);
  477. UA_ByteString_deleteMembers(&ANext_and_seed);
  478. return UA_STATUSCODE_GOOD;
  479. }
  480. static UA_StatusCode
  481. sym_generateNonce_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  482. UA_ByteString *out) {
  483. if(securityPolicy == NULL || securityPolicy->policyContext == NULL || out == NULL)
  484. return UA_STATUSCODE_BADINTERNALERROR;
  485. Basic256Sha256_PolicyContext *data =
  486. (Basic256Sha256_PolicyContext *)securityPolicy->policyContext;
  487. int mbedErr = mbedtls_ctr_drbg_random(&data->drbgContext, out->data, out->length);
  488. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADUNEXPECTEDERROR);
  489. return UA_STATUSCODE_GOOD;
  490. }
  491. /*****************/
  492. /* ChannelModule */
  493. /*****************/
  494. /* Assumes that the certificate has been verified externally */
  495. static UA_StatusCode
  496. parseRemoteCertificate_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  497. const UA_ByteString *remoteCertificate) {
  498. if(remoteCertificate == NULL || cc == NULL)
  499. return UA_STATUSCODE_BADINTERNALERROR;
  500. const UA_SecurityPolicy *securityPolicy = cc->policyContext->securityPolicy;
  501. /* Parse the certificate */
  502. int mbedErr = mbedtls_x509_crt_parse(&cc->remoteCertificate, remoteCertificate->data,
  503. remoteCertificate->length);
  504. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  505. /* Check the key length */
  506. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  507. if(rsaContext->len < UA_SECURITYPOLICY_BASIC256SHA256_MINASYMKEYLENGTH ||
  508. rsaContext->len > UA_SECURITYPOLICY_BASIC256SHA256_MAXASYMKEYLENGTH)
  509. return UA_STATUSCODE_BADCERTIFICATEUSENOTALLOWED;
  510. return UA_STATUSCODE_GOOD;
  511. }
  512. static void
  513. channelContext_deleteContext_sp_basic256sha256(Basic256Sha256_ChannelContext *cc) {
  514. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  515. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  516. UA_ByteString_deleteMembers(&cc->localSymIv);
  517. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  518. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  519. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  520. mbedtls_x509_crt_free(&cc->remoteCertificate);
  521. UA_free(cc);
  522. }
  523. static UA_StatusCode
  524. channelContext_newContext_sp_basic256sha256(const UA_SecurityPolicy *securityPolicy,
  525. const UA_ByteString *remoteCertificate,
  526. void **pp_contextData) {
  527. if(securityPolicy == NULL || remoteCertificate == NULL || pp_contextData == NULL)
  528. return UA_STATUSCODE_BADINTERNALERROR;
  529. /* Allocate the channel context */
  530. *pp_contextData = UA_malloc(sizeof(Basic256Sha256_ChannelContext));
  531. if(*pp_contextData == NULL)
  532. return UA_STATUSCODE_BADOUTOFMEMORY;
  533. Basic256Sha256_ChannelContext *cc = (Basic256Sha256_ChannelContext *)*pp_contextData;
  534. /* Initialize the channel context */
  535. cc->policyContext = (Basic256Sha256_PolicyContext *)securityPolicy->policyContext;
  536. UA_ByteString_init(&cc->localSymSigningKey);
  537. UA_ByteString_init(&cc->localSymEncryptingKey);
  538. UA_ByteString_init(&cc->localSymIv);
  539. UA_ByteString_init(&cc->remoteSymSigningKey);
  540. UA_ByteString_init(&cc->remoteSymEncryptingKey);
  541. UA_ByteString_init(&cc->remoteSymIv);
  542. mbedtls_x509_crt_init(&cc->remoteCertificate);
  543. // TODO: this can be optimized so that we dont allocate memory before parsing the certificate
  544. UA_StatusCode retval = parseRemoteCertificate_sp_basic256sha256(cc, remoteCertificate);
  545. if(retval != UA_STATUSCODE_GOOD) {
  546. channelContext_deleteContext_sp_basic256sha256(cc);
  547. *pp_contextData = NULL;
  548. }
  549. return retval;
  550. }
  551. static UA_StatusCode
  552. channelContext_setLocalSymEncryptingKey_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  553. const UA_ByteString *key) {
  554. if(key == NULL || cc == NULL)
  555. return UA_STATUSCODE_BADINTERNALERROR;
  556. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  557. return UA_ByteString_copy(key, &cc->localSymEncryptingKey);
  558. }
  559. static UA_StatusCode
  560. channelContext_setLocalSymSigningKey_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  561. const UA_ByteString *key) {
  562. if(key == NULL || cc == NULL)
  563. return UA_STATUSCODE_BADINTERNALERROR;
  564. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  565. return UA_ByteString_copy(key, &cc->localSymSigningKey);
  566. }
  567. static UA_StatusCode
  568. channelContext_setLocalSymIv_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  569. const UA_ByteString *iv) {
  570. if(iv == NULL || cc == NULL)
  571. return UA_STATUSCODE_BADINTERNALERROR;
  572. UA_ByteString_deleteMembers(&cc->localSymIv);
  573. return UA_ByteString_copy(iv, &cc->localSymIv);
  574. }
  575. static UA_StatusCode
  576. channelContext_setRemoteSymEncryptingKey_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  577. const UA_ByteString *key) {
  578. if(key == NULL || cc == NULL)
  579. return UA_STATUSCODE_BADINTERNALERROR;
  580. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  581. return UA_ByteString_copy(key, &cc->remoteSymEncryptingKey);
  582. }
  583. static UA_StatusCode
  584. channelContext_setRemoteSymSigningKey_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  585. const UA_ByteString *key) {
  586. if(key == NULL || cc == NULL)
  587. return UA_STATUSCODE_BADINTERNALERROR;
  588. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  589. return UA_ByteString_copy(key, &cc->remoteSymSigningKey);
  590. }
  591. static UA_StatusCode
  592. channelContext_setRemoteSymIv_sp_basic256sha256(Basic256Sha256_ChannelContext *cc,
  593. const UA_ByteString *iv) {
  594. if(iv == NULL || cc == NULL)
  595. return UA_STATUSCODE_BADINTERNALERROR;
  596. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  597. return UA_ByteString_copy(iv, &cc->remoteSymIv);
  598. }
  599. static UA_StatusCode
  600. channelContext_compareCertificate_sp_basic256sha256(const Basic256Sha256_ChannelContext *cc,
  601. const UA_ByteString *certificate) {
  602. if(cc == NULL || certificate == NULL)
  603. return UA_STATUSCODE_BADINTERNALERROR;
  604. const UA_SecurityPolicy *securityPolicy = cc->policyContext->securityPolicy;
  605. mbedtls_x509_crt cert;
  606. int mbedErr = mbedtls_x509_crt_parse(&cert, certificate->data, certificate->length);
  607. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  608. if(cert.raw.len != cc->remoteCertificate.raw.len)
  609. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  610. if(memcmp(cert.raw.p, cc->remoteCertificate.raw.p, cert.raw.len) != 0)
  611. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  612. return UA_STATUSCODE_GOOD;
  613. }
  614. static void
  615. deleteMembers_sp_basic256sha256(UA_SecurityPolicy *securityPolicy) {
  616. if(securityPolicy == NULL)
  617. return;
  618. if(securityPolicy->policyContext == NULL)
  619. return;
  620. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  621. /* delete all allocated members in the context */
  622. Basic256Sha256_PolicyContext *pc = (Basic256Sha256_PolicyContext *)
  623. securityPolicy->policyContext;
  624. mbedtls_ctr_drbg_free(&pc->drbgContext);
  625. mbedtls_entropy_free(&pc->entropyContext);
  626. mbedtls_pk_free(&pc->localPrivateKey);
  627. mbedtls_md_free(&pc->sha256MdContext);
  628. UA_ByteString_deleteMembers(&pc->localCertThumbprint);
  629. UA_LOG_DEBUG(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  630. "Deleted members of EndpointContext for sp_basic256sha256");
  631. UA_free(pc);
  632. securityPolicy->policyContext = NULL;
  633. }
  634. static UA_StatusCode
  635. policyContext_newContext_sp_basic256sha256(UA_SecurityPolicy *securityPolicy,
  636. const UA_ByteString localPrivateKey) {
  637. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  638. if(securityPolicy == NULL)
  639. return UA_STATUSCODE_BADINTERNALERROR;
  640. Basic256Sha256_PolicyContext *pc = (Basic256Sha256_PolicyContext *)
  641. UA_malloc(sizeof(Basic256Sha256_PolicyContext));
  642. securityPolicy->policyContext = (void *)pc;
  643. if(!pc) {
  644. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  645. goto error;
  646. }
  647. /* Initialize the PolicyContext */
  648. memset(pc, 0, sizeof(Basic256Sha256_PolicyContext));
  649. mbedtls_ctr_drbg_init(&pc->drbgContext);
  650. mbedtls_entropy_init(&pc->entropyContext);
  651. mbedtls_pk_init(&pc->localPrivateKey);
  652. mbedtls_md_init(&pc->sha256MdContext);
  653. pc->securityPolicy = securityPolicy;
  654. /* Initialized the message digest */
  655. const mbedtls_md_info_t *const mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
  656. int mbedErr = mbedtls_md_setup(&pc->sha256MdContext, mdInfo, MBEDTLS_MD_SHA256);
  657. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADOUTOFMEMORY);
  658. if(retval != UA_STATUSCODE_GOOD)
  659. goto error;
  660. /* Add the system entropy source */
  661. mbedErr = mbedtls_entropy_add_source(&pc->entropyContext,
  662. mbedtls_platform_entropy_poll, NULL, 0,
  663. MBEDTLS_ENTROPY_SOURCE_STRONG);
  664. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  665. if(retval != UA_STATUSCODE_GOOD)
  666. goto error;
  667. /* Seed the RNG */
  668. char *personalization = "open62541-drbg";
  669. mbedErr = mbedtls_ctr_drbg_seed(&pc->drbgContext, mbedtls_entropy_func,
  670. &pc->entropyContext,
  671. (const unsigned char *)personalization, 14);
  672. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  673. if(retval != UA_STATUSCODE_GOOD)
  674. goto error;
  675. /* Set the private key */
  676. mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  677. localPrivateKey.data, localPrivateKey.length,
  678. NULL, 0);
  679. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  680. if(retval != UA_STATUSCODE_GOOD)
  681. goto error;
  682. /* Set the local certificate thumbprint */
  683. retval = UA_ByteString_allocBuffer(&pc->localCertThumbprint, UA_SHA1_LENGTH);
  684. if(retval != UA_STATUSCODE_GOOD)
  685. goto error;
  686. retval = asym_makeThumbprint_sp_basic256sha256(pc->securityPolicy,
  687. &securityPolicy->localCertificate,
  688. &pc->localCertThumbprint);
  689. if(retval != UA_STATUSCODE_GOOD)
  690. goto error;
  691. return UA_STATUSCODE_GOOD;
  692. error:
  693. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  694. "Could not create securityContext");
  695. if(securityPolicy->policyContext != NULL)
  696. deleteMembers_sp_basic256sha256(securityPolicy);
  697. return retval;
  698. }
  699. UA_StatusCode
  700. UA_SecurityPolicy_Basic256Sha256(UA_SecurityPolicy *policy, UA_CertificateVerification *certificateVerification,
  701. const UA_ByteString localCertificate, const UA_ByteString localPrivateKey,
  702. UA_Logger logger) {
  703. memset(policy, 0, sizeof(UA_SecurityPolicy));
  704. policy->logger = logger;
  705. policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  706. UA_SecurityPolicyAsymmetricModule *const asymmetricModule = &policy->asymmetricModule;
  707. UA_SecurityPolicySymmetricModule *const symmetricModule = &policy->symmetricModule;
  708. UA_SecurityPolicyChannelModule *const channelModule = &policy->channelModule;
  709. /* Copy the certificate and add a NULL to the end */
  710. UA_StatusCode retval =
  711. UA_ByteString_allocBuffer(&policy->localCertificate, localCertificate.length + 1);
  712. if(retval != UA_STATUSCODE_GOOD)
  713. return retval;
  714. memcpy(policy->localCertificate.data, localCertificate.data, localCertificate.length);
  715. policy->localCertificate.data[localCertificate.length] = '\0';
  716. policy->localCertificate.length--;
  717. policy->certificateVerification = certificateVerification;
  718. /* AsymmetricModule */
  719. UA_SecurityPolicySignatureAlgorithm *asym_signatureAlgorithm =
  720. &asymmetricModule->cryptoModule.signatureAlgorithm;
  721. asym_signatureAlgorithm->uri =
  722. UA_STRING("http://www.w3.org/2000/09/xmldsig#rsa-sha1\0");
  723. asym_signatureAlgorithm->verify =
  724. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  725. const UA_ByteString *, const UA_ByteString *))asym_verify_sp_basic256sha256;
  726. asym_signatureAlgorithm->sign =
  727. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  728. const UA_ByteString *, UA_ByteString *))asym_sign_sp_basic256sha256;
  729. asym_signatureAlgorithm->getLocalSignatureSize =
  730. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getLocalSignatureSize_sp_basic256sha256;
  731. asym_signatureAlgorithm->getRemoteSignatureSize =
  732. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteSignatureSize_sp_basic256sha256;
  733. asym_signatureAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  734. asym_signatureAlgorithm->getRemoteKeyLength = NULL; // TODO: Write function
  735. UA_SecurityPolicyEncryptionAlgorithm *asym_encryptionAlgorithm =
  736. &asymmetricModule->cryptoModule.encryptionAlgorithm;
  737. asym_encryptionAlgorithm->uri = UA_STRING("TODO: ALG URI");
  738. asym_encryptionAlgorithm->encrypt =
  739. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))asym_encrypt_sp_basic256sha256;
  740. asym_encryptionAlgorithm->decrypt =
  741. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))
  742. asym_decrypt_sp_basic256sha256;
  743. asym_encryptionAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  744. asym_encryptionAlgorithm->getRemoteKeyLength =
  745. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteEncryptionKeyLength_sp_basic256sha256;
  746. asym_encryptionAlgorithm->getLocalBlockSize = NULL; // TODO: Write function
  747. asym_encryptionAlgorithm->getRemoteBlockSize = (size_t (*)(const UA_SecurityPolicy *,
  748. const void *))asym_getRemoteBlockSize_sp_basic256sha256;
  749. asym_encryptionAlgorithm->getLocalPlainTextBlockSize = NULL; // TODO: Write function
  750. asym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  751. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemotePlainTextBlockSize_sp_basic256sha256;
  752. asymmetricModule->makeCertificateThumbprint = asym_makeThumbprint_sp_basic256sha256;
  753. asymmetricModule->compareCertificateThumbprint =
  754. asymmetricModule_compareCertificateThumbprint_sp_basic256sha256;
  755. /* SymmetricModule */
  756. symmetricModule->generateKey = sym_generateKey_sp_basic256sha256;
  757. symmetricModule->generateNonce = sym_generateNonce_sp_basic256sha256;
  758. UA_SecurityPolicySignatureAlgorithm *sym_signatureAlgorithm =
  759. &symmetricModule->cryptoModule.signatureAlgorithm;
  760. sym_signatureAlgorithm->uri =
  761. UA_STRING("http://www.w3.org/2000/09/xmldsig#hmac-sha1\0");
  762. sym_signatureAlgorithm->verify =
  763. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *, const UA_ByteString *,
  764. const UA_ByteString *))sym_verify_sp_basic256sha256;
  765. sym_signatureAlgorithm->sign =
  766. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  767. const UA_ByteString *, UA_ByteString *))sym_sign_sp_basic256sha256;
  768. sym_signatureAlgorithm->getLocalSignatureSize = sym_getSignatureSize_sp_basic256sha256;
  769. sym_signatureAlgorithm->getRemoteSignatureSize = sym_getSignatureSize_sp_basic256sha256;
  770. sym_signatureAlgorithm->getLocalKeyLength =
  771. (size_t (*)(const UA_SecurityPolicy *,
  772. const void *))sym_getSigningKeyLength_sp_basic256sha256;
  773. sym_signatureAlgorithm->getRemoteKeyLength =
  774. (size_t (*)(const UA_SecurityPolicy *,
  775. const void *))sym_getSigningKeyLength_sp_basic256sha256;
  776. UA_SecurityPolicyEncryptionAlgorithm *sym_encryptionAlgorithm =
  777. &symmetricModule->cryptoModule.encryptionAlgorithm;
  778. sym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
  779. sym_encryptionAlgorithm->encrypt =
  780. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_encrypt_sp_basic256sha256;
  781. sym_encryptionAlgorithm->decrypt =
  782. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_decrypt_sp_basic256sha256;
  783. sym_encryptionAlgorithm->getLocalKeyLength = sym_getEncryptionKeyLength_sp_basic256sha256;
  784. sym_encryptionAlgorithm->getRemoteKeyLength = sym_getEncryptionKeyLength_sp_basic256sha256;
  785. sym_encryptionAlgorithm->getLocalBlockSize =
  786. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256sha256;
  787. sym_encryptionAlgorithm->getRemoteBlockSize =
  788. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256sha256;
  789. sym_encryptionAlgorithm->getLocalPlainTextBlockSize =
  790. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256sha256;
  791. sym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  792. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256sha256;
  793. symmetricModule->secureChannelNonceLength = 32;
  794. // Use the same signature algorithm as the asymmetric component for certificate signing (see standard)
  795. policy->certificateSigningAlgorithm = policy->asymmetricModule.cryptoModule.signatureAlgorithm;
  796. /* ChannelModule */
  797. channelModule->newContext = channelContext_newContext_sp_basic256sha256;
  798. channelModule->deleteContext = (void (*)(void *))
  799. channelContext_deleteContext_sp_basic256sha256;
  800. channelModule->setLocalSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  801. channelContext_setLocalSymEncryptingKey_sp_basic256sha256;
  802. channelModule->setLocalSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  803. channelContext_setLocalSymSigningKey_sp_basic256sha256;
  804. channelModule->setLocalSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  805. channelContext_setLocalSymIv_sp_basic256sha256;
  806. channelModule->setRemoteSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  807. channelContext_setRemoteSymEncryptingKey_sp_basic256sha256;
  808. channelModule->setRemoteSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  809. channelContext_setRemoteSymSigningKey_sp_basic256sha256;
  810. channelModule->setRemoteSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  811. channelContext_setRemoteSymIv_sp_basic256sha256;
  812. channelModule->compareCertificate = (UA_StatusCode (*)(const void *, const UA_ByteString *))
  813. channelContext_compareCertificate_sp_basic256sha256;
  814. policy->deleteMembers = deleteMembers_sp_basic256sha256;
  815. return policyContext_newContext_sp_basic256sha256(policy, localPrivateKey);
  816. }