2
0

cms_ec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <openssl/cms.h>
  11. #include <openssl/err.h>
  12. #include <openssl/decoder.h>
  13. #include "internal/sizes.h"
  14. #include "crypto/asn1.h"
  15. #include "crypto/evp.h"
  16. #include "cms_local.h"
  17. static EVP_PKEY *pkey_type2param(int ptype, const void *pval,
  18. OSSL_LIB_CTX *libctx, const char *propq)
  19. {
  20. EVP_PKEY *pkey = NULL;
  21. EVP_PKEY_CTX *pctx = NULL;
  22. OSSL_DECODER_CTX *ctx = NULL;
  23. if (ptype == V_ASN1_SEQUENCE) {
  24. const ASN1_STRING *pstr = pval;
  25. const unsigned char *pm = pstr->data;
  26. size_t pmlen = (size_t)pstr->length;
  27. int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  28. ctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, "EC",
  29. selection, libctx, propq);
  30. if (ctx == NULL)
  31. goto err;
  32. if (!OSSL_DECODER_from_data(ctx, &pm, &pmlen)) {
  33. ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
  34. goto err;
  35. }
  36. OSSL_DECODER_CTX_free(ctx);
  37. return pkey;
  38. } else if (ptype == V_ASN1_OBJECT) {
  39. const ASN1_OBJECT *poid = pval;
  40. char groupname[OSSL_MAX_NAME_SIZE];
  41. /* type == V_ASN1_OBJECT => the parameters are given by an asn1 OID */
  42. pctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
  43. if (pctx == NULL || EVP_PKEY_paramgen_init(pctx) <= 0)
  44. goto err;
  45. if (OBJ_obj2txt(groupname, sizeof(groupname), poid, 0) <= 0
  46. || EVP_PKEY_CTX_set_group_name(pctx, groupname) <= 0) {
  47. ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
  48. goto err;
  49. }
  50. if (EVP_PKEY_paramgen(pctx, &pkey) <= 0)
  51. goto err;
  52. EVP_PKEY_CTX_free(pctx);
  53. return pkey;
  54. }
  55. ERR_raise(ERR_LIB_CMS, CMS_R_DECODE_ERROR);
  56. return NULL;
  57. err:
  58. EVP_PKEY_free(pkey);
  59. EVP_PKEY_CTX_free(pctx);
  60. OSSL_DECODER_CTX_free(ctx);
  61. return NULL;
  62. }
  63. static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  64. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  65. {
  66. const ASN1_OBJECT *aoid;
  67. int atype;
  68. const void *aval;
  69. int rv = 0;
  70. EVP_PKEY *pkpeer = NULL;
  71. const unsigned char *p;
  72. int plen;
  73. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  74. if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
  75. goto err;
  76. /* If absent parameters get group from main key */
  77. if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
  78. EVP_PKEY *pk;
  79. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  80. if (pk == NULL)
  81. goto err;
  82. pkpeer = EVP_PKEY_new();
  83. if (pkpeer == NULL)
  84. goto err;
  85. if (!EVP_PKEY_copy_parameters(pkpeer, pk))
  86. goto err;
  87. } else {
  88. pkpeer = pkey_type2param(atype, aval,
  89. EVP_PKEY_CTX_get0_libctx(pctx),
  90. EVP_PKEY_CTX_get0_propq(pctx));
  91. if (pkpeer == NULL)
  92. goto err;
  93. }
  94. /* We have parameters now set public key */
  95. plen = ASN1_STRING_length(pubkey);
  96. p = ASN1_STRING_get0_data(pubkey);
  97. if (p == NULL || plen == 0)
  98. goto err;
  99. if (!EVP_PKEY_set1_encoded_public_key(pkpeer, p, plen))
  100. goto err;
  101. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  102. rv = 1;
  103. err:
  104. EVP_PKEY_free(pkpeer);
  105. return rv;
  106. }
  107. /* Set KDF parameters based on KDF NID */
  108. static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
  109. {
  110. int kdf_nid, kdfmd_nid, cofactor;
  111. const EVP_MD *kdf_md;
  112. if (eckdf_nid == NID_undef)
  113. return 0;
  114. /* Lookup KDF type, cofactor mode and digest */
  115. if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
  116. return 0;
  117. if (kdf_nid == NID_dh_std_kdf)
  118. cofactor = 0;
  119. else if (kdf_nid == NID_dh_cofactor_kdf)
  120. cofactor = 1;
  121. else
  122. return 0;
  123. if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
  124. return 0;
  125. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
  126. return 0;
  127. kdf_md = EVP_get_digestbynid(kdfmd_nid);
  128. if (!kdf_md)
  129. return 0;
  130. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  131. return 0;
  132. return 1;
  133. }
  134. static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  135. {
  136. int rv = 0;
  137. X509_ALGOR *alg, *kekalg = NULL;
  138. ASN1_OCTET_STRING *ukm;
  139. const unsigned char *p;
  140. unsigned char *der = NULL;
  141. int plen, keylen;
  142. EVP_CIPHER *kekcipher = NULL;
  143. EVP_CIPHER_CTX *kekctx;
  144. char name[OSSL_MAX_NAME_SIZE];
  145. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  146. return 0;
  147. if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
  148. ERR_raise(ERR_LIB_CMS, CMS_R_KDF_PARAMETER_ERROR);
  149. return 0;
  150. }
  151. if (alg->parameter->type != V_ASN1_SEQUENCE)
  152. return 0;
  153. p = alg->parameter->value.sequence->data;
  154. plen = alg->parameter->value.sequence->length;
  155. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  156. if (kekalg == NULL)
  157. goto err;
  158. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  159. if (kekctx == NULL)
  160. goto err;
  161. OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0);
  162. kekcipher = EVP_CIPHER_fetch(pctx->libctx, name, pctx->propquery);
  163. if (kekcipher == NULL || EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  164. goto err;
  165. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  166. goto err;
  167. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  168. goto err;
  169. keylen = EVP_CIPHER_CTX_get_key_length(kekctx);
  170. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  171. goto err;
  172. plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
  173. if (plen <= 0)
  174. goto err;
  175. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
  176. goto err;
  177. der = NULL;
  178. rv = 1;
  179. err:
  180. EVP_CIPHER_free(kekcipher);
  181. X509_ALGOR_free(kekalg);
  182. OPENSSL_free(der);
  183. return rv;
  184. }
  185. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
  186. {
  187. EVP_PKEY_CTX *pctx;
  188. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  189. if (pctx == NULL)
  190. return 0;
  191. /* See if we need to set peer key */
  192. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  193. X509_ALGOR *alg;
  194. ASN1_BIT_STRING *pubkey;
  195. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  196. NULL, NULL, NULL))
  197. return 0;
  198. if (alg == NULL || pubkey == NULL)
  199. return 0;
  200. if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
  201. ERR_raise(ERR_LIB_CMS, CMS_R_PEER_KEY_ERROR);
  202. return 0;
  203. }
  204. }
  205. /* Set ECDH derivation parameters and initialise unwrap context */
  206. if (!ecdh_cms_set_shared_info(pctx, ri)) {
  207. ERR_raise(ERR_LIB_CMS, CMS_R_SHARED_INFO_ERROR);
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
  213. {
  214. EVP_PKEY_CTX *pctx;
  215. EVP_PKEY *pkey;
  216. EVP_CIPHER_CTX *ctx;
  217. int keylen;
  218. X509_ALGOR *talg, *wrap_alg = NULL;
  219. const ASN1_OBJECT *aoid;
  220. ASN1_BIT_STRING *pubkey;
  221. ASN1_STRING *wrap_str;
  222. ASN1_OCTET_STRING *ukm;
  223. unsigned char *penc = NULL;
  224. size_t penclen;
  225. int rv = 0;
  226. int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
  227. const EVP_MD *kdf_md;
  228. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  229. if (pctx == NULL)
  230. return 0;
  231. /* Get ephemeral key */
  232. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  233. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  234. NULL, NULL, NULL))
  235. goto err;
  236. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  237. /* Is everything uninitialised? */
  238. if (aoid == OBJ_nid2obj(NID_undef)) {
  239. /* Set the key */
  240. penclen = EVP_PKEY_get1_encoded_public_key(pkey, &penc);
  241. ASN1_STRING_set0(pubkey, penc, penclen);
  242. ossl_asn1_string_set_bits_left(pubkey, 0);
  243. penc = NULL;
  244. (void)X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
  245. V_ASN1_UNDEF, NULL); /* cannot fail */
  246. }
  247. /* See if custom parameters set */
  248. kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
  249. if (kdf_type <= 0)
  250. goto err;
  251. if (EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md) <= 0)
  252. goto err;
  253. ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
  254. if (ecdh_nid < 0)
  255. goto err;
  256. else if (ecdh_nid == 0)
  257. ecdh_nid = NID_dh_std_kdf;
  258. else if (ecdh_nid == 1)
  259. ecdh_nid = NID_dh_cofactor_kdf;
  260. if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  261. kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
  262. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
  263. goto err;
  264. } else
  265. /* Unknown KDF */
  266. goto err;
  267. if (kdf_md == NULL) {
  268. /* Fixme later for better MD */
  269. kdf_md = EVP_sha1();
  270. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  271. goto err;
  272. }
  273. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  274. goto err;
  275. /* Lookup NID for KDF+cofactor+digest */
  276. if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_get_type(kdf_md), ecdh_nid))
  277. goto err;
  278. /* Get wrap NID */
  279. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  280. wrap_nid = EVP_CIPHER_CTX_get_type(ctx);
  281. keylen = EVP_CIPHER_CTX_get_key_length(ctx);
  282. /* Package wrap algorithm in an AlgorithmIdentifier */
  283. wrap_alg = X509_ALGOR_new();
  284. if (wrap_alg == NULL)
  285. goto err;
  286. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  287. wrap_alg->parameter = ASN1_TYPE_new();
  288. if (wrap_alg->parameter == NULL)
  289. goto err;
  290. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  291. goto err;
  292. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  293. ASN1_TYPE_free(wrap_alg->parameter);
  294. wrap_alg->parameter = NULL;
  295. }
  296. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  297. goto err;
  298. penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
  299. if (penclen == 0)
  300. goto err;
  301. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
  302. goto err;
  303. penc = NULL;
  304. /*
  305. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  306. * of another AlgorithmIdentifier.
  307. */
  308. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  309. if (penc == NULL || penclen == 0)
  310. goto err;
  311. wrap_str = ASN1_STRING_new();
  312. if (wrap_str == NULL)
  313. goto err;
  314. ASN1_STRING_set0(wrap_str, penc, penclen);
  315. penc = NULL;
  316. rv = X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
  317. if (!rv)
  318. ASN1_STRING_free(wrap_str);
  319. err:
  320. OPENSSL_free(penc);
  321. X509_ALGOR_free(wrap_alg);
  322. return rv;
  323. }
  324. int ossl_cms_ecdh_envelope(CMS_RecipientInfo *ri, int decrypt)
  325. {
  326. assert(decrypt == 0 || decrypt == 1);
  327. if (decrypt == 1)
  328. return ecdh_cms_decrypt(ri);
  329. if (decrypt == 0)
  330. return ecdh_cms_encrypt(ri);
  331. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  332. return 0;
  333. }