ua_securitypolicy_basic128rsa15.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. //#include "ua_types.h"
  9. //#include "ua_plugin_pki.h"
  10. //#include "ua_securitypolicies.h"
  11. //#include "ua_securitypolicy_mbedtls_common.h"
  12. //#include "ua_types_generated_handling.h"
  13. //#include "ua_util.h"
  14. #ifdef UA_ENABLE_ENCRYPTION
  15. #include <mbedtls/aes.h>
  16. #include <mbedtls/md.h>
  17. #include <mbedtls/x509_crt.h>
  18. #include <mbedtls/ctr_drbg.h>
  19. #include <mbedtls/entropy.h>
  20. #include <mbedtls/entropy_poll.h>
  21. #include <mbedtls/error.h>
  22. #include <mbedtls/version.h>
  23. #include <mbedtls/sha1.h>
  24. /* Notes:
  25. * mbedTLS' AES allows in-place encryption and decryption. Sow we don't have to
  26. * allocate temp buffers.
  27. * https://tls.mbed.org/discussions/generic/in-place-decryption-with-aes256-same-input-output-buffer
  28. */
  29. #define UA_SECURITYPOLICY_BASIC128RSA15_RSAPADDING_LEN 11
  30. #define UA_SECURITYPOLICY_BASIC128RSA15_SYM_KEY_LENGTH 16
  31. #define UA_BASIC128RSA15_SYM_SIGNING_KEY_LENGTH 16
  32. #define UA_SECURITYPOLICY_BASIC128RSA15_SYM_ENCRYPTION_BLOCK_SIZE 16
  33. #define UA_SECURITYPOLICY_BASIC128RSA15_SYM_PLAIN_TEXT_BLOCK_SIZE 16
  34. #define UA_SECURITYPOLICY_BASIC128RSA15_MINASYMKEYLENGTH 128
  35. #define UA_SECURITYPOLICY_BASIC128RSA15_MAXASYMKEYLENGTH 256
  36. typedef struct {
  37. const UA_SecurityPolicy *securityPolicy;
  38. UA_ByteString localCertThumbprint;
  39. mbedtls_ctr_drbg_context drbgContext;
  40. mbedtls_entropy_context entropyContext;
  41. mbedtls_md_context_t sha1MdContext;
  42. mbedtls_pk_context localPrivateKey;
  43. } Basic128Rsa15_PolicyContext;
  44. typedef struct {
  45. Basic128Rsa15_PolicyContext *policyContext;
  46. UA_ByteString localSymSigningKey;
  47. UA_ByteString localSymEncryptingKey;
  48. UA_ByteString localSymIv;
  49. UA_ByteString remoteSymSigningKey;
  50. UA_ByteString remoteSymEncryptingKey;
  51. UA_ByteString remoteSymIv;
  52. mbedtls_x509_crt remoteCertificate;
  53. } Basic128Rsa15_ChannelContext;
  54. /********************/
  55. /* AsymmetricModule */
  56. /********************/
  57. static UA_StatusCode
  58. asym_verify_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  59. Basic128Rsa15_ChannelContext *cc,
  60. const UA_ByteString *message,
  61. const UA_ByteString *signature) {
  62. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  63. return UA_STATUSCODE_BADINTERNALERROR;
  64. return mbedtls_verifySig_sha1(&cc->remoteCertificate, message, signature);
  65. }
  66. static UA_StatusCode
  67. asym_sign_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  68. Basic128Rsa15_ChannelContext *cc,
  69. const UA_ByteString *message,
  70. UA_ByteString *signature) {
  71. if(securityPolicy == NULL || message == NULL || signature == NULL || cc == NULL)
  72. return UA_STATUSCODE_BADINTERNALERROR;
  73. Basic128Rsa15_PolicyContext *pc = cc->policyContext;
  74. return mbedtls_sign_sha1(&pc->localPrivateKey, &pc->drbgContext,
  75. message, signature);
  76. }
  77. static size_t
  78. asym_getLocalSignatureSize_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  79. const Basic128Rsa15_ChannelContext *cc) {
  80. if(securityPolicy == NULL || cc == NULL)
  81. return 0;
  82. return mbedtls_pk_rsa(cc->policyContext->localPrivateKey)->len;
  83. }
  84. static size_t
  85. asym_getRemoteSignatureSize_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  86. const Basic128Rsa15_ChannelContext *cc) {
  87. if(securityPolicy == NULL || cc == NULL)
  88. return 0;
  89. return mbedtls_pk_rsa(cc->remoteCertificate.pk)->len;
  90. }
  91. static UA_StatusCode
  92. asym_encrypt_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  93. Basic128Rsa15_ChannelContext *cc,
  94. UA_ByteString *data) {
  95. if(securityPolicy == NULL || cc == NULL || data == NULL)
  96. return UA_STATUSCODE_BADINTERNALERROR;
  97. const size_t plainTextBlockSize = securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm.
  98. getRemotePlainTextBlockSize(securityPolicy, cc);
  99. if(data->length % plainTextBlockSize != 0)
  100. return UA_STATUSCODE_BADINTERNALERROR;
  101. mbedtls_rsa_context *remoteRsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  102. mbedtls_rsa_set_padding(remoteRsaContext, MBEDTLS_RSA_PKCS_V15, 0);
  103. UA_ByteString encrypted;
  104. const size_t bufferOverhead =
  105. UA_SecurityPolicy_getRemoteAsymEncryptionBufferLengthOverhead(securityPolicy, cc, data->length);
  106. UA_StatusCode retval = UA_ByteString_allocBuffer(&encrypted, data->length + bufferOverhead);
  107. if(retval != UA_STATUSCODE_GOOD)
  108. return retval;
  109. size_t lenDataToEncrypt = data->length;
  110. size_t inOffset = 0;
  111. size_t offset = 0;
  112. size_t outLength = 0;
  113. Basic128Rsa15_PolicyContext *pc = cc->policyContext;
  114. while(lenDataToEncrypt >= plainTextBlockSize) {
  115. int mbedErr = mbedtls_pk_encrypt(&cc->remoteCertificate.pk,
  116. data->data + inOffset, plainTextBlockSize,
  117. encrypted.data + offset, &outLength,
  118. encrypted.length - offset,
  119. mbedtls_ctr_drbg_random,
  120. &pc->drbgContext);
  121. if(mbedErr) {
  122. UA_ByteString_deleteMembers(&encrypted);
  123. return UA_STATUSCODE_BADINTERNALERROR;
  124. }
  125. inOffset += plainTextBlockSize;
  126. offset += outLength;
  127. lenDataToEncrypt -= plainTextBlockSize;
  128. }
  129. memcpy(data->data, encrypted.data, offset);
  130. UA_ByteString_deleteMembers(&encrypted);
  131. return UA_STATUSCODE_GOOD;
  132. }
  133. static UA_StatusCode
  134. asym_decrypt_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  135. Basic128Rsa15_ChannelContext *cc,
  136. UA_ByteString *data) {
  137. if(securityPolicy == NULL || cc == NULL || data == NULL)
  138. return UA_STATUSCODE_BADINTERNALERROR;
  139. mbedtls_rsa_context *rsaContext = mbedtls_pk_rsa(cc->policyContext->localPrivateKey);
  140. mbedtls_rsa_set_padding(rsaContext, MBEDTLS_RSA_PKCS_V15, 0);
  141. if(data->length % rsaContext->len != 0)
  142. return UA_STATUSCODE_BADINTERNALERROR;
  143. size_t inOffset = 0;
  144. size_t outOffset = 0;
  145. size_t outLength = 0;
  146. unsigned char buf[512];
  147. while(inOffset < data->length) {
  148. int mbedErr = mbedtls_pk_decrypt(&cc->policyContext->localPrivateKey,
  149. data->data + inOffset, rsaContext->len,
  150. buf, &outLength, 512, NULL, NULL);
  151. if(mbedErr)
  152. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  153. memcpy(data->data + outOffset, buf, outLength);
  154. inOffset += rsaContext->len;
  155. outOffset += outLength;
  156. }
  157. data->length = outOffset;
  158. return UA_STATUSCODE_GOOD;
  159. }
  160. static size_t
  161. asym_getRemoteEncryptionKeyLength_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  162. const Basic128Rsa15_ChannelContext *cc) {
  163. return mbedtls_pk_get_len(&cc->remoteCertificate.pk) * 8;
  164. }
  165. static size_t
  166. asym_getRemoteBlockSize_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  167. const Basic128Rsa15_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_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  173. const Basic128Rsa15_ChannelContext *cc) {
  174. mbedtls_rsa_context *const rsaContext = mbedtls_pk_rsa(cc->remoteCertificate.pk);
  175. return rsaContext->len - UA_SECURITYPOLICY_BASIC128RSA15_RSAPADDING_LEN;
  176. }
  177. static UA_StatusCode
  178. asym_makeThumbprint_sp_basic128rsa15(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_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  187. const UA_ByteString *certificateThumbprint) {
  188. if(securityPolicy == NULL || certificateThumbprint == NULL)
  189. return UA_STATUSCODE_BADINTERNALERROR;
  190. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_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_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  200. Basic128Rsa15_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_SHA1_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. Basic128Rsa15_PolicyContext *pc =
  212. (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  213. unsigned char mac[UA_SHA1_LENGTH];
  214. mbedtls_hmac(&pc->sha1MdContext, &cc->remoteSymSigningKey, message, mac);
  215. /* Compare with Signature */
  216. if(!UA_constantTimeEqual(signature->data, mac, UA_SHA1_LENGTH))
  217. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  218. return UA_STATUSCODE_GOOD;
  219. }
  220. static UA_StatusCode
  221. sym_sign_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  222. const Basic128Rsa15_ChannelContext *cc,
  223. const UA_ByteString *message,
  224. UA_ByteString *signature) {
  225. if(signature->length != UA_SHA1_LENGTH)
  226. return UA_STATUSCODE_BADINTERNALERROR;
  227. mbedtls_hmac(&cc->policyContext->sha1MdContext, &cc->localSymSigningKey,
  228. message, signature->data);
  229. return UA_STATUSCODE_GOOD;
  230. }
  231. static size_t
  232. sym_getSignatureSize_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  233. const void *channelContext) {
  234. return UA_SHA1_LENGTH;
  235. }
  236. static size_t
  237. sym_getSigningKeyLength_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  238. const void *const channelContext) {
  239. return UA_BASIC128RSA15_SYM_SIGNING_KEY_LENGTH;
  240. }
  241. static size_t
  242. sym_getEncryptionKeyLength_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  243. const void *channelContext) {
  244. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_KEY_LENGTH;
  245. }
  246. static size_t
  247. sym_getEncryptionBlockSize_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  248. const void *const channelContext) {
  249. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_ENCRYPTION_BLOCK_SIZE;
  250. }
  251. static size_t
  252. sym_getPlainTextBlockSize_sp_basic128rsa15(const UA_SecurityPolicy *const securityPolicy,
  253. const void *const channelContext) {
  254. return UA_SECURITYPOLICY_BASIC128RSA15_SYM_PLAIN_TEXT_BLOCK_SIZE;
  255. }
  256. static UA_StatusCode
  257. sym_encrypt_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  258. const Basic128Rsa15_ChannelContext *cc,
  259. UA_ByteString *data) {
  260. if(securityPolicy == NULL || cc == NULL || data == NULL)
  261. return UA_STATUSCODE_BADINTERNALERROR;
  262. if(cc->localSymIv.length !=
  263. securityPolicy->symmetricModule.cryptoModule.encryptionAlgorithm.getLocalBlockSize(securityPolicy, cc))
  264. return UA_STATUSCODE_BADINTERNALERROR;
  265. size_t plainTextBlockSize =
  266. securityPolicy->symmetricModule.cryptoModule.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_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  292. const Basic128Rsa15_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_basic128rsa15(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. Basic128Rsa15_PolicyContext *pc =
  328. (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  329. return mbedtls_generateKey(&pc->sha1MdContext, secret, seed, out);
  330. }
  331. static UA_StatusCode
  332. sym_generateNonce_sp_basic128rsa15(const UA_SecurityPolicy *securityPolicy,
  333. UA_ByteString *out) {
  334. if(securityPolicy == NULL || securityPolicy->policyContext == NULL || out == NULL)
  335. return UA_STATUSCODE_BADINTERNALERROR;
  336. Basic128Rsa15_PolicyContext *pc =
  337. (Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_BASIC128RSA15_MINASYMKEYLENGTH ||
  360. rsaContext->len > UA_SECURITYPOLICY_BASIC128RSA15_MAXASYMKEYLENGTH)
  361. return UA_STATUSCODE_BADCERTIFICATEUSENOTALLOWED;
  362. return UA_STATUSCODE_GOOD;
  363. }
  364. static void
  365. channelContext_deleteContext_sp_basic128rsa15(Basic128Rsa15_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_basic128rsa15(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(Basic128Rsa15_ChannelContext));
  383. if(*pp_contextData == NULL)
  384. return UA_STATUSCODE_BADOUTOFMEMORY;
  385. Basic128Rsa15_ChannelContext *cc = (Basic128Rsa15_ChannelContext *)*pp_contextData;
  386. /* Initialize the channel context */
  387. cc->policyContext = (Basic128Rsa15_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_basic128rsa15(cc, remoteCertificate);
  397. if(retval != UA_STATUSCODE_GOOD) {
  398. channelContext_deleteContext_sp_basic128rsa15(cc);
  399. *pp_contextData = NULL;
  400. }
  401. return retval;
  402. }
  403. static UA_StatusCode
  404. channelContext_setLocalSymEncryptingKey_sp_basic128rsa15(Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_basic128rsa15(Basic128Rsa15_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_basic128rsa15(const Basic128Rsa15_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_basic128rsa15(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. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_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->sha1MdContext);
  482. UA_ByteString_deleteMembers(&pc->localCertThumbprint);
  483. UA_LOG_DEBUG(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  484. "Deleted members of EndpointContext for sp_basic128rsa15");
  485. UA_free(pc);
  486. securityPolicy->policyContext = NULL;
  487. }
  488. static UA_StatusCode
  489. updateCertificateAndPrivateKey_sp_basic128rsa15(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. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_PolicyContext *)securityPolicy->policyContext;
  497. UA_ByteString_deleteMembers(&securityPolicy->localCertificate);
  498. UA_StatusCode retval = UA_ByteString_allocBuffer(&securityPolicy->localCertificate, newCertificate.length + 1);
  499. if(retval != UA_STATUSCODE_GOOD)
  500. return retval;
  501. memcpy(securityPolicy->localCertificate.data, newCertificate.data, newCertificate.length);
  502. securityPolicy->localCertificate.data[newCertificate.length] = '\0';
  503. securityPolicy->localCertificate.length--;
  504. /* Set the new private key */
  505. mbedtls_pk_free(&pc->localPrivateKey);
  506. mbedtls_pk_init(&pc->localPrivateKey);
  507. int mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  508. newPrivateKey.data, newPrivateKey.length,
  509. NULL, 0);
  510. if(mbedErr) {
  511. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  512. goto error;
  513. }
  514. retval = asym_makeThumbprint_sp_basic128rsa15(pc->securityPolicy,
  515. &securityPolicy->localCertificate,
  516. &pc->localCertThumbprint);
  517. if(retval != UA_STATUSCODE_GOOD)
  518. goto error;
  519. return retval;
  520. error:
  521. UA_LOG_ERROR(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
  522. "Could not update certificate and private key");
  523. if(securityPolicy->policyContext != NULL)
  524. deleteMembers_sp_basic128rsa15(securityPolicy);
  525. return retval;
  526. }
  527. static UA_StatusCode
  528. policyContext_newContext_sp_basic128rsa15(UA_SecurityPolicy *securityPolicy,
  529. const UA_ByteString localPrivateKey) {
  530. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  531. if(securityPolicy == NULL)
  532. return UA_STATUSCODE_BADINTERNALERROR;
  533. Basic128Rsa15_PolicyContext *pc = (Basic128Rsa15_PolicyContext *)
  534. UA_malloc(sizeof(Basic128Rsa15_PolicyContext));
  535. securityPolicy->policyContext = (void *)pc;
  536. if(!pc) {
  537. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  538. goto error;
  539. }
  540. /* Initialize the PolicyContext */
  541. memset(pc, 0, sizeof(Basic128Rsa15_PolicyContext));
  542. mbedtls_ctr_drbg_init(&pc->drbgContext);
  543. mbedtls_entropy_init(&pc->entropyContext);
  544. mbedtls_pk_init(&pc->localPrivateKey);
  545. mbedtls_md_init(&pc->sha1MdContext);
  546. pc->securityPolicy = securityPolicy;
  547. /* Initialized the message digest */
  548. const mbedtls_md_info_t *const mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  549. int mbedErr = mbedtls_md_setup(&pc->sha1MdContext, mdInfo, MBEDTLS_MD_SHA1);
  550. if(mbedErr) {
  551. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  552. goto error;
  553. }
  554. /* Add the system entropy source */
  555. mbedErr = mbedtls_entropy_add_source(&pc->entropyContext,
  556. mbedtls_platform_entropy_poll, NULL, 0,
  557. MBEDTLS_ENTROPY_SOURCE_STRONG);
  558. if(mbedErr) {
  559. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  560. goto error;
  561. }
  562. /* Seed the RNG */
  563. char *personalization = "open62541-drbg";
  564. mbedErr = mbedtls_ctr_drbg_seed(&pc->drbgContext, mbedtls_entropy_func,
  565. &pc->entropyContext,
  566. (const unsigned char *)personalization, 14);
  567. if(mbedErr) {
  568. retval = UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  569. goto error;
  570. }
  571. /* Set the private key */
  572. mbedErr = mbedtls_pk_parse_key(&pc->localPrivateKey,
  573. localPrivateKey.data, localPrivateKey.length,
  574. 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_basic128rsa15(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_basic128rsa15(securityPolicy);
  594. return retval;
  595. }
  596. UA_StatusCode
  597. UA_SecurityPolicy_Basic128Rsa15(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#Basic128Rsa15");
  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/2000/09/xmldsig#rsa-sha1\0");
  621. asym_signatureAlgorithm->verify =
  622. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  623. const UA_ByteString *, const UA_ByteString *))asym_verify_sp_basic128rsa15;
  624. asym_signatureAlgorithm->sign =
  625. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  626. const UA_ByteString *, UA_ByteString *))asym_sign_sp_basic128rsa15;
  627. asym_signatureAlgorithm->getLocalSignatureSize =
  628. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getLocalSignatureSize_sp_basic128rsa15;
  629. asym_signatureAlgorithm->getRemoteSignatureSize =
  630. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteSignatureSize_sp_basic128rsa15;
  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-1_5");
  636. asym_encryptionAlgorithm->encrypt =
  637. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))asym_encrypt_sp_basic128rsa15;
  638. asym_encryptionAlgorithm->decrypt =
  639. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))
  640. asym_decrypt_sp_basic128rsa15;
  641. asym_encryptionAlgorithm->getLocalKeyLength = NULL; // TODO: Write function
  642. asym_encryptionAlgorithm->getRemoteKeyLength =
  643. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemoteEncryptionKeyLength_sp_basic128rsa15;
  644. asym_encryptionAlgorithm->getLocalBlockSize = NULL; // TODO: Write function
  645. asym_encryptionAlgorithm->getRemoteBlockSize = (size_t (*)(const UA_SecurityPolicy *,
  646. const void *))asym_getRemoteBlockSize_sp_basic128rsa15;
  647. asym_encryptionAlgorithm->getLocalPlainTextBlockSize = NULL; // TODO: Write function
  648. asym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  649. (size_t (*)(const UA_SecurityPolicy *, const void *))asym_getRemotePlainTextBlockSize_sp_basic128rsa15;
  650. asymmetricModule->makeCertificateThumbprint = asym_makeThumbprint_sp_basic128rsa15;
  651. asymmetricModule->compareCertificateThumbprint =
  652. asymmetricModule_compareCertificateThumbprint_sp_basic128rsa15;
  653. /* SymmetricModule */
  654. symmetricModule->generateKey = sym_generateKey_sp_basic128rsa15;
  655. symmetricModule->generateNonce = sym_generateNonce_sp_basic128rsa15;
  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_basic128rsa15;
  663. sym_signatureAlgorithm->sign =
  664. (UA_StatusCode (*)(const UA_SecurityPolicy *, void *,
  665. const UA_ByteString *, UA_ByteString *))sym_sign_sp_basic128rsa15;
  666. sym_signatureAlgorithm->getLocalSignatureSize = sym_getSignatureSize_sp_basic128rsa15;
  667. sym_signatureAlgorithm->getRemoteSignatureSize = sym_getSignatureSize_sp_basic128rsa15;
  668. sym_signatureAlgorithm->getLocalKeyLength =
  669. (size_t (*)(const UA_SecurityPolicy *,
  670. const void *))sym_getSigningKeyLength_sp_basic128rsa15;
  671. sym_signatureAlgorithm->getRemoteKeyLength =
  672. (size_t (*)(const UA_SecurityPolicy *,
  673. const void *))sym_getSigningKeyLength_sp_basic128rsa15;
  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_basic128rsa15;
  679. sym_encryptionAlgorithm->decrypt =
  680. (UA_StatusCode(*)(const UA_SecurityPolicy *, void *, UA_ByteString *))sym_decrypt_sp_basic128rsa15;
  681. sym_encryptionAlgorithm->getLocalKeyLength = sym_getEncryptionKeyLength_sp_basic128rsa15;
  682. sym_encryptionAlgorithm->getRemoteKeyLength = sym_getEncryptionKeyLength_sp_basic128rsa15;
  683. sym_encryptionAlgorithm->getLocalBlockSize =
  684. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic128rsa15;
  685. sym_encryptionAlgorithm->getRemoteBlockSize =
  686. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getEncryptionBlockSize_sp_basic128rsa15;
  687. sym_encryptionAlgorithm->getLocalPlainTextBlockSize =
  688. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic128rsa15;
  689. sym_encryptionAlgorithm->getRemotePlainTextBlockSize =
  690. (size_t (*)(const UA_SecurityPolicy *, const void *))sym_getPlainTextBlockSize_sp_basic128rsa15;
  691. symmetricModule->secureChannelNonceLength = 16;
  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_basic128rsa15;
  696. channelModule->deleteContext = (void (*)(void *))
  697. channelContext_deleteContext_sp_basic128rsa15;
  698. channelModule->setLocalSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  699. channelContext_setLocalSymEncryptingKey_sp_basic128rsa15;
  700. channelModule->setLocalSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  701. channelContext_setLocalSymSigningKey_sp_basic128rsa15;
  702. channelModule->setLocalSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  703. channelContext_setLocalSymIv_sp_basic128rsa15;
  704. channelModule->setRemoteSymEncryptingKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  705. channelContext_setRemoteSymEncryptingKey_sp_basic128rsa15;
  706. channelModule->setRemoteSymSigningKey = (UA_StatusCode (*)(void *, const UA_ByteString *))
  707. channelContext_setRemoteSymSigningKey_sp_basic128rsa15;
  708. channelModule->setRemoteSymIv = (UA_StatusCode (*)(void *, const UA_ByteString *))
  709. channelContext_setRemoteSymIv_sp_basic128rsa15;
  710. channelModule->compareCertificate = (UA_StatusCode (*)(const void *, const UA_ByteString *))
  711. channelContext_compareCertificate_sp_basic128rsa15;
  712. policy->updateCertificateAndPrivateKey = updateCertificateAndPrivateKey_sp_basic128rsa15;
  713. policy->deleteMembers = deleteMembers_sp_basic128rsa15;
  714. return policyContext_newContext_sp_basic128rsa15(policy, localPrivateKey);
  715. }
  716. #endif