ua_securitypolicy_basic256sha256.c 44 KB

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