ua_securitypolicy_basic128rsa15.c 39 KB

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