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"
26 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
27 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
29 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
31 /* This array needs to be in order of NIDs */
32 static pmeth_fn standard_methods[] = {
33 #ifndef OPENSSL_NO_RSA
39 #ifndef OPENSSL_NO_DSA
46 #ifndef OPENSSL_NO_CMAC
49 #ifndef OPENSSL_NO_RSA
55 #ifndef OPENSSL_NO_SCRYPT
64 #ifndef OPENSSL_NO_POLY1305
67 #ifndef OPENSSL_NO_SIPHASH
74 #ifndef OPENSSL_NO_SM2
79 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
81 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
83 return ((*a)->pkey_id - ((**b)())->pkey_id);
86 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
88 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
89 const EVP_PKEY_METHOD *const *b)
91 return ((*a)->pkey_id - (*b)->pkey_id);
94 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
98 const EVP_PKEY_METHOD *t = &tmp;
101 if (app_pkey_methods) {
103 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
105 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
107 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
108 sizeof(standard_methods) /
110 if (ret == NULL || *ret == NULL)
115 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
116 EVP_PKEY *pkey, ENGINE *e,
117 const char *name, const char *propquery,
121 const EVP_PKEY_METHOD *pmeth = NULL;
124 * When using providers, the context is bound to the algo implementation
127 if (pkey == NULL && e == NULL && id == -1)
130 /* TODO(3.0) Legacy code should be removed when all is provider based */
139 * Here, we extract what information we can for the purpose of
140 * supporting usage with implementations from providers, to make
141 * for a smooth transition from legacy stuff to provider based stuff.
143 * If an engine is given, this is entirely legacy, and we should not
144 * pretend anything else, so we only set the name when no engine is
145 * given. If both are already given, someone made a mistake, and
146 * since that can only happen internally, it's safe to make an
149 if (!ossl_assert(e == NULL || name == NULL))
152 name = OBJ_nid2sn(id);
155 * We were called using legacy data, or an EVP_PKEY, but an EVP_PKEY
156 * isn't tied to a specific library context, so we fall back to the
157 * default library context.
158 * TODO(v3.0): an EVP_PKEY that doesn't originate from a leagacy key
159 * structure only has the pkeys[] cache, where the first element is
160 * considered the "origin". Investigate if that could be a suitable
161 * way to find a library context.
165 #ifndef OPENSSL_NO_ENGINE
166 if (e == NULL && pkey != NULL)
167 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
168 /* Try to find an ENGINE which implements this method */
170 if (!ENGINE_init(e)) {
171 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
175 e = ENGINE_get_pkey_meth_engine(id);
179 * If an ENGINE handled this method look it up. Otherwise use internal
183 pmeth = ENGINE_get_pkey_meth(e, id);
186 pmeth = EVP_PKEY_meth_find(id);
189 #ifndef OPENSSL_NO_ENGINE
192 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
198 ret = OPENSSL_zalloc(sizeof(*ret));
200 #ifndef OPENSSL_NO_ENGINE
203 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
206 ret->libctx = libctx;
207 ret->algorithm = name;
208 ret->propquery = propquery;
211 ret->operation = EVP_PKEY_OP_UNDEFINED;
214 EVP_PKEY_up_ref(pkey);
216 if (pmeth != NULL && pmeth->init != NULL) {
217 if (pmeth->init(ret) <= 0) {
219 EVP_PKEY_CTX_free(ret);
227 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
229 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
230 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
231 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
232 EVP_KEYEXCH_free(ctx->op.kex.exchange);
233 ctx->op.kex.exchprovctx = NULL;
234 ctx->op.kex.exchange = NULL;
235 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
236 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
237 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
238 EVP_SIGNATURE_free(ctx->op.sig.signature);
239 ctx->op.sig.sigprovctx = NULL;
240 ctx->op.sig.signature = NULL;
241 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
242 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
243 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
244 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
245 ctx->op.ciph.ciphprovctx = NULL;
246 ctx->op.ciph.cipher = NULL;
250 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
252 EVP_PKEY_METHOD *pmeth;
254 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
256 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
261 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
265 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
266 const EVP_PKEY_METHOD *meth)
269 *ppkey_id = meth->pkey_id;
271 *pflags = meth->flags;
274 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
277 dst->init = src->init;
278 dst->copy = src->copy;
279 dst->cleanup = src->cleanup;
281 dst->paramgen_init = src->paramgen_init;
282 dst->paramgen = src->paramgen;
284 dst->keygen_init = src->keygen_init;
285 dst->keygen = src->keygen;
287 dst->sign_init = src->sign_init;
288 dst->sign = src->sign;
290 dst->verify_init = src->verify_init;
291 dst->verify = src->verify;
293 dst->verify_recover_init = src->verify_recover_init;
294 dst->verify_recover = src->verify_recover;
296 dst->signctx_init = src->signctx_init;
297 dst->signctx = src->signctx;
299 dst->verifyctx_init = src->verifyctx_init;
300 dst->verifyctx = src->verifyctx;
302 dst->encrypt_init = src->encrypt_init;
303 dst->encrypt = src->encrypt;
305 dst->decrypt_init = src->decrypt_init;
306 dst->decrypt = src->decrypt;
308 dst->derive_init = src->derive_init;
309 dst->derive = src->derive;
311 dst->ctrl = src->ctrl;
312 dst->ctrl_str = src->ctrl_str;
314 dst->check = src->check;
317 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
319 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
323 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
325 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
328 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
330 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
333 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx,
335 const char *propquery)
337 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
340 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
344 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
345 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
346 && pctx->op.kex.exchprovctx == NULL)
347 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
348 && pctx->op.sig.sigprovctx == NULL)))
350 #ifndef OPENSSL_NO_ENGINE
351 /* Make sure it's safe to copy a pkey context using an ENGINE */
352 if (pctx->engine && !ENGINE_init(pctx->engine)) {
353 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
357 rctx = OPENSSL_zalloc(sizeof(*rctx));
359 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
363 if (pctx->pkey != NULL)
364 EVP_PKEY_up_ref(pctx->pkey);
365 rctx->pkey = pctx->pkey;
366 rctx->operation = pctx->operation;
367 rctx->libctx = pctx->libctx;
368 rctx->algorithm = pctx->algorithm;
369 rctx->propquery = pctx->propquery;
371 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
372 if (pctx->op.kex.exchange != NULL) {
373 rctx->op.kex.exchange = pctx->op.kex.exchange;
374 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
379 if (pctx->op.kex.exchprovctx != NULL) {
380 if (!ossl_assert(pctx->op.kex.exchange != NULL))
382 rctx->op.kex.exchprovctx
383 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
384 if (rctx->op.kex.exchprovctx == NULL) {
385 EVP_KEYEXCH_free(rctx->op.kex.exchange);
391 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
392 if (pctx->op.sig.signature != NULL) {
393 rctx->op.sig.signature = pctx->op.sig.signature;
394 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
399 if (pctx->op.sig.sigprovctx != NULL) {
400 if (!ossl_assert(pctx->op.sig.signature != NULL))
402 rctx->op.sig.sigprovctx
403 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
404 if (rctx->op.sig.sigprovctx == NULL) {
405 EVP_SIGNATURE_free(rctx->op.sig.signature);
411 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
412 if (pctx->op.ciph.cipher != NULL) {
413 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
414 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
419 if (pctx->op.ciph.ciphprovctx != NULL) {
420 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
422 rctx->op.ciph.ciphprovctx
423 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
424 if (rctx->op.ciph.ciphprovctx == NULL) {
425 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
433 rctx->pmeth = pctx->pmeth;
434 #ifndef OPENSSL_NO_ENGINE
435 rctx->engine = pctx->engine;
439 EVP_PKEY_up_ref(pctx->peerkey);
440 rctx->peerkey = pctx->peerkey;
442 if (pctx->pmeth->copy(rctx, pctx) > 0)
446 EVP_PKEY_CTX_free(rctx);
451 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
453 if (app_pkey_methods == NULL) {
454 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
455 if (app_pkey_methods == NULL){
456 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
460 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
461 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
464 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
468 void evp_app_cleanup_int(void)
470 if (app_pkey_methods != NULL)
471 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
474 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
476 const EVP_PKEY_METHOD *ret;
478 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
480 return ret == NULL ? 0 : 1;
483 size_t EVP_PKEY_meth_get_count(void)
485 size_t rv = OSSL_NELEM(standard_methods);
487 if (app_pkey_methods)
488 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
492 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
494 if (idx < OSSL_NELEM(standard_methods))
495 return (standard_methods[idx])();
496 if (app_pkey_methods == NULL)
498 idx -= OSSL_NELEM(standard_methods);
499 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
501 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
504 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
508 if (ctx->pmeth && ctx->pmeth->cleanup)
509 ctx->pmeth->cleanup(ctx);
511 evp_pkey_ctx_free_old_ops(ctx);
512 EVP_KEYMGMT_free(ctx->keymgmt);
514 EVP_PKEY_free(ctx->pkey);
515 EVP_PKEY_free(ctx->peerkey);
516 #ifndef OPENSSL_NO_ENGINE
517 ENGINE_finish(ctx->engine);
522 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
524 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
525 && ctx->op.sig.sigprovctx != NULL
526 && ctx->op.sig.signature != NULL
527 && ctx->op.sig.signature->get_ctx_params != NULL)
528 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
530 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
531 && ctx->op.ciph.ciphprovctx != NULL
532 && ctx->op.ciph.cipher != NULL
533 && ctx->op.ciph.cipher->get_ctx_params != NULL)
534 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
539 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
541 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
542 && ctx->op.sig.signature != NULL
543 && ctx->op.sig.signature->gettable_ctx_params != NULL)
544 return ctx->op.sig.signature->gettable_ctx_params();
546 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
547 && ctx->op.ciph.cipher != NULL
548 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
549 return ctx->op.ciph.cipher->gettable_ctx_params();
554 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
556 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
557 && ctx->op.kex.exchprovctx != NULL
558 && ctx->op.kex.exchange != NULL
559 && ctx->op.kex.exchange->set_ctx_params != NULL)
560 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
562 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
563 && ctx->op.sig.sigprovctx != NULL
564 && ctx->op.sig.signature != NULL
565 && ctx->op.sig.signature->set_ctx_params != NULL)
566 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
568 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
569 && ctx->op.ciph.ciphprovctx != NULL
570 && ctx->op.ciph.cipher != NULL
571 && ctx->op.ciph.cipher->set_ctx_params != NULL)
572 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
577 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
579 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
580 && ctx->op.kex.exchange != NULL
581 && ctx->op.kex.exchange->settable_ctx_params != NULL)
582 return ctx->op.kex.exchange->settable_ctx_params();
583 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
584 && ctx->op.sig.signature != NULL
585 && ctx->op.sig.signature->settable_ctx_params != NULL)
586 return ctx->op.sig.signature->settable_ctx_params();
587 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
588 && ctx->op.ciph.cipher != NULL
589 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
590 return ctx->op.ciph.cipher->settable_ctx_params();
595 #ifndef OPENSSL_NO_DH
596 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
598 OSSL_PARAM dh_pad_params[2];
599 unsigned int upad = pad;
601 /* We use EVP_PKEY_CTX_ctrl return values */
602 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
603 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
607 /* TODO(3.0): Remove this eventually when no more legacy */
608 if (ctx->op.kex.exchprovctx == NULL)
609 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
610 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
612 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
613 dh_pad_params[1] = OSSL_PARAM_construct_end();
615 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
619 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
621 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
622 /* 80 should be big enough */
626 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
627 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
628 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
632 /* TODO(3.0): Remove this eventually when no more legacy */
633 if (ctx->op.sig.sigprovctx == NULL)
634 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
635 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
637 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
640 *p++ = OSSL_PARAM_construct_end();
642 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
645 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
654 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
656 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
660 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
661 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
662 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
666 /* TODO(3.0): Remove this eventually when no more legacy */
667 if (ctx->op.sig.sigprovctx == NULL)
668 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
669 EVP_PKEY_CTRL_MD, 0, (void *)(md));
675 mdsize = EVP_MD_size(md);
676 name = EVP_MD_name(md);
679 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
681 * Cast away the const. This is read
682 * only so should be safe
686 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
688 *p++ = OSSL_PARAM_construct_end();
690 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
693 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
694 int cmd, int p1, void *p2)
697 #ifndef OPENSSL_NO_DH
698 case EVP_PKEY_CTRL_DH_PAD:
699 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
701 case EVP_PKEY_CTRL_MD:
702 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
703 case EVP_PKEY_CTRL_GET_MD:
704 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
705 case EVP_PKEY_CTRL_RSA_PADDING:
706 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
707 case EVP_PKEY_CTRL_GET_RSA_PADDING:
708 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
709 case EVP_PKEY_CTRL_RSA_OAEP_MD:
710 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
711 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
712 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
713 case EVP_PKEY_CTRL_RSA_MGF1_MD:
714 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
715 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
716 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
717 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
718 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
719 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
720 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
721 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
722 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
723 #ifndef OPENSSL_NO_CMS
724 case EVP_PKEY_CTRL_CMS_DECRYPT:
725 case EVP_PKEY_CTRL_CMS_ENCRYPT:
727 if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
729 ERR_raise(ERR_LIB_EVP,
730 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
736 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
737 int cmd, int p1, void *p2)
742 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
746 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
747 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
748 && ctx->op.sig.sigprovctx != NULL)
749 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
750 && ctx->op.ciph.ciphprovctx != NULL))
751 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
753 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
754 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
757 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
760 /* Skip the operation checks since this is called in a very early stage */
761 if (ctx->pmeth->digest_custom != NULL)
764 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
765 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
769 if ((optype != -1) && !(ctx->operation & optype)) {
770 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
775 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
778 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
783 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
784 int cmd, uint64_t value)
786 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
789 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
792 #ifndef OPENSSL_NO_DH
793 if (strcmp(name, "dh_pad") == 0) {
797 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
800 if (strcmp(name, "digest") == 0) {
804 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
806 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
810 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
811 EVP_MD_meth_free(md);
815 if (strcmp(name, "rsa_padding_mode") == 0) {
818 if (strcmp(value, "pkcs1") == 0) {
819 pm = RSA_PKCS1_PADDING;
820 } else if (strcmp(value, "sslv23") == 0) {
821 pm = RSA_SSLV23_PADDING;
822 } else if (strcmp(value, "none") == 0) {
824 } else if (strcmp(value, "oeap") == 0) {
825 pm = RSA_PKCS1_OAEP_PADDING;
826 } else if (strcmp(value, "oaep") == 0) {
827 pm = RSA_PKCS1_OAEP_PADDING;
828 } else if (strcmp(value, "x931") == 0) {
829 pm = RSA_X931_PADDING;
830 } else if (strcmp(value, "pss") == 0) {
831 pm = RSA_PKCS1_PSS_PADDING;
833 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
836 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
839 if (strcmp(name, "rsa_mgf1_md") == 0)
840 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, value, NULL);
842 if (strcmp(name, "rsa_oaep_md") == 0)
843 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, value, NULL);
845 if (strcmp(name, "rsa_oaep_label") == 0) {
850 lab = OPENSSL_hexstr2buf(value, &lablen);
853 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
864 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
865 const char *name, const char *value)
868 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
872 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
873 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
874 && ctx->op.sig.sigprovctx != NULL)
875 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
876 && ctx->op.ciph.ciphprovctx != NULL))
877 return legacy_ctrl_str_to_param(ctx, name, value);
879 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
880 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
883 if (strcmp(name, "digest") == 0)
884 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
886 return ctx->pmeth->ctrl_str(ctx, name, value);
889 /* Utility functions to send a string of hex string to a ctrl */
891 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
898 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
901 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
907 bin = OPENSSL_hexstr2buf(hex, &binlen);
910 if (binlen <= INT_MAX)
911 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
916 /* Pass a message digest to a ctrl */
917 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
921 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
922 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
925 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
928 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
930 return ctx->operation;
933 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
935 ctx->keygen_info = dat;
936 ctx->keygen_info_count = datlen;
939 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
944 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
949 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
954 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
959 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
961 ctx->app_data = data;
964 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
966 return ctx->app_data;
969 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
970 int (*init) (EVP_PKEY_CTX *ctx))
975 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
976 int (*copy) (EVP_PKEY_CTX *dst,
977 const EVP_PKEY_CTX *src))
982 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
983 void (*cleanup) (EVP_PKEY_CTX *ctx))
985 pmeth->cleanup = cleanup;
988 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
989 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
990 int (*paramgen) (EVP_PKEY_CTX *ctx,
993 pmeth->paramgen_init = paramgen_init;
994 pmeth->paramgen = paramgen;
997 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
998 int (*keygen_init) (EVP_PKEY_CTX *ctx),
999 int (*keygen) (EVP_PKEY_CTX *ctx,
1002 pmeth->keygen_init = keygen_init;
1003 pmeth->keygen = keygen;
1006 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1007 int (*sign_init) (EVP_PKEY_CTX *ctx),
1008 int (*sign) (EVP_PKEY_CTX *ctx,
1009 unsigned char *sig, size_t *siglen,
1010 const unsigned char *tbs,
1013 pmeth->sign_init = sign_init;
1017 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1018 int (*verify_init) (EVP_PKEY_CTX *ctx),
1019 int (*verify) (EVP_PKEY_CTX *ctx,
1020 const unsigned char *sig,
1022 const unsigned char *tbs,
1025 pmeth->verify_init = verify_init;
1026 pmeth->verify = verify;
1029 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1030 int (*verify_recover_init) (EVP_PKEY_CTX
1032 int (*verify_recover) (EVP_PKEY_CTX
1041 pmeth->verify_recover_init = verify_recover_init;
1042 pmeth->verify_recover = verify_recover;
1045 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1046 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1048 int (*signctx) (EVP_PKEY_CTX *ctx,
1053 pmeth->signctx_init = signctx_init;
1054 pmeth->signctx = signctx;
1057 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1058 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1060 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1061 const unsigned char *sig,
1065 pmeth->verifyctx_init = verifyctx_init;
1066 pmeth->verifyctx = verifyctx;
1069 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1070 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1071 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1074 const unsigned char *in,
1077 pmeth->encrypt_init = encrypt_init;
1078 pmeth->encrypt = encryptfn;
1081 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1082 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1083 int (*decrypt) (EVP_PKEY_CTX *ctx,
1086 const unsigned char *in,
1089 pmeth->decrypt_init = decrypt_init;
1090 pmeth->decrypt = decrypt;
1093 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1094 int (*derive_init) (EVP_PKEY_CTX *ctx),
1095 int (*derive) (EVP_PKEY_CTX *ctx,
1099 pmeth->derive_init = derive_init;
1100 pmeth->derive = derive;
1103 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1104 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1106 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1111 pmeth->ctrl_str = ctrl_str;
1114 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1115 int (*check) (EVP_PKEY *pkey))
1117 pmeth->check = check;
1120 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1121 int (*check) (EVP_PKEY *pkey))
1123 pmeth->public_check = check;
1126 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1127 int (*check) (EVP_PKEY *pkey))
1129 pmeth->param_check = check;
1132 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1133 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1136 pmeth->digest_custom = digest_custom;
1139 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1140 int (**pinit) (EVP_PKEY_CTX *ctx))
1142 *pinit = pmeth->init;
1145 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1146 int (**pcopy) (EVP_PKEY_CTX *dst,
1147 const EVP_PKEY_CTX *src))
1149 *pcopy = pmeth->copy;
1152 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1153 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1155 *pcleanup = pmeth->cleanup;
1158 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1159 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1160 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1164 *pparamgen_init = pmeth->paramgen_init;
1166 *pparamgen = pmeth->paramgen;
1169 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1170 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1171 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1175 *pkeygen_init = pmeth->keygen_init;
1177 *pkeygen = pmeth->keygen;
1180 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1181 int (**psign_init) (EVP_PKEY_CTX *ctx),
1182 int (**psign) (EVP_PKEY_CTX *ctx,
1183 unsigned char *sig, size_t *siglen,
1184 const unsigned char *tbs,
1188 *psign_init = pmeth->sign_init;
1190 *psign = pmeth->sign;
1193 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1194 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1195 int (**pverify) (EVP_PKEY_CTX *ctx,
1196 const unsigned char *sig,
1198 const unsigned char *tbs,
1202 *pverify_init = pmeth->verify_init;
1204 *pverify = pmeth->verify;
1207 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1208 int (**pverify_recover_init) (EVP_PKEY_CTX
1210 int (**pverify_recover) (EVP_PKEY_CTX
1219 if (pverify_recover_init)
1220 *pverify_recover_init = pmeth->verify_recover_init;
1221 if (pverify_recover)
1222 *pverify_recover = pmeth->verify_recover;
1225 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1226 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1228 int (**psignctx) (EVP_PKEY_CTX *ctx,
1234 *psignctx_init = pmeth->signctx_init;
1236 *psignctx = pmeth->signctx;
1239 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1240 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1242 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1243 const unsigned char *sig,
1247 if (pverifyctx_init)
1248 *pverifyctx_init = pmeth->verifyctx_init;
1250 *pverifyctx = pmeth->verifyctx;
1253 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1254 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1255 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1258 const unsigned char *in,
1262 *pencrypt_init = pmeth->encrypt_init;
1264 *pencryptfn = pmeth->encrypt;
1267 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1268 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1269 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1272 const unsigned char *in,
1276 *pdecrypt_init = pmeth->decrypt_init;
1278 *pdecrypt = pmeth->decrypt;
1281 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1282 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1283 int (**pderive) (EVP_PKEY_CTX *ctx,
1288 *pderive_init = pmeth->derive_init;
1290 *pderive = pmeth->derive;
1293 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1294 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1296 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1301 *pctrl = pmeth->ctrl;
1303 *pctrl_str = pmeth->ctrl_str;
1306 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1307 int (**pcheck) (EVP_PKEY *pkey))
1310 *pcheck = pmeth->check;
1313 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1314 int (**pcheck) (EVP_PKEY *pkey))
1317 *pcheck = pmeth->public_check;
1320 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1321 int (**pcheck) (EVP_PKEY *pkey))
1324 *pcheck = pmeth->param_check;
1327 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1328 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1331 if (pdigest_custom != NULL)
1332 *pdigest_custom = pmeth->digest_custom;