1 /* $OpenBSD: p_lib.c,v 1.46 2023/12/25 21:37:26 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 int 84 EVP_PKEY_bits(const EVP_PKEY *pkey) 85 { 86 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) 87 return pkey->ameth->pkey_bits(pkey); 88 return 0; 89 } 90 91 int 92 EVP_PKEY_security_bits(const EVP_PKEY *pkey) 93 { 94 if (pkey == NULL) 95 return 0; 96 if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL) 97 return -2; 98 99 return pkey->ameth->pkey_security_bits(pkey); 100 } 101 102 int 103 EVP_PKEY_size(const EVP_PKEY *pkey) 104 { 105 if (pkey && pkey->ameth && pkey->ameth->pkey_size) 106 return pkey->ameth->pkey_size(pkey); 107 return 0; 108 } 109 110 int 111 EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) 112 { 113 #ifndef OPENSSL_NO_DSA 114 if (pkey->type == EVP_PKEY_DSA) { 115 int ret = pkey->save_parameters; 116 117 if (mode >= 0) 118 pkey->save_parameters = mode; 119 return (ret); 120 } 121 #endif 122 #ifndef OPENSSL_NO_EC 123 if (pkey->type == EVP_PKEY_EC) { 124 int ret = pkey->save_parameters; 125 126 if (mode >= 0) 127 pkey->save_parameters = mode; 128 return (ret); 129 } 130 #endif 131 return (0); 132 } 133 134 int 135 EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 136 { 137 if (to->type != from->type) { 138 EVPerror(EVP_R_DIFFERENT_KEY_TYPES); 139 goto err; 140 } 141 142 if (EVP_PKEY_missing_parameters(from)) { 143 EVPerror(EVP_R_MISSING_PARAMETERS); 144 goto err; 145 } 146 if (from->ameth && from->ameth->param_copy) 147 return from->ameth->param_copy(to, from); 148 149 err: 150 return 0; 151 } 152 153 int 154 EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) 155 { 156 if (pkey->ameth && pkey->ameth->param_missing) 157 return pkey->ameth->param_missing(pkey); 158 return 0; 159 } 160 161 int 162 EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 163 { 164 if (a->type != b->type) 165 return -1; 166 if (a->ameth && a->ameth->param_cmp) 167 return a->ameth->param_cmp(a, b); 168 return -2; 169 } 170 171 int 172 EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 173 { 174 if (a->type != b->type) 175 return -1; 176 177 if (a->ameth) { 178 int ret; 179 /* Compare parameters if the algorithm has them */ 180 if (a->ameth->param_cmp) { 181 ret = a->ameth->param_cmp(a, b); 182 if (ret <= 0) 183 return ret; 184 } 185 186 if (a->ameth->pub_cmp) 187 return a->ameth->pub_cmp(a, b); 188 } 189 190 return -2; 191 } 192 193 EVP_PKEY * 194 EVP_PKEY_new(void) 195 { 196 EVP_PKEY *ret; 197 198 if ((ret = calloc(1, sizeof(*ret))) == NULL) { 199 EVPerror(ERR_R_MALLOC_FAILURE); 200 return NULL; 201 } 202 203 ret->type = EVP_PKEY_NONE; 204 ret->save_type = EVP_PKEY_NONE; 205 ret->references = 1; 206 ret->save_parameters = 1; 207 208 return ret; 209 } 210 211 int 212 EVP_PKEY_up_ref(EVP_PKEY *pkey) 213 { 214 return CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) > 1; 215 } 216 217 static void 218 evp_pkey_free_pkey_ptr(EVP_PKEY *pkey) 219 { 220 if (pkey == NULL || pkey->ameth == NULL || pkey->ameth->pkey_free == NULL) 221 return; 222 223 pkey->ameth->pkey_free(pkey); 224 pkey->pkey.ptr = NULL; 225 } 226 227 void 228 EVP_PKEY_free(EVP_PKEY *x) 229 { 230 int i; 231 232 if (x == NULL) 233 return; 234 235 i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY); 236 if (i > 0) 237 return; 238 239 evp_pkey_free_pkey_ptr(x); 240 if (x->attributes) 241 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); 242 free(x); 243 } 244 245 /* Setup a public key ASN1 method from a NID or a string. 246 * If pkey is NULL just return 1 or 0 if the algorithm exists. 247 */ 248 249 static int 250 pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len) 251 { 252 const EVP_PKEY_ASN1_METHOD *ameth; 253 254 if (pkey) { 255 if (pkey->pkey.ptr) 256 evp_pkey_free_pkey_ptr(pkey); 257 /* If key type matches and a method exists then this 258 * lookup has succeeded once so just indicate success. 259 */ 260 if ((type == pkey->save_type) && pkey->ameth) 261 return 1; 262 } 263 if (str != NULL) 264 ameth = EVP_PKEY_asn1_find_str(NULL, str, len); 265 else 266 ameth = EVP_PKEY_asn1_find(NULL, type); 267 if (!ameth) { 268 EVPerror(EVP_R_UNSUPPORTED_ALGORITHM); 269 return 0; 270 } 271 if (pkey) { 272 pkey->ameth = ameth; 273 274 pkey->type = pkey->ameth->pkey_id; 275 pkey->save_type = type; 276 } 277 return 1; 278 } 279 280 int 281 EVP_PKEY_set_type(EVP_PKEY *pkey, int type) 282 { 283 return pkey_set_type(pkey, type, NULL, -1); 284 } 285 286 EVP_PKEY * 287 EVP_PKEY_new_raw_private_key(int type, ENGINE *engine, 288 const unsigned char *private_key, size_t len) 289 { 290 EVP_PKEY *ret; 291 292 if ((ret = EVP_PKEY_new()) == NULL) 293 goto err; 294 295 if (!pkey_set_type(ret, type, NULL, -1)) 296 goto err; 297 298 if (ret->ameth->set_priv_key == NULL) { 299 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 300 goto err; 301 } 302 if (!ret->ameth->set_priv_key(ret, private_key, len)) { 303 EVPerror(EVP_R_KEY_SETUP_FAILED); 304 goto err; 305 } 306 307 return ret; 308 309 err: 310 EVP_PKEY_free(ret); 311 312 return NULL; 313 } 314 315 EVP_PKEY * 316 EVP_PKEY_new_raw_public_key(int type, ENGINE *engine, 317 const unsigned char *public_key, size_t len) 318 { 319 EVP_PKEY *ret; 320 321 if ((ret = EVP_PKEY_new()) == NULL) 322 goto err; 323 324 if (!pkey_set_type(ret, type, NULL, -1)) 325 goto err; 326 327 if (ret->ameth->set_pub_key == NULL) { 328 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 329 goto err; 330 } 331 if (!ret->ameth->set_pub_key(ret, public_key, len)) { 332 EVPerror(EVP_R_KEY_SETUP_FAILED); 333 goto err; 334 } 335 336 return ret; 337 338 err: 339 EVP_PKEY_free(ret); 340 341 return NULL; 342 } 343 344 int 345 EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, 346 unsigned char *out_private_key, size_t *out_len) 347 { 348 if (pkey->ameth->get_priv_key == NULL) { 349 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 350 return 0; 351 } 352 if (!pkey->ameth->get_priv_key(pkey, out_private_key, out_len)) { 353 EVPerror(EVP_R_GET_RAW_KEY_FAILED); 354 return 0; 355 } 356 357 return 1; 358 } 359 360 int 361 EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, 362 unsigned char *out_public_key, size_t *out_len) 363 { 364 if (pkey->ameth->get_pub_key == NULL) { 365 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 366 return 0; 367 } 368 if (!pkey->ameth->get_pub_key(pkey, out_public_key, out_len)) { 369 EVPerror(EVP_R_GET_RAW_KEY_FAILED); 370 return 0; 371 } 372 373 return 1; 374 } 375 376 EVP_PKEY * 377 EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len, 378 const EVP_CIPHER *cipher) 379 { 380 EVP_PKEY *ret = NULL; 381 CMAC_CTX *cmctx = NULL; 382 383 if ((ret = EVP_PKEY_new()) == NULL) 384 goto err; 385 if ((cmctx = CMAC_CTX_new()) == NULL) 386 goto err; 387 388 if (!pkey_set_type(ret, EVP_PKEY_CMAC, NULL, -1)) 389 goto err; 390 391 if (!CMAC_Init(cmctx, priv, len, cipher, NULL)) { 392 EVPerror(EVP_R_KEY_SETUP_FAILED); 393 goto err; 394 } 395 396 ret->pkey.ptr = cmctx; 397 398 return ret; 399 400 err: 401 EVP_PKEY_free(ret); 402 CMAC_CTX_free(cmctx); 403 return NULL; 404 } 405 406 int 407 EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) 408 { 409 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len); 410 } 411 412 int 413 EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) 414 { 415 if (!EVP_PKEY_set_type(pkey, type)) 416 return 0; 417 pkey->pkey.ptr = key; 418 return (key != NULL); 419 } 420 421 void * 422 EVP_PKEY_get0(const EVP_PKEY *pkey) 423 { 424 return pkey->pkey.ptr; 425 } 426 427 const unsigned char * 428 EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) 429 { 430 ASN1_OCTET_STRING *os; 431 432 if (pkey->type != EVP_PKEY_HMAC) { 433 EVPerror(EVP_R_EXPECTING_AN_HMAC_KEY); 434 return NULL; 435 } 436 437 os = EVP_PKEY_get0(pkey); 438 *len = os->length; 439 440 return os->data; 441 } 442 443 #ifndef OPENSSL_NO_RSA 444 RSA * 445 EVP_PKEY_get0_RSA(EVP_PKEY *pkey) 446 { 447 if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA_PSS) 448 return pkey->pkey.rsa; 449 450 EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); 451 return NULL; 452 } 453 454 RSA * 455 EVP_PKEY_get1_RSA(EVP_PKEY *pkey) 456 { 457 RSA *rsa; 458 459 if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) 460 return NULL; 461 462 RSA_up_ref(rsa); 463 464 return rsa; 465 } 466 467 int 468 EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) 469 { 470 int ret = EVP_PKEY_assign_RSA(pkey, key); 471 if (ret != 0) 472 RSA_up_ref(key); 473 return ret; 474 } 475 #endif 476 477 #ifndef OPENSSL_NO_DSA 478 DSA * 479 EVP_PKEY_get0_DSA(EVP_PKEY *pkey) 480 { 481 if (pkey->type != EVP_PKEY_DSA) { 482 EVPerror(EVP_R_EXPECTING_A_DSA_KEY); 483 return NULL; 484 } 485 return pkey->pkey.dsa; 486 } 487 488 DSA * 489 EVP_PKEY_get1_DSA(EVP_PKEY *pkey) 490 { 491 DSA *dsa; 492 493 if ((dsa = EVP_PKEY_get0_DSA(pkey)) == NULL) 494 return NULL; 495 496 DSA_up_ref(dsa); 497 498 return dsa; 499 } 500 501 int 502 EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) 503 { 504 int ret = EVP_PKEY_assign_DSA(pkey, key); 505 if (ret != 0) 506 DSA_up_ref(key); 507 return ret; 508 } 509 #endif 510 511 #ifndef OPENSSL_NO_EC 512 EC_KEY * 513 EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) 514 { 515 if (pkey->type != EVP_PKEY_EC) { 516 EVPerror(EVP_R_EXPECTING_A_EC_KEY); 517 return NULL; 518 } 519 return pkey->pkey.ec; 520 } 521 522 EC_KEY * 523 EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) 524 { 525 EC_KEY *key; 526 527 if ((key = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) 528 return NULL; 529 530 EC_KEY_up_ref(key); 531 532 return key; 533 } 534 535 int 536 EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) 537 { 538 int ret = EVP_PKEY_assign_EC_KEY(pkey, key); 539 if (ret != 0) 540 EC_KEY_up_ref(key); 541 return ret; 542 } 543 #endif 544 545 546 #ifndef OPENSSL_NO_DH 547 DH * 548 EVP_PKEY_get0_DH(EVP_PKEY *pkey) 549 { 550 if (pkey->type != EVP_PKEY_DH) { 551 EVPerror(EVP_R_EXPECTING_A_DH_KEY); 552 return NULL; 553 } 554 return pkey->pkey.dh; 555 } 556 557 DH * 558 EVP_PKEY_get1_DH(EVP_PKEY *pkey) 559 { 560 DH *dh; 561 562 if ((dh = EVP_PKEY_get0_DH(pkey)) == NULL) 563 return NULL; 564 565 DH_up_ref(dh); 566 567 return dh; 568 } 569 570 int 571 EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) 572 { 573 int ret = EVP_PKEY_assign_DH(pkey, key); 574 if (ret != 0) 575 DH_up_ref(key); 576 return ret; 577 } 578 #endif 579 580 int 581 EVP_PKEY_type(int type) 582 { 583 const EVP_PKEY_ASN1_METHOD *ameth; 584 585 if ((ameth = EVP_PKEY_asn1_find(NULL, type)) != NULL) 586 return ameth->pkey_id; 587 588 return NID_undef; 589 } 590 591 int 592 EVP_PKEY_id(const EVP_PKEY *pkey) 593 { 594 return pkey->type; 595 } 596 597 int 598 EVP_PKEY_base_id(const EVP_PKEY *pkey) 599 { 600 return EVP_PKEY_type(pkey->type); 601 } 602 603 static int 604 unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr) 605 { 606 if (!BIO_indent(out, indent, 128)) 607 return 0; 608 BIO_printf(out, "%s algorithm \"%s\" unsupported\n", 609 kstr, OBJ_nid2ln(pkey->type)); 610 return 1; 611 } 612 613 int 614 EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent, 615 ASN1_PCTX *pctx) 616 { 617 if (pkey->ameth && pkey->ameth->pub_print) 618 return pkey->ameth->pub_print(out, pkey, indent, pctx); 619 620 return unsup_alg(out, pkey, indent, "Public Key"); 621 } 622 623 int 624 EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent, 625 ASN1_PCTX *pctx) 626 { 627 if (pkey->ameth && pkey->ameth->priv_print) 628 return pkey->ameth->priv_print(out, pkey, indent, pctx); 629 630 return unsup_alg(out, pkey, indent, "Private Key"); 631 } 632 633 int 634 EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent, 635 ASN1_PCTX *pctx) 636 { 637 if (pkey->ameth && pkey->ameth->param_print) 638 return pkey->ameth->param_print(out, pkey, indent, pctx); 639 return unsup_alg(out, pkey, indent, "Parameters"); 640 } 641 642 int 643 EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) 644 { 645 if (!pkey->ameth || !pkey->ameth->pkey_ctrl) 646 return -2; 647 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 648 0, pnid); 649 } 650