ec_ameth.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/x509.h>
  61. #include <openssl/ec.h>
  62. #include <openssl/bn.h>
  63. #ifndef OPENSSL_NO_CMS
  64. # include <openssl/cms.h>
  65. #endif
  66. #include <openssl/asn1t.h>
  67. #include "internal/asn1_int.h"
  68. #include "internal/evp_int.h"
  69. #ifndef OPENSSL_NO_CMS
  70. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
  71. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
  72. #endif
  73. static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
  74. {
  75. const EC_GROUP *group;
  76. int nid;
  77. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  78. ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
  79. return 0;
  80. }
  81. if (EC_GROUP_get_asn1_flag(group)
  82. && (nid = EC_GROUP_get_curve_name(group)))
  83. /* we have a 'named curve' => just set the OID */
  84. {
  85. *ppval = OBJ_nid2obj(nid);
  86. *pptype = V_ASN1_OBJECT;
  87. } else { /* explicit parameters */
  88. ASN1_STRING *pstr = NULL;
  89. pstr = ASN1_STRING_new();
  90. if (pstr == NULL)
  91. return 0;
  92. pstr->length = i2d_ECParameters(ec_key, &pstr->data);
  93. if (pstr->length <= 0) {
  94. ASN1_STRING_free(pstr);
  95. ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
  96. return 0;
  97. }
  98. *ppval = pstr;
  99. *pptype = V_ASN1_SEQUENCE;
  100. }
  101. return 1;
  102. }
  103. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  104. {
  105. EC_KEY *ec_key = pkey->pkey.ec;
  106. void *pval = NULL;
  107. int ptype;
  108. unsigned char *penc = NULL, *p;
  109. int penclen;
  110. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  111. ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
  112. return 0;
  113. }
  114. penclen = i2o_ECPublicKey(ec_key, NULL);
  115. if (penclen <= 0)
  116. goto err;
  117. penc = OPENSSL_malloc(penclen);
  118. if (penc == NULL)
  119. goto err;
  120. p = penc;
  121. penclen = i2o_ECPublicKey(ec_key, &p);
  122. if (penclen <= 0)
  123. goto err;
  124. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
  125. ptype, pval, penc, penclen))
  126. return 1;
  127. err:
  128. if (ptype == V_ASN1_OBJECT)
  129. ASN1_OBJECT_free(pval);
  130. else
  131. ASN1_STRING_free(pval);
  132. OPENSSL_free(penc);
  133. return 0;
  134. }
  135. static EC_KEY *eckey_type2param(int ptype, void *pval)
  136. {
  137. EC_KEY *eckey = NULL;
  138. if (ptype == V_ASN1_SEQUENCE) {
  139. ASN1_STRING *pstr = pval;
  140. const unsigned char *pm = NULL;
  141. int pmlen;
  142. pm = pstr->data;
  143. pmlen = pstr->length;
  144. if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
  145. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  146. goto ecerr;
  147. }
  148. } else if (ptype == V_ASN1_OBJECT) {
  149. ASN1_OBJECT *poid = pval;
  150. EC_GROUP *group;
  151. /*
  152. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  153. */
  154. if ((eckey = EC_KEY_new()) == NULL) {
  155. ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
  156. goto ecerr;
  157. }
  158. group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
  159. if (group == NULL)
  160. goto ecerr;
  161. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  162. if (EC_KEY_set_group(eckey, group) == 0)
  163. goto ecerr;
  164. EC_GROUP_free(group);
  165. } else {
  166. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  167. goto ecerr;
  168. }
  169. return eckey;
  170. ecerr:
  171. EC_KEY_free(eckey);
  172. return NULL;
  173. }
  174. static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  175. {
  176. const unsigned char *p = NULL;
  177. void *pval;
  178. int ptype, pklen;
  179. EC_KEY *eckey = NULL;
  180. X509_ALGOR *palg;
  181. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  182. return 0;
  183. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  184. eckey = eckey_type2param(ptype, pval);
  185. if (!eckey) {
  186. ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
  187. return 0;
  188. }
  189. /* We have parameters now set public key */
  190. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  191. ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
  192. goto ecerr;
  193. }
  194. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  195. return 1;
  196. ecerr:
  197. EC_KEY_free(eckey);
  198. return 0;
  199. }
  200. static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  201. {
  202. int r;
  203. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  204. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  205. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  206. r = EC_POINT_cmp(group, pa, pb, NULL);
  207. if (r == 0)
  208. return 1;
  209. if (r == 1)
  210. return 0;
  211. return -2;
  212. }
  213. static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  214. {
  215. const unsigned char *p = NULL;
  216. void *pval;
  217. int ptype, pklen;
  218. EC_KEY *eckey = NULL;
  219. X509_ALGOR *palg;
  220. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  221. return 0;
  222. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  223. eckey = eckey_type2param(ptype, pval);
  224. if (!eckey)
  225. goto ecliberr;
  226. /* We have parameters now set private key */
  227. if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
  228. ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
  229. goto ecerr;
  230. }
  231. /* calculate public key (if necessary) */
  232. if (EC_KEY_get0_public_key(eckey) == NULL) {
  233. const BIGNUM *priv_key;
  234. const EC_GROUP *group;
  235. EC_POINT *pub_key;
  236. /*
  237. * the public key was not included in the SEC1 private key =>
  238. * calculate the public key
  239. */
  240. group = EC_KEY_get0_group(eckey);
  241. pub_key = EC_POINT_new(group);
  242. if (pub_key == NULL) {
  243. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  244. goto ecliberr;
  245. }
  246. if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
  247. EC_POINT_free(pub_key);
  248. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  249. goto ecliberr;
  250. }
  251. priv_key = EC_KEY_get0_private_key(eckey);
  252. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
  253. EC_POINT_free(pub_key);
  254. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  255. goto ecliberr;
  256. }
  257. if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
  258. EC_POINT_free(pub_key);
  259. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  260. goto ecliberr;
  261. }
  262. EC_POINT_free(pub_key);
  263. }
  264. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  265. return 1;
  266. ecliberr:
  267. ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
  268. ecerr:
  269. EC_KEY_free(eckey);
  270. return 0;
  271. }
  272. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  273. {
  274. EC_KEY *ec_key;
  275. unsigned char *ep, *p;
  276. int eplen, ptype;
  277. void *pval;
  278. unsigned int tmp_flags, old_flags;
  279. ec_key = pkey->pkey.ec;
  280. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  281. ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
  282. return 0;
  283. }
  284. /* set the private key */
  285. /*
  286. * do not include the parameters in the SEC1 private key see PKCS#11
  287. * 12.11
  288. */
  289. old_flags = EC_KEY_get_enc_flags(ec_key);
  290. tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
  291. EC_KEY_set_enc_flags(ec_key, tmp_flags);
  292. eplen = i2d_ECPrivateKey(ec_key, NULL);
  293. if (!eplen) {
  294. EC_KEY_set_enc_flags(ec_key, old_flags);
  295. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  296. return 0;
  297. }
  298. ep = OPENSSL_malloc(eplen);
  299. if (ep == NULL) {
  300. EC_KEY_set_enc_flags(ec_key, old_flags);
  301. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  302. return 0;
  303. }
  304. p = ep;
  305. if (!i2d_ECPrivateKey(ec_key, &p)) {
  306. EC_KEY_set_enc_flags(ec_key, old_flags);
  307. OPENSSL_free(ep);
  308. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  309. return 0;
  310. }
  311. /* restore old encoding flags */
  312. EC_KEY_set_enc_flags(ec_key, old_flags);
  313. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
  314. ptype, pval, ep, eplen))
  315. return 0;
  316. return 1;
  317. }
  318. static int int_ec_size(const EVP_PKEY *pkey)
  319. {
  320. return ECDSA_size(pkey->pkey.ec);
  321. }
  322. static int ec_bits(const EVP_PKEY *pkey)
  323. {
  324. BIGNUM *order = BN_new();
  325. const EC_GROUP *group;
  326. int ret;
  327. if (order == NULL) {
  328. ERR_clear_error();
  329. return 0;
  330. }
  331. group = EC_KEY_get0_group(pkey->pkey.ec);
  332. if (!EC_GROUP_get_order(group, order, NULL)) {
  333. ERR_clear_error();
  334. return 0;
  335. }
  336. ret = BN_num_bits(order);
  337. BN_free(order);
  338. return ret;
  339. }
  340. static int ec_security_bits(const EVP_PKEY *pkey)
  341. {
  342. int ecbits = ec_bits(pkey);
  343. if (ecbits >= 512)
  344. return 256;
  345. if (ecbits >= 384)
  346. return 192;
  347. if (ecbits >= 256)
  348. return 128;
  349. if (ecbits >= 224)
  350. return 112;
  351. if (ecbits >= 160)
  352. return 80;
  353. return ecbits / 2;
  354. }
  355. static int ec_missing_parameters(const EVP_PKEY *pkey)
  356. {
  357. if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  358. return 1;
  359. return 0;
  360. }
  361. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  362. {
  363. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  364. if (group == NULL)
  365. return 0;
  366. if (to->pkey.ec == NULL) {
  367. to->pkey.ec = EC_KEY_new();
  368. if (to->pkey.ec == NULL)
  369. return 0;
  370. }
  371. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  372. return 0;
  373. EC_GROUP_free(group);
  374. return 1;
  375. }
  376. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  377. {
  378. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  379. *group_b = EC_KEY_get0_group(b->pkey.ec);
  380. if (EC_GROUP_cmp(group_a, group_b, NULL))
  381. return 0;
  382. else
  383. return 1;
  384. }
  385. static void int_ec_free(EVP_PKEY *pkey)
  386. {
  387. EC_KEY_free(pkey->pkey.ec);
  388. }
  389. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
  390. {
  391. unsigned char *buffer = NULL;
  392. const char *ecstr;
  393. size_t buf_len = 0, i;
  394. int ret = 0, reason = ERR_R_BIO_LIB;
  395. BIGNUM *pub_key = NULL, *order = NULL;
  396. BN_CTX *ctx = NULL;
  397. const EC_GROUP *group;
  398. const EC_POINT *public_key;
  399. const BIGNUM *priv_key;
  400. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  401. reason = ERR_R_PASSED_NULL_PARAMETER;
  402. goto err;
  403. }
  404. ctx = BN_CTX_new();
  405. if (ctx == NULL) {
  406. reason = ERR_R_MALLOC_FAILURE;
  407. goto err;
  408. }
  409. if (ktype > 0) {
  410. public_key = EC_KEY_get0_public_key(x);
  411. if (public_key != NULL) {
  412. if ((pub_key = EC_POINT_point2bn(group, public_key,
  413. EC_KEY_get_conv_form(x), NULL,
  414. ctx)) == NULL) {
  415. reason = ERR_R_EC_LIB;
  416. goto err;
  417. }
  418. buf_len = (size_t)BN_num_bytes(pub_key);
  419. }
  420. }
  421. if (ktype == 2) {
  422. priv_key = EC_KEY_get0_private_key(x);
  423. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len)
  424. buf_len = i;
  425. } else
  426. priv_key = NULL;
  427. if (ktype > 0) {
  428. buf_len += 10;
  429. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  430. reason = ERR_R_MALLOC_FAILURE;
  431. goto err;
  432. }
  433. }
  434. if (ktype == 2)
  435. ecstr = "Private-Key";
  436. else if (ktype == 1)
  437. ecstr = "Public-Key";
  438. else
  439. ecstr = "ECDSA-Parameters";
  440. if (!BIO_indent(bp, off, 128))
  441. goto err;
  442. if ((order = BN_new()) == NULL)
  443. goto err;
  444. if (!EC_GROUP_get_order(group, order, NULL))
  445. goto err;
  446. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0)
  447. goto err;
  448. if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
  449. buffer, off))
  450. goto err;
  451. if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
  452. buffer, off))
  453. goto err;
  454. if (!ECPKParameters_print(bp, group, off))
  455. goto err;
  456. ret = 1;
  457. err:
  458. if (!ret)
  459. ECerr(EC_F_DO_EC_KEY_PRINT, reason);
  460. BN_free(pub_key);
  461. BN_free(order);
  462. BN_CTX_free(ctx);
  463. OPENSSL_free(buffer);
  464. return (ret);
  465. }
  466. static int eckey_param_decode(EVP_PKEY *pkey,
  467. const unsigned char **pder, int derlen)
  468. {
  469. EC_KEY *eckey;
  470. if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
  471. ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
  472. return 0;
  473. }
  474. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  475. return 1;
  476. }
  477. static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  478. {
  479. return i2d_ECParameters(pkey->pkey.ec, pder);
  480. }
  481. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  482. ASN1_PCTX *ctx)
  483. {
  484. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  485. }
  486. static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  487. ASN1_PCTX *ctx)
  488. {
  489. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
  490. }
  491. static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  492. ASN1_PCTX *ctx)
  493. {
  494. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
  495. }
  496. static int old_ec_priv_decode(EVP_PKEY *pkey,
  497. const unsigned char **pder, int derlen)
  498. {
  499. EC_KEY *ec;
  500. if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
  501. ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
  502. return 0;
  503. }
  504. EVP_PKEY_assign_EC_KEY(pkey, ec);
  505. return 1;
  506. }
  507. static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  508. {
  509. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  510. }
  511. static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  512. {
  513. switch (op) {
  514. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  515. if (arg1 == 0) {
  516. int snid, hnid;
  517. X509_ALGOR *alg1, *alg2;
  518. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  519. if (alg1 == NULL || alg1->algorithm == NULL)
  520. return -1;
  521. hnid = OBJ_obj2nid(alg1->algorithm);
  522. if (hnid == NID_undef)
  523. return -1;
  524. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  525. return -1;
  526. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  527. }
  528. return 1;
  529. #ifndef OPENSSL_NO_CMS
  530. case ASN1_PKEY_CTRL_CMS_SIGN:
  531. if (arg1 == 0) {
  532. int snid, hnid;
  533. X509_ALGOR *alg1, *alg2;
  534. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  535. if (alg1 == NULL || alg1->algorithm == NULL)
  536. return -1;
  537. hnid = OBJ_obj2nid(alg1->algorithm);
  538. if (hnid == NID_undef)
  539. return -1;
  540. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  541. return -1;
  542. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  543. }
  544. return 1;
  545. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  546. if (arg1 == 1)
  547. return ecdh_cms_decrypt(arg2);
  548. else if (arg1 == 0)
  549. return ecdh_cms_encrypt(arg2);
  550. return -2;
  551. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  552. *(int *)arg2 = CMS_RECIPINFO_AGREE;
  553. return 1;
  554. #endif
  555. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  556. *(int *)arg2 = NID_sha256;
  557. return 2;
  558. default:
  559. return -2;
  560. }
  561. }
  562. const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
  563. EVP_PKEY_EC,
  564. EVP_PKEY_EC,
  565. 0,
  566. "EC",
  567. "OpenSSL EC algorithm",
  568. eckey_pub_decode,
  569. eckey_pub_encode,
  570. eckey_pub_cmp,
  571. eckey_pub_print,
  572. eckey_priv_decode,
  573. eckey_priv_encode,
  574. eckey_priv_print,
  575. int_ec_size,
  576. ec_bits,
  577. ec_security_bits,
  578. eckey_param_decode,
  579. eckey_param_encode,
  580. ec_missing_parameters,
  581. ec_copy_parameters,
  582. ec_cmp_parameters,
  583. eckey_param_print,
  584. 0,
  585. int_ec_free,
  586. ec_pkey_ctrl,
  587. old_ec_priv_decode,
  588. old_ec_priv_encode
  589. };
  590. #ifndef OPENSSL_NO_CMS
  591. static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  592. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  593. {
  594. ASN1_OBJECT *aoid;
  595. int atype;
  596. void *aval;
  597. int rv = 0;
  598. EVP_PKEY *pkpeer = NULL;
  599. EC_KEY *ecpeer = NULL;
  600. const unsigned char *p;
  601. int plen;
  602. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  603. if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
  604. goto err;
  605. /* If absent parameters get group from main key */
  606. if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
  607. const EC_GROUP *grp;
  608. EVP_PKEY *pk;
  609. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  610. if (!pk)
  611. goto err;
  612. grp = EC_KEY_get0_group(pk->pkey.ec);
  613. ecpeer = EC_KEY_new();
  614. if (ecpeer == NULL)
  615. goto err;
  616. if (!EC_KEY_set_group(ecpeer, grp))
  617. goto err;
  618. } else {
  619. ecpeer = eckey_type2param(atype, aval);
  620. if (!ecpeer)
  621. goto err;
  622. }
  623. /* We have parameters now set public key */
  624. plen = ASN1_STRING_length(pubkey);
  625. p = ASN1_STRING_data(pubkey);
  626. if (!p || !plen)
  627. goto err;
  628. if (!o2i_ECPublicKey(&ecpeer, &p, plen))
  629. goto err;
  630. pkpeer = EVP_PKEY_new();
  631. if (pkpeer == NULL)
  632. goto err;
  633. EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
  634. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  635. rv = 1;
  636. err:
  637. EC_KEY_free(ecpeer);
  638. EVP_PKEY_free(pkpeer);
  639. return rv;
  640. }
  641. /* Set KDF parameters based on KDF NID */
  642. static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
  643. {
  644. int kdf_nid, kdfmd_nid, cofactor;
  645. const EVP_MD *kdf_md;
  646. if (eckdf_nid == NID_undef)
  647. return 0;
  648. /* Lookup KDF type, cofactor mode and digest */
  649. if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
  650. return 0;
  651. if (kdf_nid == NID_dh_std_kdf)
  652. cofactor = 0;
  653. else if (kdf_nid == NID_dh_cofactor_kdf)
  654. cofactor = 1;
  655. else
  656. return 0;
  657. if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
  658. return 0;
  659. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_62) <= 0)
  660. return 0;
  661. kdf_md = EVP_get_digestbynid(kdfmd_nid);
  662. if (!kdf_md)
  663. return 0;
  664. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  665. return 0;
  666. return 1;
  667. }
  668. static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  669. {
  670. int rv = 0;
  671. X509_ALGOR *alg, *kekalg = NULL;
  672. ASN1_OCTET_STRING *ukm;
  673. const unsigned char *p;
  674. unsigned char *der = NULL;
  675. int plen, keylen;
  676. const EVP_CIPHER *kekcipher;
  677. EVP_CIPHER_CTX *kekctx;
  678. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  679. return 0;
  680. if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
  681. ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
  682. return 0;
  683. }
  684. if (alg->parameter->type != V_ASN1_SEQUENCE)
  685. return 0;
  686. p = alg->parameter->value.sequence->data;
  687. plen = alg->parameter->value.sequence->length;
  688. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  689. if (!kekalg)
  690. goto err;
  691. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  692. if (!kekctx)
  693. goto err;
  694. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  695. if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  696. goto err;
  697. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  698. goto err;
  699. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  700. goto err;
  701. keylen = EVP_CIPHER_CTX_key_length(kekctx);
  702. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  703. goto err;
  704. plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
  705. if (!plen)
  706. goto err;
  707. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
  708. goto err;
  709. der = NULL;
  710. rv = 1;
  711. err:
  712. X509_ALGOR_free(kekalg);
  713. OPENSSL_free(der);
  714. return rv;
  715. }
  716. static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
  717. {
  718. EVP_PKEY_CTX *pctx;
  719. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  720. if (!pctx)
  721. return 0;
  722. /* See if we need to set peer key */
  723. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  724. X509_ALGOR *alg;
  725. ASN1_BIT_STRING *pubkey;
  726. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  727. NULL, NULL, NULL))
  728. return 0;
  729. if (!alg || !pubkey)
  730. return 0;
  731. if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
  732. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
  733. return 0;
  734. }
  735. }
  736. /* Set ECDH derivation parameters and initialise unwrap context */
  737. if (!ecdh_cms_set_shared_info(pctx, ri)) {
  738. ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
  739. return 0;
  740. }
  741. return 1;
  742. }
  743. static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
  744. {
  745. EVP_PKEY_CTX *pctx;
  746. EVP_PKEY *pkey;
  747. EVP_CIPHER_CTX *ctx;
  748. int keylen;
  749. X509_ALGOR *talg, *wrap_alg = NULL;
  750. ASN1_OBJECT *aoid;
  751. ASN1_BIT_STRING *pubkey;
  752. ASN1_STRING *wrap_str;
  753. ASN1_OCTET_STRING *ukm;
  754. unsigned char *penc = NULL;
  755. int penclen;
  756. int rv = 0;
  757. int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
  758. const EVP_MD *kdf_md;
  759. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  760. if (!pctx)
  761. return 0;
  762. /* Get ephemeral key */
  763. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  764. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  765. NULL, NULL, NULL))
  766. goto err;
  767. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  768. /* Is everything uninitialised? */
  769. if (aoid == OBJ_nid2obj(NID_undef)) {
  770. EC_KEY *eckey = pkey->pkey.ec;
  771. /* Set the key */
  772. unsigned char *p;
  773. penclen = i2o_ECPublicKey(eckey, NULL);
  774. if (penclen <= 0)
  775. goto err;
  776. penc = OPENSSL_malloc(penclen);
  777. if (penc == NULL)
  778. goto err;
  779. p = penc;
  780. penclen = i2o_ECPublicKey(eckey, &p);
  781. if (penclen <= 0)
  782. goto err;
  783. ASN1_STRING_set0(pubkey, penc, penclen);
  784. pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  785. pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  786. penc = NULL;
  787. X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
  788. V_ASN1_UNDEF, NULL);
  789. }
  790. /* See if custom paraneters set */
  791. kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
  792. if (kdf_type <= 0)
  793. goto err;
  794. if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
  795. goto err;
  796. ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
  797. if (ecdh_nid < 0)
  798. goto err;
  799. else if (ecdh_nid == 0)
  800. ecdh_nid = NID_dh_std_kdf;
  801. else if (ecdh_nid == 1)
  802. ecdh_nid = NID_dh_cofactor_kdf;
  803. if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
  804. kdf_type = EVP_PKEY_ECDH_KDF_X9_62;
  805. if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
  806. goto err;
  807. } else
  808. /* Uknown KDF */
  809. goto err;
  810. if (kdf_md == NULL) {
  811. /* Fixme later for better MD */
  812. kdf_md = EVP_sha1();
  813. if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
  814. goto err;
  815. }
  816. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  817. goto err;
  818. /* Lookup NID for KDF+cofactor+digest */
  819. if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
  820. goto err;
  821. /* Get wrap NID */
  822. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  823. wrap_nid = EVP_CIPHER_CTX_type(ctx);
  824. keylen = EVP_CIPHER_CTX_key_length(ctx);
  825. /* Package wrap algorithm in an AlgorithmIdentifier */
  826. wrap_alg = X509_ALGOR_new();
  827. if (wrap_alg == NULL)
  828. goto err;
  829. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  830. wrap_alg->parameter = ASN1_TYPE_new();
  831. if (wrap_alg->parameter == NULL)
  832. goto err;
  833. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  834. goto err;
  835. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  836. ASN1_TYPE_free(wrap_alg->parameter);
  837. wrap_alg->parameter = NULL;
  838. }
  839. if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
  840. goto err;
  841. penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
  842. if (!penclen)
  843. goto err;
  844. if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
  845. goto err;
  846. penc = NULL;
  847. /*
  848. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  849. * of another AlgorithmIdentifier.
  850. */
  851. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  852. if (!penc || !penclen)
  853. goto err;
  854. wrap_str = ASN1_STRING_new();
  855. if (wrap_str == NULL)
  856. goto err;
  857. ASN1_STRING_set0(wrap_str, penc, penclen);
  858. penc = NULL;
  859. X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
  860. rv = 1;
  861. err:
  862. OPENSSL_free(penc);
  863. X509_ALGOR_free(wrap_alg);
  864. return rv;
  865. }
  866. #endif