2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Low level key APIs (DH etc) are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include <openssl/engine.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/core_names.h>
22 #include <openssl/dh.h>
23 #include <openssl/rsa.h>
24 #include <openssl/kdf.h>
25 #include "internal/cryptlib.h"
26 #include "crypto/asn1.h"
27 #include "crypto/evp.h"
28 #include "crypto/dh.h"
29 #include "internal/ffc.h"
30 #include "internal/numbers.h"
31 #include "internal/provider.h"
32 #include "evp_local.h"
36 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
37 int keytype, int optype,
38 int cmd, const char *name,
39 const void *data, size_t data_len);
40 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
41 int cmd, const char *name);
42 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx);
44 typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
45 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
47 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
49 /* This array needs to be in order of NIDs */
50 static pmeth_fn standard_methods[] = {
51 # ifndef OPENSSL_NO_RSA
54 # ifndef OPENSSL_NO_DH
57 # ifndef OPENSSL_NO_DSA
60 # ifndef OPENSSL_NO_EC
63 # ifndef OPENSSL_NO_RSA
66 # ifndef OPENSSL_NO_DH
69 # ifndef OPENSSL_NO_EC
73 # ifndef OPENSSL_NO_EC
77 # ifndef OPENSSL_NO_SM2
82 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
84 static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
86 return ((*a)->pkey_id - ((**b)())->pkey_id);
89 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
91 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
92 const EVP_PKEY_METHOD *const *b)
94 return ((*a)->pkey_id - (*b)->pkey_id);
97 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
101 const EVP_PKEY_METHOD *t = &tmp;
104 if (app_pkey_methods) {
106 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
108 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
110 ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
111 sizeof(standard_methods) /
113 if (ret == NULL || *ret == NULL)
118 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
120 EVP_PKEY_METHOD *pmeth;
122 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
124 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
129 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
133 /* Three possible states: */
134 # define EVP_PKEY_STATE_UNKNOWN 0
135 # define EVP_PKEY_STATE_LEGACY 1
136 # define EVP_PKEY_STATE_PROVIDER 2
138 static int evp_pkey_ctx_state(EVP_PKEY_CTX *ctx)
140 if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
141 return EVP_PKEY_STATE_UNKNOWN;
143 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
144 && ctx->op.kex.exchprovctx != NULL)
145 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
146 && ctx->op.sig.sigprovctx != NULL)
147 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
148 && ctx->op.ciph.ciphprovctx != NULL)
149 || (EVP_PKEY_CTX_IS_GEN_OP(ctx)
150 && ctx->op.keymgmt.genctx != NULL))
151 return EVP_PKEY_STATE_PROVIDER;
153 return EVP_PKEY_STATE_LEGACY;
156 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
161 if (*type == NID_undef)
162 *type = evp_pkey_name2type(keytype);
165 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
167 int type = NID_undef;
169 EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
173 #endif /* FIPS_MODULE */
175 static int is_legacy_alg(int id, const char *keytype)
178 /* Certain EVP_PKEY keytypes are only available in legacy form */
180 id = evp_pkey_name2type(keytype);
184 * TODO(3.0): Remove SM2 when they are converted to have provider
197 static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx,
198 EVP_PKEY *pkey, ENGINE *e,
199 const char *keytype, const char *propquery,
203 EVP_PKEY_CTX *ret = NULL;
204 const EVP_PKEY_METHOD *pmeth = NULL;
205 EVP_KEYMGMT *keymgmt = NULL;
208 * If the given |pkey| is provided, we extract the keytype from its
209 * keymgmt and skip over the legacy code.
211 if (pkey != NULL && evp_pkey_is_provided(pkey)) {
212 /* If we have an engine, something went wrong somewhere... */
213 if (!ossl_assert(e == NULL))
215 keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id);
221 * TODO(3.0) This legacy code section should be removed when we stop
228 else if (keytype != NULL)
229 id = evp_pkey_name2type(keytype);
233 /* If no ID was found here, we can only resort to find a keymgmt */
238 * Here, we extract what information we can for the purpose of
239 * supporting usage with implementations from providers, to make
240 * for a smooth transition from legacy stuff to provider based stuff.
242 * If an engine is given, this is entirely legacy, and we should not
243 * pretend anything else, so we only set the name when no engine is
244 * given. If both are already given, someone made a mistake, and
245 * since that can only happen internally, it's safe to make an
248 if (!ossl_assert(e == NULL || keytype == NULL))
251 keytype = OBJ_nid2sn(id);
253 # ifndef OPENSSL_NO_ENGINE
254 if (e == NULL && pkey != NULL)
255 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
256 /* Try to find an ENGINE which implements this method */
258 if (!ENGINE_init(e)) {
259 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
263 e = ENGINE_get_pkey_meth_engine(id);
267 * If an ENGINE handled this method look it up. Otherwise use internal
271 pmeth = ENGINE_get_pkey_meth(e, id);
274 pmeth = EVP_PKEY_meth_find(id);
277 #endif /* FIPS_MODULE */
280 * If there's no engine and there's a name, we try fetching a provider
283 if (e == NULL && keytype != NULL) {
284 int legacy = is_legacy_alg(id, keytype);
286 /* This could fail so ignore errors */
290 keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery);
293 else if (keymgmt == NULL)
294 return NULL; /* EVP_KEYMGMT_fetch() recorded an error */
298 * Chase down the legacy NID, as that might be needed for diverse
299 * purposes, such as ensure that EVP_PKEY_type() can return sensible
300 * values, or that there's a better chance to "downgrade" a key when
301 * needed. We go through all keymgmt names, because the keytype
302 * that's passed to this function doesn't necessarily translate
304 * TODO: Remove this when #legacy keys are gone.
306 if (keymgmt != NULL) {
307 int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt);
309 if (tmp_id != NID_undef) {
314 * It really really shouldn't differ. If it still does,
315 * something is very wrong.
317 if (!ossl_assert(id == tmp_id)) {
318 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_INTERNAL_ERROR);
319 EVP_KEYMGMT_free(keymgmt);
328 if (pmeth == NULL && keymgmt == NULL) {
329 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
331 ret = OPENSSL_zalloc(sizeof(*ret));
333 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
336 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
337 if ((ret == NULL || pmeth == NULL) && e != NULL)
342 EVP_KEYMGMT_free(keymgmt);
346 ret->libctx = libctx;
347 ret->propquery = propquery;
348 ret->keytype = keytype;
349 ret->keymgmt = keymgmt;
350 ret->legacy_keytype = id; /* TODO: Remove when #legacy key are gone */
353 ret->operation = EVP_PKEY_OP_UNDEFINED;
356 EVP_PKEY_up_ref(pkey);
358 if (pmeth != NULL && pmeth->init != NULL) {
359 if (pmeth->init(ret) <= 0) {
361 EVP_PKEY_CTX_free(ret);
369 /*- All methods below can also be used in FIPS_MODULE */
371 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx,
373 const char *propquery)
375 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
378 EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey,
379 const char *propquery)
381 return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1);
384 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
386 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
387 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
388 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
389 EVP_SIGNATURE_free(ctx->op.sig.signature);
390 ctx->op.sig.sigprovctx = NULL;
391 ctx->op.sig.signature = NULL;
392 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
393 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
394 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
395 EVP_KEYEXCH_free(ctx->op.kex.exchange);
396 ctx->op.kex.exchprovctx = NULL;
397 ctx->op.kex.exchange = NULL;
399 /* TODO(3.0): add dependancies and uncomment this when available for fips mode */
401 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
402 if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
403 ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
404 EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
405 ctx->op.ciph.ciphprovctx = NULL;
406 ctx->op.ciph.cipher = NULL;
407 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
408 if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL)
409 evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx);
414 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
418 if (ctx->pmeth && ctx->pmeth->cleanup)
419 ctx->pmeth->cleanup(ctx);
421 evp_pkey_ctx_free_old_ops(ctx);
423 evp_pkey_ctx_free_all_cached_data(ctx);
425 EVP_KEYMGMT_free(ctx->keymgmt);
427 EVP_PKEY_free(ctx->pkey);
428 EVP_PKEY_free(ctx->peerkey);
429 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
430 ENGINE_finish(ctx->engine);
437 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
438 const EVP_PKEY_METHOD *meth)
441 *ppkey_id = meth->pkey_id;
443 *pflags = meth->flags;
446 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
448 int pkey_id = dst->pkey_id;
449 int flags = dst->flags;
453 /* We only copy the function pointers so restore the other values */
454 dst->pkey_id = pkey_id;
458 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
460 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
464 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
466 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
469 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
471 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
474 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
478 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
479 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
480 && pctx->op.kex.exchprovctx == NULL)
481 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
482 && pctx->op.sig.sigprovctx == NULL)))
484 # ifndef OPENSSL_NO_ENGINE
485 /* Make sure it's safe to copy a pkey context using an ENGINE */
486 if (pctx->engine && !ENGINE_init(pctx->engine)) {
487 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
491 rctx = OPENSSL_zalloc(sizeof(*rctx));
493 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
497 if (pctx->pkey != NULL)
498 EVP_PKEY_up_ref(pctx->pkey);
499 rctx->pkey = pctx->pkey;
500 rctx->operation = pctx->operation;
501 rctx->libctx = pctx->libctx;
502 rctx->keytype = pctx->keytype;
503 rctx->propquery = pctx->propquery;
505 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
506 if (pctx->op.kex.exchange != NULL) {
507 rctx->op.kex.exchange = pctx->op.kex.exchange;
508 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
513 if (pctx->op.kex.exchprovctx != NULL) {
514 if (!ossl_assert(pctx->op.kex.exchange != NULL))
516 rctx->op.kex.exchprovctx
517 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
518 if (rctx->op.kex.exchprovctx == NULL) {
519 EVP_KEYEXCH_free(rctx->op.kex.exchange);
525 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
526 if (pctx->op.sig.signature != NULL) {
527 rctx->op.sig.signature = pctx->op.sig.signature;
528 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
533 if (pctx->op.sig.sigprovctx != NULL) {
534 if (!ossl_assert(pctx->op.sig.signature != NULL))
536 rctx->op.sig.sigprovctx
537 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
538 if (rctx->op.sig.sigprovctx == NULL) {
539 EVP_SIGNATURE_free(rctx->op.sig.signature);
545 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
546 if (pctx->op.ciph.cipher != NULL) {
547 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
548 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
553 if (pctx->op.ciph.ciphprovctx != NULL) {
554 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
556 rctx->op.ciph.ciphprovctx
557 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
558 if (rctx->op.ciph.ciphprovctx == NULL) {
559 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
567 rctx->pmeth = pctx->pmeth;
568 # ifndef OPENSSL_NO_ENGINE
569 rctx->engine = pctx->engine;
573 EVP_PKEY_up_ref(pctx->peerkey);
574 rctx->peerkey = pctx->peerkey;
576 if (pctx->pmeth->copy(rctx, pctx) > 0)
580 EVP_PKEY_CTX_free(rctx);
585 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
587 if (app_pkey_methods == NULL) {
588 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
589 if (app_pkey_methods == NULL){
590 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
594 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
595 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
598 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
602 void evp_app_cleanup_int(void)
604 if (app_pkey_methods != NULL)
605 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
608 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
610 const EVP_PKEY_METHOD *ret;
612 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
614 return ret == NULL ? 0 : 1;
617 size_t EVP_PKEY_meth_get_count(void)
619 size_t rv = OSSL_NELEM(standard_methods);
621 if (app_pkey_methods)
622 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
626 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
628 if (idx < OSSL_NELEM(standard_methods))
629 return (standard_methods[idx])();
630 if (app_pkey_methods == NULL)
632 idx -= OSSL_NELEM(standard_methods);
633 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
635 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
639 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
641 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
642 && ctx->op.kex.exchprovctx != NULL
643 && ctx->op.kex.exchange != NULL
644 && ctx->op.kex.exchange->set_ctx_params != NULL)
645 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
647 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
648 && ctx->op.sig.sigprovctx != NULL
649 && ctx->op.sig.signature != NULL
650 && ctx->op.sig.signature->set_ctx_params != NULL)
651 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
653 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
654 && ctx->op.ciph.ciphprovctx != NULL
655 && ctx->op.ciph.cipher != NULL
656 && ctx->op.ciph.cipher->set_ctx_params != NULL)
657 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
659 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
660 && ctx->op.keymgmt.genctx != NULL
661 && ctx->keymgmt != NULL
662 && ctx->keymgmt->gen_set_params != NULL)
663 return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx,
668 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
670 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
671 && ctx->op.kex.exchprovctx != NULL
672 && ctx->op.kex.exchange != NULL
673 && ctx->op.kex.exchange->get_ctx_params != NULL)
674 return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx,
676 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
677 && ctx->op.sig.sigprovctx != NULL
678 && ctx->op.sig.signature != NULL
679 && ctx->op.sig.signature->get_ctx_params != NULL)
680 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
682 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
683 && ctx->op.ciph.ciphprovctx != NULL
684 && ctx->op.ciph.cipher != NULL
685 && ctx->op.ciph.cipher->get_ctx_params != NULL)
686 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
692 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
696 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
697 && ctx->op.kex.exchange != NULL
698 && ctx->op.kex.exchange->gettable_ctx_params != NULL) {
699 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
700 return ctx->op.kex.exchange->gettable_ctx_params(provctx);
702 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
703 && ctx->op.sig.signature != NULL
704 && ctx->op.sig.signature->gettable_ctx_params != NULL) {
705 provctx = ossl_provider_ctx(
706 EVP_SIGNATURE_provider(ctx->op.sig.signature));
707 return ctx->op.sig.signature->gettable_ctx_params(provctx);
709 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
710 && ctx->op.ciph.cipher != NULL
711 && ctx->op.ciph.cipher->gettable_ctx_params != NULL) {
712 provctx = ossl_provider_ctx(
713 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
714 return ctx->op.ciph.cipher->gettable_ctx_params(provctx);
719 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
723 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
724 && ctx->op.kex.exchange != NULL
725 && ctx->op.kex.exchange->settable_ctx_params != NULL) {
726 provctx = ossl_provider_ctx(EVP_KEYEXCH_provider(ctx->op.kex.exchange));
727 return ctx->op.kex.exchange->settable_ctx_params(provctx);
729 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
730 && ctx->op.sig.signature != NULL
731 && ctx->op.sig.signature->settable_ctx_params != NULL) {
732 provctx = ossl_provider_ctx(
733 EVP_SIGNATURE_provider(ctx->op.sig.signature));
734 return ctx->op.sig.signature->settable_ctx_params(provctx);
736 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
737 && ctx->op.ciph.cipher != NULL
738 && ctx->op.ciph.cipher->settable_ctx_params != NULL) {
739 provctx = ossl_provider_ctx(
740 EVP_ASYM_CIPHER_provider(ctx->op.ciph.cipher));
741 return ctx->op.ciph.cipher->settable_ctx_params(provctx);
743 if (EVP_PKEY_CTX_IS_GEN_OP(ctx)
744 && ctx->keymgmt != NULL)
745 return EVP_KEYMGMT_gen_settable_params(ctx->keymgmt);
751 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
753 * Return 1 on success, 0 or negative for errors.
755 * In particular they return -2 if any of the params is not supported.
757 * They are not available in FIPS_MODULE as they depend on
758 * - EVP_PKEY_CTX_{get,set}_params()
759 * - EVP_PKEY_CTX_{gettable,settable}_params()
762 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
766 if (ctx == NULL || params == NULL)
769 for (p = params; p->key != NULL; p++) {
770 /* Check the ctx actually understands this parameter */
771 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
776 return EVP_PKEY_CTX_set_params(ctx, params);
779 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
783 if (ctx == NULL || params == NULL)
786 for (p = params; p->key != NULL; p++ ) {
787 /* Check the ctx actually understands this parameter */
788 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
793 return EVP_PKEY_CTX_get_params(ctx, params);
796 # ifndef OPENSSL_NO_DH
797 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
799 OSSL_PARAM dh_pad_params[2];
800 unsigned int upad = pad;
802 /* We use EVP_PKEY_CTX_ctrl return values */
803 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
804 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
808 /* TODO(3.0): Remove this eventually when no more legacy */
809 if (ctx->op.kex.exchprovctx == NULL)
810 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
811 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
813 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
814 dh_pad_params[1] = OSSL_PARAM_construct_end();
816 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
820 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
822 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
823 /* 80 should be big enough */
827 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
828 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
829 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
833 /* TODO(3.0): Remove this eventually when no more legacy */
834 if (ctx->op.sig.sigprovctx == NULL)
835 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
836 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
838 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
841 *p = OSSL_PARAM_construct_end();
843 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
846 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
855 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
856 int fallback, const char *param, int op,
859 OSSL_PARAM md_params[2], *p = md_params;
862 if (ctx == NULL || (ctx->operation & op) == 0) {
863 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
864 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
868 /* TODO(3.0): Remove this eventually when no more legacy */
870 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
875 name = EVP_MD_name(md);
878 *p++ = OSSL_PARAM_construct_utf8_string(param,
880 * Cast away the const. This is read
881 * only so should be safe
884 *p = OSSL_PARAM_construct_end();
886 return EVP_PKEY_CTX_set_params(ctx, md_params);
889 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
891 return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.sigprovctx == NULL,
892 OSSL_SIGNATURE_PARAM_DIGEST,
893 EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD);
896 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
898 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
899 OSSL_KDF_PARAM_DIGEST,
900 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD);
903 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
904 const char *param, int op, int ctrl,
905 const unsigned char *data,
908 OSSL_PARAM octet_string_params[2], *p = octet_string_params;
910 if (ctx == NULL || (ctx->operation & op) == 0) {
911 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
912 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
916 /* TODO(3.0): Remove this eventually when no more legacy */
918 return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data));
921 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
925 *p++ = OSSL_PARAM_construct_octet_string(param,
927 * Cast away the const. This is read
928 * only so should be safe
930 (unsigned char *)data,
932 *p = OSSL_PARAM_construct_end();
934 return EVP_PKEY_CTX_set_params(ctx, octet_string_params);
937 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx,
938 const unsigned char *sec, int seclen)
940 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
941 OSSL_KDF_PARAM_SECRET,
943 EVP_PKEY_CTRL_TLS_SECRET,
947 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx,
948 const unsigned char *seed, int seedlen)
950 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
953 EVP_PKEY_CTRL_TLS_SEED,
957 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
959 return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.exchprovctx == NULL,
960 OSSL_KDF_PARAM_DIGEST,
961 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD);
964 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx,
965 const unsigned char *salt, int saltlen)
967 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
970 EVP_PKEY_CTRL_HKDF_SALT,
974 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx,
975 const unsigned char *key, int keylen)
977 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
980 EVP_PKEY_CTRL_HKDF_KEY,
984 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx,
985 const unsigned char *info, int infolen)
987 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
990 EVP_PKEY_CTRL_HKDF_INFO,
994 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode)
996 OSSL_PARAM int_params[2], *p = int_params;
998 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
999 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1000 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1004 /* TODO(3.0): Remove this eventually when no more legacy */
1005 if (ctx->op.kex.exchprovctx == NULL)
1006 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE,
1007 EVP_PKEY_CTRL_HKDF_MODE, mode, NULL);
1011 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
1015 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
1016 *p = OSSL_PARAM_construct_end();
1018 return EVP_PKEY_CTX_set_params(ctx, int_params);
1021 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass,
1024 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1025 OSSL_KDF_PARAM_PASSWORD,
1028 (const unsigned char *)pass, passlen);
1031 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx,
1032 const unsigned char *salt, int saltlen)
1034 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.exchprovctx == NULL,
1035 OSSL_KDF_PARAM_SALT,
1037 EVP_PKEY_CTRL_SCRYPT_SALT,
1041 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param,
1042 int op, int ctrl, uint64_t val)
1044 OSSL_PARAM uint64_params[2], *p = uint64_params;
1046 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
1047 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1048 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1052 /* TODO(3.0): Remove this eventually when no more legacy */
1053 if (ctx->op.kex.exchprovctx == NULL)
1054 return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val);
1056 *p++ = OSSL_PARAM_construct_uint64(param, &val);
1057 *p = OSSL_PARAM_construct_end();
1059 return EVP_PKEY_CTX_set_params(ctx, uint64_params);
1062 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n)
1064 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N,
1065 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N,
1069 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r)
1071 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R,
1072 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R,
1076 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p)
1078 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P,
1079 EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P,
1083 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx,
1084 uint64_t maxmem_bytes)
1086 return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM,
1088 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
1092 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key,
1095 return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL,
1096 OSSL_PKEY_PARAM_PRIV_KEY,
1098 EVP_PKEY_CTRL_SET_MAC_KEY,
1102 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
1103 int cmd, int p1, void *p2)
1106 case EVP_PKEY_CTRL_SET1_ID:
1107 return evp_pkey_ctx_set1_id_prov(ctx, p2, p1);
1108 case EVP_PKEY_CTRL_GET1_ID:
1109 return evp_pkey_ctx_get1_id_prov(ctx, p2);
1110 case EVP_PKEY_CTRL_GET1_ID_LEN:
1111 return evp_pkey_ctx_get1_id_len_prov(ctx, p2);
1114 # ifndef OPENSSL_NO_DH
1115 if (keytype == EVP_PKEY_DHX) {
1117 case EVP_PKEY_CTRL_DH_KDF_TYPE:
1118 return EVP_PKEY_CTX_set_dh_kdf_type(ctx, p1);
1119 case EVP_PKEY_CTRL_DH_KDF_MD:
1120 return EVP_PKEY_CTX_set_dh_kdf_md(ctx, p2);
1121 case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
1122 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, p1);
1123 case EVP_PKEY_CTRL_DH_KDF_UKM:
1124 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p2, p1);
1125 case EVP_PKEY_CTRL_DH_KDF_OID:
1126 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, p2);
1127 case EVP_PKEY_CTRL_GET_DH_KDF_MD:
1128 return EVP_PKEY_CTX_get_dh_kdf_md(ctx, p2);
1129 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
1130 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, p2);
1131 case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
1132 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p2);
1133 case EVP_PKEY_CTRL_GET_DH_KDF_OID:
1134 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, p2);
1137 if (keytype == EVP_PKEY_DH) {
1139 case EVP_PKEY_CTRL_DH_PAD:
1140 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
1141 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
1142 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1);
1143 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
1144 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1);
1145 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
1146 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1);
1147 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
1148 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1);
1149 case EVP_PKEY_CTRL_DH_RFC5114:
1150 return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1);
1154 # ifndef OPENSSL_NO_DSA
1155 if (keytype == EVP_PKEY_DSA) {
1157 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
1158 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1);
1159 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
1160 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1);
1161 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
1162 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2);
1166 # ifndef OPENSSL_NO_EC
1167 if (keytype == EVP_PKEY_EC) {
1169 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
1170 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1);
1171 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
1173 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
1174 } else if (p1 < -1 || p1 > 1) {
1175 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1178 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
1180 case EVP_PKEY_CTRL_EC_KDF_TYPE:
1182 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
1184 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
1186 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
1187 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
1188 case EVP_PKEY_CTRL_EC_KDF_MD:
1189 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
1190 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
1191 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
1192 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
1193 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
1194 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
1195 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
1196 case EVP_PKEY_CTRL_EC_KDF_UKM:
1197 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
1201 if (keytype == EVP_PKEY_RSA) {
1203 case EVP_PKEY_CTRL_RSA_OAEP_MD:
1204 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1205 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
1206 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1207 case EVP_PKEY_CTRL_RSA_MGF1_MD:
1208 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
1209 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
1210 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
1211 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
1212 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
1213 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
1214 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1);
1215 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
1216 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2);
1217 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
1218 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1);
1222 * keytype == -1 is used when several key types share the same structure,
1223 * or for generic controls that are the same across multiple key types.
1225 if (keytype == -1) {
1226 if (optype == EVP_PKEY_OP_DERIVE) {
1229 case EVP_PKEY_CTRL_TLS_MD:
1230 return EVP_PKEY_CTX_set_tls1_prf_md(ctx, p2);
1231 case EVP_PKEY_CTRL_TLS_SECRET:
1232 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx, p2, p1);
1233 case EVP_PKEY_CTRL_TLS_SEED:
1234 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx, p2, p1);
1237 case EVP_PKEY_CTRL_HKDF_MD:
1238 return EVP_PKEY_CTX_set_hkdf_md(ctx, p2);
1239 case EVP_PKEY_CTRL_HKDF_SALT :
1240 return EVP_PKEY_CTX_set1_hkdf_salt(ctx, p2, p1);
1241 case EVP_PKEY_CTRL_HKDF_KEY:
1242 return EVP_PKEY_CTX_set1_hkdf_key(ctx, p2, p1);
1243 case EVP_PKEY_CTRL_HKDF_INFO:
1244 return EVP_PKEY_CTX_add1_hkdf_info(ctx, p2, p1);
1245 case EVP_PKEY_CTRL_HKDF_MODE:
1246 return EVP_PKEY_CTX_hkdf_mode(ctx, p1);
1249 case EVP_PKEY_CTRL_PASS:
1250 return EVP_PKEY_CTX_set1_pbe_pass(ctx, p2, p1);
1251 case EVP_PKEY_CTRL_SCRYPT_SALT:
1252 return EVP_PKEY_CTX_set1_scrypt_salt(ctx, p2, p1);
1253 case EVP_PKEY_CTRL_SCRYPT_N:
1254 return EVP_PKEY_CTX_set_scrypt_N(ctx, p1);
1255 case EVP_PKEY_CTRL_SCRYPT_R:
1256 return EVP_PKEY_CTX_set_scrypt_r(ctx, p1);
1257 case EVP_PKEY_CTRL_SCRYPT_P:
1258 return EVP_PKEY_CTX_set_scrypt_p(ctx, p1);
1259 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
1260 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx, p1);
1262 } else if (optype == EVP_PKEY_OP_KEYGEN) {
1263 OSSL_PARAM params[2], *p = params;
1266 case EVP_PKEY_CTRL_CIPHER:
1268 char *ciphname = (char *)EVP_CIPHER_name(p2);
1270 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
1272 *p = OSSL_PARAM_construct_end();
1274 return EVP_PKEY_CTX_set_params(ctx, params);
1276 case EVP_PKEY_CTRL_SET_MAC_KEY:
1278 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
1280 *p = OSSL_PARAM_construct_end();
1282 return EVP_PKEY_CTX_set_params(ctx, params);
1287 case EVP_PKEY_CTRL_MD:
1288 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
1289 case EVP_PKEY_CTRL_GET_MD:
1290 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
1291 case EVP_PKEY_CTRL_RSA_PADDING:
1292 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
1293 case EVP_PKEY_CTRL_GET_RSA_PADDING:
1294 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
1295 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
1296 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
1297 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
1298 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1);
1299 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
1300 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2);
1301 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
1302 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
1303 # ifndef OPENSSL_NO_CMS
1304 case EVP_PKEY_CTRL_CMS_DECRYPT:
1305 case EVP_PKEY_CTRL_CMS_ENCRYPT:
1307 /* TODO (3.0) Temporary hack, this should probe */
1308 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS"))
1310 ERR_raise(ERR_LIB_EVP,
1311 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1317 * GOST CMS format is different for different cipher algorithms.
1318 * Most of other algorithms don't have such a difference
1319 * so this ctrl is just ignored.
1321 if (cmd == EVP_PKEY_CTRL_CIPHER)
1327 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype,
1328 int cmd, int p1, void *p2)
1333 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED);
1338 * If the method has a |digest_custom| function, we can relax the
1339 * operation type check, since this can be called before the operation
1342 if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) {
1343 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
1344 EVPerr(0, EVP_R_NO_OPERATION_SET);
1348 if ((optype != -1) && !(ctx->operation & optype)) {
1349 EVPerr(0, EVP_R_INVALID_OPERATION);
1354 switch (evp_pkey_ctx_state(ctx)) {
1355 case EVP_PKEY_STATE_PROVIDER:
1356 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
1357 case EVP_PKEY_STATE_UNKNOWN:
1358 case EVP_PKEY_STATE_LEGACY:
1359 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
1360 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED);
1363 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
1366 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
1369 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED);
1375 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
1376 int cmd, int p1, void *p2)
1380 /* If unsupported, we don't want that reported here */
1382 ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype,
1387 ERR_clear_last_mark();
1389 * If there was an error, there was an error.
1390 * If the operation isn't initialized yet, we also return, as
1391 * the saved values will be used then anyway.
1393 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1397 return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2);
1400 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
1401 int cmd, uint64_t value)
1403 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
1406 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
1409 if (strcmp(name, "md") == 0)
1410 name = OSSL_ALG_PARAM_DIGEST;
1411 else if (strcmp(name, "rsa_padding_mode") == 0)
1412 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
1413 else if (strcmp(name, "rsa_mgf1_md") == 0)
1414 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
1415 else if (strcmp(name, "rsa_oaep_md") == 0)
1416 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
1417 else if (strcmp(name, "rsa_oaep_label") == 0)
1418 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
1419 else if (strcmp(name, "rsa_pss_saltlen") == 0)
1420 name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN;
1421 else if (strcmp(name, "rsa_keygen_bits") == 0)
1422 name = OSSL_PKEY_PARAM_RSA_BITS;
1423 else if (strcmp(name, "rsa_keygen_pubexp") == 0)
1424 name = OSSL_PKEY_PARAM_RSA_E;
1425 else if (strcmp(name, "rsa_keygen_primes") == 0)
1426 name = OSSL_PKEY_PARAM_RSA_PRIMES;
1427 else if (strcmp(name, "rsa_pss_keygen_md") == 0)
1428 name = OSSL_PKEY_PARAM_RSA_DIGEST;
1429 else if (strcmp(name, "rsa_pss_keygen_mgf1_md") == 0)
1430 name = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
1431 else if (strcmp(name, "rsa_pss_keygen_saltlen") == 0)
1432 name = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
1433 # ifndef OPENSSL_NO_DSA
1434 else if (strcmp(name, "dsa_paramgen_bits") == 0)
1435 name = OSSL_PKEY_PARAM_FFC_PBITS;
1436 else if (strcmp(name, "dsa_paramgen_q_bits") == 0)
1437 name = OSSL_PKEY_PARAM_FFC_QBITS;
1438 else if (strcmp(name, "dsa_paramgen_md") == 0)
1439 name = OSSL_PKEY_PARAM_FFC_DIGEST;
1441 # ifndef OPENSSL_NO_DH
1442 else if (strcmp(name, "dh_paramgen_generator") == 0)
1443 name = OSSL_PKEY_PARAM_DH_GENERATOR;
1444 else if (strcmp(name, "dh_paramgen_prime_len") == 0)
1445 name = OSSL_PKEY_PARAM_FFC_PBITS;
1446 else if (strcmp(name, "dh_paramgen_subprime_len") == 0)
1447 name = OSSL_PKEY_PARAM_FFC_QBITS;
1448 else if (strcmp(name, "dh_paramgen_type") == 0) {
1449 name = OSSL_PKEY_PARAM_FFC_TYPE;
1450 value = dh_gen_type_id2name(atoi(value));
1451 } else if (strcmp(name, "dh_param") == 0)
1452 name = OSSL_PKEY_PARAM_GROUP_NAME;
1453 else if (strcmp(name, "dh_rfc5114") == 0) {
1454 name = OSSL_PKEY_PARAM_GROUP_NAME;
1455 value = ffc_named_group_from_uid(atoi(value));
1456 } else if (strcmp(name, "dh_pad") == 0)
1457 name = OSSL_EXCHANGE_PARAM_PAD;
1459 # ifndef OPENSSL_NO_EC
1460 else if (strcmp(name, "ec_paramgen_curve") == 0)
1461 name = OSSL_PKEY_PARAM_GROUP_NAME;
1462 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
1463 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
1464 else if (strcmp(name, "ecdh_kdf_md") == 0)
1465 name = OSSL_EXCHANGE_PARAM_KDF_DIGEST;
1466 else if (strcmp(name, "ec_param_enc") == 0)
1467 name = OSSL_PKEY_PARAM_EC_ENCODING;
1469 else if (strcmp(name, "N") == 0)
1470 name = OSSL_KDF_PARAM_SCRYPT_N;
1474 * TODO(3.0) reduce the code above to only translate known legacy
1475 * string to the corresponding core name (see core_names.h), but
1476 * otherwise leave it to this code block to do the actual work.
1478 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
1479 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1483 if (!OSSL_PARAM_allocate_from_text(¶ms[0], settable, name, value,
1484 strlen(value), &exists)) {
1486 ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
1487 "name=%s,value=%s", name, value);
1492 if (EVP_PKEY_CTX_set_params(ctx, params))
1494 OPENSSL_free(params[0].data);
1499 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx,
1500 const char *name, const char *value)
1505 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED);
1509 switch (evp_pkey_ctx_state(ctx)) {
1510 case EVP_PKEY_STATE_PROVIDER:
1511 return legacy_ctrl_str_to_param(ctx, name, value);
1512 case EVP_PKEY_STATE_UNKNOWN:
1513 case EVP_PKEY_STATE_LEGACY:
1514 if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) {
1515 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED);
1518 if (strcmp(name, "digest") == 0)
1519 ret = EVP_PKEY_CTX_md(ctx,
1520 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1521 EVP_PKEY_CTRL_MD, value);
1523 ret = ctx->pmeth->ctrl_str(ctx, name, value);
1530 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
1531 const char *name, const char *value)
1535 /* If unsupported, we don't want that reported here */
1537 ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1,
1538 name, value, strlen(value) + 1);
1542 ERR_clear_last_mark();
1544 * If there was an error, there was an error.
1545 * If the operation isn't initialized yet, we also return, as
1546 * the saved values will be used then anyway.
1548 if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED)
1552 return evp_pkey_ctx_ctrl_str_int(ctx, name, value);
1555 static int decode_cmd(int cmd, const char *name)
1559 * The consequence of the assertion not being true is that this
1560 * function will return -1, which will cause the calling functions
1561 * to signal that the command is unsupported... in non-debug mode.
1563 if (ossl_assert(name != NULL))
1564 if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0)
1565 cmd = EVP_PKEY_CTRL_SET1_ID;
1571 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
1572 int keytype, int optype,
1573 int cmd, const char *name,
1574 const void *data, size_t data_len)
1576 if ((keytype != -1 && ctx->pmeth->pkey_id != keytype)
1577 || ((optype != -1) && !(ctx->operation & optype))) {
1578 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
1582 cmd = decode_cmd(cmd, name);
1584 case EVP_PKEY_CTRL_SET1_ID:
1585 evp_pkey_ctx_free_cached_data(ctx, cmd, name);
1587 ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name);
1588 if (ctx->cached_parameters.dist_id_name == NULL) {
1589 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1594 ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
1595 if (ctx->cached_parameters.dist_id == NULL) {
1596 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1600 ctx->cached_parameters.dist_id_set = 1;
1601 ctx->cached_parameters.dist_id_len = data_len;
1605 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1609 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx,
1610 int cmd, const char *name)
1612 cmd = decode_cmd(cmd, name);
1614 case EVP_PKEY_CTRL_SET1_ID:
1615 OPENSSL_free(ctx->cached_parameters.dist_id);
1616 OPENSSL_free(ctx->cached_parameters.dist_id_name);
1617 ctx->cached_parameters.dist_id = NULL;
1618 ctx->cached_parameters.dist_id_name = NULL;
1623 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx)
1625 evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL);
1628 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx)
1632 if (ret && ctx->cached_parameters.dist_id_set) {
1633 const char *name = ctx->cached_parameters.dist_id_name;
1634 const void *val = ctx->cached_parameters.dist_id;
1635 size_t len = ctx->cached_parameters.dist_id_len;
1638 ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val);
1640 ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation,
1641 EVP_PKEY_CTRL_SET1_ID,
1642 (int)len, (void *)val);
1648 /* Utility functions to send a string of hex string to a ctrl */
1650 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
1657 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
1660 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
1666 bin = OPENSSL_hexstr2buf(hex, &binlen);
1669 if (binlen <= INT_MAX)
1670 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
1675 /* Pass a message digest to a ctrl */
1676 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1680 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1681 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1684 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1687 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1689 return ctx->operation;
1692 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1694 ctx->keygen_info = dat;
1695 ctx->keygen_info_count = datlen;
1698 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1703 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1708 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1713 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1715 return ctx->peerkey;
1718 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1720 ctx->app_data = data;
1723 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1725 return ctx->app_data;
1728 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1729 int (*init) (EVP_PKEY_CTX *ctx))
1734 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1735 int (*copy) (EVP_PKEY_CTX *dst,
1736 const EVP_PKEY_CTX *src))
1741 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1742 void (*cleanup) (EVP_PKEY_CTX *ctx))
1744 pmeth->cleanup = cleanup;
1747 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1748 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1749 int (*paramgen) (EVP_PKEY_CTX *ctx,
1752 pmeth->paramgen_init = paramgen_init;
1753 pmeth->paramgen = paramgen;
1756 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1757 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1758 int (*keygen) (EVP_PKEY_CTX *ctx,
1761 pmeth->keygen_init = keygen_init;
1762 pmeth->keygen = keygen;
1765 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1766 int (*sign_init) (EVP_PKEY_CTX *ctx),
1767 int (*sign) (EVP_PKEY_CTX *ctx,
1768 unsigned char *sig, size_t *siglen,
1769 const unsigned char *tbs,
1772 pmeth->sign_init = sign_init;
1776 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1777 int (*verify_init) (EVP_PKEY_CTX *ctx),
1778 int (*verify) (EVP_PKEY_CTX *ctx,
1779 const unsigned char *sig,
1781 const unsigned char *tbs,
1784 pmeth->verify_init = verify_init;
1785 pmeth->verify = verify;
1788 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1789 int (*verify_recover_init) (EVP_PKEY_CTX
1791 int (*verify_recover) (EVP_PKEY_CTX
1800 pmeth->verify_recover_init = verify_recover_init;
1801 pmeth->verify_recover = verify_recover;
1804 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1805 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1807 int (*signctx) (EVP_PKEY_CTX *ctx,
1812 pmeth->signctx_init = signctx_init;
1813 pmeth->signctx = signctx;
1816 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1817 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1819 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1820 const unsigned char *sig,
1824 pmeth->verifyctx_init = verifyctx_init;
1825 pmeth->verifyctx = verifyctx;
1828 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1829 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1830 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1833 const unsigned char *in,
1836 pmeth->encrypt_init = encrypt_init;
1837 pmeth->encrypt = encryptfn;
1840 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1841 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1842 int (*decrypt) (EVP_PKEY_CTX *ctx,
1845 const unsigned char *in,
1848 pmeth->decrypt_init = decrypt_init;
1849 pmeth->decrypt = decrypt;
1852 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1853 int (*derive_init) (EVP_PKEY_CTX *ctx),
1854 int (*derive) (EVP_PKEY_CTX *ctx,
1858 pmeth->derive_init = derive_init;
1859 pmeth->derive = derive;
1862 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1863 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1865 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1870 pmeth->ctrl_str = ctrl_str;
1873 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1874 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1875 const unsigned char *tbs, size_t tbslen))
1877 pmeth->digestsign = digestsign;
1880 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1881 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1882 size_t siglen, const unsigned char *tbs,
1885 pmeth->digestverify = digestverify;
1888 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1889 int (*check) (EVP_PKEY *pkey))
1891 pmeth->check = check;
1894 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1895 int (*check) (EVP_PKEY *pkey))
1897 pmeth->public_check = check;
1900 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1901 int (*check) (EVP_PKEY *pkey))
1903 pmeth->param_check = check;
1906 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1907 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1910 pmeth->digest_custom = digest_custom;
1913 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1914 int (**pinit) (EVP_PKEY_CTX *ctx))
1916 *pinit = pmeth->init;
1919 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1920 int (**pcopy) (EVP_PKEY_CTX *dst,
1921 const EVP_PKEY_CTX *src))
1923 *pcopy = pmeth->copy;
1926 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1927 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1929 *pcleanup = pmeth->cleanup;
1932 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1933 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1934 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1938 *pparamgen_init = pmeth->paramgen_init;
1940 *pparamgen = pmeth->paramgen;
1943 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1944 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1945 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1949 *pkeygen_init = pmeth->keygen_init;
1951 *pkeygen = pmeth->keygen;
1954 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1955 int (**psign_init) (EVP_PKEY_CTX *ctx),
1956 int (**psign) (EVP_PKEY_CTX *ctx,
1957 unsigned char *sig, size_t *siglen,
1958 const unsigned char *tbs,
1962 *psign_init = pmeth->sign_init;
1964 *psign = pmeth->sign;
1967 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1968 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1969 int (**pverify) (EVP_PKEY_CTX *ctx,
1970 const unsigned char *sig,
1972 const unsigned char *tbs,
1976 *pverify_init = pmeth->verify_init;
1978 *pverify = pmeth->verify;
1981 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1982 int (**pverify_recover_init) (EVP_PKEY_CTX
1984 int (**pverify_recover) (EVP_PKEY_CTX
1993 if (pverify_recover_init)
1994 *pverify_recover_init = pmeth->verify_recover_init;
1995 if (pverify_recover)
1996 *pverify_recover = pmeth->verify_recover;
1999 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
2000 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
2002 int (**psignctx) (EVP_PKEY_CTX *ctx,
2008 *psignctx_init = pmeth->signctx_init;
2010 *psignctx = pmeth->signctx;
2013 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
2014 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
2016 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
2017 const unsigned char *sig,
2021 if (pverifyctx_init)
2022 *pverifyctx_init = pmeth->verifyctx_init;
2024 *pverifyctx = pmeth->verifyctx;
2027 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
2028 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
2029 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
2032 const unsigned char *in,
2036 *pencrypt_init = pmeth->encrypt_init;
2038 *pencryptfn = pmeth->encrypt;
2041 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
2042 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
2043 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
2046 const unsigned char *in,
2050 *pdecrypt_init = pmeth->decrypt_init;
2052 *pdecrypt = pmeth->decrypt;
2055 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
2056 int (**pderive_init) (EVP_PKEY_CTX *ctx),
2057 int (**pderive) (EVP_PKEY_CTX *ctx,
2062 *pderive_init = pmeth->derive_init;
2064 *pderive = pmeth->derive;
2067 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
2068 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
2070 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
2075 *pctrl = pmeth->ctrl;
2077 *pctrl_str = pmeth->ctrl_str;
2080 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
2081 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
2082 const unsigned char *tbs, size_t tbslen))
2085 *digestsign = pmeth->digestsign;
2088 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
2089 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
2090 size_t siglen, const unsigned char *tbs,
2094 *digestverify = pmeth->digestverify;
2097 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
2098 int (**pcheck) (EVP_PKEY *pkey))
2101 *pcheck = pmeth->check;
2104 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
2105 int (**pcheck) (EVP_PKEY *pkey))
2108 *pcheck = pmeth->public_check;
2111 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
2112 int (**pcheck) (EVP_PKEY *pkey))
2115 *pcheck = pmeth->param_check;
2118 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
2119 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
2122 if (pdigest_custom != NULL)
2123 *pdigest_custom = pmeth->digest_custom;
2126 #endif /* FIPS_MODULE */