ua_securitypolicy_basic256.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/entropy.h>
  10. #include <mbedtls/entropy_poll.h>
  11. #include <mbedtls/error.h>
  12. #include <mbedtls/version.h>
  13. #include <mbedtls/sha1.h>
  14. #include "ua_types.h"
  15. #include "ua_plugin_pki.h"
  16. #include "ua_securitypolicies.h"
  17. #include "ua_securitypolicy_mbedtls_common.h"
  18. #include "ua_types_generated_handling.h"
  19. #include "ua_util.h"
  20. /* Notes:
  21. * mbedTLS' AES allows in-place encryption and decryption. Sow we don't have to
  22. * allocate temp buffers.
  23. * https://tls.mbed.org/discussions/generic/in-place-decryption-with-aes256-same-input-output-buffer
  24. */
  25. #define UA_SECURITYPOLICY_BASIC256SHA1_RSAPADDING_LEN 42
  26. #define UA_SHA1_LENGTH 20
  27. #define UA_BASIC256_SYM_SIGNING_KEY_LENGTH 24
  28. #define UA_SECURITYPOLICY_BASIC256_SYM_KEY_LENGTH 32
  29. #define UA_SECURITYPOLICY_BASIC256_SYM_ENCRYPTION_BLOCK_SIZE 16
  30. #define UA_SECURITYPOLICY_BASIC256_SYM_PLAIN_TEXT_BLOCK_SIZE 16
  31. #define UA_SECURITYPOLICY_BASIC256_MINASYMKEYLENGTH 128
  32. #define UA_SECURITYPOLICY_BASIC256_MAXASYMKEYLENGTH 256
  33. typedef struct {
  34. const UA_SecurityPolicy *securityPolicy;
  35. UA_ByteString localCertThumbprint;
  36. mbedtls_ctr_drbg_context drbgContext;
  37. mbedtls_entropy_context entropyContext;
  38. mbedtls_md_context_t sha1MdContext;
  39. mbedtls_pk_context localPrivateKey;
  40. } Basic256_PolicyContext;
  41. typedef struct {
  42. Basic256_PolicyContext *policyContext;
  43. UA_ByteString localSymSigningKey;
  44. UA_ByteString localSymEncryptingKey;
  45. UA_ByteString localSymIv;
  46. UA_ByteString remoteSymSigningKey;
  47. UA_ByteString remoteSymEncryptingKey;
  48. UA_ByteString remoteSymIv;
  49. mbedtls_x509_crt remoteCertificate;
  50. } Basic256_ChannelContext;
  51. /********************/
  52. /* AsymmetricModule */
  53. /********************/
  54. /* VERIFY AsymmetricSignatureAlgorithm_RSA-PKCS15-SHA2-256 */
  55. static UA_StatusCode
  56. asym_verify_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  57. Basic256_ChannelContext *cc,
  58. const UA_ByteString *message,
  59. const UA_ByteString *signature) {
  60. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  61. return UA_STATUSCODE_BADINTERNALERROR;
  62. return mbedtls_verifySig_sha1(&cc->remoteCertificate, message, signature);
  63. }
  64. /* AsymmetricSignatureAlgorithm_RSA-PKCS15-SHA2-256 */
  65. static UA_StatusCode
  66. asym_sign_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  67. Basic256_ChannelContext *cc,
  68. const UA_ByteString *message,
  69. UA_ByteString *signature) {
  70. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  71. return UA_STATUSCODE_BADINTERNALERROR;
  72. Basic256_PolicyContext *pc = cc->policyContext;
  73. return mbedtls_sign_sha1(&pc->localPrivateKey, &pc->drbgContext,
  74. message, signature);
  75. }
  76. static size_t
  77. asym_getLocalSignatureSize_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  78. const Basic256_ChannelContext *cc) {
  79. if(securityPolicy == NULL || cc == NULL)
  80. return 0;
  81. return mbedtls_pk_rsa(cc->policyContext->localPrivateKey)->len;
  82. }
  83. static size_t
  84. asym_getRemoteSignatureSize_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  85. const Basic256_ChannelContext *cc) {
  86. if(securityPolicy == NULL || cc == NULL)
  87. return 0;
  88. return mbedtls_pk_rsa(cc->remoteCertificate.pk)->len;
  89. }
  90. /* AsymmetricEncryptionAlgorithm_RSA-OAEP-SHA1 */
  91. static UA_StatusCode
  92. asym_encrypt_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  93. Basic256_ChannelContext *cc,
  94. UA_ByteString *data) {
  95. if(securityPolicy == NULL || cc == NULL || data == NULL)
  96. return UA_STATUSCODE_BADINTERNALERROR;
  97. const size_t plainTextBlockSize = securityPolicy->asymmetricModule.cryptoModule.
  98. encryptionAlgorithm.getRemotePlainTextBlockSize(securityPolicy, cc);
  99. mbedtls_rsa_context *remoteRsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  100. mbedtls_rsa_set_padding(remoteRsaContext, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1);
  101. return mbedtls_encrypt_rsaOaep(remoteRsaContext, &cc->policyContext->drbgContext,
  102. data, plainTextBlockSize);
  103. }
  104. /* AsymmetricEncryptionAlgorithm_RSA-OAEP-SHA1 */
  105. static UA_StatusCode
  106. asym_decrypt_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  107. Basic256_ChannelContext *cc,
  108. UA_ByteString *data) {
  109. if(securityPolicy == NULL || cc == NULL || data == NULL)
  110. return UA_STATUSCODE_BADINTERNALERROR;
  111. return mbedtls_decrypt_rsaOaep(&cc->policyContext->localPrivateKey,
  112. &cc->policyContext->drbgContext, data);
  113. }
  114. static size_t
  115. asym_getRemoteEncryptionKeyLength_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  116. const Basic256_ChannelContext *cc) {
  117. return mbedtls_pk_get_len(&cc->remoteCertificate.pk) * 8;
  118. }
  119. static size_t
  120. asym_getRemoteBlockSize_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  121. const Basic256_ChannelContext *cc) {
  122. mbedtls_rsa_context *const rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  123. return rsaContext->len;
  124. }
  125. static size_t
  126. asym_getRemotePlainTextBlockSize_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  127. const Basic256_ChannelContext *cc) {
  128. mbedtls_rsa_context *const rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  129. return rsaContext->len - UA_SECURITYPOLICY_BASIC256SHA1_RSAPADDING_LEN;
  130. }
  131. static UA_StatusCode
  132. asym_makeThumbprint_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  133. const UA_ByteString *certificate,
  134. UA_ByteString *thumbprint) {
  135. if(securityPolicy == NULL || certificate == NULL || thumbprint == NULL)
  136. return UA_STATUSCODE_BADINTERNALERROR;
  137. return mbedtls_thumbprint_sha1(certificate, thumbprint);
  138. }
  139. static UA_StatusCode
  140. asymmetricModule_compareCertificateThumbprint_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  141. const UA_ByteString *certificateThumbprint) {
  142. if(securityPolicy == NULL || certificateThumbprint == NULL)
  143. return UA_STATUSCODE_BADINTERNALERROR;
  144. Basic256_PolicyContext *pc = (Basic256_PolicyContext *)securityPolicy->policyContext;
  145. if(!UA_ByteString_equal(certificateThumbprint, &pc->localCertThumbprint))
  146. return UA_STATUSCODE_BADCERTIFICATEINVALID;
  147. return UA_STATUSCODE_GOOD;
  148. }
  149. /*******************/
  150. /* SymmetricModule */
  151. /*******************/
  152. static UA_StatusCode
  153. sym_verify_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  154. Basic256_ChannelContext *cc,
  155. const UA_ByteString *message,
  156. const UA_ByteString *signature) {
  157. if(securityPolicy == NULL || cc == NULL || message == NULL || signature == NULL)
  158. return UA_STATUSCODE_BADINTERNALERROR;
  159. /* Compute MAC */
  160. if(signature->length != UA_SHA1_LENGTH) {
  161. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  162. "Signature size does not have the desired size defined by the security policy");
  163. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  164. }
  165. Basic256_PolicyContext *pc =
  166. (Basic256_PolicyContext *)securityPolicy->policyContext;
  167. unsigned char mac[UA_SHA1_LENGTH];
  168. mbedtls_hmac(&pc->sha1MdContext, &cc->remoteSymSigningKey, message, mac);
  169. /* Compare with Signature */
  170. if(!UA_constantTimeEqual(signature->data, mac, UA_SHA1_LENGTH))
  171. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  172. return UA_STATUSCODE_GOOD;
  173. }
  174. static UA_StatusCode
  175. sym_sign_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  176. const Basic256_ChannelContext *cc,
  177. const UA_ByteString *message,
  178. UA_ByteString *signature) {
  179. if(signature->length != UA_SHA1_LENGTH)
  180. return UA_STATUSCODE_BADINTERNALERROR;
  181. mbedtls_hmac(&cc->policyContext->sha1MdContext, &cc->localSymSigningKey,
  182. message, signature->data);
  183. return UA_STATUSCODE_GOOD;
  184. }
  185. static size_t
  186. sym_getSignatureSize_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  187. const void *channelContext) {
  188. return UA_SHA1_LENGTH;
  189. }
  190. static size_t
  191. sym_getSigningKeyLength_sp_basic256(const UA_SecurityPolicy *const securityPolicy,
  192. const void *const channelContext) {
  193. return UA_BASIC256_SYM_SIGNING_KEY_LENGTH;
  194. }
  195. static size_t
  196. sym_getEncryptionKeyLength_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  197. const void *channelContext) {
  198. return UA_SECURITYPOLICY_BASIC256_SYM_KEY_LENGTH;
  199. }
  200. static size_t
  201. sym_getEncryptionBlockSize_sp_basic256(const UA_SecurityPolicy *const securityPolicy,
  202. const void *const channelContext) {
  203. return UA_SECURITYPOLICY_BASIC256_SYM_ENCRYPTION_BLOCK_SIZE;
  204. }
  205. static size_t
  206. sym_getPlainTextBlockSize_sp_basic256(const UA_SecurityPolicy *const securityPolicy,
  207. const void *const channelContext) {
  208. return UA_SECURITYPOLICY_BASIC256_SYM_PLAIN_TEXT_BLOCK_SIZE;
  209. }
  210. static UA_StatusCode
  211. sym_encrypt_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  212. const Basic256_ChannelContext *cc,
  213. UA_ByteString *data) {
  214. if(securityPolicy == NULL || cc == NULL || data == NULL)
  215. return UA_STATUSCODE_BADINTERNALERROR;
  216. if(cc->localSymIv.length !=
  217. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.
  218. getLocalBlockSize(securityPolicy, cc))
  219. return UA_STATUSCODE_BADINTERNALERROR;
  220. size_t plainTextBlockSize =
  221. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.
  222. getLocalPlainTextBlockSize(securityPolicy, cc);
  223. if(data->length % plainTextBlockSize != 0) {
  224. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  225. "Length of data to encrypt is not a multiple of the plain text block size."
  226. "Padding might not have been calculated appropriately.");
  227. return UA_STATUSCODE_BADINTERNALERROR;
  228. }
  229. /* Keylength in bits */
  230. unsigned int keylength = (unsigned int)(cc->localSymEncryptingKey.length * 8);
  231. mbedtls_aes_context aesContext;
  232. int mbedErr = mbedtls_aes_setkey_enc(&aesContext, cc->localSymEncryptingKey.data, keylength);
  233. if(mbedErr)
  234. return UA_STATUSCODE_BADINTERNALERROR;
  235. UA_ByteString ivCopy;
  236. UA_StatusCode retval = UA_ByteString_copy(&cc->localSymIv, &ivCopy);
  237. if(retval != UA_STATUSCODE_GOOD)
  238. return retval;
  239. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_ENCRYPT, data->length,
  240. ivCopy.data, data->data, data->data);
  241. if(mbedErr)
  242. retval = UA_STATUSCODE_BADINTERNALERROR;
  243. UA_ByteString_deleteMembers(&ivCopy);
  244. return retval;
  245. }
  246. static UA_StatusCode
  247. sym_decrypt_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  248. const Basic256_ChannelContext *cc,
  249. UA_ByteString *data) {
  250. if(securityPolicy == NULL || cc == NULL || data == NULL)
  251. return UA_STATUSCODE_BADINTERNALERROR;
  252. size_t encryptionBlockSize =
  253. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.
  254. getRemoteBlockSize(securityPolicy, cc);
  255. if(cc->remoteSymIv.length != encryptionBlockSize)
  256. return UA_STATUSCODE_BADINTERNALERROR;
  257. if(data->length % encryptionBlockSize != 0) {
  258. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  259. "Length of data to decrypt is not a multiple of the encryptingBlock size.");
  260. return UA_STATUSCODE_BADINTERNALERROR;
  261. }
  262. unsigned int keylength = (unsigned int)(cc->remoteSymEncryptingKey.length * 8);
  263. mbedtls_aes_context aesContext;
  264. int mbedErr = mbedtls_aes_setkey_dec(&aesContext, cc->remoteSymEncryptingKey.data, keylength);
  265. if(mbedErr)
  266. return UA_STATUSCODE_BADINTERNALERROR;
  267. UA_ByteString ivCopy;
  268. UA_StatusCode retval = UA_ByteString_copy(&cc->remoteSymIv, &ivCopy);
  269. if(retval != UA_STATUSCODE_GOOD)
  270. return retval;
  271. mbedErr = mbedtls_aes_crypt_cbc(&aesContext, MBEDTLS_AES_DECRYPT, data->length,
  272. ivCopy.data, data->data, data->data);
  273. if(mbedErr)
  274. retval = UA_STATUSCODE_BADINTERNALERROR;
  275. UA_ByteString_deleteMembers(&ivCopy);
  276. return retval;
  277. }
  278. static UA_StatusCode
  279. sym_generateKey_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  280. const UA_ByteString *secret, const UA_ByteString *seed,
  281. UA_ByteString *out) {
  282. if(securityPolicy == NULL || secret == NULL || seed == NULL || out == NULL)
  283. return UA_STATUSCODE_BADINTERNALERROR;
  284. Basic256_PolicyContext *pc =
  285. (Basic256_PolicyContext *)securityPolicy->policyContext;
  286. return mbedtls_generateKey(&pc->sha1MdContext, secret, seed, out);
  287. }
  288. static UA_StatusCode
  289. sym_generateNonce_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  290. UA_ByteString *out) {
  291. if(securityPolicy == NULL || securityPolicy->policyContext == NULL || out == NULL)
  292. return UA_STATUSCODE_BADINTERNALERROR;
  293. Basic256_PolicyContext *pc =
  294. (Basic256_PolicyContext *)securityPolicy->policyContext;
  295. int mbedErr = mbedtls_ctr_drbg_random(&pc->drbgContext, out->data, out->length);
  296. if(mbedErr)
  297. return UA_STATUSCODE_BADUNEXPECTEDERROR;
  298. return UA_STATUSCODE_GOOD;
  299. }
  300. /*****************/
  301. /* ChannelModule */
  302. /*****************/
  303. /* Assumes that the certificate has been verified externally */
  304. static UA_StatusCode
  305. parseRemoteCertificate_sp_basic256(Basic256_ChannelContext *cc,
  306. const UA_ByteString *remoteCertificate) {
  307. if(remoteCertificate == NULL || cc == NULL)
  308. return UA_STATUSCODE_BADINTERNALERROR;
  309. /* Parse the certificate */
  310. int mbedErr = mbedtls_x509_crt_parse(&cc->remoteCertificate, remoteCertificate->data,
  311. remoteCertificate->length);
  312. if(mbedErr)
  313. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  314. /* Check the key length */
  315. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  316. if(rsaContext->len < UA_SECURITYPOLICY_BASIC256_MINASYMKEYLENGTH ||
  317. rsaContext->len > UA_SECURITYPOLICY_BASIC256_MAXASYMKEYLENGTH)
  318. return UA_STATUSCODE_BADCERTIFICATEUSENOTALLOWED;
  319. return UA_STATUSCODE_GOOD;
  320. }
  321. static void
  322. channelContext_deleteContext_sp_basic256(Basic256_ChannelContext *cc) {
  323. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  324. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  325. UA_ByteString_deleteMembers(&cc->localSymIv);
  326. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  327. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  328. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  329. mbedtls_x509_crt_free(&cc->remoteCertificate);
  330. UA_free(cc);
  331. }
  332. static UA_StatusCode
  333. channelContext_newContext_sp_basic256(const UA_SecurityPolicy *securityPolicy,
  334. const UA_ByteString *remoteCertificate,
  335. void **pp_contextData) {
  336. if(securityPolicy == NULL || remoteCertificate == NULL || pp_contextData == NULL)
  337. return UA_STATUSCODE_BADINTERNALERROR;
  338. /* Allocate the channel context */
  339. *pp_contextData = UA_malloc(sizeof(Basic256_ChannelContext));
  340. if(*pp_contextData == NULL)
  341. return UA_STATUSCODE_BADOUTOFMEMORY;
  342. Basic256_ChannelContext *cc = (Basic256_ChannelContext *)*pp_contextData;
  343. /* Initialize the channel context */
  344. cc->policyContext = (Basic256_PolicyContext *)securityPolicy->policyContext;
  345. UA_ByteString_init(&cc->localSymSigningKey);
  346. UA_ByteString_init(&cc->localSymEncryptingKey);
  347. UA_ByteString_init(&cc->localSymIv);
  348. UA_ByteString_init(&cc->remoteSymSigningKey);
  349. UA_ByteString_init(&cc->remoteSymEncryptingKey);
  350. UA_ByteString_init(&cc->remoteSymIv);
  351. mbedtls_x509_crt_init(&cc->remoteCertificate);
  352. // TODO: this can be optimized so that we dont allocate memory before parsing the certificate
  353. UA_StatusCode retval = parseRemoteCertificate_sp_basic256(cc, remoteCertificate);
  354. if(retval != UA_STATUSCODE_GOOD) {
  355. channelContext_deleteContext_sp_basic256(cc);
  356. *pp_contextData = NULL;
  357. }
  358. return retval;
  359. }
  360. static UA_StatusCode
  361. channelContext_setLocalSymEncryptingKey_sp_basic256(Basic256_ChannelContext *cc,
  362. const UA_ByteString *key) {
  363. if(key == NULL || cc == NULL)
  364. return UA_STATUSCODE_BADINTERNALERROR;
  365. UA_ByteString_deleteMembers(&cc->localSymEncryptingKey);
  366. return UA_ByteString_copy(key, &cc->localSymEncryptingKey);
  367. }
  368. static UA_StatusCode
  369. channelContext_setLocalSymSigningKey_sp_basic256(Basic256_ChannelContext *cc,
  370. const UA_ByteString *key) {
  371. if(key == NULL || cc == NULL)
  372. return UA_STATUSCODE_BADINTERNALERROR;
  373. UA_ByteString_deleteMembers(&cc->localSymSigningKey);
  374. return UA_ByteString_copy(key, &cc->localSymSigningKey);
  375. }
  376. static UA_StatusCode
  377. channelContext_setLocalSymIv_sp_basic256(Basic256_ChannelContext *cc,
  378. const UA_ByteString *iv) {
  379. if(iv == NULL || cc == NULL)
  380. return UA_STATUSCODE_BADINTERNALERROR;
  381. UA_ByteString_deleteMembers(&cc->localSymIv);
  382. return UA_ByteString_copy(iv, &cc->localSymIv);
  383. }
  384. static UA_StatusCode
  385. channelContext_setRemoteSymEncryptingKey_sp_basic256(Basic256_ChannelContext *cc,
  386. const UA_ByteString *key) {
  387. if(key == NULL || cc == NULL)
  388. return UA_STATUSCODE_BADINTERNALERROR;
  389. UA_ByteString_deleteMembers(&cc->remoteSymEncryptingKey);
  390. return UA_ByteString_copy(key, &cc->remoteSymEncryptingKey);
  391. }
  392. static UA_StatusCode
  393. channelContext_setRemoteSymSigningKey_sp_basic256(Basic256_ChannelContext *cc,
  394. const UA_ByteString *key) {
  395. if(key == NULL || cc == NULL)
  396. return UA_STATUSCODE_BADINTERNALERROR;
  397. UA_ByteString_deleteMembers(&cc->remoteSymSigningKey);
  398. return UA_ByteString_copy(key, &cc->remoteSymSigningKey);
  399. }
  400. static UA_StatusCode
  401. channelContext_setRemoteSymIv_sp_basic256(Basic256_ChannelContext *cc,
  402. const UA_ByteString *iv) {
  403. if(iv == NULL || cc == NULL)
  404. return UA_STATUSCODE_BADINTERNALERROR;
  405. UA_ByteString_deleteMembers(&cc->remoteSymIv);
  406. return UA_ByteString_copy(iv, &cc->remoteSymIv);
  407. }
  408. static UA_StatusCode
  409. channelContext_compareCertificate_sp_basic256(const Basic256_ChannelContext *cc,
  410. const UA_ByteString *certificate) {
  411. if(cc == NULL || certificate == NULL)
  412. return UA_STATUSCODE_BADINTERNALERROR;
  413. mbedtls_x509_crt cert;
  414. mbedtls_x509_crt_init(&cert);
  415. int mbedErr = mbedtls_x509_crt_parse(&cert, certificate->data, certificate->length);
  416. if(mbedErr)
  417. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  418. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  419. if(cert.raw.len != cc->remoteCertificate.raw.len ||
  420. memcmp(cert.raw.p, cc->remoteCertificate.raw.p, cert.raw.len) != 0)
  421. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  422. mbedtls_x509_crt_free(&cert);
  423. return retval;
  424. }
  425. static void
  426. deleteMembers_sp_basic256(UA_SecurityPolicy *securityPolicy) {
  427. if(securityPolicy == NULL)
  428. return;
  429. if(securityPolicy->policyContext == NULL)
  430. return;
  431. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  432. /* delete all allocated members in the context */
  433. Basic256_PolicyContext *pc = (Basic256_PolicyContext *)
  434. securityPolicy->policyContext;
  435. mbedtls_ctr_drbg_free(&pc->drbgContext);
  436. mbedtls_entropy_free(&pc->entropyContext);
  437. mbedtls_pk_free(&pc->localPrivateKey);
  438. mbedtls_md_free(&pc->sha1MdContext);
  439. UA_ByteString_deleteMembers(&pc->localCertThumbprint);
  440. UA_LOG_DEBUG(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  441. "Deleted members of EndpointContext for sp_basic256");
  442. UA_free(pc);
  443. securityPolicy->policyContext = NULL;
  444. }
  445. static UA_StatusCode
  446. updateCertificateAndPrivateKey_sp_basic256(UA_SecurityPolicy *securityPolicy,
  447. const UA_ByteString newCertificate,
  448. const UA_ByteString newPrivateKey) {
  449. if(securityPolicy == NULL)
  450. return UA_STATUSCODE_BADINTERNALERROR;
  451. if(securityPolicy->policyContext == NULL)
  452. return UA_STATUSCODE_BADINTERNALERROR;
  453. Basic256_PolicyContext *pc = (Basic256_PolicyContext *)
  454. securityPolicy->policyContext;
  455. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  456. UA_StatusCode retval = UA_ByteString_allocBuffer(&securityPolicy->localCertificate,
  457. newCertificate.length + 1);
  458. if(retval != UA_STATUSCODE_GOOD)
  459. return retval;
  460. memcpy(securityPolicy->localCertificate.data, newCertificate.data, newCertificate.length);
  461. securityPolicy->localCertificate.data[newCertificate.length] = '\0';
  462. securityPolicy->localCertificate.length--;
  463. /* Set the new private key */
  464. mbedtls_pk_free(&pc->localPrivateKey);
  465. mbedtls_pk_init(&pc->localPrivateKey);
  466. int mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  467. newPrivateKey.data, newPrivateKey.length,
  468. NULL, 0);
  469. if(mbedErr) {
  470. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  471. goto error;
  472. }
  473. retval = asym_makeThumbprint_sp_basic256(pc->securityPolicy,
  474. &securityPolicy->localCertificate,
  475. &pc->localCertThumbprint);
  476. if(retval != UA_STATUSCODE_GOOD)
  477. goto error;
  478. return retval;
  479. error:
  480. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  481. "Could not update certificate and private key");
  482. if(securityPolicy->policyContext != NULL)
  483. deleteMembers_sp_basic256(securityPolicy);
  484. return retval;
  485. }
  486. static UA_StatusCode
  487. policyContext_newContext_sp_basic256(UA_SecurityPolicy *securityPolicy,
  488. const UA_ByteString localPrivateKey) {
  489. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  490. if(securityPolicy == NULL)
  491. return UA_STATUSCODE_BADINTERNALERROR;
  492. Basic256_PolicyContext *pc = (Basic256_PolicyContext *)
  493. UA_malloc(sizeof(Basic256_PolicyContext));
  494. securityPolicy->policyContext = (void *)pc;
  495. if(!pc) {
  496. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  497. goto error;
  498. }
  499. /* Initialize the PolicyContext */
  500. memset(pc, 0, sizeof(Basic256_PolicyContext));
  501. mbedtls_ctr_drbg_init(&pc->drbgContext);
  502. mbedtls_entropy_init(&pc->entropyContext);
  503. mbedtls_pk_init(&pc->localPrivateKey);
  504. mbedtls_md_init(&pc->sha1MdContext);
  505. pc->securityPolicy = securityPolicy;
  506. /* Initialized the message digest */
  507. const mbedtls_md_info_t *mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  508. int mbedErr = mbedtls_md_setup(&pc->sha1MdContext, mdInfo, MBEDTLS_MD_SHA1);
  509. if(mbedErr) {
  510. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  511. goto error;
  512. }
  513. /* Add the system entropy source */
  514. mbedErr = mbedtls_entropy_add_source(&pc->entropyContext,
  515. mbedtls_platform_entropy_poll, NULL, 0,
  516. MBEDTLS_ENTROPY_SOURCE_STRONG);
  517. if(mbedErr) {
  518. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  519. goto error;
  520. }
  521. /* Seed the RNG */
  522. char *personalization = "open62541-drbg";
  523. mbedErr = mbedtls_ctr_drbg_seed(&pc->drbgContext, mbedtls_entropy_func,
  524. &pc->entropyContext,
  525. (const unsigned char *)personalization, 14);
  526. if(mbedErr) {
  527. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  528. goto error;
  529. }
  530. /* Set the private key */
  531. mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey, localPrivateKey.data,
  532. localPrivateKey.length, NULL, 0);
  533. if(mbedErr) {
  534. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  535. goto error;
  536. }
  537. /* Set the local certificate thumbprint */
  538. retval = UA_ByteString_allocBuffer(&pc->localCertThumbprint, UA_SHA1_LENGTH);
  539. if(retval != UA_STATUSCODE_GOOD)
  540. goto error;
  541. retval = asym_makeThumbprint_sp_basic256(pc->securityPolicy,
  542. &securityPolicy->localCertificate,
  543. &pc->localCertThumbprint);
  544. if(retval != UA_STATUSCODE_GOOD)
  545. goto error;
  546. return UA_STATUSCODE_GOOD;
  547. error:
  548. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  549. "Could not create securityContext");
  550. if(securityPolicy->policyContext != NULL)
  551. deleteMembers_sp_basic256(securityPolicy);
  552. return retval;
  553. }
  554. UA_StatusCode
  555. UA_SecurityPolicy_Basic256(UA_SecurityPolicy *policy,
  556. UA_CertificateVerification *certificateVerification,
  557. const UA_ByteString localCertificate,
  558. const UA_ByteString localPrivateKey, const UA_Logger *logger) {
  559. memset(policy, 0, sizeof(UA_SecurityPolicy));
  560. policy->logger = logger;
  561. policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256");
  562. UA_SecurityPolicyAsymmetricModule *const asymmetricModule = &policy->asymmetricModule;
  563. UA_SecurityPolicySymmetricModule *const symmetricModule = &policy->symmetricModule;
  564. UA_SecurityPolicyChannelModule *const channelModule = &policy->channelModule;
  565. /* Copy the certificate and add a NULL to the end */
  566. UA_StatusCode retval =
  567. UA_ByteString_allocBuffer(&policy->localCertificate, localCertificate.length + 1);
  568. if(retval != UA_STATUSCODE_GOOD)
  569. return retval;
  570. memcpy(policy->localCertificate.data, localCertificate.data, localCertificate.length);
  571. policy->localCertificate.data[localCertificate.length] = '\0';
  572. policy->localCertificate.length--;
  573. policy->certificateVerification = certificateVerification;
  574. /* AsymmetricModule */
  575. UA_SecurityPolicySignatureAlgorithm *asym_signatureAlgorithm =
  576. &asymmetricModule->cryptoModule.signatureAlgorithm;
  577. asym_signatureAlgorithm->uri =
  578. UA_STRING("http://www.w3.org/2000/09/xmldsig#rsa-sha1\0");
  579. asym_signatureAlgorithm->verify =
  580. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  581. const UA_ByteString *, const UA_ByteString *))asym_verify_sp_basic256;
  582. asym_signatureAlgorithm->sign =
  583. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  584. const UA_ByteString *, UA_ByteString *))asym_sign_sp_basic256;
  585. asym_signatureAlgorithm->getLocalSignatureSize =
  586. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getLocalSignatureSize_sp_basic256;
  587. asym_signatureAlgorithm->getRemoteSignatureSize =
  588. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteSignatureSize_sp_basic256;
  589. asym_signatureAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  590. asym_signatureAlgorithm->getRemoteKeyLength = NULL; // TODO: Write function
  591. UA_SecurityPolicyEncryptionAlgorithm *asym_encryptionAlgorithm =
  592. &asymmetricModule->cryptoModule.encryptionAlgorithm;
  593. asym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#rsa-oaep\0");
  594. asym_encryptionAlgorithm->encrypt =
  595. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))asym_encrypt_sp_basic256;
  596. asym_encryptionAlgorithm->decrypt =
  597. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))
  598. asym_decrypt_sp_basic256;
  599. asym_encryptionAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  600. asym_encryptionAlgorithm->getRemoteKeyLength =
  601. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteEncryptionKeyLength_sp_basic256;
  602. asym_encryptionAlgorithm->getLocalBlockSize = NULL; // TODO: Write function
  603. asym_encryptionAlgorithm->getRemoteBlockSize = (size_t (*)(const UA_SecurityPolicy *,
  604. const void *))asym_getRemoteBlockSize_sp_basic256;
  605. asym_encryptionAlgorithm->getLocalPlainTextBlockSize = NULL; // TODO: Write function
  606. asym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  607. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemotePlainTextBlockSize_sp_basic256;
  608. asymmetricModule->makeCertificateThumbprint = asym_makeThumbprint_sp_basic256;
  609. asymmetricModule->compareCertificateThumbprint =
  610. asymmetricModule_compareCertificateThumbprint_sp_basic256;
  611. /* SymmetricModule */
  612. symmetricModule->generateKey = sym_generateKey_sp_basic256;
  613. symmetricModule->generateNonce = sym_generateNonce_sp_basic256;
  614. UA_SecurityPolicySignatureAlgorithm *sym_signatureAlgorithm =
  615. &symmetricModule->cryptoModule.signatureAlgorithm;
  616. sym_signatureAlgorithm->uri =
  617. UA_STRING("http://www.w3.org/2000/09/xmldsig#hmac-sha1\0");
  618. sym_signatureAlgorithm->verify =
  619. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *, const UA_ByteString *,
  620. const UA_ByteString *))sym_verify_sp_basic256;
  621. sym_signatureAlgorithm->sign =
  622. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  623. const UA_ByteString *, UA_ByteString *))sym_sign_sp_basic256;
  624. sym_signatureAlgorithm->getLocalSignatureSize = sym_getSignatureSize_sp_basic256;
  625. sym_signatureAlgorithm->getRemoteSignatureSize = sym_getSignatureSize_sp_basic256;
  626. sym_signatureAlgorithm->getLocalKeyLength =
  627. (size_t (*)(const UA_SecurityPolicy *,
  628. const void *))sym_getSigningKeyLength_sp_basic256;
  629. sym_signatureAlgorithm->getRemoteKeyLength =
  630. (size_t (*)(const UA_SecurityPolicy *,
  631. const void *))sym_getSigningKeyLength_sp_basic256;
  632. UA_SecurityPolicyEncryptionAlgorithm *sym_encryptionAlgorithm =
  633. &symmetricModule->cryptoModule.encryptionAlgorithm;
  634. sym_encryptionAlgorithm->uri = UA_STRING("http://www.w3.org/2001/04/xmlenc#aes256-cbc\0");
  635. sym_encryptionAlgorithm->encrypt =
  636. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_encrypt_sp_basic256;
  637. sym_encryptionAlgorithm->decrypt =
  638. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_decrypt_sp_basic256;
  639. sym_encryptionAlgorithm->getLocalKeyLength = sym_getEncryptionKeyLength_sp_basic256;
  640. sym_encryptionAlgorithm->getRemoteKeyLength = sym_getEncryptionKeyLength_sp_basic256;
  641. sym_encryptionAlgorithm->getLocalBlockSize =
  642. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256;
  643. sym_encryptionAlgorithm->getRemoteBlockSize =
  644. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic256;
  645. sym_encryptionAlgorithm->getLocalPlainTextBlockSize =
  646. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256;
  647. sym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  648. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic256;
  649. symmetricModule->secureChannelNonceLength = 32;
  650. // Use the same signature algorithm as the asymmetric component for certificate signing (see standard)
  651. policy->certificateSigningAlgorithm = policy->asymmetricModule.cryptoModule.signatureAlgorithm;
  652. /* ChannelModule */
  653. channelModule->newContext = channelContext_newContext_sp_basic256;
  654. channelModule->deleteContext = (void (*)(void *))
  655. channelContext_deleteContext_sp_basic256;
  656. channelModule->setLocalSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  657. channelContext_setLocalSymEncryptingKey_sp_basic256;
  658. channelModule->setLocalSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  659. channelContext_setLocalSymSigningKey_sp_basic256;
  660. channelModule->setLocalSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  661. channelContext_setLocalSymIv_sp_basic256;
  662. channelModule->setRemoteSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  663. channelContext_setRemoteSymEncryptingKey_sp_basic256;
  664. channelModule->setRemoteSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  665. channelContext_setRemoteSymSigningKey_sp_basic256;
  666. channelModule->setRemoteSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  667. channelContext_setRemoteSymIv_sp_basic256;
  668. channelModule->compareCertificate = (UA_StatusCode (*)(const void *, const UA_ByteString *))
  669. channelContext_compareCertificate_sp_basic256;
  670. policy->updateCertificateAndPrivateKey = updateCertificateAndPrivateKey_sp_basic256;
  671. policy->deleteMembers = deleteMembers_sp_basic256;
  672. return policyContext_newContext_sp_basic256(policy, localPrivateKey);
  673. }