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)
131 * If the key doesn't contain anything legacy, then it must be provided,
132 * so we extract the necessary information and use that.
134 if (pkey != NULL && pkey->pkey.ptr == NULL) {
135 /* If we have an engine, something went wrong somewhere... */
136 if (!ossl_assert(e == NULL))
138 name = evp_first_name(pkey->pkeys[0].keymgmt->prov,
139 pkey->pkeys[0].keymgmt->name_id);
141 * TODO: I wonder if the EVP_PKEY should have the name and propquery
142 * that were used when building it.... /RL
147 /* TODO(3.0) Legacy code should be removed when all is provider based */
156 * Here, we extract what information we can for the purpose of
157 * supporting usage with implementations from providers, to make
158 * for a smooth transition from legacy stuff to provider based stuff.
160 * If an engine is given, this is entirely legacy, and we should not
161 * pretend anything else, so we only set the name when no engine is
162 * given. If both are already given, someone made a mistake, and
163 * since that can only happen internally, it's safe to make an
166 if (!ossl_assert(e == NULL || name == NULL))
169 name = OBJ_nid2sn(id);
172 * We were called using legacy data, or an EVP_PKEY, but an EVP_PKEY
173 * isn't tied to a specific library context, so we fall back to the
174 * default library context.
175 * TODO(v3.0): an EVP_PKEY that doesn't originate from a leagacy key
176 * structure only has the pkeys[] cache, where the first element is
177 * considered the "origin". Investigate if that could be a suitable
178 * way to find a library context.
182 #ifndef OPENSSL_NO_ENGINE
183 if (e == NULL && pkey != NULL)
184 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
185 /* Try to find an ENGINE which implements this method */
187 if (!ENGINE_init(e)) {
188 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
192 e = ENGINE_get_pkey_meth_engine(id);
196 * If an ENGINE handled this method look it up. Otherwise use internal
200 pmeth = ENGINE_get_pkey_meth(e, id);
203 pmeth = EVP_PKEY_meth_find(id);
206 #ifndef OPENSSL_NO_ENGINE
209 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
215 ret = OPENSSL_zalloc(sizeof(*ret));
217 #ifndef OPENSSL_NO_ENGINE
220 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
223 ret->libctx = libctx;
224 ret->algorithm = name;
225 ret->propquery = propquery;
228 ret->operation = EVP_PKEY_OP_UNDEFINED;
231 EVP_PKEY_up_ref(pkey);
233 if (pmeth != NULL && pmeth->init != NULL) {
234 if (pmeth->init(ret) <= 0) {
236 EVP_PKEY_CTX_free(ret);
244 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
246 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
247 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
248 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
249 EVP_KEYEXCH_free(ctx->op.kex.exchange);
250 ctx->op.kex.exchprovctx = NULL;
251 ctx->op.kex.exchange = NULL;
252 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
253 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
254 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
255 EVP_SIGNATURE_free(ctx->op.sig.signature);
256 ctx->op.sig.sigprovctx = NULL;
257 ctx->op.sig.signature = NULL;
258 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
259 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
260 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
261 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
262 ctx->op.ciph.ciphprovctx = NULL;
263 ctx->op.ciph.cipher = NULL;
267 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
269 EVP_PKEY_METHOD *pmeth;
271 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
273 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
278 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
282 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
283 const EVP_PKEY_METHOD *meth)
286 *ppkey_id = meth->pkey_id;
288 *pflags = meth->flags;
291 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
294 dst->init = src->init;
295 dst->copy = src->copy;
296 dst->cleanup = src->cleanup;
298 dst->paramgen_init = src->paramgen_init;
299 dst->paramgen = src->paramgen;
301 dst->keygen_init = src->keygen_init;
302 dst->keygen = src->keygen;
304 dst->sign_init = src->sign_init;
305 dst->sign = src->sign;
307 dst->verify_init = src->verify_init;
308 dst->verify = src->verify;
310 dst->verify_recover_init = src->verify_recover_init;
311 dst->verify_recover = src->verify_recover;
313 dst->signctx_init = src->signctx_init;
314 dst->signctx = src->signctx;
316 dst->verifyctx_init = src->verifyctx_init;
317 dst->verifyctx = src->verifyctx;
319 dst->encrypt_init = src->encrypt_init;
320 dst->encrypt = src->encrypt;
322 dst->decrypt_init = src->decrypt_init;
323 dst->decrypt = src->decrypt;
325 dst->derive_init = src->derive_init;
326 dst->derive = src->derive;
328 dst->ctrl = src->ctrl;
329 dst->ctrl_str = src->ctrl_str;
331 dst->check = src->check;
334 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
336 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
340 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
342 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
345 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
347 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
350 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx,
352 const char *propquery)
354 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
357 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
361 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
362 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
363 && pctx->op.kex.exchprovctx == NULL)
364 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
365 && pctx->op.sig.sigprovctx == NULL)))
367 #ifndef OPENSSL_NO_ENGINE
368 /* Make sure it's safe to copy a pkey context using an ENGINE */
369 if (pctx->engine && !ENGINE_init(pctx->engine)) {
370 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
374 rctx = OPENSSL_zalloc(sizeof(*rctx));
376 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
380 if (pctx->pkey != NULL)
381 EVP_PKEY_up_ref(pctx->pkey);
382 rctx->pkey = pctx->pkey;
383 rctx->operation = pctx->operation;
384 rctx->libctx = pctx->libctx;
385 rctx->algorithm = pctx->algorithm;
386 rctx->propquery = pctx->propquery;
388 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
389 if (pctx->op.kex.exchange != NULL) {
390 rctx->op.kex.exchange = pctx->op.kex.exchange;
391 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
396 if (pctx->op.kex.exchprovctx != NULL) {
397 if (!ossl_assert(pctx->op.kex.exchange != NULL))
399 rctx->op.kex.exchprovctx
400 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
401 if (rctx->op.kex.exchprovctx == NULL) {
402 EVP_KEYEXCH_free(rctx->op.kex.exchange);
408 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
409 if (pctx->op.sig.signature != NULL) {
410 rctx->op.sig.signature = pctx->op.sig.signature;
411 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
416 if (pctx->op.sig.sigprovctx != NULL) {
417 if (!ossl_assert(pctx->op.sig.signature != NULL))
419 rctx->op.sig.sigprovctx
420 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
421 if (rctx->op.sig.sigprovctx == NULL) {
422 EVP_SIGNATURE_free(rctx->op.sig.signature);
428 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
429 if (pctx->op.ciph.cipher != NULL) {
430 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
431 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
436 if (pctx->op.ciph.ciphprovctx != NULL) {
437 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
439 rctx->op.ciph.ciphprovctx
440 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
441 if (rctx->op.ciph.ciphprovctx == NULL) {
442 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
450 rctx->pmeth = pctx->pmeth;
451 #ifndef OPENSSL_NO_ENGINE
452 rctx->engine = pctx->engine;
456 EVP_PKEY_up_ref(pctx->peerkey);
457 rctx->peerkey = pctx->peerkey;
459 if (pctx->pmeth->copy(rctx, pctx) > 0)
463 EVP_PKEY_CTX_free(rctx);
468 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
470 if (app_pkey_methods == NULL) {
471 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
472 if (app_pkey_methods == NULL){
473 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
477 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
478 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
481 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
485 void evp_app_cleanup_int(void)
487 if (app_pkey_methods != NULL)
488 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
491 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
493 const EVP_PKEY_METHOD *ret;
495 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
497 return ret == NULL ? 0 : 1;
500 size_t EVP_PKEY_meth_get_count(void)
502 size_t rv = OSSL_NELEM(standard_methods);
504 if (app_pkey_methods)
505 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
509 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
511 if (idx < OSSL_NELEM(standard_methods))
512 return (standard_methods[idx])();
513 if (app_pkey_methods == NULL)
515 idx -= OSSL_NELEM(standard_methods);
516 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
518 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
521 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
525 if (ctx->pmeth && ctx->pmeth->cleanup)
526 ctx->pmeth->cleanup(ctx);
528 evp_pkey_ctx_free_old_ops(ctx);
529 EVP_KEYMGMT_free(ctx->keymgmt);
531 EVP_PKEY_free(ctx->pkey);
532 EVP_PKEY_free(ctx->peerkey);
533 #ifndef OPENSSL_NO_ENGINE
534 ENGINE_finish(ctx->engine);
539 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
541 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
542 && ctx->op.sig.sigprovctx != NULL
543 && ctx->op.sig.signature != NULL
544 && ctx->op.sig.signature->get_ctx_params != NULL)
545 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
547 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
548 && ctx->op.ciph.ciphprovctx != NULL
549 && ctx->op.ciph.cipher != NULL
550 && ctx->op.ciph.cipher->get_ctx_params != NULL)
551 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
556 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
558 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
559 && ctx->op.sig.signature != NULL
560 && ctx->op.sig.signature->gettable_ctx_params != NULL)
561 return ctx->op.sig.signature->gettable_ctx_params();
563 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
564 && ctx->op.ciph.cipher != NULL
565 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
566 return ctx->op.ciph.cipher->gettable_ctx_params();
571 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
573 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
574 && ctx->op.kex.exchprovctx != NULL
575 && ctx->op.kex.exchange != NULL
576 && ctx->op.kex.exchange->set_ctx_params != NULL)
577 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
579 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
580 && ctx->op.sig.sigprovctx != NULL
581 && ctx->op.sig.signature != NULL
582 && ctx->op.sig.signature->set_ctx_params != NULL)
583 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
585 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
586 && ctx->op.ciph.ciphprovctx != NULL
587 && ctx->op.ciph.cipher != NULL
588 && ctx->op.ciph.cipher->set_ctx_params != NULL)
589 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
594 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
596 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
597 && ctx->op.kex.exchange != NULL
598 && ctx->op.kex.exchange->settable_ctx_params != NULL)
599 return ctx->op.kex.exchange->settable_ctx_params();
600 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
601 && ctx->op.sig.signature != NULL
602 && ctx->op.sig.signature->settable_ctx_params != NULL)
603 return ctx->op.sig.signature->settable_ctx_params();
604 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
605 && ctx->op.ciph.cipher != NULL
606 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
607 return ctx->op.ciph.cipher->settable_ctx_params();
612 #ifndef OPENSSL_NO_DH
613 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
615 OSSL_PARAM dh_pad_params[2];
616 unsigned int upad = pad;
618 /* We use EVP_PKEY_CTX_ctrl return values */
619 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
620 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
624 /* TODO(3.0): Remove this eventually when no more legacy */
625 if (ctx->op.kex.exchprovctx == NULL)
626 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
627 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
629 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
630 dh_pad_params[1] = OSSL_PARAM_construct_end();
632 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
636 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
638 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
639 /* 80 should be big enough */
643 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
644 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
645 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
649 /* TODO(3.0): Remove this eventually when no more legacy */
650 if (ctx->op.sig.sigprovctx == NULL)
651 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
652 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
654 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
657 *p++ = OSSL_PARAM_construct_end();
659 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
662 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
671 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
673 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
677 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
678 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
679 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
683 /* TODO(3.0): Remove this eventually when no more legacy */
684 if (ctx->op.sig.sigprovctx == NULL)
685 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
686 EVP_PKEY_CTRL_MD, 0, (void *)(md));
692 mdsize = EVP_MD_size(md);
693 name = EVP_MD_name(md);
696 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
698 * Cast away the const. This is read
699 * only so should be safe
703 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
705 *p++ = OSSL_PARAM_construct_end();
707 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
710 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
711 int cmd, int p1, void *p2)
714 #ifndef OPENSSL_NO_DH
715 case EVP_PKEY_CTRL_DH_PAD:
716 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
718 case EVP_PKEY_CTRL_MD:
719 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
720 case EVP_PKEY_CTRL_GET_MD:
721 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
722 case EVP_PKEY_CTRL_RSA_PADDING:
723 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
724 case EVP_PKEY_CTRL_GET_RSA_PADDING:
725 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
726 case EVP_PKEY_CTRL_RSA_OAEP_MD:
727 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
728 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
729 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
730 case EVP_PKEY_CTRL_RSA_MGF1_MD:
731 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
732 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
733 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
734 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
735 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
736 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
737 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
738 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
739 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
740 #ifndef OPENSSL_NO_CMS
741 case EVP_PKEY_CTRL_CMS_DECRYPT:
742 case EVP_PKEY_CTRL_CMS_ENCRYPT:
744 if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
746 ERR_raise(ERR_LIB_EVP,
747 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
753 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
754 int cmd, int p1, void *p2)
759 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
763 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
764 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
765 && ctx->op.sig.sigprovctx != NULL)
766 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
767 && ctx->op.ciph.ciphprovctx != NULL))
768 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
770 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
771 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
774 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
777 /* Skip the operation checks since this is called in a very early stage */
778 if (ctx->pmeth->digest_custom != NULL)
781 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
782 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
786 if ((optype != -1) && !(ctx->operation & optype)) {
787 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
792 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
795 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
800 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
801 int cmd, uint64_t value)
803 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
806 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
809 #ifndef OPENSSL_NO_DH
810 if (strcmp(name, "dh_pad") == 0) {
814 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
817 if (strcmp(name, "digest") == 0) {
821 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
823 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
827 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
828 EVP_MD_meth_free(md);
832 if (strcmp(name, "rsa_padding_mode") == 0) {
835 if (strcmp(value, "pkcs1") == 0) {
836 pm = RSA_PKCS1_PADDING;
837 } else if (strcmp(value, "sslv23") == 0) {
838 pm = RSA_SSLV23_PADDING;
839 } else if (strcmp(value, "none") == 0) {
841 } else if (strcmp(value, "oeap") == 0) {
842 pm = RSA_PKCS1_OAEP_PADDING;
843 } else if (strcmp(value, "oaep") == 0) {
844 pm = RSA_PKCS1_OAEP_PADDING;
845 } else if (strcmp(value, "x931") == 0) {
846 pm = RSA_X931_PADDING;
847 } else if (strcmp(value, "pss") == 0) {
848 pm = RSA_PKCS1_PSS_PADDING;
850 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
853 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
856 if (strcmp(name, "rsa_mgf1_md") == 0)
857 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, value, NULL);
859 if (strcmp(name, "rsa_oaep_md") == 0)
860 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, value, NULL);
862 if (strcmp(name, "rsa_oaep_label") == 0) {
867 lab = OPENSSL_hexstr2buf(value, &lablen);
870 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
881 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
882 const char *name, const char *value)
885 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
889 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
890 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
891 && ctx->op.sig.sigprovctx != NULL)
892 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
893 && ctx->op.ciph.ciphprovctx != NULL))
894 return legacy_ctrl_str_to_param(ctx, name, value);
896 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
897 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
900 if (strcmp(name, "digest") == 0)
901 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
903 return ctx->pmeth->ctrl_str(ctx, name, value);
906 /* Utility functions to send a string of hex string to a ctrl */
908 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
915 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
918 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
924 bin = OPENSSL_hexstr2buf(hex, &binlen);
927 if (binlen <= INT_MAX)
928 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
933 /* Pass a message digest to a ctrl */
934 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
938 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
939 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
942 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
945 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
947 return ctx->operation;
950 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
952 ctx->keygen_info = dat;
953 ctx->keygen_info_count = datlen;
956 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
961 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
966 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
971 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
976 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
978 ctx->app_data = data;
981 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
983 return ctx->app_data;
986 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
987 int (*init) (EVP_PKEY_CTX *ctx))
992 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
993 int (*copy) (EVP_PKEY_CTX *dst,
994 const EVP_PKEY_CTX *src))
999 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1000 void (*cleanup) (EVP_PKEY_CTX *ctx))
1002 pmeth->cleanup = cleanup;
1005 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1006 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1007 int (*paramgen) (EVP_PKEY_CTX *ctx,
1010 pmeth->paramgen_init = paramgen_init;
1011 pmeth->paramgen = paramgen;
1014 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1015 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1016 int (*keygen) (EVP_PKEY_CTX *ctx,
1019 pmeth->keygen_init = keygen_init;
1020 pmeth->keygen = keygen;
1023 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1024 int (*sign_init) (EVP_PKEY_CTX *ctx),
1025 int (*sign) (EVP_PKEY_CTX *ctx,
1026 unsigned char *sig, size_t *siglen,
1027 const unsigned char *tbs,
1030 pmeth->sign_init = sign_init;
1034 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1035 int (*verify_init) (EVP_PKEY_CTX *ctx),
1036 int (*verify) (EVP_PKEY_CTX *ctx,
1037 const unsigned char *sig,
1039 const unsigned char *tbs,
1042 pmeth->verify_init = verify_init;
1043 pmeth->verify = verify;
1046 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1047 int (*verify_recover_init) (EVP_PKEY_CTX
1049 int (*verify_recover) (EVP_PKEY_CTX
1058 pmeth->verify_recover_init = verify_recover_init;
1059 pmeth->verify_recover = verify_recover;
1062 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1063 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1065 int (*signctx) (EVP_PKEY_CTX *ctx,
1070 pmeth->signctx_init = signctx_init;
1071 pmeth->signctx = signctx;
1074 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1075 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1077 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1078 const unsigned char *sig,
1082 pmeth->verifyctx_init = verifyctx_init;
1083 pmeth->verifyctx = verifyctx;
1086 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1087 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1088 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1091 const unsigned char *in,
1094 pmeth->encrypt_init = encrypt_init;
1095 pmeth->encrypt = encryptfn;
1098 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1099 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1100 int (*decrypt) (EVP_PKEY_CTX *ctx,
1103 const unsigned char *in,
1106 pmeth->decrypt_init = decrypt_init;
1107 pmeth->decrypt = decrypt;
1110 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1111 int (*derive_init) (EVP_PKEY_CTX *ctx),
1112 int (*derive) (EVP_PKEY_CTX *ctx,
1116 pmeth->derive_init = derive_init;
1117 pmeth->derive = derive;
1120 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1121 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1123 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1128 pmeth->ctrl_str = ctrl_str;
1131 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1132 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1133 const unsigned char *tbs, size_t tbslen))
1135 pmeth->digestsign = digestsign;
1138 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1139 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1140 size_t siglen, const unsigned char *tbs,
1143 pmeth->digestverify = digestverify;
1146 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1147 int (*check) (EVP_PKEY *pkey))
1149 pmeth->check = check;
1152 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1153 int (*check) (EVP_PKEY *pkey))
1155 pmeth->public_check = check;
1158 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1159 int (*check) (EVP_PKEY *pkey))
1161 pmeth->param_check = check;
1164 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1165 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1168 pmeth->digest_custom = digest_custom;
1171 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1172 int (**pinit) (EVP_PKEY_CTX *ctx))
1174 *pinit = pmeth->init;
1177 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1178 int (**pcopy) (EVP_PKEY_CTX *dst,
1179 const EVP_PKEY_CTX *src))
1181 *pcopy = pmeth->copy;
1184 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1185 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1187 *pcleanup = pmeth->cleanup;
1190 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1191 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1192 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1196 *pparamgen_init = pmeth->paramgen_init;
1198 *pparamgen = pmeth->paramgen;
1201 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1202 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1203 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1207 *pkeygen_init = pmeth->keygen_init;
1209 *pkeygen = pmeth->keygen;
1212 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1213 int (**psign_init) (EVP_PKEY_CTX *ctx),
1214 int (**psign) (EVP_PKEY_CTX *ctx,
1215 unsigned char *sig, size_t *siglen,
1216 const unsigned char *tbs,
1220 *psign_init = pmeth->sign_init;
1222 *psign = pmeth->sign;
1225 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1226 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1227 int (**pverify) (EVP_PKEY_CTX *ctx,
1228 const unsigned char *sig,
1230 const unsigned char *tbs,
1234 *pverify_init = pmeth->verify_init;
1236 *pverify = pmeth->verify;
1239 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1240 int (**pverify_recover_init) (EVP_PKEY_CTX
1242 int (**pverify_recover) (EVP_PKEY_CTX
1251 if (pverify_recover_init)
1252 *pverify_recover_init = pmeth->verify_recover_init;
1253 if (pverify_recover)
1254 *pverify_recover = pmeth->verify_recover;
1257 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1258 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1260 int (**psignctx) (EVP_PKEY_CTX *ctx,
1266 *psignctx_init = pmeth->signctx_init;
1268 *psignctx = pmeth->signctx;
1271 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1272 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1274 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1275 const unsigned char *sig,
1279 if (pverifyctx_init)
1280 *pverifyctx_init = pmeth->verifyctx_init;
1282 *pverifyctx = pmeth->verifyctx;
1285 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1286 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1287 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1290 const unsigned char *in,
1294 *pencrypt_init = pmeth->encrypt_init;
1296 *pencryptfn = pmeth->encrypt;
1299 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1300 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1301 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1304 const unsigned char *in,
1308 *pdecrypt_init = pmeth->decrypt_init;
1310 *pdecrypt = pmeth->decrypt;
1313 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1314 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1315 int (**pderive) (EVP_PKEY_CTX *ctx,
1320 *pderive_init = pmeth->derive_init;
1322 *pderive = pmeth->derive;
1325 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1326 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1328 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1333 *pctrl = pmeth->ctrl;
1335 *pctrl_str = pmeth->ctrl_str;
1338 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1339 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1340 const unsigned char *tbs, size_t tbslen))
1343 *digestsign = pmeth->digestsign;
1346 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1347 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1348 size_t siglen, const unsigned char *tbs,
1352 *digestverify = pmeth->digestverify;
1355 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1356 int (**pcheck) (EVP_PKEY *pkey))
1359 *pcheck = pmeth->check;
1362 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1363 int (**pcheck) (EVP_PKEY *pkey))
1366 *pcheck = pmeth->public_check;
1369 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1370 int (**pcheck) (EVP_PKEY *pkey))
1373 *pcheck = pmeth->param_check;
1376 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1377 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1380 if (pdigest_custom != NULL)
1381 *pdigest_custom = pmeth->digest_custom;