ec_ameth.c 27 KB

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