cms_ec.c 12 KB

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