ua_securitypolicy_basic128rsa15.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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/sha1.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 "ua_plugin_pki.h"
  16. #include "ua_plugin_securitypolicy.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. static void
  67. sha1(const unsigned char *input, size_t ilen, unsigned char output[20] ) {
  68. mbedtls_sha1_context sha1Context;
  69. mbedtls_sha1_init(&sha1Context);
  70. mbedtls_sha1_starts(&sha1Context);
  71. mbedtls_sha1_update(&sha1Context, input, ilen);
  72. mbedtls_sha1_finish(&sha1Context, output);
  73. mbedtls_sha1_free(&sha1Context);
  74. }
  75. /********************/
  76. /* AsymmetricModule */
  77. /********************/
  78. static UA_StatusCode
  79. asym_verify_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  80. Basic128Rsa15_ChannelContext *cc,
  81. const UA_ByteString *message,
  82. const UA_ByteString *signature) {
  83. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  84. return UA_STATUSCODE_BADINTERNALERROR;
  85. /* Compute the sha1 hash */
  86. unsigned char hash[UA_SHA1_LENGTH];
  87. sha1(message->data, message->length, hash);
  88. /* Set the RSA settings */
  89. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  90. mbedtls_rsa_set_padding(rsaContext, MBEDTLS_RSA_PKCS_V15, 0);
  91. /* Verify */
  92. int mbedErr = mbedtls_pk_verify(&cc->remoteCertificate.pk,
  93. MBEDTLS_MD_SHA1, hash, UA_SHA1_LENGTH,
  94. signature->data, signature->length);
  95. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  96. return UA_STATUSCODE_GOOD;
  97. }
  98. static UA_StatusCode
  99. asym_sign_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  100. Basic128Rsa15_ChannelContext *cc,
  101. const UA_ByteString *message,
  102. UA_ByteString *signature) {
  103. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  104. return UA_STATUSCODE_BADINTERNALERROR;
  105. unsigned char hash[UA_SHA1_LENGTH];
  106. sha1(message->data, message->length, hash);
  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(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(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. sha1(certificate->data, certificate->length, thumbprint->data);
  243. return UA_STATUSCODE_GOOD;
  244. }
  245. static UA_StatusCode
  246. asymmetricModule_compareCertificateThumbprint_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  247. const UA_ByteString *certificateThumbprint) {
  248. if(securityPolicy == NULL || certificateThumbprint == NULL)
  249. return UA_STATUSCODE_BADINTERNALERROR;
  250. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  251. if(!UA_ByteString_equal(certificateThumbprint, &pc->localCertThumbprint))
  252. return UA_STATUSCODE_BADCERTIFICATEINVALID;
  253. return UA_STATUSCODE_GOOD;
  254. }
  255. /*******************/
  256. /* SymmetricModule */
  257. /*******************/
  258. static void
  259. md_hmac(mbedtls_md_context_t *context, const UA_ByteString *key,
  260. const UA_ByteString *in, unsigned char out[20]) {
  261. mbedtls_md_hmac_starts(context, key->data, key->length);
  262. mbedtls_md_hmac_update(context, in->data, in->length);
  263. mbedtls_md_hmac_finish(context, out);
  264. }
  265. static UA_StatusCode
  266. sym_verify_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  267. Basic128Rsa15_ChannelContext *cc,
  268. const UA_ByteString *message,
  269. const UA_ByteString *signature) {
  270. if(securityPolicy == NULL || cc == NULL || message == NULL || signature == NULL)
  271. return UA_STATUSCODE_BADINTERNALERROR;
  272. /* Compute MAC */
  273. if(signature->length != UA_SHA1_LENGTH) {
  274. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  275. "Signature size does not have the desired size defined by the security policy");
  276. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  277. }
  278. Basic128Rsa15_PolicyContext *pc =
  279. (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  280. unsigned char mac[UA_SHA1_LENGTH];
  281. md_hmac(&pc->sha1MdContext, &cc->remoteSymSigningKey, message, mac);
  282. /* Compare with Signature */
  283. if(memcmp(signature->data, mac, UA_SHA1_LENGTH) != 0)
  284. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  285. return UA_STATUSCODE_GOOD;
  286. }
  287. static UA_StatusCode
  288. sym_sign_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  289. const Basic128Rsa15_ChannelContext *cc,
  290. const UA_ByteString *message,
  291. UA_ByteString *signature) {
  292. if(signature->length != UA_SHA1_LENGTH)
  293. return UA_STATUSCODE_BADINTERNALERROR;
  294. md_hmac(&cc->policyContext->sha1MdContext, &cc->localSymSigningKey,
  295. message, signature->data);
  296. return UA_STATUSCODE_GOOD;
  297. }
  298. static size_t
  299. sym_getSignatureSize_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  300. const void *channelContext) {
  301. return UA_SHA1_LENGTH;
  302. }
  303. static size_t
  304. sym_getSigningKeyLength_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  305. const void *const channelContext) {
  306. return UA_BASIC128RSA15_SYM_SIGNING_KEY_LENGTH;
  307. }
  308. static size_t
  309. sym_getEncryptionKeyLength_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  310. const void *channelContext) {
  311. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_KEY_LENGTH;
  312. }
  313. static size_t
  314. sym_getEncryptionBlockSize_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  315. const void *const channelContext) {
  316. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_ENCRYPTION_BLOCK_SIZE;
  317. }
  318. static size_t
  319. sym_getPlainTextBlockSize_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  320. const void *const channelContext) {
  321. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_PLAIN_TEXT_BLOCK_SIZE;
  322. }
  323. static UA_StatusCode
  324. sym_encrypt_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  325. const Basic128Rsa15_ChannelContext *cc,
  326. UA_ByteString *data) {
  327. if(securityPolicy == NULL || cc == NULL || data == NULL)
  328. return UA_STATUSCODE_BADINTERNALERROR;
  329. if(cc->localSymIv.length !=
  330. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalBlockSize(securityPolicy, cc))
  331. return UA_STATUSCODE_BADINTERNALERROR;
  332. size_t plainTextBlockSize =
  333. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalPlainTextBlockSize(securityPolicy, cc);
  334. if(data->length % plainTextBlockSize != 0) {
  335. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  336. "Length of data to encrypt is not a multiple of the plain text block size."
  337. "Padding might not have been calculated appropriately.");
  338. return UA_STATUSCODE_BADINTERNALERROR;
  339. }
  340. /* Keylength in bits */
  341. unsigned int keylength = (unsigned int)(cc->localSymEncryptingKey.length * 8);
  342. mbedtls_aes_context aesContext;
  343. int mbedErr = mbedtls_aes_setkey_enc(&aesContext, cc->localSymEncryptingKey.data, keylength);
  344. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADINTERNALERROR);
  345. UA_ByteString ivCopy;
  346. UA_StatusCode retval = UA_ByteString_copy(&cc->localSymIv, &ivCopy);
  347. if(retval != UA_STATUSCODE_GOOD)
  348. return retval;
  349. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_ENCRYPT, data->length,
  350. ivCopy.data, data->data, data->data);
  351. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADINTERNALERROR);
  352. UA_ByteString_deleteMembers(&ivCopy);
  353. return retval;
  354. }
  355. static UA_StatusCode
  356. sym_decrypt_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  357. const Basic128Rsa15_ChannelContext *cc,
  358. UA_ByteString *data) {
  359. if(securityPolicy == NULL || cc == NULL || data == NULL)
  360. return UA_STATUSCODE_BADINTERNALERROR;
  361. size_t encryptionBlockSize =
  362. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalBlockSize(securityPolicy, cc);
  363. if(cc->remoteSymIv.length != encryptionBlockSize)
  364. return UA_STATUSCODE_BADINTERNALERROR;
  365. if(data->length % encryptionBlockSize != 0) {
  366. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  367. "Length of data to decrypt is not a multiple of the encryptingBlock size.");
  368. return UA_STATUSCODE_BADINTERNALERROR;
  369. }
  370. unsigned int keylength = (unsigned int)(cc->remoteSymEncryptingKey.length * 8);
  371. mbedtls_aes_context aesContext;
  372. int mbedErr = mbedtls_aes_setkey_dec(&aesContext, cc->remoteSymEncryptingKey.data, keylength);
  373. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADINTERNALERROR);
  374. UA_ByteString ivCopy;
  375. UA_StatusCode retval = UA_ByteString_copy(&cc->remoteSymIv, &ivCopy);
  376. if(retval != UA_STATUSCODE_GOOD)
  377. return retval;
  378. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_DECRYPT, data->length,
  379. ivCopy.data, data->data, data->data);
  380. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADINTERNALERROR);
  381. UA_ByteString_deleteMembers(&ivCopy);
  382. return retval;
  383. }
  384. static void
  385. swapBuffers(UA_ByteString *const bufA, UA_ByteString *const bufB) {
  386. UA_ByteString tmp = *bufA;
  387. *bufA = *bufB;
  388. *bufB = tmp;
  389. }
  390. static UA_StatusCode
  391. sym_generateKey_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  392. const UA_ByteString *secret, const UA_ByteString *seed,
  393. UA_ByteString *out) {
  394. if(securityPolicy == NULL || secret == NULL || seed == NULL || out == NULL)
  395. return UA_STATUSCODE_BADINTERNALERROR;
  396. Basic128Rsa15_PolicyContext *pc =
  397. (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  398. size_t hashLen = 0;
  399. const mbedtls_md_info_t *mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  400. hashLen = (size_t)mbedtls_md_get_size(mdInfo);
  401. UA_ByteString A_and_seed;
  402. UA_ByteString_allocBuffer(&A_and_seed, hashLen + seed->length);
  403. memcpy(A_and_seed.data + hashLen, seed->data, seed->length);
  404. UA_ByteString ANext_and_seed;
  405. UA_ByteString_allocBuffer(&ANext_and_seed, hashLen + seed->length);
  406. memcpy(ANext_and_seed.data + hashLen, seed->data, seed->length);
  407. UA_ByteString A = {
  408. hashLen,
  409. A_and_seed.data
  410. };
  411. UA_ByteString ANext = {
  412. hashLen,
  413. ANext_and_seed.data
  414. };
  415. md_hmac(&pc->sha1MdContext, secret, seed, A.data);
  416. UA_StatusCode retval = 0;
  417. for(size_t offset = 0; offset < out->length; offset += hashLen) {
  418. UA_ByteString outSegment = {
  419. hashLen,
  420. out->data + offset
  421. };
  422. UA_Boolean bufferAllocated = UA_FALSE;
  423. // Not enough room in out buffer to write the hash.
  424. if(offset + hashLen > out->length) {
  425. outSegment.data = NULL;
  426. outSegment.length = 0;
  427. retval |= UA_ByteString_allocBuffer(&outSegment, hashLen);
  428. if(retval != UA_STATUSCODE_GOOD) {
  429. UA_ByteString_deleteMembers(&A_and_seed);
  430. UA_ByteString_deleteMembers(&ANext_and_seed);
  431. return retval;
  432. }
  433. bufferAllocated = UA_TRUE;
  434. }
  435. md_hmac(&pc->sha1MdContext, secret, &A_and_seed, outSegment.data);
  436. md_hmac(&pc->sha1MdContext, secret, &A, ANext.data);
  437. if(retval != UA_STATUSCODE_GOOD) {
  438. if(bufferAllocated)
  439. UA_ByteString_deleteMembers(&outSegment);
  440. UA_ByteString_deleteMembers(&A_and_seed);
  441. UA_ByteString_deleteMembers(&ANext_and_seed);
  442. return retval;
  443. }
  444. if(bufferAllocated) {
  445. memcpy(out->data + offset, outSegment.data, out->length - offset);
  446. UA_ByteString_deleteMembers(&outSegment);
  447. }
  448. swapBuffers(&ANext_and_seed, &A_and_seed);
  449. swapBuffers(&ANext, &A);
  450. }
  451. UA_ByteString_deleteMembers(&A_and_seed);
  452. UA_ByteString_deleteMembers(&ANext_and_seed);
  453. return UA_STATUSCODE_GOOD;
  454. }
  455. static UA_StatusCode
  456. sym_generateNonce_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  457. UA_ByteString *out) {
  458. if(securityPolicy == NULL || securityPolicy->policyContext == NULL || out == NULL)
  459. return UA_STATUSCODE_BADINTERNALERROR;
  460. Basic128Rsa15_PolicyContext *data =
  461. (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  462. int mbedErr = mbedtls_ctr_drbg_random(&data->drbgContext, out->data, out->length);
  463. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADUNEXPECTEDERROR);
  464. return UA_STATUSCODE_GOOD;
  465. }
  466. /*****************/
  467. /* ChannelModule */
  468. /*****************/
  469. /* Assumes that the certificate has been verified externally */
  470. static UA_StatusCode
  471. parseRemoteCertificate_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  472. const UA_ByteString *remoteCertificate) {
  473. if(remoteCertificate == NULL || cc == NULL)
  474. return UA_STATUSCODE_BADINTERNALERROR;
  475. const UA_SecurityPolicy *securityPolicy = cc->policyContext->securityPolicy;
  476. /* Parse the certificate */
  477. int mbedErr = mbedtls_x509_crt_parse(&cc->remoteCertificate, remoteCertificate->data,
  478. remoteCertificate->length);
  479. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  480. /* Check the key length */
  481. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  482. if(rsaContext->len < UA_SECURITYPOLICY_BASIC128RSA15_MINASYMKEYLENGTH ||
  483. rsaContext->len > UA_SECURITYPOLICY_BASIC128RSA15_MAXASYMKEYLENGTH)
  484. return UA_STATUSCODE_BADCERTIFICATEUSENOTALLOWED;
  485. return UA_STATUSCODE_GOOD;
  486. }
  487. static void
  488. channelContext_deleteContext_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc) {
  489. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  490. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  491. UA_ByteString_deleteMembers(&cc->localSymIv);
  492. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  493. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  494. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  495. mbedtls_x509_crt_free(&cc->remoteCertificate);
  496. UA_free(cc);
  497. }
  498. static UA_StatusCode
  499. channelContext_newContext_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  500. const UA_ByteString *remoteCertificate,
  501. void **pp_contextData) {
  502. if(securityPolicy == NULL || remoteCertificate == NULL || pp_contextData == NULL)
  503. return UA_STATUSCODE_BADINTERNALERROR;
  504. /* Allocate the channel context */
  505. *pp_contextData = UA_malloc(sizeof(Basic128Rsa15_ChannelContext));
  506. if(*pp_contextData == NULL)
  507. return UA_STATUSCODE_BADOUTOFMEMORY;
  508. Basic128Rsa15_ChannelContext *cc = (Basic128Rsa15_ChannelContext *)*pp_contextData;
  509. /* Initialize the channel context */
  510. cc->policyContext = (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  511. UA_ByteString_init(&cc->localSymSigningKey);
  512. UA_ByteString_init(&cc->localSymEncryptingKey);
  513. UA_ByteString_init(&cc->localSymIv);
  514. UA_ByteString_init(&cc->remoteSymSigningKey);
  515. UA_ByteString_init(&cc->remoteSymEncryptingKey);
  516. UA_ByteString_init(&cc->remoteSymIv);
  517. mbedtls_x509_crt_init(&cc->remoteCertificate);
  518. // TODO: this can be optimized so that we dont allocate memory before parsing the certificate
  519. UA_StatusCode retval = parseRemoteCertificate_sp_basic128rsa15(cc, remoteCertificate);
  520. if(retval != UA_STATUSCODE_GOOD) {
  521. channelContext_deleteContext_sp_basic128rsa15(cc);
  522. *pp_contextData = NULL;
  523. }
  524. return retval;
  525. }
  526. static UA_StatusCode
  527. channelContext_setLocalSymEncryptingKey_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  528. const UA_ByteString *key) {
  529. if(key == NULL || cc == NULL)
  530. return UA_STATUSCODE_BADINTERNALERROR;
  531. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  532. return UA_ByteString_copy(key, &cc->localSymEncryptingKey);
  533. }
  534. static UA_StatusCode
  535. channelContext_setLocalSymSigningKey_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  536. const UA_ByteString *key) {
  537. if(key == NULL || cc == NULL)
  538. return UA_STATUSCODE_BADINTERNALERROR;
  539. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  540. return UA_ByteString_copy(key, &cc->localSymSigningKey);
  541. }
  542. static UA_StatusCode
  543. channelContext_setLocalSymIv_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  544. const UA_ByteString *iv) {
  545. if(iv == NULL || cc == NULL)
  546. return UA_STATUSCODE_BADINTERNALERROR;
  547. UA_ByteString_deleteMembers(&cc->localSymIv);
  548. return UA_ByteString_copy(iv, &cc->localSymIv);
  549. }
  550. static UA_StatusCode
  551. channelContext_setRemoteSymEncryptingKey_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  552. const UA_ByteString *key) {
  553. if(key == NULL || cc == NULL)
  554. return UA_STATUSCODE_BADINTERNALERROR;
  555. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  556. return UA_ByteString_copy(key, &cc->remoteSymEncryptingKey);
  557. }
  558. static UA_StatusCode
  559. channelContext_setRemoteSymSigningKey_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  560. const UA_ByteString *key) {
  561. if(key == NULL || cc == NULL)
  562. return UA_STATUSCODE_BADINTERNALERROR;
  563. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  564. return UA_ByteString_copy(key, &cc->remoteSymSigningKey);
  565. }
  566. static UA_StatusCode
  567. channelContext_setRemoteSymIv_sp_basic128rsa15(Basic128Rsa15_ChannelContext *cc,
  568. const UA_ByteString *iv) {
  569. if(iv == NULL || cc == NULL)
  570. return UA_STATUSCODE_BADINTERNALERROR;
  571. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  572. return UA_ByteString_copy(iv, &cc->remoteSymIv);
  573. }
  574. static UA_StatusCode
  575. channelContext_compareCertificate_sp_basic128rsa15(const Basic128Rsa15_ChannelContext *cc,
  576. const UA_ByteString *certificate) {
  577. if(cc == NULL || certificate == NULL)
  578. return UA_STATUSCODE_BADINTERNALERROR;
  579. const UA_SecurityPolicy *securityPolicy = cc->policyContext->securityPolicy;
  580. mbedtls_x509_crt cert;
  581. int mbedErr = mbedtls_x509_crt_parse(&cert, certificate->data, certificate->length);
  582. UA_MBEDTLS_ERRORHANDLING_RETURN(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  583. if(cert.raw.len != cc->remoteCertificate.raw.len)
  584. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  585. if(memcmp(cert.raw.p, cc->remoteCertificate.raw.p, cert.raw.len) != 0)
  586. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  587. return UA_STATUSCODE_GOOD;
  588. }
  589. static void
  590. deleteMembers_sp_basic128rsa15(UA_SecurityPolicy *securityPolicy) {
  591. if(securityPolicy == NULL)
  592. return;
  593. if(securityPolicy->policyContext == NULL)
  594. return;
  595. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  596. /* delete all allocated members in the context */
  597. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_PolicyContext *)
  598. securityPolicy->policyContext;
  599. mbedtls_ctr_drbg_free(&pc->drbgContext);
  600. mbedtls_entropy_free(&pc->entropyContext);
  601. mbedtls_pk_free(&pc->localPrivateKey);
  602. mbedtls_md_free(&pc->sha1MdContext);
  603. UA_ByteString_deleteMembers(&pc->localCertThumbprint);
  604. UA_LOG_DEBUG(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  605. "Deleted members of EndpointContext for sp_basic128rsa15");
  606. UA_free(pc);
  607. securityPolicy->policyContext = NULL;
  608. }
  609. static UA_StatusCode
  610. policyContext_newContext_sp_basic128rsa15(UA_SecurityPolicy *securityPolicy,
  611. const UA_ByteString localPrivateKey) {
  612. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  613. if(securityPolicy == NULL)
  614. return UA_STATUSCODE_BADINTERNALERROR;
  615. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_PolicyContext *)
  616. UA_malloc(sizeof(Basic128Rsa15_PolicyContext));
  617. securityPolicy->policyContext = (void *)pc;
  618. if(!pc) {
  619. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  620. goto error;
  621. }
  622. /* Initialize the PolicyContext */
  623. memset(pc, 0, sizeof(Basic128Rsa15_PolicyContext));
  624. mbedtls_ctr_drbg_init(&pc->drbgContext);
  625. mbedtls_entropy_init(&pc->entropyContext);
  626. mbedtls_pk_init(&pc->localPrivateKey);
  627. mbedtls_md_init(&pc->sha1MdContext);
  628. pc->securityPolicy = securityPolicy;
  629. /* Initialized the message digest */
  630. const mbedtls_md_info_t *const mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  631. int mbedErr = mbedtls_md_setup(&pc->sha1MdContext, mdInfo, MBEDTLS_MD_SHA1);
  632. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADOUTOFMEMORY);
  633. if(retval != UA_STATUSCODE_GOOD)
  634. goto error;
  635. /* Add the system entropy source */
  636. mbedErr = mbedtls_entropy_add_source(&pc->entropyContext,
  637. mbedtls_platform_entropy_poll, NULL, 0,
  638. MBEDTLS_ENTROPY_SOURCE_STRONG);
  639. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  640. if(retval != UA_STATUSCODE_GOOD)
  641. goto error;
  642. /* Seed the RNG */
  643. char *personalization = "open62541-drbg";
  644. mbedErr = mbedtls_ctr_drbg_seed(&pc->drbgContext, mbedtls_entropy_func,
  645. &pc->entropyContext,
  646. (const unsigned char *)personalization, 14);
  647. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  648. if(retval != UA_STATUSCODE_GOOD)
  649. goto error;
  650. /* Set the private key */
  651. mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  652. localPrivateKey.data, localPrivateKey.length,
  653. NULL, 0);
  654. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  655. if(retval != UA_STATUSCODE_GOOD)
  656. goto error;
  657. /* Set the local certificate thumbprint */
  658. retval = UA_ByteString_allocBuffer(&pc->localCertThumbprint, UA_SHA1_LENGTH);
  659. if(retval != UA_STATUSCODE_GOOD)
  660. goto error;
  661. retval = asym_makeThumbprint_sp_basic128rsa15(pc->securityPolicy,
  662. &securityPolicy->localCertificate,
  663. &pc->localCertThumbprint);
  664. if(retval != UA_STATUSCODE_GOOD)
  665. goto error;
  666. return UA_STATUSCODE_GOOD;
  667. error:
  668. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  669. "Could not create securityContext");
  670. if(securityPolicy->policyContext != NULL)
  671. deleteMembers_sp_basic128rsa15(securityPolicy);
  672. return retval;
  673. }
  674. UA_StatusCode
  675. UA_SecurityPolicy_Basic128Rsa15(UA_SecurityPolicy *policy, UA_CertificateVerification *certificateVerification,
  676. const UA_ByteString localCertificate, const UA_ByteString localPrivateKey,
  677. UA_Logger logger) {
  678. memset(policy, 0, sizeof(UA_SecurityPolicy));
  679. policy->logger = logger;
  680. policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  681. UA_SecurityPolicyAsymmetricModule *const asymmetricModule = &policy->asymmetricModule;
  682. UA_SecurityPolicySymmetricModule *const symmetricModule = &policy->symmetricModule;
  683. UA_SecurityPolicyChannelModule *const channelModule = &policy->channelModule;
  684. /* Copy the certificate and add a NULL to the end */
  685. UA_StatusCode retval =
  686. UA_ByteString_allocBuffer(&policy->localCertificate, localCertificate.length + 1);
  687. if(retval != UA_STATUSCODE_GOOD)
  688. return retval;
  689. memcpy(policy->localCertificate.data, localCertificate.data, localCertificate.length);
  690. policy->localCertificate.data[localCertificate.length] = '\0';
  691. policy->localCertificate.length--;
  692. policy->certificateVerification = certificateVerification;
  693. /* AsymmetricModule */
  694. UA_SecurityPolicySignatureAlgorithm *asym_signatureAlgorithm =
  695. &asymmetricModule->cryptoModule.signatureAlgorithm;
  696. asym_signatureAlgorithm->uri =
  697. UA_STRING("http://www.w3.org/2000/09/xmldsig#rsa-sha1\0");
  698. asym_signatureAlgorithm->verify =
  699. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  700. const UA_ByteString *, const UA_ByteString *))asym_verify_sp_basic128rsa15;
  701. asym_signatureAlgorithm->sign =
  702. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  703. const UA_ByteString *, UA_ByteString *))asym_sign_sp_basic128rsa15;
  704. asym_signatureAlgorithm->getLocalSignatureSize =
  705. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getLocalSignatureSize_sp_basic128rsa15;
  706. asym_signatureAlgorithm->getRemoteSignatureSize =
  707. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteSignatureSize_sp_basic128rsa15;
  708. asym_signatureAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  709. asym_signatureAlgorithm->getRemoteKeyLength = NULL; // TODO: Write function
  710. UA_SecurityPolicyEncryptionAlgorithm *asym_encryptionAlgorithm =
  711. &asymmetricModule->cryptoModule.encryptionAlgorithm;
  712. asym_encryptionAlgorithm->uri = UA_STRING("TODO: ALG URI");
  713. asym_encryptionAlgorithm->encrypt =
  714. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))asym_encrypt_sp_basic128rsa15;
  715. asym_encryptionAlgorithm->decrypt =
  716. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))
  717. asym_decrypt_sp_basic128rsa15;
  718. asym_encryptionAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  719. asym_encryptionAlgorithm->getRemoteKeyLength =
  720. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteEncryptionKeyLength;
  721. asym_encryptionAlgorithm->getLocalBlockSize = NULL; // TODO: Write function
  722. asym_encryptionAlgorithm->getRemoteBlockSize = (size_t (*)(const UA_SecurityPolicy *,
  723. const void *))asym_getRemoteBlockSize_sp_basic128rsa15;
  724. asym_encryptionAlgorithm->getLocalPlainTextBlockSize = NULL; // TODO: Write function
  725. asym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  726. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemotePlainTextBlockSize;
  727. asymmetricModule->makeCertificateThumbprint = asym_makeThumbprint_sp_basic128rsa15;
  728. asymmetricModule->compareCertificateThumbprint =
  729. asymmetricModule_compareCertificateThumbprint_sp_basic128rsa15;
  730. /* SymmetricModule */
  731. symmetricModule->generateKey = sym_generateKey_sp_basic128rsa15;
  732. symmetricModule->generateNonce = sym_generateNonce_sp_basic128rsa15;
  733. UA_SecurityPolicySignatureAlgorithm *sym_signatureAlgorithm =
  734. &symmetricModule->cryptoModule.signatureAlgorithm;
  735. sym_signatureAlgorithm->uri =
  736. UA_STRING("http://www.w3.org/2000/09/xmldsig#hmac-sha1\0");
  737. sym_signatureAlgorithm->verify =
  738. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *, const UA_ByteString *,
  739. const UA_ByteString *))sym_verify_sp_basic128rsa15;
  740. sym_signatureAlgorithm->sign =
  741. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  742. const UA_ByteString *, UA_ByteString *))sym_sign_sp_basic128rsa15;
  743. sym_signatureAlgorithm->getLocalSignatureSize = sym_getSignatureSize_sp_basic128rsa15;
  744. sym_signatureAlgorithm->getRemoteSignatureSize = sym_getSignatureSize_sp_basic128rsa15;
  745. sym_signatureAlgorithm->getLocalKeyLength =
  746. (size_t (*)(const UA_SecurityPolicy *,
  747. const void *))sym_getSigningKeyLength_sp_basic128rsa15;
  748. sym_signatureAlgorithm->getRemoteKeyLength =
  749. (size_t (*)(const UA_SecurityPolicy *,
  750. const void *))sym_getSigningKeyLength_sp_basic128rsa15;
  751. UA_SecurityPolicyEncryptionAlgorithm *sym_encryptionAlgorithm =
  752. &symmetricModule->cryptoModule.encryptionAlgorithm;
  753. sym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
  754. sym_encryptionAlgorithm->encrypt =
  755. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_encrypt_sp_basic128rsa15;
  756. sym_encryptionAlgorithm->decrypt =
  757. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_decrypt_sp_basic128rsa15;
  758. sym_encryptionAlgorithm->getLocalKeyLength = sym_getEncryptionKeyLength_sp_basic128rsa15;
  759. sym_encryptionAlgorithm->getRemoteKeyLength = sym_getEncryptionKeyLength_sp_basic128rsa15;
  760. sym_encryptionAlgorithm->getLocalBlockSize =
  761. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic128rsa15;
  762. sym_encryptionAlgorithm->getRemoteBlockSize =
  763. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic128rsa15;
  764. sym_encryptionAlgorithm->getLocalPlainTextBlockSize =
  765. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic128rsa15;
  766. sym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  767. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic128rsa15;
  768. symmetricModule->secureChannelNonceLength = 16;
  769. // Use the same signature algorithm as the asymmetric component for certificate signing (see standard)
  770. policy->certificateSigningAlgorithm = policy->asymmetricModule.cryptoModule.signatureAlgorithm;
  771. /* ChannelModule */
  772. channelModule->newContext = channelContext_newContext_sp_basic128rsa15;
  773. channelModule->deleteContext = (void (*)(void *))
  774. channelContext_deleteContext_sp_basic128rsa15;
  775. channelModule->setLocalSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  776. channelContext_setLocalSymEncryptingKey_sp_basic128rsa15;
  777. channelModule->setLocalSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  778. channelContext_setLocalSymSigningKey_sp_basic128rsa15;
  779. channelModule->setLocalSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  780. channelContext_setLocalSymIv_sp_basic128rsa15;
  781. channelModule->setRemoteSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  782. channelContext_setRemoteSymEncryptingKey_sp_basic128rsa15;
  783. channelModule->setRemoteSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  784. channelContext_setRemoteSymSigningKey_sp_basic128rsa15;
  785. channelModule->setRemoteSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  786. channelContext_setRemoteSymIv_sp_basic128rsa15;
  787. channelModule->compareCertificate = (UA_StatusCode (*)(const void *, const UA_ByteString *))
  788. channelContext_compareCertificate_sp_basic128rsa15;
  789. policy->deleteMembers = deleteMembers_sp_basic128rsa15;
  790. return policyContext_newContext_sp_basic128rsa15(policy, localPrivateKey);
  791. }