ua_securitypolicy_basic128rsa15.c 42 KB

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