1 /* $OpenBSD: ec_ameth.c,v 1.16 2015/02/11 04:05:14 beck Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 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 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #include <openssl/bn.h> 64 #include <openssl/ec.h> 65 #include <openssl/err.h> 66 #include <openssl/x509.h> 67 68 #ifndef OPENSSL_NO_CMS 69 #include <openssl/cms.h> 70 #endif 71 72 #include "asn1_locl.h" 73 74 static int 75 eckey_param2type(int *pptype, void **ppval, EC_KEY * ec_key) 76 { 77 const EC_GROUP *group; 78 int nid; 79 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) { 80 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS); 81 return 0; 82 } 83 if (EC_GROUP_get_asn1_flag(group) && 84 (nid = EC_GROUP_get_curve_name(group))) { 85 /* we have a 'named curve' => just set the OID */ 86 *ppval = OBJ_nid2obj(nid); 87 *pptype = V_ASN1_OBJECT; 88 } else { 89 /* explicit parameters */ 90 ASN1_STRING *pstr = NULL; 91 pstr = ASN1_STRING_new(); 92 if (!pstr) 93 return 0; 94 pstr->length = i2d_ECParameters(ec_key, &pstr->data); 95 if (pstr->length <= 0) { 96 ASN1_STRING_free(pstr); 97 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB); 98 return 0; 99 } 100 *ppval = pstr; 101 *pptype = V_ASN1_SEQUENCE; 102 } 103 return 1; 104 } 105 106 static int 107 eckey_pub_encode(X509_PUBKEY * pk, const EVP_PKEY * pkey) 108 { 109 EC_KEY *ec_key = pkey->pkey.ec; 110 void *pval = NULL; 111 int ptype; 112 unsigned char *penc = NULL, *p; 113 int penclen; 114 115 if (!eckey_param2type(&ptype, &pval, ec_key)) { 116 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB); 117 return 0; 118 } 119 penclen = i2o_ECPublicKey(ec_key, NULL); 120 if (penclen <= 0) 121 goto err; 122 penc = malloc(penclen); 123 if (!penc) 124 goto err; 125 p = penc; 126 penclen = i2o_ECPublicKey(ec_key, &p); 127 if (penclen <= 0) 128 goto err; 129 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), 130 ptype, pval, penc, penclen)) 131 return 1; 132 err: 133 if (ptype == V_ASN1_OBJECT) 134 ASN1_OBJECT_free(pval); 135 else 136 ASN1_STRING_free(pval); 137 free(penc); 138 return 0; 139 } 140 141 static EC_KEY * 142 eckey_type2param(int ptype, void *pval) 143 { 144 EC_KEY *eckey = NULL; 145 146 if (ptype == V_ASN1_SEQUENCE) { 147 ASN1_STRING *pstr = pval; 148 const unsigned char *pm = NULL; 149 int pmlen; 150 151 pm = pstr->data; 152 pmlen = pstr->length; 153 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) { 154 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); 155 goto ecerr; 156 } 157 } else if (ptype == V_ASN1_OBJECT) { 158 ASN1_OBJECT *poid = pval; 159 EC_GROUP *group; 160 161 /* 162 * type == V_ASN1_OBJECT => the parameters are given by an 163 * asn1 OID 164 */ 165 if ((eckey = EC_KEY_new()) == NULL) { 166 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE); 167 goto ecerr; 168 } 169 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid)); 170 if (group == NULL) 171 goto ecerr; 172 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); 173 if (EC_KEY_set_group(eckey, group) == 0) 174 goto ecerr; 175 EC_GROUP_free(group); 176 } else { 177 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); 178 goto ecerr; 179 } 180 181 return eckey; 182 183 ecerr: 184 if (eckey) 185 EC_KEY_free(eckey); 186 return NULL; 187 } 188 189 static int 190 eckey_pub_decode(EVP_PKEY * pkey, X509_PUBKEY * pubkey) 191 { 192 const unsigned char *p = NULL; 193 void *pval; 194 int ptype, pklen; 195 EC_KEY *eckey = NULL; 196 X509_ALGOR *palg; 197 198 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) 199 return 0; 200 X509_ALGOR_get0(NULL, &ptype, &pval, palg); 201 202 eckey = eckey_type2param(ptype, pval); 203 204 if (!eckey) { 205 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB); 206 return 0; 207 } 208 /* We have parameters now set public key */ 209 if (!o2i_ECPublicKey(&eckey, &p, pklen)) { 210 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR); 211 goto ecerr; 212 } 213 EVP_PKEY_assign_EC_KEY(pkey, eckey); 214 return 1; 215 216 ecerr: 217 if (eckey) 218 EC_KEY_free(eckey); 219 return 0; 220 } 221 222 static int 223 eckey_pub_cmp(const EVP_PKEY * a, const EVP_PKEY * b) 224 { 225 int r; 226 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); 227 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), *pb = EC_KEY_get0_public_key(b->pkey.ec); 228 229 r = EC_POINT_cmp(group, pa, pb, NULL); 230 if (r == 0) 231 return 1; 232 if (r == 1) 233 return 0; 234 return -2; 235 } 236 237 static int 238 eckey_priv_decode(EVP_PKEY * pkey, PKCS8_PRIV_KEY_INFO * p8) 239 { 240 const unsigned char *p = NULL; 241 void *pval; 242 int ptype, pklen; 243 EC_KEY *eckey = NULL; 244 X509_ALGOR *palg; 245 246 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) 247 return 0; 248 X509_ALGOR_get0(NULL, &ptype, &pval, palg); 249 250 eckey = eckey_type2param(ptype, pval); 251 252 if (!eckey) 253 goto ecliberr; 254 255 /* We have parameters now set private key */ 256 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) { 257 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR); 258 goto ecerr; 259 } 260 /* calculate public key (if necessary) */ 261 if (EC_KEY_get0_public_key(eckey) == NULL) { 262 const BIGNUM *priv_key; 263 const EC_GROUP *group; 264 EC_POINT *pub_key; 265 /* 266 * the public key was not included in the SEC1 private key => 267 * calculate the public key 268 */ 269 group = EC_KEY_get0_group(eckey); 270 pub_key = EC_POINT_new(group); 271 if (pub_key == NULL) { 272 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 273 goto ecliberr; 274 } 275 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); 278 goto ecliberr; 279 } 280 priv_key = EC_KEY_get0_private_key(eckey); 281 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) { 282 EC_POINT_free(pub_key); 283 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 284 goto ecliberr; 285 } 286 if (EC_KEY_set_public_key(eckey, pub_key) == 0) { 287 EC_POINT_free(pub_key); 288 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 289 goto ecliberr; 290 } 291 EC_POINT_free(pub_key); 292 } 293 EVP_PKEY_assign_EC_KEY(pkey, eckey); 294 return 1; 295 296 ecliberr: 297 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 298 ecerr: 299 if (eckey) 300 EC_KEY_free(eckey); 301 return 0; 302 } 303 304 static int 305 eckey_priv_encode(PKCS8_PRIV_KEY_INFO * p8, const EVP_PKEY * pkey) 306 { 307 EC_KEY *ec_key; 308 unsigned char *ep, *p; 309 int eplen, ptype; 310 void *pval; 311 unsigned int tmp_flags, old_flags; 312 313 ec_key = pkey->pkey.ec; 314 315 if (!eckey_param2type(&ptype, &pval, ec_key)) { 316 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR); 317 return 0; 318 } 319 /* set the private key */ 320 321 /* 322 * do not include the parameters in the SEC1 private key see PKCS#11 323 * 12.11 324 */ 325 old_flags = EC_KEY_get_enc_flags(ec_key); 326 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS; 327 EC_KEY_set_enc_flags(ec_key, tmp_flags); 328 eplen = i2d_ECPrivateKey(ec_key, NULL); 329 if (!eplen) { 330 EC_KEY_set_enc_flags(ec_key, old_flags); 331 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); 332 return 0; 333 } 334 ep = malloc(eplen); 335 if (!ep) { 336 EC_KEY_set_enc_flags(ec_key, old_flags); 337 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); 338 return 0; 339 } 340 p = ep; 341 if (!i2d_ECPrivateKey(ec_key, &p)) { 342 EC_KEY_set_enc_flags(ec_key, old_flags); 343 free(ep); 344 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); 345 return 0; 346 } 347 /* restore old encoding flags */ 348 EC_KEY_set_enc_flags(ec_key, old_flags); 349 350 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0, 351 ptype, pval, ep, eplen)) 352 return 0; 353 354 return 1; 355 } 356 357 static int 358 int_ec_size(const EVP_PKEY * pkey) 359 { 360 return ECDSA_size(pkey->pkey.ec); 361 } 362 363 static int 364 ec_bits(const EVP_PKEY * pkey) 365 { 366 BIGNUM *order = BN_new(); 367 const EC_GROUP *group; 368 int ret; 369 370 if (!order) { 371 ERR_clear_error(); 372 return 0; 373 } 374 group = EC_KEY_get0_group(pkey->pkey.ec); 375 if (!EC_GROUP_get_order(group, order, NULL)) { 376 BN_free(order); 377 ERR_clear_error(); 378 return 0; 379 } 380 ret = BN_num_bits(order); 381 BN_free(order); 382 return ret; 383 } 384 385 static int 386 ec_missing_parameters(const EVP_PKEY * pkey) 387 { 388 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL) 389 return 1; 390 return 0; 391 } 392 393 static int 394 ec_copy_parameters(EVP_PKEY * to, const EVP_PKEY * from) 395 { 396 return EC_KEY_set_group(to->pkey.ec, EC_KEY_get0_group(from->pkey.ec)); 397 } 398 399 static int 400 ec_cmp_parameters(const EVP_PKEY * a, const EVP_PKEY * b) 401 { 402 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), *group_b = EC_KEY_get0_group(b->pkey.ec); 403 if (EC_GROUP_cmp(group_a, group_b, NULL)) 404 return 0; 405 else 406 return 1; 407 } 408 409 static void 410 int_ec_free(EVP_PKEY * pkey) 411 { 412 EC_KEY_free(pkey->pkey.ec); 413 } 414 415 static int 416 do_EC_KEY_print(BIO * bp, const EC_KEY * x, int off, int ktype) 417 { 418 unsigned char *buffer = NULL; 419 const char *ecstr; 420 size_t buf_len = 0, i; 421 int ret = 0, reason = ERR_R_BIO_LIB; 422 BIGNUM *pub_key = NULL, *order = NULL; 423 BN_CTX *ctx = NULL; 424 const EC_GROUP *group; 425 const EC_POINT *public_key; 426 const BIGNUM *priv_key; 427 428 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) { 429 reason = ERR_R_PASSED_NULL_PARAMETER; 430 goto err; 431 } 432 ctx = BN_CTX_new(); 433 if (ctx == NULL) { 434 reason = ERR_R_MALLOC_FAILURE; 435 goto err; 436 } 437 if (ktype > 0) { 438 public_key = EC_KEY_get0_public_key(x); 439 if ((pub_key = EC_POINT_point2bn(group, public_key, 440 EC_KEY_get_conv_form(x), NULL, ctx)) == NULL) { 441 reason = ERR_R_EC_LIB; 442 goto err; 443 } 444 if (pub_key) 445 buf_len = (size_t) BN_num_bytes(pub_key); 446 } 447 if (ktype == 2) { 448 priv_key = EC_KEY_get0_private_key(x); 449 if (priv_key && (i = (size_t) BN_num_bytes(priv_key)) > buf_len) 450 buf_len = i; 451 } else 452 priv_key = NULL; 453 454 if (ktype > 0) { 455 buf_len += 10; 456 if ((buffer = malloc(buf_len)) == NULL) { 457 reason = ERR_R_MALLOC_FAILURE; 458 goto err; 459 } 460 } 461 if (ktype == 2) 462 ecstr = "Private-Key"; 463 else if (ktype == 1) 464 ecstr = "Public-Key"; 465 else 466 ecstr = "ECDSA-Parameters"; 467 468 if (!BIO_indent(bp, off, 128)) 469 goto err; 470 if ((order = BN_new()) == NULL) 471 goto err; 472 if (!EC_GROUP_get_order(group, order, NULL)) 473 goto err; 474 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, 475 BN_num_bits(order)) <= 0) 476 goto err; 477 478 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key, 479 buffer, off)) 480 goto err; 481 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key, 482 buffer, off)) 483 goto err; 484 if (!ECPKParameters_print(bp, group, off)) 485 goto err; 486 ret = 1; 487 err: 488 if (!ret) 489 ECerr(EC_F_DO_EC_KEY_PRINT, reason); 490 BN_free(pub_key); 491 BN_free(order); 492 BN_CTX_free(ctx); 493 free(buffer); 494 return (ret); 495 } 496 497 static int 498 eckey_param_decode(EVP_PKEY * pkey, 499 const unsigned char **pder, int derlen) 500 { 501 EC_KEY *eckey; 502 if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) { 503 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB); 504 return 0; 505 } 506 EVP_PKEY_assign_EC_KEY(pkey, eckey); 507 return 1; 508 } 509 510 static int 511 eckey_param_encode(const EVP_PKEY * pkey, unsigned char **pder) 512 { 513 return i2d_ECParameters(pkey->pkey.ec, pder); 514 } 515 516 static int 517 eckey_param_print(BIO * bp, const EVP_PKEY * pkey, int indent, 518 ASN1_PCTX * ctx) 519 { 520 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0); 521 } 522 523 static int 524 eckey_pub_print(BIO * bp, const EVP_PKEY * pkey, int indent, 525 ASN1_PCTX * ctx) 526 { 527 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1); 528 } 529 530 531 static int 532 eckey_priv_print(BIO * bp, const EVP_PKEY * pkey, int indent, 533 ASN1_PCTX * ctx) 534 { 535 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2); 536 } 537 538 static int 539 old_ec_priv_decode(EVP_PKEY * pkey, 540 const unsigned char **pder, int derlen) 541 { 542 EC_KEY *ec; 543 if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) { 544 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR); 545 return 0; 546 } 547 EVP_PKEY_assign_EC_KEY(pkey, ec); 548 return 1; 549 } 550 551 static int 552 old_ec_priv_encode(const EVP_PKEY * pkey, unsigned char **pder) 553 { 554 return i2d_ECPrivateKey(pkey->pkey.ec, pder); 555 } 556 557 static int 558 ec_pkey_ctrl(EVP_PKEY * pkey, int op, long arg1, void *arg2) 559 { 560 switch (op) { 561 case ASN1_PKEY_CTRL_PKCS7_SIGN: 562 if (arg1 == 0) { 563 int snid, hnid; 564 X509_ALGOR *alg1, *alg2; 565 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); 566 if (alg1 == NULL || alg1->algorithm == NULL) 567 return -1; 568 hnid = OBJ_obj2nid(alg1->algorithm); 569 if (hnid == NID_undef) 570 return -1; 571 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) 572 return -1; 573 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); 574 } 575 return 1; 576 #ifndef OPENSSL_NO_CMS 577 case ASN1_PKEY_CTRL_CMS_SIGN: 578 if (arg1 == 0) { 579 int snid, hnid; 580 X509_ALGOR *alg1, *alg2; 581 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, 582 &alg1, &alg2); 583 if (alg1 == NULL || alg1->algorithm == NULL) 584 return -1; 585 hnid = OBJ_obj2nid(alg1->algorithm); 586 if (hnid == NID_undef) 587 return -1; 588 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) 589 return -1; 590 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); 591 } 592 return 1; 593 #endif 594 595 case ASN1_PKEY_CTRL_DEFAULT_MD_NID: 596 *(int *) arg2 = NID_sha1; 597 return 2; 598 599 default: 600 return -2; 601 602 } 603 604 } 605 606 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = { 607 .pkey_id = EVP_PKEY_EC, 608 .pkey_base_id = EVP_PKEY_EC, 609 610 .pem_str = "EC", 611 .info = "OpenSSL EC algorithm", 612 613 .pub_decode = eckey_pub_decode, 614 .pub_encode = eckey_pub_encode, 615 .pub_cmp = eckey_pub_cmp, 616 .pub_print = eckey_pub_print, 617 618 .priv_decode = eckey_priv_decode, 619 .priv_encode = eckey_priv_encode, 620 .priv_print = eckey_priv_print, 621 622 .pkey_size = int_ec_size, 623 .pkey_bits = ec_bits, 624 625 .param_decode = eckey_param_decode, 626 .param_encode = eckey_param_encode, 627 .param_missing = ec_missing_parameters, 628 .param_copy = ec_copy_parameters, 629 .param_cmp = ec_cmp_parameters, 630 .param_print = eckey_param_print, 631 632 .pkey_free = int_ec_free, 633 .pkey_ctrl = ec_pkey_ctrl, 634 .old_priv_decode = old_ec_priv_decode, 635 .old_priv_encode = old_ec_priv_encode 636 }; 637