2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <openssl/x509.h>
62 #include <openssl/ec.h>
63 #include <openssl/bn.h>
64 #ifndef OPENSSL_NO_CMS
65 # include <openssl/cms.h>
67 #include "asn1_locl.h"
69 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
71 const EC_GROUP *group;
73 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;
83 } else { /* explicit parameters */
85 ASN1_STRING *pstr = NULL;
86 pstr = ASN1_STRING_new();
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);
96 *pptype = V_ASN1_SEQUENCE;
101 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
103 EC_KEY *ec_key = pkey->pkey.ec;
106 unsigned char *penc = NULL, *p;
109 if (!eckey_param2type(&ptype, &pval, ec_key)) {
110 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
113 penclen = i2o_ECPublicKey(ec_key, NULL);
116 penc = OPENSSL_malloc(penclen);
120 penclen = i2o_ECPublicKey(ec_key, &p);
123 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
124 ptype, pval, penc, penclen))
127 if (ptype == V_ASN1_OBJECT)
128 ASN1_OBJECT_free(pval);
130 ASN1_STRING_free(pval);
136 static EC_KEY *eckey_type2param(int ptype, void *pval)
138 EC_KEY *eckey = NULL;
139 if (ptype == V_ASN1_SEQUENCE) {
140 ASN1_STRING *pstr = pval;
141 const unsigned char *pm = NULL;
144 pmlen = pstr->length;
145 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) {
146 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
149 } else if (ptype == V_ASN1_OBJECT) {
150 ASN1_OBJECT *poid = pval;
154 * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
156 if ((eckey = EC_KEY_new()) == NULL) {
157 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
160 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
163 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
164 if (EC_KEY_set_group(eckey, group) == 0)
166 EC_GROUP_free(group);
168 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
180 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
182 const unsigned char *p = NULL;
185 EC_KEY *eckey = NULL;
188 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
190 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
192 eckey = eckey_type2param(ptype, pval);
195 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
199 /* We have parameters now set public key */
200 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
201 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
205 EVP_PKEY_assign_EC_KEY(pkey, eckey);
214 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
217 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
218 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
219 *pb = EC_KEY_get0_public_key(b->pkey.ec);
220 r = EC_POINT_cmp(group, pa, pb, NULL);
228 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
230 const unsigned char *p = NULL;
233 EC_KEY *eckey = NULL;
236 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
238 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
240 eckey = eckey_type2param(ptype, pval);
245 /* We have parameters now set private key */
246 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
247 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
251 /* calculate public key (if necessary) */
252 if (EC_KEY_get0_public_key(eckey) == NULL) {
253 const BIGNUM *priv_key;
254 const EC_GROUP *group;
257 * the public key was not included in the SEC1 private key =>
258 * calculate the public key
260 group = EC_KEY_get0_group(eckey);
261 pub_key = EC_POINT_new(group);
262 if (pub_key == NULL) {
263 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
266 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
267 EC_POINT_free(pub_key);
268 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
271 priv_key = EC_KEY_get0_private_key(eckey);
272 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
273 EC_POINT_free(pub_key);
274 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
277 if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
278 EC_POINT_free(pub_key);
279 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
282 EC_POINT_free(pub_key);
285 EVP_PKEY_assign_EC_KEY(pkey, eckey);
289 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
296 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
299 unsigned char *ep, *p;
302 unsigned int tmp_flags, old_flags;
304 ec_key = pkey->pkey.ec;
306 if (!eckey_param2type(&ptype, &pval, ec_key)) {
307 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
311 /* set the private key */
314 * do not include the parameters in the SEC1 private key see PKCS#11
317 old_flags = EC_KEY_get_enc_flags(ec_key);
318 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
319 EC_KEY_set_enc_flags(ec_key, tmp_flags);
320 eplen = i2d_ECPrivateKey(ec_key, NULL);
322 EC_KEY_set_enc_flags(ec_key, old_flags);
323 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
326 ep = (unsigned char *)OPENSSL_malloc(eplen);
328 EC_KEY_set_enc_flags(ec_key, old_flags);
329 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
333 if (!i2d_ECPrivateKey(ec_key, &p)) {
334 EC_KEY_set_enc_flags(ec_key, old_flags);
336 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
339 /* restore old encoding flags */
340 EC_KEY_set_enc_flags(ec_key, old_flags);
342 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
343 ptype, pval, ep, eplen))
349 static int int_ec_size(const EVP_PKEY *pkey)
351 return ECDSA_size(pkey->pkey.ec);
354 static int ec_bits(const EVP_PKEY *pkey)
356 BIGNUM *order = BN_new();
357 const EC_GROUP *group;
364 group = EC_KEY_get0_group(pkey->pkey.ec);
365 if (!EC_GROUP_get_order(group, order, NULL)) {
370 ret = BN_num_bits(order);
375 static int ec_missing_parameters(const EVP_PKEY *pkey)
377 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
382 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
384 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
387 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
389 EC_GROUP_free(group);
393 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
395 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
396 *group_b = EC_KEY_get0_group(b->pkey.ec);
397 if (EC_GROUP_cmp(group_a, group_b, NULL))
403 static void int_ec_free(EVP_PKEY *pkey)
405 EC_KEY_free(pkey->pkey.ec);
408 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
410 unsigned char *buffer = NULL;
412 size_t buf_len = 0, i;
413 int ret = 0, reason = ERR_R_BIO_LIB;
414 BIGNUM *pub_key = NULL, *order = NULL;
416 const EC_GROUP *group;
417 const EC_POINT *public_key;
418 const BIGNUM *priv_key;
420 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
421 reason = ERR_R_PASSED_NULL_PARAMETER;
427 reason = ERR_R_MALLOC_FAILURE;
432 public_key = EC_KEY_get0_public_key(x);
433 if (public_key != NULL) {
434 if ((pub_key = EC_POINT_point2bn(group, public_key,
435 EC_KEY_get_conv_form(x), NULL,
437 reason = ERR_R_EC_LIB;
440 buf_len = (size_t)BN_num_bytes(pub_key);
445 priv_key = EC_KEY_get0_private_key(x);
446 if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len)
453 if ((buffer = OPENSSL_malloc(buf_len)) == NULL) {
454 reason = ERR_R_MALLOC_FAILURE;
459 ecstr = "Private-Key";
461 ecstr = "Public-Key";
463 ecstr = "ECDSA-Parameters";
465 if (!BIO_indent(bp, off, 128))
467 if ((order = BN_new()) == NULL)
469 if (!EC_GROUP_get_order(group, order, NULL))
471 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0)
474 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
477 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
480 if (!ECPKParameters_print(bp, group, off))
485 ECerr(EC_F_DO_EC_KEY_PRINT, reason);
493 OPENSSL_free(buffer);
497 static int eckey_param_decode(EVP_PKEY *pkey,
498 const unsigned char **pder, int derlen)
501 if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) {
502 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
505 EVP_PKEY_assign_EC_KEY(pkey, eckey);
509 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
511 return i2d_ECParameters(pkey->pkey.ec, pder);
514 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
517 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
520 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
523 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
526 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
529 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
532 static int old_ec_priv_decode(EVP_PKEY *pkey,
533 const unsigned char **pder, int derlen)
536 if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
537 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
540 EVP_PKEY_assign_EC_KEY(pkey, ec);
544 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
546 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
549 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
552 case ASN1_PKEY_CTRL_PKCS7_SIGN:
555 X509_ALGOR *alg1, *alg2;
556 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
557 if (alg1 == NULL || alg1->algorithm == NULL)
559 hnid = OBJ_obj2nid(alg1->algorithm);
560 if (hnid == NID_undef)
562 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
564 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
567 #ifndef OPENSSL_NO_CMS
568 case ASN1_PKEY_CTRL_CMS_SIGN:
571 X509_ALGOR *alg1, *alg2;
572 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
573 if (alg1 == NULL || alg1->algorithm == NULL)
575 hnid = OBJ_obj2nid(alg1->algorithm);
576 if (hnid == NID_undef)
578 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
580 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
585 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
586 *(int *)arg2 = NID_sha1;
596 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
601 "OpenSSL EC algorithm",
617 ec_missing_parameters,