ua_securitypolicy_basic128rsa15.c 42 KB

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