ua_securitypolicy_basic256sha256.c 42 KB

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