ua_securitypolicy_basic256sha256.c 44 KB

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