ec_ameth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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 "asn1_locl.h"
  67. static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
  68. {
  69. const EC_GROUP *group;
  70. int nid;
  71. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  72. ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
  73. return 0;
  74. }
  75. if (EC_GROUP_get_asn1_flag(group)
  76. && (nid = EC_GROUP_get_curve_name(group)))
  77. /* we have a 'named curve' => just set the OID */
  78. {
  79. *ppval = OBJ_nid2obj(nid);
  80. *pptype = V_ASN1_OBJECT;
  81. } else { /* explicit parameters */
  82. ASN1_STRING *pstr = NULL;
  83. pstr = ASN1_STRING_new();
  84. if (!pstr)
  85. return 0;
  86. pstr->length = i2d_ECParameters(ec_key, &pstr->data);
  87. if (pstr->length <= 0) {
  88. ASN1_STRING_free(pstr);
  89. ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
  90. return 0;
  91. }
  92. *ppval = pstr;
  93. *pptype = V_ASN1_SEQUENCE;
  94. }
  95. return 1;
  96. }
  97. static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  98. {
  99. EC_KEY *ec_key = pkey->pkey.ec;
  100. void *pval = NULL;
  101. int ptype;
  102. unsigned char *penc = NULL, *p;
  103. int penclen;
  104. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  105. ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
  106. return 0;
  107. }
  108. penclen = i2o_ECPublicKey(ec_key, NULL);
  109. if (penclen <= 0)
  110. goto err;
  111. penc = OPENSSL_malloc(penclen);
  112. if (!penc)
  113. goto err;
  114. p = penc;
  115. penclen = i2o_ECPublicKey(ec_key, &p);
  116. if (penclen <= 0)
  117. goto err;
  118. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
  119. ptype, pval, penc, penclen))
  120. return 1;
  121. err:
  122. if (ptype == V_ASN1_OBJECT)
  123. ASN1_OBJECT_free(pval);
  124. else
  125. ASN1_STRING_free(pval);
  126. if (penc)
  127. OPENSSL_free(penc);
  128. return 0;
  129. }
  130. static EC_KEY *eckey_type2param(int ptype, void *pval)
  131. {
  132. EC_KEY *eckey = NULL;
  133. if (ptype == V_ASN1_SEQUENCE) {
  134. ASN1_STRING *pstr = pval;
  135. const unsigned char *pm = NULL;
  136. int pmlen;
  137. pm = pstr->data;
  138. pmlen = pstr->length;
  139. if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) {
  140. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  141. goto ecerr;
  142. }
  143. } else if (ptype == V_ASN1_OBJECT) {
  144. ASN1_OBJECT *poid = pval;
  145. EC_GROUP *group;
  146. /*
  147. * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
  148. */
  149. if ((eckey = EC_KEY_new()) == NULL) {
  150. ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
  151. goto ecerr;
  152. }
  153. group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
  154. if (group == NULL)
  155. goto ecerr;
  156. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  157. if (EC_KEY_set_group(eckey, group) == 0)
  158. goto ecerr;
  159. EC_GROUP_free(group);
  160. } else {
  161. ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
  162. goto ecerr;
  163. }
  164. return eckey;
  165. ecerr:
  166. if (eckey)
  167. EC_KEY_free(eckey);
  168. return NULL;
  169. }
  170. static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  171. {
  172. const unsigned char *p = NULL;
  173. void *pval;
  174. int ptype, pklen;
  175. EC_KEY *eckey = NULL;
  176. X509_ALGOR *palg;
  177. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  178. return 0;
  179. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  180. eckey = eckey_type2param(ptype, pval);
  181. if (!eckey) {
  182. ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
  183. return 0;
  184. }
  185. /* We have parameters now set public key */
  186. if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
  187. ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
  188. goto ecerr;
  189. }
  190. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  191. return 1;
  192. ecerr:
  193. if (eckey)
  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. if (eckey)
  267. EC_KEY_free(eckey);
  268. return 0;
  269. }
  270. static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  271. {
  272. EC_KEY *ec_key;
  273. unsigned char *ep, *p;
  274. int eplen, ptype;
  275. void *pval;
  276. unsigned int tmp_flags, old_flags;
  277. ec_key = pkey->pkey.ec;
  278. if (!eckey_param2type(&ptype, &pval, ec_key)) {
  279. ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
  280. return 0;
  281. }
  282. /* set the private key */
  283. /*
  284. * do not include the parameters in the SEC1 private key see PKCS#11
  285. * 12.11
  286. */
  287. old_flags = EC_KEY_get_enc_flags(ec_key);
  288. tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
  289. EC_KEY_set_enc_flags(ec_key, tmp_flags);
  290. eplen = i2d_ECPrivateKey(ec_key, NULL);
  291. if (!eplen) {
  292. EC_KEY_set_enc_flags(ec_key, old_flags);
  293. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  294. return 0;
  295. }
  296. ep = (unsigned char *)OPENSSL_malloc(eplen);
  297. if (!ep) {
  298. EC_KEY_set_enc_flags(ec_key, old_flags);
  299. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  300. return 0;
  301. }
  302. p = ep;
  303. if (!i2d_ECPrivateKey(ec_key, &p)) {
  304. EC_KEY_set_enc_flags(ec_key, old_flags);
  305. OPENSSL_free(ep);
  306. ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
  307. return 0;
  308. }
  309. /* restore old encoding flags */
  310. EC_KEY_set_enc_flags(ec_key, old_flags);
  311. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
  312. ptype, pval, ep, eplen))
  313. return 0;
  314. return 1;
  315. }
  316. static int int_ec_size(const EVP_PKEY *pkey)
  317. {
  318. return ECDSA_size(pkey->pkey.ec);
  319. }
  320. static int ec_bits(const EVP_PKEY *pkey)
  321. {
  322. BIGNUM *order = BN_new();
  323. const EC_GROUP *group;
  324. int ret;
  325. if (!order) {
  326. ERR_clear_error();
  327. return 0;
  328. }
  329. group = EC_KEY_get0_group(pkey->pkey.ec);
  330. if (!EC_GROUP_get_order(group, order, NULL)) {
  331. ERR_clear_error();
  332. return 0;
  333. }
  334. ret = BN_num_bits(order);
  335. BN_free(order);
  336. return ret;
  337. }
  338. static int ec_missing_parameters(const EVP_PKEY *pkey)
  339. {
  340. if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  341. return 1;
  342. return 0;
  343. }
  344. static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  345. {
  346. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  347. if (group == NULL)
  348. return 0;
  349. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  350. return 0;
  351. EC_GROUP_free(group);
  352. return 1;
  353. }
  354. static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  355. {
  356. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  357. *group_b = EC_KEY_get0_group(b->pkey.ec);
  358. if (EC_GROUP_cmp(group_a, group_b, NULL))
  359. return 0;
  360. else
  361. return 1;
  362. }
  363. static void int_ec_free(EVP_PKEY *pkey)
  364. {
  365. EC_KEY_free(pkey->pkey.ec);
  366. }
  367. static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
  368. {
  369. unsigned char *buffer = NULL;
  370. const char *ecstr;
  371. size_t buf_len = 0, i;
  372. int ret = 0, reason = ERR_R_BIO_LIB;
  373. BIGNUM *pub_key = NULL, *order = NULL;
  374. BN_CTX *ctx = NULL;
  375. const EC_GROUP *group;
  376. const EC_POINT *public_key;
  377. const BIGNUM *priv_key;
  378. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
  379. reason = ERR_R_PASSED_NULL_PARAMETER;
  380. goto err;
  381. }
  382. ctx = BN_CTX_new();
  383. if (ctx == NULL) {
  384. reason = ERR_R_MALLOC_FAILURE;
  385. goto err;
  386. }
  387. if (ktype > 0) {
  388. public_key = EC_KEY_get0_public_key(x);
  389. if (public_key != NULL) {
  390. if ((pub_key = EC_POINT_point2bn(group, public_key,
  391. EC_KEY_get_conv_form(x), NULL,
  392. ctx)) == NULL) {
  393. reason = ERR_R_EC_LIB;
  394. goto err;
  395. }
  396. buf_len = (size_t)BN_num_bytes(pub_key);
  397. }
  398. }
  399. if (ktype == 2) {
  400. priv_key = EC_KEY_get0_private_key(x);
  401. if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len)
  402. buf_len = i;
  403. } else
  404. priv_key = NULL;
  405. if (ktype > 0) {
  406. buf_len += 10;
  407. if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
  408. reason = ERR_R_MALLOC_FAILURE;
  409. goto err;
  410. }
  411. }
  412. if (ktype == 2)
  413. ecstr = "Private-Key";
  414. else if (ktype == 1)
  415. ecstr = "Public-Key";
  416. else
  417. ecstr = "ECDSA-Parameters";
  418. if (!BIO_indent(bp, off, 128))
  419. goto err;
  420. if ((order = BN_new()) == NULL)
  421. goto err;
  422. if (!EC_GROUP_get_order(group, order, NULL))
  423. goto err;
  424. if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0)
  425. goto err;
  426. if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
  427. buffer, off))
  428. goto err;
  429. if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
  430. buffer, off))
  431. goto err;
  432. if (!ECPKParameters_print(bp, group, off))
  433. goto err;
  434. ret = 1;
  435. err:
  436. if (!ret)
  437. ECerr(EC_F_DO_EC_KEY_PRINT, reason);
  438. if (pub_key)
  439. BN_free(pub_key);
  440. if (order)
  441. BN_free(order);
  442. if (ctx)
  443. BN_CTX_free(ctx);
  444. if (buffer != NULL)
  445. OPENSSL_free(buffer);
  446. return (ret);
  447. }
  448. static int eckey_param_decode(EVP_PKEY *pkey,
  449. const unsigned char **pder, int derlen)
  450. {
  451. EC_KEY *eckey;
  452. if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) {
  453. ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
  454. return 0;
  455. }
  456. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  457. return 1;
  458. }
  459. static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  460. {
  461. return i2d_ECParameters(pkey->pkey.ec, pder);
  462. }
  463. static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  464. ASN1_PCTX *ctx)
  465. {
  466. return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
  467. }
  468. static int eckey_pub_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, 1);
  472. }
  473. static int eckey_priv_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, 2);
  477. }
  478. static int old_ec_priv_decode(EVP_PKEY *pkey,
  479. const unsigned char **pder, int derlen)
  480. {
  481. EC_KEY *ec;
  482. if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
  483. ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
  484. return 0;
  485. }
  486. EVP_PKEY_assign_EC_KEY(pkey, ec);
  487. return 1;
  488. }
  489. static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  490. {
  491. return i2d_ECPrivateKey(pkey->pkey.ec, pder);
  492. }
  493. static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  494. {
  495. switch (op) {
  496. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  497. if (arg1 == 0) {
  498. int snid, hnid;
  499. X509_ALGOR *alg1, *alg2;
  500. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  501. if (alg1 == NULL || alg1->algorithm == NULL)
  502. return -1;
  503. hnid = OBJ_obj2nid(alg1->algorithm);
  504. if (hnid == NID_undef)
  505. return -1;
  506. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  507. return -1;
  508. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  509. }
  510. return 1;
  511. #ifndef OPENSSL_NO_CMS
  512. case ASN1_PKEY_CTRL_CMS_SIGN:
  513. if (arg1 == 0) {
  514. int snid, hnid;
  515. X509_ALGOR *alg1, *alg2;
  516. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  517. if (alg1 == NULL || alg1->algorithm == NULL)
  518. return -1;
  519. hnid = OBJ_obj2nid(alg1->algorithm);
  520. if (hnid == NID_undef)
  521. return -1;
  522. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  523. return -1;
  524. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  525. }
  526. return 1;
  527. #endif
  528. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  529. *(int *)arg2 = NID_sha1;
  530. return 2;
  531. default:
  532. return -2;
  533. }
  534. }
  535. const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
  536. EVP_PKEY_EC,
  537. EVP_PKEY_EC,
  538. 0,
  539. "EC",
  540. "OpenSSL EC algorithm",
  541. eckey_pub_decode,
  542. eckey_pub_encode,
  543. eckey_pub_cmp,
  544. eckey_pub_print,
  545. eckey_priv_decode,
  546. eckey_priv_encode,
  547. eckey_priv_print,
  548. int_ec_size,
  549. ec_bits,
  550. eckey_param_decode,
  551. eckey_param_encode,
  552. ec_missing_parameters,
  553. ec_copy_parameters,
  554. ec_cmp_parameters,
  555. eckey_param_print,
  556. 0,
  557. int_ec_free,
  558. ec_pkey_ctrl,
  559. old_ec_priv_decode,
  560. old_ec_priv_encode
  561. };