ua_securitypolicy_basic256sha256.c 41 KB

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