ua_securitypolicy_basic256.c 40 KB

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