ua_securitypolicy_basic128rsa15.c 40 KB

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