ua_securitypolicy_basic256sha256.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. #include "ua_util.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(!UA_constantTimeEqual(signature->data, mac, UA_SHA256_LENGTH))
  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. mbedtls_x509_crt_init(&cert);
  605. int mbedErr = mbedtls_x509_crt_parse(&cert, certificate->data, certificate->length);
  606. if(mbedErr) {
  607. UA_LOG_MBEDERR;
  608. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  609. }
  610. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  611. if(cert.raw.len != cc->remoteCertificate.raw.len ||
  612. memcmp(cert.raw.p, cc->remoteCertificate.raw.p, cert.raw.len) != 0)
  613. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  614. mbedtls_x509_crt_free(&cert);
  615. return retval;
  616. }
  617. static void
  618. deleteMembers_sp_basic256sha256(UA_SecurityPolicy *securityPolicy) {
  619. if(securityPolicy == NULL)
  620. return;
  621. if(securityPolicy->policyContext == NULL)
  622. return;
  623. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  624. /* delete all allocated members in the context */
  625. Basic256Sha256_PolicyContext *pc = (Basic256Sha256_PolicyContext *)
  626. securityPolicy->policyContext;
  627. mbedtls_ctr_drbg_free(&pc->drbgContext);
  628. mbedtls_entropy_free(&pc->entropyContext);
  629. mbedtls_pk_free(&pc->localPrivateKey);
  630. mbedtls_md_free(&pc->sha256MdContext);
  631. UA_ByteString_deleteMembers(&pc->localCertThumbprint);
  632. UA_LOG_DEBUG(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  633. "Deleted members of EndpointContext for sp_basic256sha256");
  634. UA_free(pc);
  635. securityPolicy->policyContext = NULL;
  636. }
  637. static UA_StatusCode
  638. updateCertificateAndPrivateKey_sp_basic256sha256(UA_SecurityPolicy *securityPolicy,
  639. const UA_ByteString newCertificate,
  640. const UA_ByteString newPrivateKey) {
  641. if(securityPolicy == NULL)
  642. return UA_STATUSCODE_BADINTERNALERROR;
  643. if(securityPolicy->policyContext == NULL)
  644. return UA_STATUSCODE_BADINTERNALERROR;
  645. Basic256Sha256_PolicyContext *pc =
  646. (Basic256Sha256_PolicyContext *) securityPolicy->policyContext;
  647. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  648. UA_StatusCode retval =
  649. UA_ByteString_allocBuffer(&securityPolicy->localCertificate, newCertificate.length + 1);
  650. if(retval != UA_STATUSCODE_GOOD)
  651. return retval;
  652. memcpy(securityPolicy->localCertificate.data, newCertificate.data, newCertificate.length);
  653. securityPolicy->localCertificate.data[newCertificate.length] = '\0';
  654. securityPolicy->localCertificate.length--;
  655. /* Set the new private key */
  656. mbedtls_pk_free(&pc->localPrivateKey);
  657. mbedtls_pk_init(&pc->localPrivateKey);
  658. int mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  659. newPrivateKey.data, newPrivateKey.length,
  660. NULL, 0);
  661. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  662. if(retval != UA_STATUSCODE_GOOD)
  663. goto error;
  664. retval = asym_makeThumbprint_sp_basic256sha256(pc->securityPolicy,
  665. &securityPolicy->localCertificate,
  666. &pc->localCertThumbprint);
  667. if(retval != UA_STATUSCODE_GOOD)
  668. goto error;
  669. return retval;
  670. error:
  671. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  672. "Could not update certificate and private key");
  673. if(securityPolicy->policyContext != NULL)
  674. deleteMembers_sp_basic256sha256(securityPolicy);
  675. return retval;
  676. }
  677. static UA_StatusCode
  678. policyContext_newContext_sp_basic256sha256(UA_SecurityPolicy *securityPolicy,
  679. const UA_ByteString localPrivateKey) {
  680. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  681. if(securityPolicy == NULL)
  682. return UA_STATUSCODE_BADINTERNALERROR;
  683. Basic256Sha256_PolicyContext *pc = (Basic256Sha256_PolicyContext *)
  684. UA_malloc(sizeof(Basic256Sha256_PolicyContext));
  685. securityPolicy->policyContext = (void *)pc;
  686. if(!pc) {
  687. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  688. goto error;
  689. }
  690. /* Initialize the PolicyContext */
  691. memset(pc, 0, sizeof(Basic256Sha256_PolicyContext));
  692. mbedtls_ctr_drbg_init(&pc->drbgContext);
  693. mbedtls_entropy_init(&pc->entropyContext);
  694. mbedtls_pk_init(&pc->localPrivateKey);
  695. mbedtls_md_init(&pc->sha256MdContext);
  696. pc->securityPolicy = securityPolicy;
  697. /* Initialized the message digest */
  698. const mbedtls_md_info_t *const mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
  699. int mbedErr = mbedtls_md_setup(&pc->sha256MdContext, mdInfo, MBEDTLS_MD_SHA256);
  700. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADOUTOFMEMORY);
  701. if(retval != UA_STATUSCODE_GOOD)
  702. goto error;
  703. /* Add the system entropy source */
  704. mbedErr = mbedtls_entropy_add_source(&pc->entropyContext,
  705. mbedtls_platform_entropy_poll, NULL, 0,
  706. MBEDTLS_ENTROPY_SOURCE_STRONG);
  707. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  708. if(retval != UA_STATUSCODE_GOOD)
  709. goto error;
  710. /* Seed the RNG */
  711. char *personalization = "open62541-drbg";
  712. mbedErr = mbedtls_ctr_drbg_seed(&pc->drbgContext, mbedtls_entropy_func,
  713. &pc->entropyContext,
  714. (const unsigned char *)personalization, 14);
  715. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  716. if(retval != UA_STATUSCODE_GOOD)
  717. goto error;
  718. /* Set the private key */
  719. mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  720. localPrivateKey.data, localPrivateKey.length,
  721. NULL, 0);
  722. UA_MBEDTLS_ERRORHANDLING(UA_STATUSCODE_BADSECURITYCHECKSFAILED);
  723. if(retval != UA_STATUSCODE_GOOD)
  724. goto error;
  725. /* Set the local certificate thumbprint */
  726. retval = UA_ByteString_allocBuffer(&pc->localCertThumbprint, UA_SHA1_LENGTH);
  727. if(retval != UA_STATUSCODE_GOOD)
  728. goto error;
  729. retval = asym_makeThumbprint_sp_basic256sha256(pc->securityPolicy,
  730. &securityPolicy->localCertificate,
  731. &pc->localCertThumbprint);
  732. if(retval != UA_STATUSCODE_GOOD)
  733. goto error;
  734. return UA_STATUSCODE_GOOD;
  735. error:
  736. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  737. "Could not create securityContext");
  738. if(securityPolicy->policyContext != NULL)
  739. deleteMembers_sp_basic256sha256(securityPolicy);
  740. return retval;
  741. }
  742. UA_StatusCode
  743. UA_SecurityPolicy_Basic256Sha256(UA_SecurityPolicy *policy,
  744. UA_CertificateVerification *certificateVerification,
  745. const UA_ByteString localCertificate,
  746. const UA_ByteString localPrivateKey, const UA_Logger *logger) {
  747. memset(policy, 0, sizeof(UA_SecurityPolicy));
  748. policy->logger = logger;
  749. policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  750. UA_SecurityPolicyAsymmetricModule *const asymmetricModule = &policy->asymmetricModule;
  751. UA_SecurityPolicySymmetricModule *const symmetricModule = &policy->symmetricModule;
  752. UA_SecurityPolicyChannelModule *const channelModule = &policy->channelModule;
  753. /* Copy the certificate and add a NULL to the end */
  754. UA_StatusCode retval =
  755. UA_ByteString_allocBuffer(&policy->localCertificate, localCertificate.length + 1);
  756. if(retval != UA_STATUSCODE_GOOD)
  757. return retval;
  758. memcpy(policy->localCertificate.data, localCertificate.data, localCertificate.length);
  759. policy->localCertificate.data[localCertificate.length] = '\0';
  760. policy->localCertificate.length--;
  761. policy->certificateVerification = certificateVerification;
  762. /* AsymmetricModule */
  763. UA_SecurityPolicySignatureAlgorithm *asym_signatureAlgorithm =
  764. &asymmetricModule->cryptoModule.signatureAlgorithm;
  765. asym_signatureAlgorithm->uri =
  766. UA_STRING("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\0");
  767. asym_signatureAlgorithm->verify =
  768. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  769. const UA_ByteString *, const UA_ByteString *))asym_verify_sp_basic256sha256;
  770. asym_signatureAlgorithm->sign =
  771. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  772. const UA_ByteString *, UA_ByteString *))asym_sign_sp_basic256sha256;
  773. asym_signatureAlgorithm->getLocalSignatureSize =
  774. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getLocalSignatureSize_sp_basic256sha256;
  775. asym_signatureAlgorithm->getRemoteSignatureSize =
  776. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteSignatureSize_sp_basic256sha256;
  777. asym_signatureAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  778. asym_signatureAlgorithm->getRemoteKeyLength = NULL; // TODO: Write function
  779. UA_SecurityPolicyEncryptionAlgorithm *asym_encryptionAlgorithm =
  780. &asymmetricModule->cryptoModule.encryptionAlgorithm;
  781. asym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#rsa-oaep\0");
  782. asym_encryptionAlgorithm->encrypt =
  783. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))asym_encrypt_sp_basic256sha256;
  784. asym_encryptionAlgorithm->decrypt =
  785. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))
  786. asym_decrypt_sp_basic256sha256;
  787. asym_encryptionAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  788. asym_encryptionAlgorithm->getRemoteKeyLength =
  789. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteEncryptionKeyLength_sp_basic256sha256;
  790. asym_encryptionAlgorithm->getLocalBlockSize = NULL; // TODO: Write function
  791. asym_encryptionAlgorithm->getRemoteBlockSize = (size_t (*)(const UA_SecurityPolicy *,
  792. const void *))asym_getRemoteBlockSize_sp_basic256sha256;
  793. asym_encryptionAlgorithm->getLocalPlainTextBlockSize = NULL; // TODO: Write function
  794. asym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  795. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemotePlainTextBlockSize_sp_basic256sha256;
  796. asymmetricModule->makeCertificateThumbprint = asym_makeThumbprint_sp_basic256sha256;
  797. asymmetricModule->compareCertificateThumbprint =
  798. asymmetricModule_compareCertificateThumbprint_sp_basic256sha256;
  799. /* SymmetricModule */
  800. symmetricModule->generateKey = sym_generateKey_sp_basic256sha256;
  801. symmetricModule->generateNonce = sym_generateNonce_sp_basic256sha256;
  802. UA_SecurityPolicySignatureAlgorithm *sym_signatureAlgorithm =
  803. &symmetricModule->cryptoModule.signatureAlgorithm;
  804. sym_signatureAlgorithm->uri =
  805. UA_STRING("http://www.w3.org/2000/09/xmldsig#hmac-sha1\0");
  806. sym_signatureAlgorithm->verify =
  807. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *, const UA_ByteString *,
  808. const UA_ByteString *))sym_verify_sp_basic256sha256;
  809. sym_signatureAlgorithm->sign =
  810. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  811. const UA_ByteString *, UA_ByteString *))sym_sign_sp_basic256sha256;
  812. sym_signatureAlgorithm->getLocalSignatureSize = sym_getSignatureSize_sp_basic256sha256;
  813. sym_signatureAlgorithm->getRemoteSignatureSize = sym_getSignatureSize_sp_basic256sha256;
  814. sym_signatureAlgorithm->getLocalKeyLength =
  815. (size_t (*)(const UA_SecurityPolicy *,
  816. const void *))sym_getSigningKeyLength_sp_basic256sha256;
  817. sym_signatureAlgorithm->getRemoteKeyLength =
  818. (size_t (*)(const UA_SecurityPolicy *,
  819. const void *))sym_getSigningKeyLength_sp_basic256sha256;
  820. UA_SecurityPolicyEncryptionAlgorithm *sym_encryptionAlgorithm =
  821. &symmetricModule->cryptoModule.encryptionAlgorithm;
  822. sym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
  823. sym_encryptionAlgorithm->encrypt =
  824. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_encrypt_sp_basic256sha256;
  825. sym_encryptionAlgorithm->decrypt =
  826. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_decrypt_sp_basic256sha256;
  827. sym_encryptionAlgorithm->getLocalKeyLength = sym_getEncryptionKeyLength_sp_basic256sha256;
  828. sym_encryptionAlgorithm->getRemoteKeyLength = sym_getEncryptionKeyLength_sp_basic256sha256;
  829. sym_encryptionAlgorithm->getLocalBlockSize =
  830. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256sha256;
  831. sym_encryptionAlgorithm->getRemoteBlockSize =
  832. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256sha256;
  833. sym_encryptionAlgorithm->getLocalPlainTextBlockSize =
  834. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256sha256;
  835. sym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  836. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256sha256;
  837. symmetricModule->secureChannelNonceLength = 32;
  838. // Use the same signature algorithm as the asymmetric component for certificate signing (see standard)
  839. policy->certificateSigningAlgorithm = policy->asymmetricModule.cryptoModule.signatureAlgorithm;
  840. /* ChannelModule */
  841. channelModule->newContext = channelContext_newContext_sp_basic256sha256;
  842. channelModule->deleteContext = (void (*)(void *))
  843. channelContext_deleteContext_sp_basic256sha256;
  844. channelModule->setLocalSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  845. channelContext_setLocalSymEncryptingKey_sp_basic256sha256;
  846. channelModule->setLocalSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  847. channelContext_setLocalSymSigningKey_sp_basic256sha256;
  848. channelModule->setLocalSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  849. channelContext_setLocalSymIv_sp_basic256sha256;
  850. channelModule->setRemoteSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  851. channelContext_setRemoteSymEncryptingKey_sp_basic256sha256;
  852. channelModule->setRemoteSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  853. channelContext_setRemoteSymSigningKey_sp_basic256sha256;
  854. channelModule->setRemoteSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  855. channelContext_setRemoteSymIv_sp_basic256sha256;
  856. channelModule->compareCertificate = (UA_StatusCode (*)(const void *, const UA_ByteString *))
  857. channelContext_compareCertificate_sp_basic256sha256;
  858. policy->updateCertificateAndPrivateKey = updateCertificateAndPrivateKey_sp_basic256sha256;
  859. policy->deleteMembers = deleteMembers_sp_basic256sha256;
  860. return policyContext_newContext_sp_basic256sha256(policy, localPrivateKey);
  861. }