1 /* $OpenBSD: pmeth_lib.c,v 1.10 2014/11/09 19:17:13 miod 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 #include <stdlib.h> 61 #include <string.h> 62 63 #include <openssl/opensslconf.h> 64 65 #include <openssl/evp.h> 66 #include <openssl/objects.h> 67 68 #ifndef OPENSSL_NO_ENGINE 69 #include <openssl/engine.h> 70 #endif 71 72 #include "asn1_locl.h" 73 #include "evp_locl.h" 74 75 typedef int sk_cmp_fn_type(const char * const *a, const char * const *b); 76 77 DECLARE_STACK_OF(EVP_PKEY_METHOD) 78 STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; 79 80 extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth; 81 extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth; 82 extern const EVP_PKEY_METHOD gostimit_pkey_meth, gostr01_pkey_meth; 83 84 static const EVP_PKEY_METHOD *standard_methods[] = { 85 #ifndef OPENSSL_NO_RSA 86 &rsa_pkey_meth, 87 #endif 88 #ifndef OPENSSL_NO_DH 89 &dh_pkey_meth, 90 #endif 91 #ifndef OPENSSL_NO_DSA 92 &dsa_pkey_meth, 93 #endif 94 #ifndef OPENSSL_NO_EC 95 &ec_pkey_meth, 96 #endif 97 #ifndef OPENSSL_NO_GOST 98 &gostr01_pkey_meth, 99 &gostimit_pkey_meth, 100 #endif 101 &hmac_pkey_meth, 102 &cmac_pkey_meth, 103 }; 104 105 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, 106 pmeth); 107 108 static int 109 pmeth_cmp(const EVP_PKEY_METHOD * const *a, const EVP_PKEY_METHOD * const *b) 110 { 111 return ((*a)->pkey_id - (*b)->pkey_id); 112 } 113 114 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, 115 pmeth); 116 117 const EVP_PKEY_METHOD * 118 EVP_PKEY_meth_find(int type) 119 { 120 EVP_PKEY_METHOD tmp; 121 const EVP_PKEY_METHOD *t = &tmp, **ret; 122 123 tmp.pkey_id = type; 124 if (app_pkey_methods) { 125 int idx; 126 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); 127 if (idx >= 0) 128 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); 129 } 130 ret = OBJ_bsearch_pmeth(&t, standard_methods, 131 sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *)); 132 if (!ret || !*ret) 133 return NULL; 134 return *ret; 135 } 136 137 static EVP_PKEY_CTX * 138 int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) 139 { 140 EVP_PKEY_CTX *ret; 141 const EVP_PKEY_METHOD *pmeth; 142 143 if (id == -1) { 144 if (!pkey || !pkey->ameth) 145 return NULL; 146 id = pkey->ameth->pkey_id; 147 } 148 #ifndef OPENSSL_NO_ENGINE 149 if (pkey && pkey->engine) 150 e = pkey->engine; 151 /* Try to find an ENGINE which implements this method */ 152 if (e) { 153 if (!ENGINE_init(e)) { 154 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); 155 return NULL; 156 } 157 } else 158 e = ENGINE_get_pkey_meth_engine(id); 159 160 /* If an ENGINE handled this method look it up. Othewise 161 * use internal tables. 162 */ 163 164 if (e) 165 pmeth = ENGINE_get_pkey_meth(e, id); 166 else 167 #endif 168 pmeth = EVP_PKEY_meth_find(id); 169 170 if (pmeth == NULL) { 171 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); 172 return NULL; 173 } 174 175 ret = malloc(sizeof(EVP_PKEY_CTX)); 176 if (!ret) { 177 #ifndef OPENSSL_NO_ENGINE 178 if (e) 179 ENGINE_finish(e); 180 #endif 181 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); 182 return NULL; 183 } 184 ret->engine = e; 185 ret->pmeth = pmeth; 186 ret->operation = EVP_PKEY_OP_UNDEFINED; 187 ret->pkey = pkey; 188 ret->peerkey = NULL; 189 ret->pkey_gencb = 0; 190 if (pkey) 191 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 192 ret->data = NULL; 193 194 if (pmeth->init) { 195 if (pmeth->init(ret) <= 0) { 196 EVP_PKEY_CTX_free(ret); 197 return NULL; 198 } 199 } 200 201 return ret; 202 } 203 204 EVP_PKEY_METHOD* 205 EVP_PKEY_meth_new(int id, int flags) 206 { 207 EVP_PKEY_METHOD *pmeth; 208 209 pmeth = calloc(1, sizeof(EVP_PKEY_METHOD)); 210 if (!pmeth) 211 return NULL; 212 213 pmeth->pkey_id = id; 214 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; 215 216 pmeth->init = 0; 217 pmeth->copy = 0; 218 pmeth->cleanup = 0; 219 pmeth->paramgen_init = 0; 220 pmeth->paramgen = 0; 221 pmeth->keygen_init = 0; 222 pmeth->keygen = 0; 223 pmeth->sign_init = 0; 224 pmeth->sign = 0; 225 pmeth->verify_init = 0; 226 pmeth->verify = 0; 227 pmeth->verify_recover_init = 0; 228 pmeth->verify_recover = 0; 229 pmeth->signctx_init = 0; 230 pmeth->signctx = 0; 231 pmeth->verifyctx_init = 0; 232 pmeth->verifyctx = 0; 233 pmeth->encrypt_init = 0; 234 pmeth->encrypt = 0; 235 pmeth->decrypt_init = 0; 236 pmeth->decrypt = 0; 237 pmeth->derive_init = 0; 238 pmeth->derive = 0; 239 pmeth->ctrl = 0; 240 pmeth->ctrl_str = 0; 241 242 return pmeth; 243 } 244 245 void 246 EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, const EVP_PKEY_METHOD *meth) 247 { 248 if (ppkey_id) 249 *ppkey_id = meth->pkey_id; 250 if (pflags) 251 *pflags = meth->flags; 252 } 253 254 void 255 EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) 256 { 257 dst->init = src->init; 258 dst->copy = src->copy; 259 dst->cleanup = src->cleanup; 260 261 dst->paramgen_init = src->paramgen_init; 262 dst->paramgen = src->paramgen; 263 264 dst->keygen_init = src->keygen_init; 265 dst->keygen = src->keygen; 266 267 dst->sign_init = src->sign_init; 268 dst->sign = src->sign; 269 270 dst->verify_init = src->verify_init; 271 dst->verify = src->verify; 272 273 dst->verify_recover_init = src->verify_recover_init; 274 dst->verify_recover = src->verify_recover; 275 276 dst->signctx_init = src->signctx_init; 277 dst->signctx = src->signctx; 278 279 dst->verifyctx_init = src->verifyctx_init; 280 dst->verifyctx = src->verifyctx; 281 282 dst->encrypt_init = src->encrypt_init; 283 dst->encrypt = src->encrypt; 284 285 dst->decrypt_init = src->decrypt_init; 286 dst->decrypt = src->decrypt; 287 288 dst->derive_init = src->derive_init; 289 dst->derive = src->derive; 290 291 dst->ctrl = src->ctrl; 292 dst->ctrl_str = src->ctrl_str; 293 } 294 295 void 296 EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) 297 { 298 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) 299 free(pmeth); 300 } 301 302 EVP_PKEY_CTX * 303 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) 304 { 305 return int_ctx_new(pkey, e, -1); 306 } 307 308 EVP_PKEY_CTX * 309 EVP_PKEY_CTX_new_id(int id, ENGINE *e) 310 { 311 return int_ctx_new(NULL, e, id); 312 } 313 314 EVP_PKEY_CTX * 315 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) 316 { 317 EVP_PKEY_CTX *rctx; 318 319 if (!pctx->pmeth || !pctx->pmeth->copy) 320 return NULL; 321 #ifndef OPENSSL_NO_ENGINE 322 /* Make sure it's safe to copy a pkey context using an ENGINE */ 323 if (pctx->engine && !ENGINE_init(pctx->engine)) { 324 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); 325 return 0; 326 } 327 #endif 328 rctx = malloc(sizeof(EVP_PKEY_CTX)); 329 if (!rctx) 330 return NULL; 331 332 rctx->pmeth = pctx->pmeth; 333 #ifndef OPENSSL_NO_ENGINE 334 rctx->engine = pctx->engine; 335 #endif 336 337 if (pctx->pkey) 338 CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 339 340 rctx->pkey = pctx->pkey; 341 342 if (pctx->peerkey) 343 CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 344 345 rctx->peerkey = pctx->peerkey; 346 347 rctx->data = NULL; 348 rctx->app_data = NULL; 349 rctx->operation = pctx->operation; 350 351 if (pctx->pmeth->copy(rctx, pctx) > 0) 352 return rctx; 353 354 EVP_PKEY_CTX_free(rctx); 355 return NULL; 356 } 357 358 int 359 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) 360 { 361 if (app_pkey_methods == NULL) { 362 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); 363 if (!app_pkey_methods) 364 return 0; 365 } 366 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) 367 return 0; 368 sk_EVP_PKEY_METHOD_sort(app_pkey_methods); 369 return 1; 370 } 371 372 void 373 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) 374 { 375 if (ctx == NULL) 376 return; 377 if (ctx->pmeth && ctx->pmeth->cleanup) 378 ctx->pmeth->cleanup(ctx); 379 EVP_PKEY_free(ctx->pkey); 380 EVP_PKEY_free(ctx->peerkey); 381 #ifndef OPENSSL_NO_ENGINE 382 if (ctx->engine) 383 /* The EVP_PKEY_CTX we used belongs to an ENGINE, release the 384 * functional reference we held for this reason. */ 385 ENGINE_finish(ctx->engine); 386 #endif 387 free(ctx); 388 } 389 390 int 391 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, 392 int p1, void *p2) 393 { 394 int ret; 395 396 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { 397 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); 398 return -2; 399 } 400 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) 401 return -1; 402 403 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { 404 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); 405 return -1; 406 } 407 408 if ((optype != -1) && !(ctx->operation & optype)) { 409 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); 410 return -1; 411 } 412 413 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); 414 415 if (ret == -2) 416 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); 417 418 return ret; 419 420 } 421 422 int 423 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value) 424 { 425 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { 426 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, 427 EVP_R_COMMAND_NOT_SUPPORTED); 428 return -2; 429 } 430 if (!strcmp(name, "digest")) { 431 const EVP_MD *md; 432 if (!value || !(md = EVP_get_digestbyname(value))) { 433 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, 434 EVP_R_INVALID_DIGEST); 435 return 0; 436 } 437 return EVP_PKEY_CTX_set_signature_md(ctx, md); 438 } 439 return ctx->pmeth->ctrl_str(ctx, name, value); 440 } 441 442 int 443 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) 444 { 445 return ctx->operation; 446 } 447 448 void 449 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) 450 { 451 ctx->keygen_info = dat; 452 ctx->keygen_info_count = datlen; 453 } 454 455 void 456 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) 457 { 458 ctx->data = data; 459 } 460 461 void * 462 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx) 463 { 464 return ctx->data; 465 } 466 467 EVP_PKEY * 468 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) 469 { 470 return ctx->pkey; 471 } 472 473 EVP_PKEY * 474 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) 475 { 476 return ctx->peerkey; 477 } 478 479 void 480 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) 481 { 482 ctx->app_data = data; 483 } 484 485 void * 486 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) 487 { 488 return ctx->app_data; 489 } 490 491 void 492 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, 493 int (*init)(EVP_PKEY_CTX *ctx)) 494 { 495 pmeth->init = init; 496 } 497 498 void 499 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, 500 int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)) 501 { 502 pmeth->copy = copy; 503 } 504 505 void 506 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, 507 void (*cleanup)(EVP_PKEY_CTX *ctx)) 508 { 509 pmeth->cleanup = cleanup; 510 } 511 512 void 513 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, 514 int (*paramgen_init)(EVP_PKEY_CTX *ctx), 515 int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)) 516 { 517 pmeth->paramgen_init = paramgen_init; 518 pmeth->paramgen = paramgen; 519 } 520 521 void 522 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, 523 int (*keygen_init)(EVP_PKEY_CTX *ctx), 524 int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)) 525 { 526 pmeth->keygen_init = keygen_init; 527 pmeth->keygen = keygen; 528 } 529 530 void 531 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, 532 int (*sign_init)(EVP_PKEY_CTX *ctx), 533 int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 534 const unsigned char *tbs, size_t tbslen)) 535 { 536 pmeth->sign_init = sign_init; 537 pmeth->sign = sign; 538 } 539 540 void 541 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, 542 int (*verify_init)(EVP_PKEY_CTX *ctx), 543 int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, 544 const unsigned char *tbs, size_t tbslen)) 545 { 546 pmeth->verify_init = verify_init; 547 pmeth->verify = verify; 548 } 549 550 void 551 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, 552 int (*verify_recover_init)(EVP_PKEY_CTX *ctx), 553 int (*verify_recover)(EVP_PKEY_CTX *ctx, 554 unsigned char *sig, size_t *siglen, 555 const unsigned char *tbs, size_t tbslen)) 556 { 557 pmeth->verify_recover_init = verify_recover_init; 558 pmeth->verify_recover = verify_recover; 559 } 560 561 void 562 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, 563 int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx), 564 int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 565 EVP_MD_CTX *mctx)) 566 { 567 pmeth->signctx_init = signctx_init; 568 pmeth->signctx = signctx; 569 } 570 571 void 572 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, 573 int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx), 574 int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen, 575 EVP_MD_CTX *mctx)) 576 { 577 pmeth->verifyctx_init = verifyctx_init; 578 pmeth->verifyctx = verifyctx; 579 } 580 581 void 582 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, 583 int (*encrypt_init)(EVP_PKEY_CTX *ctx), 584 int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, 585 const unsigned char *in, size_t inlen)) 586 { 587 pmeth->encrypt_init = encrypt_init; 588 pmeth->encrypt = encryptfn; 589 } 590 591 void 592 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, 593 int (*decrypt_init)(EVP_PKEY_CTX *ctx), 594 int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, 595 const unsigned char *in, size_t inlen)) 596 { 597 pmeth->decrypt_init = decrypt_init; 598 pmeth->decrypt = decrypt; 599 } 600 601 void 602 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, 603 int (*derive_init)(EVP_PKEY_CTX *ctx), 604 int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)) 605 { 606 pmeth->derive_init = derive_init; 607 pmeth->derive = derive; 608 } 609 610 void 611 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, 612 int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2), 613 int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value)) 614 { 615 pmeth->ctrl = ctrl; 616 pmeth->ctrl_str = ctrl_str; 617 } 618