1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
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>
66 #include "asn1_locl.h"
68 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
70 const EC_GROUP *group;
72 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL)
74 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
77 if (EC_GROUP_get_asn1_flag(group)
78 && (nid = EC_GROUP_get_curve_name(group)))
79 /* we have a 'named curve' => just set the OID */
81 *ppval = OBJ_nid2obj(nid);
82 *pptype = V_ASN1_OBJECT;
84 else /* explicit parameters */
86 ASN1_STRING *pstr = NULL;
87 pstr = ASN1_STRING_new();
90 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
93 ASN1_STRING_free(pstr);
94 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
98 *pptype = V_ASN1_SEQUENCE;
103 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
105 EC_KEY *ec_key = pkey->pkey.ec;
108 unsigned char *penc = NULL, *p;
111 if (!eckey_param2type(&ptype, &pval, ec_key))
113 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
116 penclen = i2o_ECPublicKey(ec_key, NULL);
119 penc = OPENSSL_malloc(penclen);
123 penclen = i2o_ECPublicKey(ec_key, &p);
126 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
127 ptype, pval, penc, penclen))
130 if (ptype == V_ASN1_OBJECT)
131 ASN1_OBJECT_free(pval);
133 ASN1_STRING_free(pval);
139 static EC_KEY *eckey_type2param(int ptype, void *pval)
141 EC_KEY *eckey = NULL;
142 if (ptype == V_ASN1_SEQUENCE)
144 ASN1_STRING *pstr = pval;
145 const unsigned char *pm = NULL;
148 pmlen = pstr->length;
149 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen)))
151 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
155 else if (ptype == V_ASN1_OBJECT)
157 ASN1_OBJECT *poid = pval;
160 /* type == V_ASN1_OBJECT => the parameters are given
163 if ((eckey = EC_KEY_new()) == NULL)
165 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
168 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
171 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
172 if (EC_KEY_set_group(eckey, group) == 0)
174 EC_GROUP_free(group);
178 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
190 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
192 const unsigned char *p = NULL;
195 EC_KEY *eckey = NULL;
198 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
200 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
202 eckey = eckey_type2param(ptype, pval);
206 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
210 /* We have parameters now set public key */
211 if (!o2i_ECPublicKey(&eckey, &p, pklen))
213 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
217 EVP_PKEY_assign_EC_KEY(pkey, eckey);
226 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
229 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
230 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
231 *pb = EC_KEY_get0_public_key(b->pkey.ec);
232 r = EC_POINT_cmp(group, pa, pb, NULL);
240 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
242 const unsigned char *p = NULL;
245 EC_KEY *eckey = NULL;
248 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
250 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
252 eckey = eckey_type2param(ptype, pval);
257 /* We have parameters now set private key */
258 if (!d2i_ECPrivateKey(&eckey, &p, pklen))
260 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
264 /* calculate public key (if necessary) */
265 if (EC_KEY_get0_public_key(eckey) == NULL)
267 const BIGNUM *priv_key;
268 const EC_GROUP *group;
270 /* the public key was not included in the SEC1 private
271 * key => calculate the public key */
272 group = EC_KEY_get0_group(eckey);
273 pub_key = EC_POINT_new(group);
276 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
279 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group)))
281 EC_POINT_free(pub_key);
282 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
285 priv_key = EC_KEY_get0_private_key(eckey);
286 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
288 EC_POINT_free(pub_key);
289 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
292 if (EC_KEY_set_public_key(eckey, pub_key) == 0)
294 EC_POINT_free(pub_key);
295 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
298 EC_POINT_free(pub_key);
301 EVP_PKEY_assign_EC_KEY(pkey, eckey);
305 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
312 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
315 unsigned char *ep, *p;
318 unsigned int tmp_flags, old_flags;
320 ec_key = pkey->pkey.ec;
322 if (!eckey_param2type(&ptype, &pval, ec_key))
324 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
328 /* set the private key */
330 /* do not include the parameters in the SEC1 private key
331 * see PKCS#11 12.11 */
332 old_flags = EC_KEY_get_enc_flags(ec_key);
333 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
334 EC_KEY_set_enc_flags(ec_key, tmp_flags);
335 eplen = i2d_ECPrivateKey(ec_key, NULL);
338 EC_KEY_set_enc_flags(ec_key, old_flags);
339 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
342 ep = (unsigned char *) OPENSSL_malloc(eplen);
345 EC_KEY_set_enc_flags(ec_key, old_flags);
346 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
350 if (!i2d_ECPrivateKey(ec_key, &p))
352 EC_KEY_set_enc_flags(ec_key, old_flags);
354 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
356 /* restore old encoding flags */
357 EC_KEY_set_enc_flags(ec_key, old_flags);
359 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
360 ptype, pval, ep, eplen))
366 static int int_ec_size(const EVP_PKEY *pkey)
368 return ECDSA_size(pkey->pkey.ec);
371 static int ec_bits(const EVP_PKEY *pkey)
373 BIGNUM *order = BN_new();
374 const EC_GROUP *group;
382 group = EC_KEY_get0_group(pkey->pkey.ec);
383 if (!EC_GROUP_get_order(group, order, NULL))
389 ret = BN_num_bits(order);
394 static int ec_missing_parameters(const EVP_PKEY *pkey)
396 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
401 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
403 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
406 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
408 EC_GROUP_free(group);
412 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
414 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
415 *group_b = EC_KEY_get0_group(b->pkey.ec);
416 if (EC_GROUP_cmp(group_a, group_b, NULL))
422 static void int_ec_free(EVP_PKEY *pkey)
424 EC_KEY_free(pkey->pkey.ec);
427 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
429 unsigned char *buffer=NULL;
432 int ret=0, reason=ERR_R_BIO_LIB;
433 BIGNUM *pub_key=NULL, *order=NULL;
435 const EC_GROUP *group;
436 const EC_POINT *public_key;
437 const BIGNUM *priv_key;
439 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
441 reason = ERR_R_PASSED_NULL_PARAMETER;
448 reason = ERR_R_MALLOC_FAILURE;
454 public_key = EC_KEY_get0_public_key(x);
455 if ((pub_key = EC_POINT_point2bn(group, public_key,
456 EC_KEY_get_conv_form(x), NULL, ctx)) == NULL)
458 reason = ERR_R_EC_LIB;
462 buf_len = (size_t)BN_num_bytes(pub_key);
467 priv_key = EC_KEY_get0_private_key(x);
468 if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len)
477 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
479 reason = ERR_R_MALLOC_FAILURE;
484 ecstr = "Private-Key";
486 ecstr = "Public-Key";
488 ecstr = "ECDSA-Parameters";
490 if (!BIO_indent(bp, off, 128))
492 if ((order = BN_new()) == NULL)
494 if (!EC_GROUP_get_order(group, order, NULL))
496 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
497 BN_num_bits(order)) <= 0) goto err;
499 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
502 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
505 if (!ECPKParameters_print(bp, group, off))
510 ECerr(EC_F_DO_EC_KEY_PRINT, reason);
518 OPENSSL_free(buffer);
522 static int eckey_param_decode(EVP_PKEY *pkey,
523 const unsigned char **pder, int derlen)
526 if (!(eckey = d2i_ECParameters(NULL, pder, derlen)))
528 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
531 EVP_PKEY_assign_EC_KEY(pkey, eckey);
535 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
537 return i2d_ECParameters(pkey->pkey.ec, pder);
540 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
543 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
546 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
549 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
553 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
556 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
559 static int old_ec_priv_decode(EVP_PKEY *pkey,
560 const unsigned char **pder, int derlen)
563 if (!(ec = d2i_ECPrivateKey (NULL, pder, derlen)))
565 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
568 EVP_PKEY_assign_EC_KEY(pkey, ec);
572 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
574 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
577 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
581 case ASN1_PKEY_CTRL_PKCS7_SIGN:
585 X509_ALGOR *alg1, *alg2;
586 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
587 if (alg1 == NULL || alg1->algorithm == NULL)
589 hnid = OBJ_obj2nid(alg1->algorithm);
590 if (hnid == NID_undef)
592 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
594 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
597 #ifndef OPENSSL_NO_CMS
598 case ASN1_PKEY_CTRL_CMS_SIGN:
602 X509_ALGOR *alg1, *alg2;
603 CMS_SignerInfo_get0_algs(arg2, NULL, NULL,
605 if (alg1 == NULL || alg1->algorithm == NULL)
607 hnid = OBJ_obj2nid(alg1->algorithm);
608 if (hnid == NID_undef)
610 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
612 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
617 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
618 *(int *)arg2 = NID_sha1;
628 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth =
634 "OpenSSL EC algorithm",
650 ec_missing_parameters,