ua_securitypolicy_basic128rsa15.c 36 KB

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