ua_securitypolicy_basic256sha256.c 42 KB

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