1 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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>
63 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
65 const EC_GROUP *group;
67 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL)
69 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
72 if (EC_GROUP_get_asn1_flag(group)
73 && (nid = EC_GROUP_get_curve_name(group)))
74 /* we have a 'named curve' => just set the OID */
76 *ppval = OBJ_nid2obj(nid);
77 *pptype = V_ASN1_OBJECT;
79 else /* explicit parameters */
81 ASN1_STRING *pstr = NULL;
82 pstr = ASN1_STRING_new();
85 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
88 ASN1_STRING_free(pstr);
89 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
93 *pptype = V_ASN1_SEQUENCE;
98 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
100 EC_KEY *ec_key = pkey->pkey.ec;
103 unsigned char *penc = NULL, *p;
106 if (!eckey_param2type(&ptype, &pval, ec_key))
108 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
111 penclen = i2o_ECPublicKey(ec_key, NULL);
114 penc = OPENSSL_malloc(penclen);
118 penclen = i2o_ECPublicKey(ec_key, &p);
121 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
122 ptype, pval, penc, penclen))
125 if (ptype == V_ASN1_OBJECT)
126 ASN1_OBJECT_free(pval);
128 ASN1_STRING_free(pval);
134 static EC_KEY *eckey_type2param(int ptype, void *pval)
136 EC_KEY *eckey = NULL;
137 if (ptype == V_ASN1_SEQUENCE)
139 ASN1_STRING *pstr = pval;
140 const unsigned char *pm = NULL;
143 pmlen = pstr->length;
144 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen)))
146 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
150 else if (ptype == V_ASN1_OBJECT)
152 ASN1_OBJECT *poid = pval;
155 /* type == V_ASN1_OBJECT => the parameters are given
158 if ((eckey = EC_KEY_new()) == NULL)
160 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
163 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
166 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
167 if (EC_KEY_set_group(eckey, group) == 0)
169 EC_GROUP_free(group);
173 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
185 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
187 const unsigned char *p = NULL;
190 EC_KEY *eckey = NULL;
193 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
195 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
197 eckey = eckey_type2param(ptype, pval);
201 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
205 /* We have parameters now set public key */
206 if (!o2i_ECPublicKey(&eckey, &p, pklen))
208 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
212 EVP_PKEY_assign_EC_KEY(pkey, eckey);
221 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
224 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
225 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
226 *pb = EC_KEY_get0_public_key(b->pkey.ec);
227 r = EC_POINT_cmp(group, pa, pb, NULL);
235 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
237 const unsigned char *p = NULL;
240 EC_KEY *eckey = NULL;
243 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
245 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
247 eckey = eckey_type2param(ptype, pval);
252 /* We have parameters now set private key */
253 if (!d2i_ECPrivateKey(&eckey, &p, pklen))
255 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
259 /* calculate public key (if necessary) */
260 if (EC_KEY_get0_public_key(eckey) == NULL)
262 const BIGNUM *priv_key;
263 const EC_GROUP *group;
265 /* the public key was not included in the SEC1 private
266 * key => calculate the public key */
267 group = EC_KEY_get0_group(eckey);
268 pub_key = EC_POINT_new(group);
271 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
274 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group)))
276 EC_POINT_free(pub_key);
277 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
280 priv_key = EC_KEY_get0_private_key(eckey);
281 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
283 EC_POINT_free(pub_key);
284 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
287 if (EC_KEY_set_public_key(eckey, pub_key) == 0)
289 EC_POINT_free(pub_key);
290 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
293 EC_POINT_free(pub_key);
296 EVP_PKEY_assign_EC_KEY(pkey, eckey);
300 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
307 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
310 unsigned char *ep, *p;
313 unsigned int tmp_flags, old_flags;
315 ec_key = pkey->pkey.ec;
317 if (!eckey_param2type(&ptype, &pval, ec_key))
319 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
323 /* set the private key */
325 /* do not include the parameters in the SEC1 private key
326 * see PKCS#11 12.11 */
327 old_flags = EC_KEY_get_enc_flags(ec_key);
328 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
329 EC_KEY_set_enc_flags(ec_key, tmp_flags);
330 eplen = i2d_ECPrivateKey(ec_key, NULL);
333 EC_KEY_set_enc_flags(ec_key, old_flags);
334 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
337 ep = (unsigned char *) OPENSSL_malloc(eplen);
340 EC_KEY_set_enc_flags(ec_key, old_flags);
341 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
345 if (!i2d_ECPrivateKey(ec_key, &p))
347 EC_KEY_set_enc_flags(ec_key, old_flags);
349 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
351 /* restore old encoding flags */
352 EC_KEY_set_enc_flags(ec_key, old_flags);
354 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
355 ptype, pval, ep, eplen))
361 static int int_ec_size(const EVP_PKEY *pkey)
363 return ECDSA_size(pkey->pkey.ec);
366 static int ec_bits(const EVP_PKEY *pkey)
368 BIGNUM *order = BN_new();
369 const EC_GROUP *group;
377 group = EC_KEY_get0_group(pkey->pkey.ec);
378 if (!EC_GROUP_get_order(group, order, NULL))
384 ret = BN_num_bits(order);
389 static int ec_missing_parameters(const EVP_PKEY *pkey)
391 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
396 int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
398 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
401 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
403 EC_GROUP_free(group);
407 int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
409 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
410 *group_b = EC_KEY_get0_group(b->pkey.ec);
411 if (EC_GROUP_cmp(group_a, group_b, NULL))
417 static void int_ec_free(EVP_PKEY *pkey)
419 EC_KEY_free(pkey->pkey.ec);
422 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
424 unsigned char *buffer=NULL;
427 int ret=0, reason=ERR_R_BIO_LIB;
428 BIGNUM *pub_key=NULL, *order=NULL;
430 const EC_GROUP *group;
431 const EC_POINT *public_key;
432 const BIGNUM *priv_key;
434 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
436 reason = ERR_R_PASSED_NULL_PARAMETER;
443 reason = ERR_R_MALLOC_FAILURE;
450 public_key = EC_KEY_get0_public_key(x);
451 if ((pub_key = EC_POINT_point2bn(group, public_key,
452 EC_KEY_get_conv_form(x), NULL, ctx)) == NULL)
454 reason = ERR_R_EC_LIB;
457 buf_len = (size_t)BN_num_bytes(pub_key);
463 if ((i = (size_t)BN_num_bytes(priv_key)) > buf_len)
465 priv_key = EC_KEY_get0_private_key(x);
473 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
475 reason = ERR_R_MALLOC_FAILURE;
480 ecstr = "Private-Key";
482 ecstr = "Public-Key";
484 ecstr = "ECDSA-Parameters";
486 if (!BIO_indent(bp, off, 128))
488 if ((order = BN_new()) == NULL)
490 if (!EC_GROUP_get_order(group, order, NULL))
492 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
493 BN_num_bits(order)) <= 0) goto err;
495 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
498 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
501 if (!ECPKParameters_print(bp, group, off))
506 ECerr(EC_F_EC_KEY_PRINT, reason);
514 OPENSSL_free(buffer);
518 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
521 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
524 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
527 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
531 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
534 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
537 EVP_PKEY_ASN1_METHOD eckey_asn1_meth =
556 ec_missing_parameters,
565 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
566 size_t len, int off);
568 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
570 unsigned char *buffer=NULL;
572 int ret=0, reason=ERR_R_BIO_LIB;
574 const EC_POINT *point=NULL;
575 BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
576 *order=NULL, *cofactor=NULL;
577 const unsigned char *seed;
580 static const char *gen_compressed = "Generator (compressed):";
581 static const char *gen_uncompressed = "Generator (uncompressed):";
582 static const char *gen_hybrid = "Generator (hybrid):";
586 reason = ERR_R_PASSED_NULL_PARAMETER;
593 reason = ERR_R_MALLOC_FAILURE;
597 if (EC_GROUP_get_asn1_flag(x))
599 /* the curve parameter are given by an asn1 OID */
602 if (!BIO_indent(bp, off, 128))
605 nid = EC_GROUP_get_curve_name(x);
609 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
611 if (BIO_printf(bp, "\n") <= 0)
616 /* explicit parameters */
618 point_conversion_form_t form;
619 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
621 if (tmp_nid == NID_X9_62_characteristic_two_field)
624 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
625 (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
626 (cofactor = BN_new()) == NULL)
628 reason = ERR_R_MALLOC_FAILURE;
634 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
636 reason = ERR_R_EC_LIB;
640 else /* prime field */
642 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
644 reason = ERR_R_EC_LIB;
649 if ((point = EC_GROUP_get0_generator(x)) == NULL)
651 reason = ERR_R_EC_LIB;
654 if (!EC_GROUP_get_order(x, order, NULL) ||
655 !EC_GROUP_get_cofactor(x, cofactor, NULL))
657 reason = ERR_R_EC_LIB;
661 form = EC_GROUP_get_point_conversion_form(x);
663 if ((gen = EC_POINT_point2bn(x, point,
664 form, NULL, ctx)) == NULL)
666 reason = ERR_R_EC_LIB;
670 buf_len = (size_t)BN_num_bytes(p);
671 if (buf_len < (i = (size_t)BN_num_bytes(a)))
673 if (buf_len < (i = (size_t)BN_num_bytes(b)))
675 if (buf_len < (i = (size_t)BN_num_bytes(gen)))
677 if (buf_len < (i = (size_t)BN_num_bytes(order)))
679 if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
682 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
683 seed_len = EC_GROUP_get_seed_len(x);
686 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
688 reason = ERR_R_MALLOC_FAILURE;
692 if (!BIO_indent(bp, off, 128))
695 /* print the 'short name' of the field type */
696 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
702 /* print the 'short name' of the base type OID */
703 int basis_type = EC_GROUP_get_basis_type(x);
707 if (!BIO_indent(bp, off, 128))
710 if (BIO_printf(bp, "Basis Type: %s\n",
711 OBJ_nid2sn(basis_type)) <= 0)
714 /* print the polynomial */
715 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
721 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off))
724 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
726 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
728 if (form == POINT_CONVERSION_COMPRESSED)
730 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
734 else if (form == POINT_CONVERSION_UNCOMPRESSED)
736 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
740 else /* form == POINT_CONVERSION_HYBRID */
742 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
746 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
747 buffer, off)) goto err;
748 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
749 buffer, off)) goto err;
750 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
756 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
772 OPENSSL_free(buffer);
776 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
789 if (BIO_write(fp, str, off) <= 0)
793 if (BIO_printf(fp,"%s", name) <= 0)
796 for (i=0; i<len; i++)
801 memset(&(str[1]),' ',off+4);
802 if (BIO_write(fp, str, off+1+4) <= 0)
805 if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
808 if (BIO_write(fp,"\n",1) <= 0)