ec_ameth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/bn.h>
  14. #include <openssl/cms.h>
  15. #include <openssl/asn1t.h>
  16. #include "internal/asn1_int.h"
  17. #include "internal/evp_int.h"
  18. #include "ec_lcl.h"
  19. #ifndef OPENSSL_NO_CMS
  20. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
  21. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
  22. #endif
  23. static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
  24. {
  25. const EC_GROUP *group;
  26. int nid;
  27. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  28. ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
  29. return 0;
  30. }
  31. if (EC_GROUP_get_asn1_flag(group)
  32. && (nid = EC_GROUP_get_curve_name(group)))
  33. /* we have a 'named curve' => just set the OID */
  34. {
  35. *ppval = OBJ_nid2obj(nid);
  36. *pptype = V_ASN1_OBJECT;
  37. } else { /* explicit parameters */
  38. ASN1_STRING *pstr = NULL;
  39. pstr = ASN1_STRING_new();
  40. if (pstr == NULL)
  41. return 0;
  42. pstr->length = i2d_ECParameters(ec_key, &pstr->data);
  43. if (pstr->length <= 0) {
  44. ASN1_STRING_free(pstr);
  45. ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
  46. return 0;
  47. }
  48. *ppval = pstr;
  49. *pptype = V_ASN1_SEQUENCE;
  50. }
  51. return 1;
  52. }
  53. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  54. {
  55. EC_KEY *ec_key = pkey->pkey.ec;
  56. void *pval = NULL;
  57. int ptype;
  58. unsigned char *penc = NULL, *p;
  59. int penclen;
  60. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  61. ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
  62. return 0;
  63. }
  64. penclen = i2o_ECPublicKey(ec_key, NULL);
  65. if (penclen <= 0)
  66. goto err;
  67. penc = OPENSSL_malloc(penclen);
  68. if (penc == NULL)
  69. goto err;
  70. p = penc;
  71. penclen = i2o_ECPublicKey(ec_key, &p);
  72. if (penclen <= 0)
  73. goto err;
  74. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
  75. ptype, pval, penc, penclen))
  76. return 1;
  77. err:
  78. if (ptype == V_ASN1_OBJECT)
  79. ASN1_OBJECT_free(pval);
  80. else
  81. ASN1_STRING_free(pval);
  82. OPENSSL_free(penc);
  83. return 0;
  84. }
  85. static EC_KEY *eckey_type2param(int ptype, const void *pval)
  86. {
  87. EC_KEY *eckey = NULL;
  88. EC_GROUP *group = NULL;
  89. if (ptype == V_ASN1_SEQUENCE) {
  90. const ASN1_STRING *pstr = pval;
  91. const unsigned char *pm = pstr->data;
  92. int pmlen = pstr->length;
  93. if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
  94. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  95. goto ecerr;
  96. }
  97. } else if (ptype == V_ASN1_OBJECT) {
  98. const ASN1_OBJECT *poid = pval;
  99. /*
  100. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  101. */
  102. if ((eckey = EC_KEY_new()) == NULL) {
  103. ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
  104. goto ecerr;
  105. }
  106. group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
  107. if (group == NULL)
  108. goto ecerr;
  109. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  110. if (EC_KEY_set_group(eckey, group) == 0)
  111. goto ecerr;
  112. EC_GROUP_free(group);
  113. } else {
  114. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  115. goto ecerr;
  116. }
  117. return eckey;
  118. ecerr:
  119. EC_KEY_free(eckey);
  120. EC_GROUP_free(group);
  121. return NULL;
  122. }
  123. static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  124. {
  125. const unsigned char *p = NULL;
  126. const void *pval;
  127. int ptype, pklen;
  128. EC_KEY *eckey = NULL;
  129. X509_ALGOR *palg;
  130. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  131. return 0;
  132. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  133. eckey = eckey_type2param(ptype, pval);
  134. if (!eckey) {
  135. ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
  136. return 0;
  137. }
  138. /* We have parameters now set public key */
  139. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  140. ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
  141. goto ecerr;
  142. }
  143. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  144. return 1;
  145. ecerr:
  146. EC_KEY_free(eckey);
  147. return 0;
  148. }
  149. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  150. {
  151. int r;
  152. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  153. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  154. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  155. if (group == NULL || pa == NULL || pb == NULL)
  156. return -2;
  157. r = EC_POINT_cmp(group, pa, pb, NULL);
  158. if (r == 0)
  159. return 1;
  160. if (r == 1)
  161. return 0;
  162. return -2;
  163. }
  164. static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  165. {
  166. const unsigned char *p = NULL;
  167. const void *pval;
  168. int ptype, pklen;
  169. EC_KEY *eckey = NULL;
  170. const X509_ALGOR *palg;
  171. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  172. return 0;
  173. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  174. eckey = eckey_type2param(ptype, pval);
  175. if (!eckey)
  176. goto ecliberr;
  177. /* We have parameters now set private key */
  178. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  179. ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
  180. goto ecerr;
  181. }
  182. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  183. return 1;
  184. ecliberr:
  185. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  186. ecerr:
  187. EC_KEY_free(eckey);
  188. return 0;
  189. }
  190. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  191. {
  192. EC_KEY ec_key = *(pkey->pkey.ec);
  193. unsigned char *ep, *p;
  194. int eplen, ptype;
  195. void *pval;
  196. unsigned int old_flags;
  197. if (!eckey_param2type(&ptype, &pval, &ec_key)) {
  198. ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
  199. return 0;
  200. }
  201. /* set the private key */
  202. /*
  203. * do not include the parameters in the SEC1 private key see PKCS#11
  204. * 12.11
  205. */
  206. old_flags = EC_KEY_get_enc_flags(&ec_key);
  207. EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
  208. eplen = i2d_ECPrivateKey(&ec_key, NULL);
  209. if (!eplen) {
  210. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  211. return 0;
  212. }
  213. ep = OPENSSL_malloc(eplen);
  214. if (ep == NULL) {
  215. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  216. return 0;
  217. }
  218. p = ep;
  219. if (!i2d_ECPrivateKey(&ec_key, &p)) {
  220. OPENSSL_free(ep);
  221. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  222. return 0;
  223. }
  224. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
  225. ptype, pval, ep, eplen)) {
  226. OPENSSL_free(ep);
  227. return 0;
  228. }
  229. return 1;
  230. }
  231. static int int_ec_size(const EVP_PKEY *pkey)
  232. {
  233. return ECDSA_size(pkey->pkey.ec);
  234. }
  235. static int ec_bits(const EVP_PKEY *pkey)
  236. {
  237. return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
  238. }
  239. static int ec_security_bits(const EVP_PKEY *pkey)
  240. {
  241. int ecbits = ec_bits(pkey);
  242. if (ecbits >= 512)
  243. return 256;
  244. if (ecbits >= 384)
  245. return 192;
  246. if (ecbits >= 256)
  247. return 128;
  248. if (ecbits >= 224)
  249. return 112;
  250. if (ecbits >= 160)
  251. return 80;
  252. return ecbits / 2;
  253. }
  254. static int ec_missing_parameters(const EVP_PKEY *pkey)
  255. {
  256. if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  257. return 1;
  258. return 0;
  259. }
  260. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  261. {
  262. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  263. if (group == NULL)
  264. return 0;
  265. if (to->pkey.ec == NULL) {
  266. to->pkey.ec = EC_KEY_new();
  267. if (to->pkey.ec == NULL)
  268. goto err;
  269. }
  270. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  271. goto err;
  272. EC_GROUP_free(group);
  273. return 1;
  274. err:
  275. EC_GROUP_free(group);
  276. return 0;
  277. }
  278. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  279. {
  280. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  281. *group_b = EC_KEY_get0_group(b->pkey.ec);
  282. if (group_a == NULL || group_b == NULL)
  283. return -2;
  284. if (EC_GROUP_cmp(group_a, group_b, NULL))
  285. return 0;
  286. else
  287. return 1;
  288. }
  289. static void int_ec_free(EVP_PKEY *pkey)
  290. {
  291. EC_KEY_free(pkey->pkey.ec);
  292. }
  293. typedef enum {
  294. EC_KEY_PRINT_PRIVATE,
  295. EC_KEY_PRINT_PUBLIC,
  296. EC_KEY_PRINT_PARAM
  297. } ec_print_t;
  298. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
  299. {
  300. const char *ecstr;
  301. unsigned char *priv = NULL, *pub = NULL;
  302. size_t privlen = 0, publen = 0;
  303. int ret = 0;
  304. const EC_GROUP *group;
  305. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  306. ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
  307. return 0;
  308. }
  309. if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
  310. publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
  311. if (publen == 0)
  312. goto err;
  313. }
  314. if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
  315. privlen = EC_KEY_priv2buf(x, &priv);
  316. if (privlen == 0)
  317. goto err;
  318. }
  319. if (ktype == EC_KEY_PRINT_PRIVATE)
  320. ecstr = "Private-Key";
  321. else if (ktype == EC_KEY_PRINT_PUBLIC)
  322. ecstr = "Public-Key";
  323. else
  324. ecstr = "ECDSA-Parameters";
  325. if (!BIO_indent(bp, off, 128))
  326. goto err;
  327. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
  328. EC_GROUP_order_bits(group)) <= 0)
  329. goto err;
  330. if (privlen != 0) {
  331. if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
  332. goto err;
  333. if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
  334. goto err;
  335. }
  336. if (publen != 0) {
  337. if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
  338. goto err;
  339. if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
  340. goto err;
  341. }
  342. if (!ECPKParameters_print(bp, group, off))
  343. goto err;
  344. ret = 1;
  345. err:
  346. if (!ret)
  347. ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
  348. OPENSSL_clear_free(priv, privlen);
  349. OPENSSL_free(pub);
  350. return ret;
  351. }
  352. static int eckey_param_decode(EVP_PKEY *pkey,
  353. const unsigned char **pder, int derlen)
  354. {
  355. EC_KEY *eckey;
  356. if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
  357. ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
  358. return 0;
  359. }
  360. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  361. return 1;
  362. }
  363. static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  364. {
  365. return i2d_ECParameters(pkey->pkey.ec, pder);
  366. }
  367. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  368. ASN1_PCTX *ctx)
  369. {
  370. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
  371. }
  372. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  373. ASN1_PCTX *ctx)
  374. {
  375. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
  376. }
  377. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  378. ASN1_PCTX *ctx)
  379. {
  380. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
  381. }
  382. static int old_ec_priv_decode(EVP_PKEY *pkey,
  383. const unsigned char **pder, int derlen)
  384. {
  385. EC_KEY *ec;
  386. if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
  387. ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
  388. return 0;
  389. }
  390. EVP_PKEY_assign_EC_KEY(pkey, ec);
  391. return 1;
  392. }
  393. static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  394. {
  395. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  396. }
  397. static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  398. {
  399. switch (op) {
  400. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  401. if (arg1 == 0) {
  402. int snid, hnid;
  403. X509_ALGOR *alg1, *alg2;
  404. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  405. if (alg1 == NULL || alg1->algorithm == NULL)
  406. return -1;
  407. hnid = OBJ_obj2nid(alg1->algorithm);
  408. if (hnid == NID_undef)
  409. return -1;
  410. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  411. return -1;
  412. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  413. }
  414. return 1;
  415. #ifndef OPENSSL_NO_CMS
  416. case ASN1_PKEY_CTRL_CMS_SIGN:
  417. if (arg1 == 0) {
  418. int snid, hnid;
  419. X509_ALGOR *alg1, *alg2;
  420. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  421. if (alg1 == NULL || alg1->algorithm == NULL)
  422. return -1;
  423. hnid = OBJ_obj2nid(alg1->algorithm);
  424. if (hnid == NID_undef)
  425. return -1;
  426. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  427. return -1;
  428. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  429. }
  430. return 1;
  431. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  432. if (arg1 == 1)
  433. return ecdh_cms_decrypt(arg2);
  434. else if (arg1 == 0)
  435. return ecdh_cms_encrypt(arg2);
  436. return -2;
  437. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  438. *(int *)arg2 = CMS_RECIPINFO_AGREE;
  439. return 1;
  440. #endif
  441. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  442. if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
  443. /* For SM2, the only valid digest-alg is SM3 */
  444. *(int *)arg2 = NID_sm3;
  445. } else {
  446. *(int *)arg2 = NID_sha256;
  447. }
  448. return 1;
  449. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
  450. return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
  451. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  452. return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
  453. POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
  454. default:
  455. return -2;
  456. }
  457. }
  458. static int ec_pkey_check(const EVP_PKEY *pkey)
  459. {
  460. EC_KEY *eckey = pkey->pkey.ec;
  461. /* stay consistent to what EVP_PKEY_check demands */
  462. if (eckey->priv_key == NULL) {
  463. ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
  464. return 0;
  465. }
  466. return EC_KEY_check_key(eckey);
  467. }
  468. static int ec_pkey_public_check(const EVP_PKEY *pkey)
  469. {
  470. EC_KEY *eckey = pkey->pkey.ec;
  471. /*
  472. * Note: it unnecessary to check eckey->pub_key here since
  473. * it will be checked in EC_KEY_check_key(). In fact, the
  474. * EC_KEY_check_key() mainly checks the public key, and checks
  475. * the private key optionally (only if there is one). So if
  476. * someone passes a whole EC key (public + private), this
  477. * will also work...
  478. */
  479. return EC_KEY_check_key(eckey);
  480. }
  481. static int ec_pkey_param_check(const EVP_PKEY *pkey)
  482. {
  483. EC_KEY *eckey = pkey->pkey.ec;
  484. /* stay consistent to what EVP_PKEY_check demands */
  485. if (eckey->group == NULL) {
  486. ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
  487. return 0;
  488. }
  489. return EC_GROUP_check(eckey->group, NULL);
  490. }
  491. const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
  492. EVP_PKEY_EC,
  493. EVP_PKEY_EC,
  494. 0,
  495. "EC",
  496. "OpenSSL EC algorithm",
  497. eckey_pub_decode,
  498. eckey_pub_encode,
  499. eckey_pub_cmp,
  500. eckey_pub_print,
  501. eckey_priv_decode,
  502. eckey_priv_encode,
  503. eckey_priv_print,
  504. int_ec_size,
  505. ec_bits,
  506. ec_security_bits,
  507. eckey_param_decode,
  508. eckey_param_encode,
  509. ec_missing_parameters,
  510. ec_copy_parameters,
  511. ec_cmp_parameters,
  512. eckey_param_print,
  513. 0,
  514. int_ec_free,
  515. ec_pkey_ctrl,
  516. old_ec_priv_decode,
  517. old_ec_priv_encode,
  518. 0, 0, 0,
  519. ec_pkey_check,
  520. ec_pkey_public_check,
  521. ec_pkey_param_check
  522. };
  523. #if !defined(OPENSSL_NO_SM2)
  524. const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
  525. EVP_PKEY_SM2,
  526. EVP_PKEY_EC,
  527. ASN1_PKEY_ALIAS
  528. };
  529. #endif
  530. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  531. {
  532. int private = EC_KEY_get0_private_key(x) != NULL;
  533. return do_EC_KEY_print(bp, x, off,
  534. private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
  535. }
  536. int ECParameters_print(BIO *bp, const EC_KEY *x)
  537. {
  538. return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
  539. }
  540. #ifndef OPENSSL_NO_CMS
  541. static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  542. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  543. {
  544. const ASN1_OBJECT *aoid;
  545. int atype;
  546. const void *aval;
  547. int rv = 0;
  548. EVP_PKEY *pkpeer = NULL;
  549. EC_KEY *ecpeer = NULL;
  550. const unsigned char *p;
  551. int plen;
  552. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  553. if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
  554. goto err;
  555. /* If absent parameters get group from main key */
  556. if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
  557. const EC_GROUP *grp;
  558. EVP_PKEY *pk;
  559. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  560. if (!pk)
  561. goto err;
  562. grp = EC_KEY_get0_group(pk->pkey.ec);
  563. ecpeer = EC_KEY_new();
  564. if (ecpeer == NULL)
  565. goto err;
  566. if (!EC_KEY_set_group(ecpeer, grp))
  567. goto err;
  568. } else {
  569. ecpeer = eckey_type2param(atype, aval);
  570. if (!ecpeer)
  571. goto err;
  572. }
  573. /* We have parameters now set public key */
  574. plen = ASN1_STRING_length(pubkey);
  575. p = ASN1_STRING_get0_data(pubkey);
  576. if (!p || !plen)
  577. goto err;
  578. if (!o2i_ECPublicKey(&ecpeer, &p, plen))
  579. goto err;
  580. pkpeer = EVP_PKEY_new();
  581. if (pkpeer == NULL)
  582. goto err;
  583. EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
  584. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  585. rv = 1;
  586. err:
  587. EC_KEY_free(ecpeer);
  588. EVP_PKEY_free(pkpeer);
  589. return rv;
  590. }
  591. /* Set KDF parameters based on KDF NID */
  592. static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
  593. {
  594. int kdf_nid, kdfmd_nid, cofactor;
  595. const EVP_MD *kdf_md;
  596. if (eckdf_nid == NID_undef)
  597. return 0;
  598. /* Lookup KDF type, cofactor mode and digest */
  599. if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
  600. return 0;
  601. if (kdf_nid == NID_dh_std_kdf)
  602. cofactor = 0;
  603. else if (kdf_nid == NID_dh_cofactor_kdf)
  604. cofactor = 1;
  605. else
  606. return 0;
  607. if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
  608. return 0;
  609. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
  610. return 0;
  611. kdf_md = EVP_get_digestbynid(kdfmd_nid);
  612. if (!kdf_md)
  613. return 0;
  614. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  615. return 0;
  616. return 1;
  617. }
  618. static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  619. {
  620. int rv = 0;
  621. X509_ALGOR *alg, *kekalg = NULL;
  622. ASN1_OCTET_STRING *ukm;
  623. const unsigned char *p;
  624. unsigned char *der = NULL;
  625. int plen, keylen;
  626. const EVP_CIPHER *kekcipher;
  627. EVP_CIPHER_CTX *kekctx;
  628. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  629. return 0;
  630. if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
  631. ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
  632. return 0;
  633. }
  634. if (alg->parameter->type != V_ASN1_SEQUENCE)
  635. return 0;
  636. p = alg->parameter->value.sequence->data;
  637. plen = alg->parameter->value.sequence->length;
  638. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  639. if (!kekalg)
  640. goto err;
  641. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  642. if (!kekctx)
  643. goto err;
  644. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  645. if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  646. goto err;
  647. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  648. goto err;
  649. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  650. goto err;
  651. keylen = EVP_CIPHER_CTX_key_length(kekctx);
  652. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  653. goto err;
  654. plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
  655. if (!plen)
  656. goto err;
  657. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
  658. goto err;
  659. der = NULL;
  660. rv = 1;
  661. err:
  662. X509_ALGOR_free(kekalg);
  663. OPENSSL_free(der);
  664. return rv;
  665. }
  666. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
  667. {
  668. EVP_PKEY_CTX *pctx;
  669. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  670. if (!pctx)
  671. return 0;
  672. /* See if we need to set peer key */
  673. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  674. X509_ALGOR *alg;
  675. ASN1_BIT_STRING *pubkey;
  676. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  677. NULL, NULL, NULL))
  678. return 0;
  679. if (!alg || !pubkey)
  680. return 0;
  681. if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
  682. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
  683. return 0;
  684. }
  685. }
  686. /* Set ECDH derivation parameters and initialise unwrap context */
  687. if (!ecdh_cms_set_shared_info(pctx, ri)) {
  688. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
  689. return 0;
  690. }
  691. return 1;
  692. }
  693. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
  694. {
  695. EVP_PKEY_CTX *pctx;
  696. EVP_PKEY *pkey;
  697. EVP_CIPHER_CTX *ctx;
  698. int keylen;
  699. X509_ALGOR *talg, *wrap_alg = NULL;
  700. const ASN1_OBJECT *aoid;
  701. ASN1_BIT_STRING *pubkey;
  702. ASN1_STRING *wrap_str;
  703. ASN1_OCTET_STRING *ukm;
  704. unsigned char *penc = NULL;
  705. int penclen;
  706. int rv = 0;
  707. int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
  708. const EVP_MD *kdf_md;
  709. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  710. if (!pctx)
  711. return 0;
  712. /* Get ephemeral key */
  713. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  714. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  715. NULL, NULL, NULL))
  716. goto err;
  717. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  718. /* Is everything uninitialised? */
  719. if (aoid == OBJ_nid2obj(NID_undef)) {
  720. EC_KEY *eckey = pkey->pkey.ec;
  721. /* Set the key */
  722. unsigned char *p;
  723. penclen = i2o_ECPublicKey(eckey, NULL);
  724. if (penclen <= 0)
  725. goto err;
  726. penc = OPENSSL_malloc(penclen);
  727. if (penc == NULL)
  728. goto err;
  729. p = penc;
  730. penclen = i2o_ECPublicKey(eckey, &p);
  731. if (penclen <= 0)
  732. goto err;
  733. ASN1_STRING_set0(pubkey, penc, penclen);
  734. pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  735. pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  736. penc = NULL;
  737. X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
  738. V_ASN1_UNDEF, NULL);
  739. }
  740. /* See if custom parameters set */
  741. kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
  742. if (kdf_type <= 0)
  743. goto err;
  744. if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
  745. goto err;
  746. ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
  747. if (ecdh_nid < 0)
  748. goto err;
  749. else if (ecdh_nid == 0)
  750. ecdh_nid = NID_dh_std_kdf;
  751. else if (ecdh_nid == 1)
  752. ecdh_nid = NID_dh_cofactor_kdf;
  753. if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  754. kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
  755. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
  756. goto err;
  757. } else
  758. /* Unknown KDF */
  759. goto err;
  760. if (kdf_md == NULL) {
  761. /* Fixme later for better MD */
  762. kdf_md = EVP_sha1();
  763. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  764. goto err;
  765. }
  766. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  767. goto err;
  768. /* Lookup NID for KDF+cofactor+digest */
  769. if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
  770. goto err;
  771. /* Get wrap NID */
  772. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  773. wrap_nid = EVP_CIPHER_CTX_type(ctx);
  774. keylen = EVP_CIPHER_CTX_key_length(ctx);
  775. /* Package wrap algorithm in an AlgorithmIdentifier */
  776. wrap_alg = X509_ALGOR_new();
  777. if (wrap_alg == NULL)
  778. goto err;
  779. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  780. wrap_alg->parameter = ASN1_TYPE_new();
  781. if (wrap_alg->parameter == NULL)
  782. goto err;
  783. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  784. goto err;
  785. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  786. ASN1_TYPE_free(wrap_alg->parameter);
  787. wrap_alg->parameter = NULL;
  788. }
  789. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  790. goto err;
  791. penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
  792. if (!penclen)
  793. goto err;
  794. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
  795. goto err;
  796. penc = NULL;
  797. /*
  798. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  799. * of another AlgorithmIdentifier.
  800. */
  801. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  802. if (!penc || !penclen)
  803. goto err;
  804. wrap_str = ASN1_STRING_new();
  805. if (wrap_str == NULL)
  806. goto err;
  807. ASN1_STRING_set0(wrap_str, penc, penclen);
  808. penc = NULL;
  809. X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
  810. rv = 1;
  811. err:
  812. OPENSSL_free(penc);
  813. X509_ALGOR_free(wrap_alg);
  814. return rv;
  815. }
  816. #endif