ua_securitypolicy_basic128rsa15.c 36 KB

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