ec_ameth.c 25 KB

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