ec_ameth.c 27 KB

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