ua_securitypolicy_basic256sha256.c 37 KB

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