ua_securitypolicy_basic256.c 34 KB

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