ua_securitypolicy_basic256sha256.c 44 KB

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