ec_ameth.c 27 KB

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