3 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/core_names.h>
17 #include <openssl/dh.h>
18 #include <openssl/rsa.h>
19 #include "internal/cryptlib.h"
20 #include "crypto/asn1.h"
21 #include "crypto/evp.h"
22 #include "internal/numbers.h"
23 #include "internal/provider.h"
24 #include "evp_local.h"
28 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
29 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
31 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
33 /* This array needs to be in order of NIDs */
34 static pmeth_fn standard_methods[] = {
35 # ifndef OPENSSL_NO_RSA
38 # ifndef OPENSSL_NO_DH
41 # ifndef OPENSSL_NO_DSA
44 # ifndef OPENSSL_NO_EC
48 # ifndef OPENSSL_NO_CMAC
51 # ifndef OPENSSL_NO_RSA
54 # ifndef OPENSSL_NO_DH
57 # ifndef OPENSSL_NO_SCRYPT
61 # ifndef OPENSSL_NO_EC
66 # ifndef OPENSSL_NO_POLY1305
69 # ifndef OPENSSL_NO_SIPHASH
72 # ifndef OPENSSL_NO_EC
76 # ifndef OPENSSL_NO_SM2
81 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
83 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
85 return ((*a)->pkey_id - ((**b)())->pkey_id);
88 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
90 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
91 const EVP_PKEY_METHOD *const *b)
93 return ((*a)->pkey_id - (*b)->pkey_id);
96 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
100 const EVP_PKEY_METHOD *t = &tmp;
103 if (app_pkey_methods) {
105 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
107 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
109 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
110 sizeof(standard_methods) /
112 if (ret == NULL || *ret == NULL)
117 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
119 EVP_PKEY_METHOD *pmeth;
121 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
123 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
128 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
131 #endif /* FIPS_MODE */
133 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
134 EVP_PKEY *pkey, ENGINE *e,
135 const char *name, const char *propquery,
140 const EVP_PKEY_METHOD *pmeth = NULL;
143 * When using providers, the context is bound to the algo implementation
146 if (pkey == NULL && e == NULL && id == -1)
150 * If the key doesn't contain anything legacy, then it must be provided,
151 * so we extract the necessary information and use that.
153 if (pkey != NULL && pkey->ameth == NULL) {
154 /* If we have an engine, something went wrong somewhere... */
155 if (!ossl_assert(e == NULL))
157 name = evp_first_name(pkey->pkeys[0].keymgmt->prov,
158 pkey->pkeys[0].keymgmt->name_id);
160 * TODO: I wonder if the EVP_PKEY should have the name and propquery
161 * that were used when building it.... /RL
166 /* TODO(3.0) Legacy code should be removed when all is provider based */
175 * Here, we extract what information we can for the purpose of
176 * supporting usage with implementations from providers, to make
177 * for a smooth transition from legacy stuff to provider based stuff.
179 * If an engine is given, this is entirely legacy, and we should not
180 * pretend anything else, so we only set the name when no engine is
181 * given. If both are already given, someone made a mistake, and
182 * since that can only happen internally, it's safe to make an
185 if (!ossl_assert(e == NULL || name == NULL))
188 name = OBJ_nid2sn(id);
190 # ifndef OPENSSL_NO_ENGINE
191 if (e == NULL && pkey != NULL)
192 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
193 /* Try to find an ENGINE which implements this method */
195 if (!ENGINE_init(e)) {
196 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
200 e = ENGINE_get_pkey_meth_engine(id);
204 * If an ENGINE handled this method look it up. Otherwise use internal
208 pmeth = ENGINE_get_pkey_meth(e, id);
211 pmeth = EVP_PKEY_meth_find(id);
214 # ifndef OPENSSL_NO_ENGINE
217 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
221 #endif /* FIPS_MODE */
223 ret = OPENSSL_zalloc(sizeof(*ret));
225 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
228 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
231 ret->libctx = libctx;
233 ret->propquery = propquery;
236 ret->operation = EVP_PKEY_OP_UNDEFINED;
239 EVP_PKEY_up_ref(pkey);
241 if (pmeth != NULL && pmeth->init != NULL) {
242 if (pmeth->init(ret) <= 0) {
244 EVP_PKEY_CTX_free(ret);
252 /*- All methods below can also be used in FIPS_MODE */
254 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx,
256 const char *propquery)
258 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
261 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey,
262 const char *propquery)
264 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
267 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
269 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
270 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
271 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
272 EVP_SIGNATURE_free(ctx->op.sig.signature);
273 ctx->op.sig.sigprovctx = NULL;
274 ctx->op.sig.signature = NULL;
275 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
276 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
277 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
278 EVP_KEYEXCH_free(ctx->op.kex.exchange);
279 ctx->op.kex.exchprovctx = NULL;
280 ctx->op.kex.exchange = NULL;
282 /* TODO(3.0): add dependancies and uncomment this when available for fips mode */
284 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
285 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
286 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
287 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
288 ctx->op.ciph.ciphprovctx = NULL;
289 ctx->op.ciph.cipher = NULL;
294 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
298 if (ctx->pmeth && ctx->pmeth->cleanup)
299 ctx->pmeth->cleanup(ctx);
301 evp_pkey_ctx_free_old_ops(ctx);
302 EVP_KEYMGMT_free(ctx->keymgmt);
304 EVP_PKEY_free(ctx->pkey);
305 EVP_PKEY_free(ctx->peerkey);
306 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
307 ENGINE_finish(ctx->engine);
314 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
315 const EVP_PKEY_METHOD *meth)
318 *ppkey_id = meth->pkey_id;
320 *pflags = meth->flags;
323 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
326 dst->init = src->init;
327 dst->copy = src->copy;
328 dst->cleanup = src->cleanup;
330 dst->paramgen_init = src->paramgen_init;
331 dst->paramgen = src->paramgen;
333 dst->keygen_init = src->keygen_init;
334 dst->keygen = src->keygen;
336 dst->sign_init = src->sign_init;
337 dst->sign = src->sign;
339 dst->verify_init = src->verify_init;
340 dst->verify = src->verify;
342 dst->verify_recover_init = src->verify_recover_init;
343 dst->verify_recover = src->verify_recover;
345 dst->signctx_init = src->signctx_init;
346 dst->signctx = src->signctx;
348 dst->verifyctx_init = src->verifyctx_init;
349 dst->verifyctx = src->verifyctx;
351 dst->encrypt_init = src->encrypt_init;
352 dst->encrypt = src->encrypt;
354 dst->decrypt_init = src->decrypt_init;
355 dst->decrypt = src->decrypt;
357 dst->derive_init = src->derive_init;
358 dst->derive = src->derive;
360 dst->ctrl = src->ctrl;
361 dst->ctrl_str = src->ctrl_str;
363 dst->check = src->check;
366 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
368 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
372 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
374 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
377 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
379 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
382 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
386 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
387 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
388 && pctx->op.kex.exchprovctx == NULL)
389 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
390 && pctx->op.sig.sigprovctx == NULL)))
392 # ifndef OPENSSL_NO_ENGINE
393 /* Make sure it's safe to copy a pkey context using an ENGINE */
394 if (pctx->engine && !ENGINE_init(pctx->engine)) {
395 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
399 rctx = OPENSSL_zalloc(sizeof(*rctx));
401 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
405 if (pctx->pkey != NULL)
406 EVP_PKEY_up_ref(pctx->pkey);
407 rctx->pkey = pctx->pkey;
408 rctx->operation = pctx->operation;
409 rctx->libctx = pctx->libctx;
410 rctx->keytype = pctx->keytype;
411 rctx->propquery = pctx->propquery;
413 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
414 if (pctx->op.kex.exchange != NULL) {
415 rctx->op.kex.exchange = pctx->op.kex.exchange;
416 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
421 if (pctx->op.kex.exchprovctx != NULL) {
422 if (!ossl_assert(pctx->op.kex.exchange != NULL))
424 rctx->op.kex.exchprovctx
425 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
426 if (rctx->op.kex.exchprovctx == NULL) {
427 EVP_KEYEXCH_free(rctx->op.kex.exchange);
433 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
434 if (pctx->op.sig.signature != NULL) {
435 rctx->op.sig.signature = pctx->op.sig.signature;
436 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
441 if (pctx->op.sig.sigprovctx != NULL) {
442 if (!ossl_assert(pctx->op.sig.signature != NULL))
444 rctx->op.sig.sigprovctx
445 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
446 if (rctx->op.sig.sigprovctx == NULL) {
447 EVP_SIGNATURE_free(rctx->op.sig.signature);
453 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
454 if (pctx->op.ciph.cipher != NULL) {
455 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
456 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
461 if (pctx->op.ciph.ciphprovctx != NULL) {
462 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
464 rctx->op.ciph.ciphprovctx
465 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
466 if (rctx->op.ciph.ciphprovctx == NULL) {
467 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
475 rctx->pmeth = pctx->pmeth;
476 # ifndef OPENSSL_NO_ENGINE
477 rctx->engine = pctx->engine;
481 EVP_PKEY_up_ref(pctx->peerkey);
482 rctx->peerkey = pctx->peerkey;
484 if (pctx->pmeth->copy(rctx, pctx) > 0)
488 EVP_PKEY_CTX_free(rctx);
493 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
495 if (app_pkey_methods == NULL) {
496 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
497 if (app_pkey_methods == NULL){
498 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
502 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
503 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
506 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
510 void evp_app_cleanup_int(void)
512 if (app_pkey_methods != NULL)
513 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
516 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
518 const EVP_PKEY_METHOD *ret;
520 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
522 return ret == NULL ? 0 : 1;
525 size_t EVP_PKEY_meth_get_count(void)
527 size_t rv = OSSL_NELEM(standard_methods);
529 if (app_pkey_methods)
530 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
534 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
536 if (idx < OSSL_NELEM(standard_methods))
537 return (standard_methods[idx])();
538 if (app_pkey_methods == NULL)
540 idx -= OSSL_NELEM(standard_methods);
541 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
543 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
547 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
549 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
550 && ctx->op.kex.exchprovctx != NULL
551 && ctx->op.kex.exchange != NULL
552 && ctx->op.kex.exchange->set_ctx_params != NULL)
553 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
555 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
556 && ctx->op.sig.sigprovctx != NULL
557 && ctx->op.sig.signature != NULL
558 && ctx->op.sig.signature->set_ctx_params != NULL)
559 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
561 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
562 && ctx->op.ciph.ciphprovctx != NULL
563 && ctx->op.ciph.cipher != NULL
564 && ctx->op.ciph.cipher->set_ctx_params != NULL)
565 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
571 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
573 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
574 && ctx->op.sig.sigprovctx != NULL
575 && ctx->op.sig.signature != NULL
576 && ctx->op.sig.signature->get_ctx_params != NULL)
577 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
579 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
580 && ctx->op.ciph.ciphprovctx != NULL
581 && ctx->op.ciph.cipher != NULL
582 && ctx->op.ciph.cipher->get_ctx_params != NULL)
583 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
588 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
590 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
591 && ctx->op.sig.signature != NULL
592 && ctx->op.sig.signature->gettable_ctx_params != NULL)
593 return ctx->op.sig.signature->gettable_ctx_params();
595 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
596 && ctx->op.ciph.cipher != NULL
597 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
598 return ctx->op.ciph.cipher->gettable_ctx_params();
603 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
605 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
606 && ctx->op.kex.exchange != NULL
607 && ctx->op.kex.exchange->settable_ctx_params != NULL)
608 return ctx->op.kex.exchange->settable_ctx_params();
609 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
610 && ctx->op.sig.signature != NULL
611 && ctx->op.sig.signature->settable_ctx_params != NULL)
612 return ctx->op.sig.signature->settable_ctx_params();
613 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
614 && ctx->op.ciph.cipher != NULL
615 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
616 return ctx->op.ciph.cipher->settable_ctx_params();
621 # ifndef OPENSSL_NO_DH
622 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
624 OSSL_PARAM dh_pad_params[2];
625 unsigned int upad = pad;
627 /* We use EVP_PKEY_CTX_ctrl return values */
628 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
629 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
633 /* TODO(3.0): Remove this eventually when no more legacy */
634 if (ctx->op.kex.exchprovctx == NULL)
635 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
636 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
638 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
639 dh_pad_params[1] = OSSL_PARAM_construct_end();
641 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
645 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
647 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
648 /* 80 should be big enough */
652 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
653 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
654 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
658 /* TODO(3.0): Remove this eventually when no more legacy */
659 if (ctx->op.sig.sigprovctx == NULL)
660 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
661 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
663 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
666 *p++ = OSSL_PARAM_construct_end();
668 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
671 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
680 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
682 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
685 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
686 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
687 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
691 /* TODO(3.0): Remove this eventually when no more legacy */
692 if (ctx->op.sig.sigprovctx == NULL)
693 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
694 EVP_PKEY_CTRL_MD, 0, (void *)(md));
699 name = EVP_MD_name(md);
702 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
704 * Cast away the const. This is read
705 * only so should be safe
708 *p++ = OSSL_PARAM_construct_end();
710 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
713 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
714 int cmd, int p1, void *p2)
717 # ifndef OPENSSL_NO_DH
718 case EVP_PKEY_CTRL_DH_PAD:
719 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
721 case EVP_PKEY_CTRL_MD:
722 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
723 case EVP_PKEY_CTRL_GET_MD:
724 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
725 case EVP_PKEY_CTRL_RSA_PADDING:
726 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
727 case EVP_PKEY_CTRL_GET_RSA_PADDING:
728 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
729 case EVP_PKEY_CTRL_RSA_OAEP_MD:
730 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
731 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
732 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
733 case EVP_PKEY_CTRL_RSA_MGF1_MD:
734 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
735 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
736 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
737 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
738 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
739 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
740 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
741 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
742 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
743 # ifndef OPENSSL_NO_CMS
744 case EVP_PKEY_CTRL_CMS_DECRYPT:
745 case EVP_PKEY_CTRL_CMS_ENCRYPT:
747 if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
749 ERR_raise(ERR_LIB_EVP,
750 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
756 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
757 int cmd, int p1, void *p2)
762 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
766 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
767 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
768 && ctx->op.sig.sigprovctx != NULL)
769 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
770 && ctx->op.ciph.ciphprovctx != NULL))
771 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
773 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
774 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
777 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
780 /* Skip the operation checks since this is called in a very early stage */
781 if (ctx->pmeth->digest_custom != NULL)
784 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
785 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
789 if ((optype != -1) && !(ctx->operation & optype)) {
790 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
795 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
798 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
803 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
804 int cmd, uint64_t value)
806 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
809 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
812 if (strcmp(name, "rsa_padding_mode") == 0)
813 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
814 else if (strcmp(name, "rsa_mgf1_md") == 0)
815 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
816 else if (strcmp(name, "rsa_oaep_md") == 0)
817 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
818 else if (strcmp(name, "rsa_oaep_label") == 0)
819 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
820 # ifndef OPENSSL_NO_DH
821 else if (strcmp(name, "dh_pad") == 0)
822 name = OSSL_EXCHANGE_PARAM_PAD;
827 * TODO(3.0) reduce the code above to only translate known legacy
828 * string to the corresponding core name (see core_names.h), but
829 * otherwise leave it to this code block to do the actual work.
831 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
832 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
835 if (!OSSL_PARAM_allocate_from_text(¶ms[0], settable, name, value,
838 if (EVP_PKEY_CTX_set_params(ctx, params))
840 OPENSSL_free(params[0].data);
845 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
846 const char *name, const char *value)
849 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
853 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
854 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
855 && ctx->op.sig.sigprovctx != NULL)
856 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
857 && ctx->op.ciph.ciphprovctx != NULL))
858 return legacy_ctrl_str_to_param(ctx, name, value);
860 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
861 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
864 if (strcmp(name, "digest") == 0)
865 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
867 return ctx->pmeth->ctrl_str(ctx, name, value);
870 /* Utility functions to send a string of hex string to a ctrl */
872 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
879 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
882 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
888 bin = OPENSSL_hexstr2buf(hex, &binlen);
891 if (binlen <= INT_MAX)
892 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
897 /* Pass a message digest to a ctrl */
898 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
902 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
903 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
906 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
909 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
911 return ctx->operation;
914 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
916 ctx->keygen_info = dat;
917 ctx->keygen_info_count = datlen;
920 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
925 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
930 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
935 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
940 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
942 ctx->app_data = data;
945 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
947 return ctx->app_data;
950 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
951 int (*init) (EVP_PKEY_CTX *ctx))
956 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
957 int (*copy) (EVP_PKEY_CTX *dst,
958 const EVP_PKEY_CTX *src))
963 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
964 void (*cleanup) (EVP_PKEY_CTX *ctx))
966 pmeth->cleanup = cleanup;
969 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
970 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
971 int (*paramgen) (EVP_PKEY_CTX *ctx,
974 pmeth->paramgen_init = paramgen_init;
975 pmeth->paramgen = paramgen;
978 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
979 int (*keygen_init) (EVP_PKEY_CTX *ctx),
980 int (*keygen) (EVP_PKEY_CTX *ctx,
983 pmeth->keygen_init = keygen_init;
984 pmeth->keygen = keygen;
987 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
988 int (*sign_init) (EVP_PKEY_CTX *ctx),
989 int (*sign) (EVP_PKEY_CTX *ctx,
990 unsigned char *sig, size_t *siglen,
991 const unsigned char *tbs,
994 pmeth->sign_init = sign_init;
998 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
999 int (*verify_init) (EVP_PKEY_CTX *ctx),
1000 int (*verify) (EVP_PKEY_CTX *ctx,
1001 const unsigned char *sig,
1003 const unsigned char *tbs,
1006 pmeth->verify_init = verify_init;
1007 pmeth->verify = verify;
1010 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1011 int (*verify_recover_init) (EVP_PKEY_CTX
1013 int (*verify_recover) (EVP_PKEY_CTX
1022 pmeth->verify_recover_init = verify_recover_init;
1023 pmeth->verify_recover = verify_recover;
1026 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1027 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1029 int (*signctx) (EVP_PKEY_CTX *ctx,
1034 pmeth->signctx_init = signctx_init;
1035 pmeth->signctx = signctx;
1038 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1039 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1041 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1042 const unsigned char *sig,
1046 pmeth->verifyctx_init = verifyctx_init;
1047 pmeth->verifyctx = verifyctx;
1050 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1051 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1052 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1055 const unsigned char *in,
1058 pmeth->encrypt_init = encrypt_init;
1059 pmeth->encrypt = encryptfn;
1062 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1063 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1064 int (*decrypt) (EVP_PKEY_CTX *ctx,
1067 const unsigned char *in,
1070 pmeth->decrypt_init = decrypt_init;
1071 pmeth->decrypt = decrypt;
1074 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1075 int (*derive_init) (EVP_PKEY_CTX *ctx),
1076 int (*derive) (EVP_PKEY_CTX *ctx,
1080 pmeth->derive_init = derive_init;
1081 pmeth->derive = derive;
1084 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1085 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1087 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1092 pmeth->ctrl_str = ctrl_str;
1095 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1096 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1097 const unsigned char *tbs, size_t tbslen))
1099 pmeth->digestsign = digestsign;
1102 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1103 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1104 size_t siglen, const unsigned char *tbs,
1107 pmeth->digestverify = digestverify;
1110 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1111 int (*check) (EVP_PKEY *pkey))
1113 pmeth->check = check;
1116 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1117 int (*check) (EVP_PKEY *pkey))
1119 pmeth->public_check = check;
1122 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1123 int (*check) (EVP_PKEY *pkey))
1125 pmeth->param_check = check;
1128 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1129 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1132 pmeth->digest_custom = digest_custom;
1135 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1136 int (**pinit) (EVP_PKEY_CTX *ctx))
1138 *pinit = pmeth->init;
1141 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1142 int (**pcopy) (EVP_PKEY_CTX *dst,
1143 const EVP_PKEY_CTX *src))
1145 *pcopy = pmeth->copy;
1148 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1149 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1151 *pcleanup = pmeth->cleanup;
1154 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1155 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1156 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1160 *pparamgen_init = pmeth->paramgen_init;
1162 *pparamgen = pmeth->paramgen;
1165 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1166 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1167 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1171 *pkeygen_init = pmeth->keygen_init;
1173 *pkeygen = pmeth->keygen;
1176 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1177 int (**psign_init) (EVP_PKEY_CTX *ctx),
1178 int (**psign) (EVP_PKEY_CTX *ctx,
1179 unsigned char *sig, size_t *siglen,
1180 const unsigned char *tbs,
1184 *psign_init = pmeth->sign_init;
1186 *psign = pmeth->sign;
1189 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1190 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1191 int (**pverify) (EVP_PKEY_CTX *ctx,
1192 const unsigned char *sig,
1194 const unsigned char *tbs,
1198 *pverify_init = pmeth->verify_init;
1200 *pverify = pmeth->verify;
1203 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1204 int (**pverify_recover_init) (EVP_PKEY_CTX
1206 int (**pverify_recover) (EVP_PKEY_CTX
1215 if (pverify_recover_init)
1216 *pverify_recover_init = pmeth->verify_recover_init;
1217 if (pverify_recover)
1218 *pverify_recover = pmeth->verify_recover;
1221 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1222 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1224 int (**psignctx) (EVP_PKEY_CTX *ctx,
1230 *psignctx_init = pmeth->signctx_init;
1232 *psignctx = pmeth->signctx;
1235 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1236 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1238 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1239 const unsigned char *sig,
1243 if (pverifyctx_init)
1244 *pverifyctx_init = pmeth->verifyctx_init;
1246 *pverifyctx = pmeth->verifyctx;
1249 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1250 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1251 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1254 const unsigned char *in,
1258 *pencrypt_init = pmeth->encrypt_init;
1260 *pencryptfn = pmeth->encrypt;
1263 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1264 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1265 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1268 const unsigned char *in,
1272 *pdecrypt_init = pmeth->decrypt_init;
1274 *pdecrypt = pmeth->decrypt;
1277 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1278 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1279 int (**pderive) (EVP_PKEY_CTX *ctx,
1284 *pderive_init = pmeth->derive_init;
1286 *pderive = pmeth->derive;
1289 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1290 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1292 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1297 *pctrl = pmeth->ctrl;
1299 *pctrl_str = pmeth->ctrl_str;
1302 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1303 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1304 const unsigned char *tbs, size_t tbslen))
1307 *digestsign = pmeth->digestsign;
1310 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1311 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1312 size_t siglen, const unsigned char *tbs,
1316 *digestverify = pmeth->digestverify;
1319 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1320 int (**pcheck) (EVP_PKEY *pkey))
1323 *pcheck = pmeth->check;
1326 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1327 int (**pcheck) (EVP_PKEY *pkey))
1330 *pcheck = pmeth->public_check;
1333 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1334 int (**pcheck) (EVP_PKEY *pkey))
1337 *pcheck = pmeth->param_check;
1340 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1341 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1344 if (pdigest_custom != NULL)
1345 *pdigest_custom = pmeth->digest_custom;
1348 #endif /* FIPS_MODE */