ua_securitypolicy_basic256sha256.c 42 KB

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