ua_securitypolicy_basic128rsa15.c 42 KB

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