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
, EVP_PKEY_OP_TYPE_SIG
,
1520 EVP_PKEY_CTRL_MD
, value
);
1522 ret
= ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
1529 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
1530 const char *name
, const char *value
)
1534 /* If unsupported, we don't want that reported here */
1536 ret
= evp_pkey_ctx_store_cached_data(ctx
, -1, -1, -1,
1537 name
, value
, strlen(value
) + 1);
1541 ERR_clear_last_mark();
1543 * If there was an error, there was an error.
1544 * If the operation isn't initialized yet, we also return, as
1545 * the saved values will be used then anyway.
1547 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1551 return evp_pkey_ctx_ctrl_str_int(ctx
, name
, value
);
1554 static int decode_cmd(int cmd
, const char *name
)
1558 * The consequence of the assertion not being true is that this
1559 * function will return -1, which will cause the calling functions
1560 * to signal that the command is unsupported... in non-debug mode.
1562 if (ossl_assert(name
!= NULL
))
1563 if (strcmp(name
, "distid") == 0 || strcmp(name
, "hexdistid") == 0)
1564 cmd
= EVP_PKEY_CTRL_SET1_ID
;
1570 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
1571 int keytype
, int optype
,
1572 int cmd
, const char *name
,
1573 const void *data
, size_t data_len
)
1575 if ((keytype
!= -1 && ctx
->pmeth
->pkey_id
!= keytype
)
1576 || ((optype
!= -1) && !(ctx
->operation
& optype
))) {
1577 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1581 cmd
= decode_cmd(cmd
, name
);
1583 case EVP_PKEY_CTRL_SET1_ID
:
1584 evp_pkey_ctx_free_cached_data(ctx
, cmd
, name
);
1586 ctx
->cached_parameters
.dist_id_name
= OPENSSL_strdup(name
);
1587 if (ctx
->cached_parameters
.dist_id_name
== NULL
) {
1588 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1593 ctx
->cached_parameters
.dist_id
= OPENSSL_memdup(data
, data_len
);
1594 if (ctx
->cached_parameters
.dist_id
== NULL
) {
1595 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1599 ctx
->cached_parameters
.dist_id_set
= 1;
1600 ctx
->cached_parameters
.dist_id_len
= data_len
;
1604 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1608 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
1609 int cmd
, const char *name
)
1611 cmd
= decode_cmd(cmd
, name
);
1613 case EVP_PKEY_CTRL_SET1_ID
:
1614 OPENSSL_free(ctx
->cached_parameters
.dist_id
);
1615 OPENSSL_free(ctx
->cached_parameters
.dist_id_name
);
1616 ctx
->cached_parameters
.dist_id
= NULL
;
1617 ctx
->cached_parameters
.dist_id_name
= NULL
;
1622 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
)
1624 evp_pkey_ctx_free_cached_data(ctx
, EVP_PKEY_CTRL_SET1_ID
, NULL
);
1627 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
)
1631 if (ret
&& ctx
->cached_parameters
.dist_id_set
) {
1632 const char *name
= ctx
->cached_parameters
.dist_id_name
;
1633 const void *val
= ctx
->cached_parameters
.dist_id
;
1634 size_t len
= ctx
->cached_parameters
.dist_id_len
;
1637 ret
= evp_pkey_ctx_ctrl_str_int(ctx
, name
, val
);
1639 ret
= evp_pkey_ctx_ctrl_int(ctx
, -1, ctx
->operation
,
1640 EVP_PKEY_CTRL_SET1_ID
,
1641 (int)len
, (void *)val
);
1647 /* Utility functions to send a string of hex string to a ctrl */
1649 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
1656 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
1659 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
1665 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1668 if (binlen
<= INT_MAX
)
1669 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
1674 /* Pass a message digest to a ctrl */
1675 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
1679 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
1680 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
1683 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
1686 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
1688 return ctx
->operation
;
1691 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
1693 ctx
->keygen_info
= dat
;
1694 ctx
->keygen_info_count
= datlen
;
1697 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
1702 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
1707 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
1712 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
1714 return ctx
->peerkey
;
1717 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
1719 ctx
->app_data
= data
;
1722 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
1724 return ctx
->app_data
;
1727 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
1728 int (*init
) (EVP_PKEY_CTX
*ctx
))
1733 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
1734 int (*copy
) (EVP_PKEY_CTX
*dst
,
1735 const EVP_PKEY_CTX
*src
))
1740 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
1741 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
1743 pmeth
->cleanup
= cleanup
;
1746 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
1747 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
1748 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
1751 pmeth
->paramgen_init
= paramgen_init
;
1752 pmeth
->paramgen
= paramgen
;
1755 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
1756 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
1757 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
1760 pmeth
->keygen_init
= keygen_init
;
1761 pmeth
->keygen
= keygen
;
1764 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
1765 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
1766 int (*sign
) (EVP_PKEY_CTX
*ctx
,
1767 unsigned char *sig
, size_t *siglen
,
1768 const unsigned char *tbs
,
1771 pmeth
->sign_init
= sign_init
;
1775 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
1776 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
1777 int (*verify
) (EVP_PKEY_CTX
*ctx
,
1778 const unsigned char *sig
,
1780 const unsigned char *tbs
,
1783 pmeth
->verify_init
= verify_init
;
1784 pmeth
->verify
= verify
;
1787 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
1788 int (*verify_recover_init
) (EVP_PKEY_CTX
1790 int (*verify_recover
) (EVP_PKEY_CTX
1799 pmeth
->verify_recover_init
= verify_recover_init
;
1800 pmeth
->verify_recover
= verify_recover
;
1803 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
1804 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
1806 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
1811 pmeth
->signctx_init
= signctx_init
;
1812 pmeth
->signctx
= signctx
;
1815 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
1816 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1818 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
1819 const unsigned char *sig
,
1823 pmeth
->verifyctx_init
= verifyctx_init
;
1824 pmeth
->verifyctx
= verifyctx
;
1827 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
1828 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
1829 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
1832 const unsigned char *in
,
1835 pmeth
->encrypt_init
= encrypt_init
;
1836 pmeth
->encrypt
= encryptfn
;
1839 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
1840 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
1841 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
1844 const unsigned char *in
,
1847 pmeth
->decrypt_init
= decrypt_init
;
1848 pmeth
->decrypt
= decrypt
;
1851 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
1852 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
1853 int (*derive
) (EVP_PKEY_CTX
*ctx
,
1857 pmeth
->derive_init
= derive_init
;
1858 pmeth
->derive
= derive
;
1861 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
1862 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1864 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
1869 pmeth
->ctrl_str
= ctrl_str
;
1872 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD
*pmeth
,
1873 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
1874 const unsigned char *tbs
, size_t tbslen
))
1876 pmeth
->digestsign
= digestsign
;
1879 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD
*pmeth
,
1880 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
1881 size_t siglen
, const unsigned char *tbs
,
1884 pmeth
->digestverify
= digestverify
;
1887 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
1888 int (*check
) (EVP_PKEY
*pkey
))
1890 pmeth
->check
= check
;
1893 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
1894 int (*check
) (EVP_PKEY
*pkey
))
1896 pmeth
->public_check
= check
;
1899 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
1900 int (*check
) (EVP_PKEY
*pkey
))
1902 pmeth
->param_check
= check
;
1905 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1906 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
1909 pmeth
->digest_custom
= digest_custom
;
1912 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
1913 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
1915 *pinit
= pmeth
->init
;
1918 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
1919 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
1920 const EVP_PKEY_CTX
*src
))
1922 *pcopy
= pmeth
->copy
;
1925 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
1926 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
1928 *pcleanup
= pmeth
->cleanup
;
1931 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
1932 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
1933 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
1937 *pparamgen_init
= pmeth
->paramgen_init
;
1939 *pparamgen
= pmeth
->paramgen
;
1942 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
1943 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
1944 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
1948 *pkeygen_init
= pmeth
->keygen_init
;
1950 *pkeygen
= pmeth
->keygen
;
1953 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
1954 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
1955 int (**psign
) (EVP_PKEY_CTX
*ctx
,
1956 unsigned char *sig
, size_t *siglen
,
1957 const unsigned char *tbs
,
1961 *psign_init
= pmeth
->sign_init
;
1963 *psign
= pmeth
->sign
;
1966 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
1967 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
1968 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
1969 const unsigned char *sig
,
1971 const unsigned char *tbs
,
1975 *pverify_init
= pmeth
->verify_init
;
1977 *pverify
= pmeth
->verify
;
1980 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
1981 int (**pverify_recover_init
) (EVP_PKEY_CTX
1983 int (**pverify_recover
) (EVP_PKEY_CTX
1992 if (pverify_recover_init
)
1993 *pverify_recover_init
= pmeth
->verify_recover_init
;
1994 if (pverify_recover
)
1995 *pverify_recover
= pmeth
->verify_recover
;
1998 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
1999 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
2001 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
2007 *psignctx_init
= pmeth
->signctx_init
;
2009 *psignctx
= pmeth
->signctx
;
2012 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
2013 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
2015 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
2016 const unsigned char *sig
,
2020 if (pverifyctx_init
)
2021 *pverifyctx_init
= pmeth
->verifyctx_init
;
2023 *pverifyctx
= pmeth
->verifyctx
;
2026 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
2027 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
2028 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
2031 const unsigned char *in
,
2035 *pencrypt_init
= pmeth
->encrypt_init
;
2037 *pencryptfn
= pmeth
->encrypt
;
2040 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
2041 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
2042 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
2045 const unsigned char *in
,
2049 *pdecrypt_init
= pmeth
->decrypt_init
;
2051 *pdecrypt
= pmeth
->decrypt
;
2054 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
2055 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
2056 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
2061 *pderive_init
= pmeth
->derive_init
;
2063 *pderive
= pmeth
->derive
;
2066 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
2067 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2069 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
2074 *pctrl
= pmeth
->ctrl
;
2076 *pctrl_str
= pmeth
->ctrl_str
;
2079 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD
*pmeth
,
2080 int (**digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2081 const unsigned char *tbs
, size_t tbslen
))
2084 *digestsign
= pmeth
->digestsign
;
2087 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD
*pmeth
,
2088 int (**digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2089 size_t siglen
, const unsigned char *tbs
,
2093 *digestverify
= pmeth
->digestverify
;
2096 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
2097 int (**pcheck
) (EVP_PKEY
*pkey
))
2100 *pcheck
= pmeth
->check
;
2103 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
2104 int (**pcheck
) (EVP_PKEY
*pkey
))
2107 *pcheck
= pmeth
->public_check
;
2110 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
2111 int (**pcheck
) (EVP_PKEY
*pkey
))
2114 *pcheck
= pmeth
->param_check
;
2117 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2118 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
2121 if (pdigest_custom
!= NULL
)
2122 *pdigest_custom
= pmeth
->digest_custom
;
2125 #endif /* FIPS_MODULE */