ua_securitypolicy_basic128rsa15.c 36 KB

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