cms_ec.c 11 KB

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