1 /* $OpenBSD: p_lib.c,v 1.41 2023/12/25 21:27:03 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #include <openssl/bn.h> 64 #include <openssl/cmac.h> 65 #include <openssl/err.h> 66 #include <openssl/evp.h> 67 #include <openssl/objects.h> 68 #include <openssl/x509.h> 69 70 #ifndef OPENSSL_NO_DH 71 #include <openssl/dh.h> 72 #endif 73 #ifndef OPENSSL_NO_DSA 74 #include <openssl/dsa.h> 75 #endif 76 #ifndef OPENSSL_NO_RSA 77 #include <openssl/rsa.h> 78 #endif 79 80 #include "asn1_local.h" 81 #include "evp_local.h" 82 83 static void EVP_PKEY_free_it(EVP_PKEY *x); 84 85 int 86 EVP_PKEY_bits(const EVP_PKEY *pkey) 87 { 88 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) 89 return pkey->ameth->pkey_bits(pkey); 90 return 0; 91 } 92 93 int 94 EVP_PKEY_security_bits(const EVP_PKEY *pkey) 95 { 96 if (pkey == NULL) 97 return 0; 98 if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL) 99 return -2; 100 101 return pkey->ameth->pkey_security_bits(pkey); 102 } 103 104 int 105 EVP_PKEY_size(const EVP_PKEY *pkey) 106 { 107 if (pkey && pkey->ameth && pkey->ameth->pkey_size) 108 return pkey->ameth->pkey_size(pkey); 109 return 0; 110 } 111 112 int 113 EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) 114 { 115 #ifndef OPENSSL_NO_DSA 116 if (pkey->type == EVP_PKEY_DSA) { 117 int ret = pkey->save_parameters; 118 119 if (mode >= 0) 120 pkey->save_parameters = mode; 121 return (ret); 122 } 123 #endif 124 #ifndef OPENSSL_NO_EC 125 if (pkey->type == EVP_PKEY_EC) { 126 int ret = pkey->save_parameters; 127 128 if (mode >= 0) 129 pkey->save_parameters = mode; 130 return (ret); 131 } 132 #endif 133 return (0); 134 } 135 136 int 137 EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 138 { 139 if (to->type != from->type) { 140 EVPerror(EVP_R_DIFFERENT_KEY_TYPES); 141 goto err; 142 } 143 144 if (EVP_PKEY_missing_parameters(from)) { 145 EVPerror(EVP_R_MISSING_PARAMETERS); 146 goto err; 147 } 148 if (from->ameth && from->ameth->param_copy) 149 return from->ameth->param_copy(to, from); 150 151 err: 152 return 0; 153 } 154 155 int 156 EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) 157 { 158 if (pkey->ameth && pkey->ameth->param_missing) 159 return pkey->ameth->param_missing(pkey); 160 return 0; 161 } 162 163 int 164 EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 165 { 166 if (a->type != b->type) 167 return -1; 168 if (a->ameth && a->ameth->param_cmp) 169 return a->ameth->param_cmp(a, b); 170 return -2; 171 } 172 173 int 174 EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 175 { 176 if (a->type != b->type) 177 return -1; 178 179 if (a->ameth) { 180 int ret; 181 /* Compare parameters if the algorithm has them */ 182 if (a->ameth->param_cmp) { 183 ret = a->ameth->param_cmp(a, b); 184 if (ret <= 0) 185 return ret; 186 } 187 188 if (a->ameth->pub_cmp) 189 return a->ameth->pub_cmp(a, b); 190 } 191 192 return -2; 193 } 194 195 EVP_PKEY * 196 EVP_PKEY_new(void) 197 { 198 EVP_PKEY *ret; 199 200 if ((ret = calloc(1, sizeof(*ret))) == NULL) { 201 EVPerror(ERR_R_MALLOC_FAILURE); 202 return NULL; 203 } 204 205 ret->type = EVP_PKEY_NONE; 206 ret->save_type = EVP_PKEY_NONE; 207 ret->references = 1; 208 ret->save_parameters = 1; 209 210 return ret; 211 } 212 213 int 214 EVP_PKEY_up_ref(EVP_PKEY *pkey) 215 { 216 return CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) > 0; 217 } 218 219 /* Setup a public key ASN1 method from a NID or a string. 220 * If pkey is NULL just return 1 or 0 if the algorithm exists. 221 */ 222 223 static int 224 pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len) 225 { 226 const EVP_PKEY_ASN1_METHOD *ameth; 227 228 if (pkey) { 229 if (pkey->pkey.ptr) 230 EVP_PKEY_free_it(pkey); 231 /* If key type matches and a method exists then this 232 * lookup has succeeded once so just indicate success. 233 */ 234 if ((type == pkey->save_type) && pkey->ameth) 235 return 1; 236 } 237 if (str != NULL) 238 ameth = EVP_PKEY_asn1_find_str(NULL, str, len); 239 else 240 ameth = EVP_PKEY_asn1_find(NULL, type); 241 if (!ameth) { 242 EVPerror(EVP_R_UNSUPPORTED_ALGORITHM); 243 return 0; 244 } 245 if (pkey) { 246 pkey->ameth = ameth; 247 248 pkey->type = pkey->ameth->pkey_id; 249 pkey->save_type = type; 250 } 251 return 1; 252 } 253 254 int 255 EVP_PKEY_set_type(EVP_PKEY *pkey, int type) 256 { 257 return pkey_set_type(pkey, type, NULL, -1); 258 } 259 260 EVP_PKEY * 261 EVP_PKEY_new_raw_private_key(int type, ENGINE *engine, 262 const unsigned char *private_key, size_t len) 263 { 264 EVP_PKEY *ret; 265 266 if ((ret = EVP_PKEY_new()) == NULL) 267 goto err; 268 269 if (!pkey_set_type(ret, type, NULL, -1)) 270 goto err; 271 272 if (ret->ameth->set_priv_key == NULL) { 273 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 274 goto err; 275 } 276 if (!ret->ameth->set_priv_key(ret, private_key, len)) { 277 EVPerror(EVP_R_KEY_SETUP_FAILED); 278 goto err; 279 } 280 281 return ret; 282 283 err: 284 EVP_PKEY_free(ret); 285 286 return NULL; 287 } 288 289 EVP_PKEY * 290 EVP_PKEY_new_raw_public_key(int type, ENGINE *engine, 291 const unsigned char *public_key, size_t len) 292 { 293 EVP_PKEY *ret; 294 295 if ((ret = EVP_PKEY_new()) == NULL) 296 goto err; 297 298 if (!pkey_set_type(ret, type, NULL, -1)) 299 goto err; 300 301 if (ret->ameth->set_pub_key == NULL) { 302 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 303 goto err; 304 } 305 if (!ret->ameth->set_pub_key(ret, public_key, len)) { 306 EVPerror(EVP_R_KEY_SETUP_FAILED); 307 goto err; 308 } 309 310 return ret; 311 312 err: 313 EVP_PKEY_free(ret); 314 315 return NULL; 316 } 317 318 int 319 EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, 320 unsigned char *out_private_key, size_t *out_len) 321 { 322 if (pkey->ameth->get_priv_key == NULL) { 323 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 324 return 0; 325 } 326 if (!pkey->ameth->get_priv_key(pkey, out_private_key, out_len)) { 327 EVPerror(EVP_R_GET_RAW_KEY_FAILED); 328 return 0; 329 } 330 331 return 1; 332 } 333 334 int 335 EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, 336 unsigned char *out_public_key, size_t *out_len) 337 { 338 if (pkey->ameth->get_pub_key == NULL) { 339 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 340 return 0; 341 } 342 if (!pkey->ameth->get_pub_key(pkey, out_public_key, out_len)) { 343 EVPerror(EVP_R_GET_RAW_KEY_FAILED); 344 return 0; 345 } 346 347 return 1; 348 } 349 350 EVP_PKEY * 351 EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len, 352 const EVP_CIPHER *cipher) 353 { 354 EVP_PKEY *ret = NULL; 355 CMAC_CTX *cmctx = NULL; 356 357 if ((ret = EVP_PKEY_new()) == NULL) 358 goto err; 359 if ((cmctx = CMAC_CTX_new()) == NULL) 360 goto err; 361 362 if (!pkey_set_type(ret, EVP_PKEY_CMAC, NULL, -1)) 363 goto err; 364 365 if (!CMAC_Init(cmctx, priv, len, cipher, NULL)) { 366 EVPerror(EVP_R_KEY_SETUP_FAILED); 367 goto err; 368 } 369 370 ret->pkey.ptr = cmctx; 371 372 return ret; 373 374 err: 375 EVP_PKEY_free(ret); 376 CMAC_CTX_free(cmctx); 377 return NULL; 378 } 379 380 int 381 EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) 382 { 383 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len); 384 } 385 386 int 387 EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) 388 { 389 if (!EVP_PKEY_set_type(pkey, type)) 390 return 0; 391 pkey->pkey.ptr = key; 392 return (key != NULL); 393 } 394 395 void * 396 EVP_PKEY_get0(const EVP_PKEY *pkey) 397 { 398 return pkey->pkey.ptr; 399 } 400 401 const unsigned char * 402 EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) 403 { 404 ASN1_OCTET_STRING *os; 405 406 if (pkey->type != EVP_PKEY_HMAC) { 407 EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY); 408 return NULL; 409 } 410 411 os = EVP_PKEY_get0(pkey); 412 *len = os->length; 413 414 return os->data; 415 } 416 417 #ifndef OPENSSL_NO_RSA 418 RSA * 419 EVP_PKEY_get0_RSA(EVP_PKEY *pkey) 420 { 421 if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS) 422 return pkey->pkey.rsa; 423 424 EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); 425 return NULL; 426 } 427 428 RSA * 429 EVP_PKEY_get1_RSA(EVP_PKEY *pkey) 430 { 431 RSA *rsa; 432 433 if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) 434 return NULL; 435 436 RSA_up_ref(rsa); 437 438 return rsa; 439 } 440 441 int 442 EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) 443 { 444 int ret = EVP_PKEY_assign_RSA(pkey, key); 445 if (ret != 0) 446 RSA_up_ref(key); 447 return ret; 448 } 449 #endif 450 451 #ifndef OPENSSL_NO_DSA 452 DSA * 453 EVP_PKEY_get0_DSA(EVP_PKEY *pkey) 454 { 455 if (pkey->type != EVP_PKEY_DSA) { 456 EVPerror(EVP_R_EXPECTING_A_DSA_KEY); 457 return NULL; 458 } 459 return pkey->pkey.dsa; 460 } 461 462 DSA * 463 EVP_PKEY_get1_DSA(EVP_PKEY *pkey) 464 { 465 DSA *dsa; 466 467 if ((dsa = EVP_PKEY_get0_DSA(pkey)) == NULL) 468 return NULL; 469 470 DSA_up_ref(dsa); 471 472 return dsa; 473 } 474 475 int 476 EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) 477 { 478 int ret = EVP_PKEY_assign_DSA(pkey, key); 479 if (ret != 0) 480 DSA_up_ref(key); 481 return ret; 482 } 483 #endif 484 485 #ifndef OPENSSL_NO_EC 486 EC_KEY * 487 EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) 488 { 489 if (pkey->type != EVP_PKEY_EC) { 490 EVPerror(EVP_R_EXPECTING_A_EC_KEY); 491 return NULL; 492 } 493 return pkey->pkey.ec; 494 } 495 496 EC_KEY * 497 EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) 498 { 499 EC_KEY *key; 500 501 if ((key = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) 502 return NULL; 503 504 EC_KEY_up_ref(key); 505 506 return key; 507 } 508 509 int 510 EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) 511 { 512 int ret = EVP_PKEY_assign_EC_KEY(pkey, key); 513 if (ret != 0) 514 EC_KEY_up_ref(key); 515 return ret; 516 } 517 #endif 518 519 520 #ifndef OPENSSL_NO_DH 521 DH * 522 EVP_PKEY_get0_DH(EVP_PKEY *pkey) 523 { 524 if (pkey->type != EVP_PKEY_DH) { 525 EVPerror(EVP_R_EXPECTING_A_DH_KEY); 526 return NULL; 527 } 528 return pkey->pkey.dh; 529 } 530 531 DH * 532 EVP_PKEY_get1_DH(EVP_PKEY *pkey) 533 { 534 DH *dh; 535 536 if ((dh = EVP_PKEY_get0_DH(pkey)) == NULL) 537 return NULL; 538 539 DH_up_ref(dh); 540 541 return dh; 542 } 543 544 int 545 EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) 546 { 547 int ret = EVP_PKEY_assign_DH(pkey, key); 548 if (ret != 0) 549 DH_up_ref(key); 550 return ret; 551 } 552 #endif 553 554 int 555 EVP_PKEY_type(int type) 556 { 557 const EVP_PKEY_ASN1_METHOD *ameth; 558 559 if ((ameth = EVP_PKEY_asn1_find(NULL, type)) != NULL) 560 return ameth->pkey_id; 561 562 return NID_undef; 563 } 564 565 int 566 EVP_PKEY_id(const EVP_PKEY *pkey) 567 { 568 return pkey->type; 569 } 570 571 int 572 EVP_PKEY_base_id(const EVP_PKEY *pkey) 573 { 574 return EVP_PKEY_type(pkey->type); 575 } 576 577 void 578 EVP_PKEY_free(EVP_PKEY *x) 579 { 580 int i; 581 582 if (x == NULL) 583 return; 584 585 i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY); 586 if (i > 0) 587 return; 588 589 EVP_PKEY_free_it(x); 590 if (x->attributes) 591 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); 592 free(x); 593 } 594 595 static void 596 EVP_PKEY_free_it(EVP_PKEY *x) 597 { 598 if (x->ameth && x->ameth->pkey_free) { 599 x->ameth->pkey_free(x); 600 x->pkey.ptr = NULL; 601 } 602 } 603 604 static int 605 unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr) 606 { 607 if (!BIO_indent(out, indent, 128)) 608 return 0; 609 BIO_printf(out, "%s algorithm \"%s\" unsupported\n", 610 kstr, OBJ_nid2ln(pkey->type)); 611 return 1; 612 } 613 614 int 615 EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent, 616 ASN1_PCTX *pctx) 617 { 618 if (pkey->ameth && pkey->ameth->pub_print) 619 return pkey->ameth->pub_print(out, pkey, indent, pctx); 620 621 return unsup_alg(out, pkey, indent, "Public Key"); 622 } 623 624 int 625 EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent, 626 ASN1_PCTX *pctx) 627 { 628 if (pkey->ameth && pkey->ameth->priv_print) 629 return pkey->ameth->priv_print(out, pkey, indent, pctx); 630 631 return unsup_alg(out, pkey, indent, "Private Key"); 632 } 633 634 int 635 EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent, 636 ASN1_PCTX *pctx) 637 { 638 if (pkey->ameth && pkey->ameth->param_print) 639 return pkey->ameth->param_print(out, pkey, indent, pctx); 640 return unsup_alg(out, pkey, indent, "Parameters"); 641 } 642 643 int 644 EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) 645 { 646 if (!pkey->ameth || !pkey->ameth->pkey_ctrl) 647 return -2; 648 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 649 0, pnid); 650 } 651