ua_securitypolicy_basic256.c 33 KB

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