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 "crypto/ec.h"
30 #include "internal/ffc.h"
31 #include "internal/numbers.h"
32 #include "internal/provider.h"
33 #include "evp_local.h"
37 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
38 int keytype
, int optype
,
39 int cmd
, const char *name
,
40 const void *data
, size_t data_len
);
41 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
42 int cmd
, const char *name
);
43 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
);
45 typedef const EVP_PKEY_METHOD
*(*pmeth_fn
)(void);
46 typedef int sk_cmp_fn_type(const char *const *a
, const char *const *b
);
48 static STACK_OF(EVP_PKEY_METHOD
) *app_pkey_methods
= NULL
;
50 /* This array needs to be in order of NIDs */
51 static pmeth_fn standard_methods
[] = {
53 # ifndef OPENSSL_NO_DH
56 # ifndef OPENSSL_NO_DSA
59 # ifndef OPENSSL_NO_EC
62 ossl_rsa_pss_pkey_method
,
63 # ifndef OPENSSL_NO_DH
66 # ifndef OPENSSL_NO_EC
70 # ifndef OPENSSL_NO_EC
76 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
78 static int pmeth_func_cmp(const EVP_PKEY_METHOD
*const *a
, pmeth_fn
const *b
)
80 return ((*a
)->pkey_id
- ((**b
)())->pkey_id
);
83 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
85 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
86 const EVP_PKEY_METHOD
*const *b
)
88 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
91 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
95 const EVP_PKEY_METHOD
*t
= &tmp
;
98 if (app_pkey_methods
) {
100 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
102 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
104 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
105 sizeof(standard_methods
) /
107 if (ret
== NULL
|| *ret
== NULL
)
112 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
114 EVP_PKEY_METHOD
*pmeth
;
116 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
118 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
123 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
127 /* Three possible states: */
128 # define EVP_PKEY_STATE_UNKNOWN 0
129 # define EVP_PKEY_STATE_LEGACY 1
130 # define EVP_PKEY_STATE_PROVIDER 2
132 static int evp_pkey_ctx_state(EVP_PKEY_CTX
*ctx
)
134 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
135 return EVP_PKEY_STATE_UNKNOWN
;
137 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
138 && ctx
->op
.kex
.exchprovctx
!= NULL
)
139 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
140 && ctx
->op
.sig
.sigprovctx
!= NULL
)
141 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
142 && ctx
->op
.ciph
.ciphprovctx
!= NULL
)
143 || (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
144 && ctx
->op
.keymgmt
.genctx
!= NULL
)
145 || (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
146 && ctx
->op
.encap
.kemprovctx
!= NULL
))
147 return EVP_PKEY_STATE_PROVIDER
;
149 return EVP_PKEY_STATE_LEGACY
;
152 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype
,
157 if (*type
== NID_undef
)
158 *type
= evp_pkey_name2type(keytype
);
161 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT
*keymgmt
)
163 int type
= NID_undef
;
165 EVP_KEYMGMT_names_do_all(keymgmt
, help_get_legacy_alg_type_from_keymgmt
,
169 #endif /* FIPS_MODULE */
171 static EVP_PKEY_CTX
*int_ctx_new(OSSL_LIB_CTX
*libctx
,
172 EVP_PKEY
*pkey
, ENGINE
*e
,
173 const char *keytype
, const char *propquery
,
177 EVP_PKEY_CTX
*ret
= NULL
;
178 const EVP_PKEY_METHOD
*pmeth
= NULL
;
179 EVP_KEYMGMT
*keymgmt
= NULL
;
182 * If the given |pkey| is provided, we extract the keytype from its
183 * keymgmt and skip over the legacy code.
185 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
186 /* If we have an engine, something went wrong somewhere... */
187 if (!ossl_assert(e
== NULL
))
189 keytype
= evp_first_name(pkey
->keymgmt
->prov
, pkey
->keymgmt
->name_id
);
195 * TODO(3.0) This legacy code section should be removed when we stop
202 else if (keytype
!= NULL
)
203 id
= evp_pkey_name2type(keytype
);
207 /* If no ID was found here, we can only resort to find a keymgmt */
212 * Here, we extract what information we can for the purpose of
213 * supporting usage with implementations from providers, to make
214 * for a smooth transition from legacy stuff to provider based stuff.
216 * If an engine is given, this is entirely legacy, and we should not
217 * pretend anything else, so we only set the name when no engine is
218 * given. If both are already given, someone made a mistake, and
219 * since that can only happen internally, it's safe to make an
222 if (!ossl_assert(e
== NULL
|| keytype
== NULL
))
225 keytype
= OBJ_nid2sn(id
);
227 # ifndef OPENSSL_NO_ENGINE
228 if (e
== NULL
&& pkey
!= NULL
)
229 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
230 /* Try to find an ENGINE which implements this method */
232 if (!ENGINE_init(e
)) {
233 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
237 e
= ENGINE_get_pkey_meth_engine(id
);
241 * If an ENGINE handled this method look it up. Otherwise use internal
245 pmeth
= ENGINE_get_pkey_meth(e
, id
);
248 pmeth
= EVP_PKEY_meth_find(id
);
251 #endif /* FIPS_MODULE */
254 * If there's no engine and there's a name, we try fetching a provider
257 if (e
== NULL
&& keytype
!= NULL
) {
258 keymgmt
= EVP_KEYMGMT_fetch(libctx
, keytype
, propquery
);
260 return NULL
; /* EVP_KEYMGMT_fetch() recorded an error */
264 * Chase down the legacy NID, as that might be needed for diverse
265 * purposes, such as ensure that EVP_PKEY_type() can return sensible
266 * values, or that there's a better chance to "downgrade" a key when
267 * needed. We go through all keymgmt names, because the keytype
268 * that's passed to this function doesn't necessarily translate
270 * TODO: Remove this when #legacy keys are gone.
272 if (keymgmt
!= NULL
) {
273 int tmp_id
= get_legacy_alg_type_from_keymgmt(keymgmt
);
275 if (tmp_id
!= NID_undef
) {
280 * It really really shouldn't differ. If it still does,
281 * something is very wrong.
283 if (!ossl_assert(id
== tmp_id
)) {
284 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
285 EVP_KEYMGMT_free(keymgmt
);
294 if (pmeth
== NULL
&& keymgmt
== NULL
) {
295 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
297 ret
= OPENSSL_zalloc(sizeof(*ret
));
299 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
302 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
303 if ((ret
== NULL
|| pmeth
== NULL
) && e
!= NULL
)
308 EVP_KEYMGMT_free(keymgmt
);
311 if (propquery
!= NULL
) {
312 ret
->propquery
= OPENSSL_strdup(propquery
);
313 if (ret
->propquery
== NULL
) {
314 EVP_KEYMGMT_free(keymgmt
);
318 ret
->libctx
= libctx
;
319 ret
->keytype
= keytype
;
320 ret
->keymgmt
= keymgmt
;
321 ret
->legacy_keytype
= id
; /* TODO: Remove when #legacy key are gone */
324 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
327 EVP_PKEY_up_ref(pkey
);
329 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
330 if (pmeth
->init(ret
) <= 0) {
332 EVP_PKEY_CTX_free(ret
);
340 /*- All methods below can also be used in FIPS_MODULE */
342 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX
*libctx
,
344 const char *propquery
)
346 return int_ctx_new(libctx
, NULL
, NULL
, name
, propquery
, -1);
349 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX
*libctx
, EVP_PKEY
*pkey
,
350 const char *propquery
)
352 return int_ctx_new(libctx
, pkey
, NULL
, NULL
, propquery
, -1);
355 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
357 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
358 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
359 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
360 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
361 ctx
->op
.sig
.sigprovctx
= NULL
;
362 ctx
->op
.sig
.signature
= NULL
;
363 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
364 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
365 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
366 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
367 ctx
->op
.kex
.exchprovctx
= NULL
;
368 ctx
->op
.kex
.exchange
= NULL
;
369 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
370 if (ctx
->op
.encap
.kemprovctx
!= NULL
&& ctx
->op
.encap
.kem
!= NULL
)
371 ctx
->op
.encap
.kem
->freectx(ctx
->op
.encap
.kemprovctx
);
372 EVP_KEM_free(ctx
->op
.encap
.kem
);
373 ctx
->op
.encap
.kemprovctx
= NULL
;
374 ctx
->op
.encap
.kem
= NULL
;
376 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
377 if (ctx
->op
.ciph
.ciphprovctx
!= NULL
&& ctx
->op
.ciph
.cipher
!= NULL
)
378 ctx
->op
.ciph
.cipher
->freectx(ctx
->op
.ciph
.ciphprovctx
);
379 EVP_ASYM_CIPHER_free(ctx
->op
.ciph
.cipher
);
380 ctx
->op
.ciph
.ciphprovctx
= NULL
;
381 ctx
->op
.ciph
.cipher
= NULL
;
382 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
383 if (ctx
->op
.keymgmt
.genctx
!= NULL
&& ctx
->keymgmt
!= NULL
)
384 evp_keymgmt_gen_cleanup(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
);
388 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
392 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
393 ctx
->pmeth
->cleanup(ctx
);
395 evp_pkey_ctx_free_old_ops(ctx
);
397 evp_pkey_ctx_free_all_cached_data(ctx
);
399 EVP_KEYMGMT_free(ctx
->keymgmt
);
401 OPENSSL_free(ctx
->propquery
);
402 EVP_PKEY_free(ctx
->pkey
);
403 EVP_PKEY_free(ctx
->peerkey
);
404 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
405 ENGINE_finish(ctx
->engine
);
407 BN_free(ctx
->rsa_pubexp
);
413 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
414 const EVP_PKEY_METHOD
*meth
)
417 *ppkey_id
= meth
->pkey_id
;
419 *pflags
= meth
->flags
;
422 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
424 int pkey_id
= dst
->pkey_id
;
425 int flags
= dst
->flags
;
429 /* We only copy the function pointers so restore the other values */
430 dst
->pkey_id
= pkey_id
;
434 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
436 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
440 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
442 return int_ctx_new(NULL
, pkey
, e
, NULL
, NULL
, -1);
445 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
447 return int_ctx_new(NULL
, NULL
, e
, NULL
, NULL
, id
);
450 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
454 # ifndef OPENSSL_NO_ENGINE
455 /* Make sure it's safe to copy a pkey context using an ENGINE */
456 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
457 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
461 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
463 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
467 if (pctx
->pkey
!= NULL
)
468 EVP_PKEY_up_ref(pctx
->pkey
);
469 rctx
->pkey
= pctx
->pkey
;
470 rctx
->operation
= pctx
->operation
;
471 rctx
->libctx
= pctx
->libctx
;
472 rctx
->keytype
= pctx
->keytype
;
473 rctx
->propquery
= NULL
;
474 if (pctx
->propquery
!= NULL
) {
475 rctx
->propquery
= OPENSSL_strdup(pctx
->propquery
);
476 if (rctx
->propquery
== NULL
)
479 rctx
->legacy_keytype
= pctx
->legacy_keytype
;
481 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
482 if (pctx
->op
.kex
.exchange
!= NULL
) {
483 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
484 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
))
487 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
488 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
490 rctx
->op
.kex
.exchprovctx
491 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
492 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
493 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
498 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
499 if (pctx
->op
.sig
.signature
!= NULL
) {
500 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
501 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
))
504 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
505 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
507 rctx
->op
.sig
.sigprovctx
508 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
509 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
510 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
515 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx
)) {
516 if (pctx
->op
.ciph
.cipher
!= NULL
) {
517 rctx
->op
.ciph
.cipher
= pctx
->op
.ciph
.cipher
;
518 if (!EVP_ASYM_CIPHER_up_ref(rctx
->op
.ciph
.cipher
))
521 if (pctx
->op
.ciph
.ciphprovctx
!= NULL
) {
522 if (!ossl_assert(pctx
->op
.ciph
.cipher
!= NULL
))
524 rctx
->op
.ciph
.ciphprovctx
525 = pctx
->op
.ciph
.cipher
->dupctx(pctx
->op
.ciph
.ciphprovctx
);
526 if (rctx
->op
.ciph
.ciphprovctx
== NULL
) {
527 EVP_ASYM_CIPHER_free(rctx
->op
.ciph
.cipher
);
532 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx
)) {
533 if (pctx
->op
.encap
.kem
!= NULL
) {
534 rctx
->op
.encap
.kem
= pctx
->op
.encap
.kem
;
535 if (!EVP_KEM_up_ref(rctx
->op
.encap
.kem
))
538 if (pctx
->op
.encap
.kemprovctx
!= NULL
) {
539 if (!ossl_assert(pctx
->op
.encap
.kem
!= NULL
))
541 rctx
->op
.encap
.kemprovctx
542 = pctx
->op
.encap
.kem
->dupctx(pctx
->op
.encap
.kemprovctx
);
543 if (rctx
->op
.encap
.kemprovctx
== NULL
) {
544 EVP_KEM_free(rctx
->op
.encap
.kem
);
549 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx
)) {
550 /* Not supported - This would need a gen_dupctx() to work */
554 rctx
->pmeth
= pctx
->pmeth
;
555 # ifndef OPENSSL_NO_ENGINE
556 rctx
->engine
= pctx
->engine
;
559 if (pctx
->peerkey
!= NULL
)
560 EVP_PKEY_up_ref(pctx
->peerkey
);
561 rctx
->peerkey
= pctx
->peerkey
;
563 if (pctx
->pmeth
== NULL
) {
564 if (rctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
565 EVP_KEYMGMT
*tmp_keymgmt
= pctx
->keymgmt
;
568 provkey
= evp_pkey_export_to_provider(pctx
->pkey
, pctx
->libctx
,
569 &tmp_keymgmt
, pctx
->propquery
);
572 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
))
574 EVP_KEYMGMT_free(rctx
->keymgmt
);
575 rctx
->keymgmt
= tmp_keymgmt
;
578 } else if (pctx
->pmeth
->copy(rctx
, pctx
) > 0) {
583 EVP_PKEY_CTX_free(rctx
);
587 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
589 if (app_pkey_methods
== NULL
) {
590 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
591 if (app_pkey_methods
== NULL
){
592 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
596 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
597 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
600 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
604 void evp_app_cleanup_int(void)
606 if (app_pkey_methods
!= NULL
)
607 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
610 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
612 const EVP_PKEY_METHOD
*ret
;
614 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
616 return ret
== NULL
? 0 : 1;
619 size_t EVP_PKEY_meth_get_count(void)
621 size_t rv
= OSSL_NELEM(standard_methods
);
623 if (app_pkey_methods
)
624 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
628 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
630 if (idx
< OSSL_NELEM(standard_methods
))
631 return (standard_methods
[idx
])();
632 if (app_pkey_methods
== NULL
)
634 idx
-= OSSL_NELEM(standard_methods
);
635 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
637 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
641 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
643 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
644 && ctx
->op
.kex
.exchprovctx
!= NULL
645 && ctx
->op
.kex
.exchange
!= NULL
646 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
647 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
649 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
650 && ctx
->op
.sig
.sigprovctx
!= NULL
651 && ctx
->op
.sig
.signature
!= NULL
652 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
653 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
655 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
656 && ctx
->op
.ciph
.ciphprovctx
!= NULL
657 && ctx
->op
.ciph
.cipher
!= NULL
658 && ctx
->op
.ciph
.cipher
->set_ctx_params
!= NULL
)
659 return ctx
->op
.ciph
.cipher
->set_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
661 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
662 && ctx
->op
.keymgmt
.genctx
!= NULL
663 && ctx
->keymgmt
!= NULL
664 && ctx
->keymgmt
->gen_set_params
!= NULL
)
665 return evp_keymgmt_gen_set_params(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
,
667 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
668 && ctx
->op
.encap
.kemprovctx
!= NULL
669 && ctx
->op
.encap
.kem
!= NULL
670 && ctx
->op
.encap
.kem
->set_ctx_params
!= NULL
)
671 return ctx
->op
.encap
.kem
->set_ctx_params(ctx
->op
.encap
.kemprovctx
,
676 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
678 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
679 && ctx
->op
.kex
.exchprovctx
!= NULL
680 && ctx
->op
.kex
.exchange
!= NULL
681 && ctx
->op
.kex
.exchange
->get_ctx_params
!= NULL
)
682 return ctx
->op
.kex
.exchange
->get_ctx_params(ctx
->op
.kex
.exchprovctx
,
684 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
685 && ctx
->op
.sig
.sigprovctx
!= NULL
686 && ctx
->op
.sig
.signature
!= NULL
687 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
688 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
690 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
691 && ctx
->op
.ciph
.ciphprovctx
!= NULL
692 && ctx
->op
.ciph
.cipher
!= NULL
693 && ctx
->op
.ciph
.cipher
->get_ctx_params
!= NULL
)
694 return ctx
->op
.ciph
.cipher
->get_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
696 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
697 && ctx
->op
.encap
.kemprovctx
!= NULL
698 && ctx
->op
.encap
.kem
!= NULL
699 && ctx
->op
.encap
.kem
->get_ctx_params
!= NULL
)
700 return ctx
->op
.encap
.kem
->get_ctx_params(ctx
->op
.encap
.kemprovctx
,
706 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
710 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
711 && ctx
->op
.kex
.exchange
!= NULL
712 && ctx
->op
.kex
.exchange
->gettable_ctx_params
!= NULL
) {
713 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
714 return ctx
->op
.kex
.exchange
->gettable_ctx_params(provctx
);
716 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
717 && ctx
->op
.sig
.signature
!= NULL
718 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
) {
719 provctx
= ossl_provider_ctx(
720 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
721 return ctx
->op
.sig
.signature
->gettable_ctx_params(provctx
);
723 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
724 && ctx
->op
.ciph
.cipher
!= NULL
725 && ctx
->op
.ciph
.cipher
->gettable_ctx_params
!= NULL
) {
726 provctx
= ossl_provider_ctx(
727 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
728 return ctx
->op
.ciph
.cipher
->gettable_ctx_params(provctx
);
730 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
731 && ctx
->op
.encap
.kem
!= NULL
732 && ctx
->op
.encap
.kem
->gettable_ctx_params
!= NULL
) {
733 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
734 return ctx
->op
.encap
.kem
->gettable_ctx_params(provctx
);
739 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
743 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
744 && ctx
->op
.kex
.exchange
!= NULL
745 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
) {
746 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
747 return ctx
->op
.kex
.exchange
->settable_ctx_params(provctx
);
749 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
750 && ctx
->op
.sig
.signature
!= NULL
751 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
) {
752 provctx
= ossl_provider_ctx(
753 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
754 return ctx
->op
.sig
.signature
->settable_ctx_params(provctx
);
756 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
757 && ctx
->op
.ciph
.cipher
!= NULL
758 && ctx
->op
.ciph
.cipher
->settable_ctx_params
!= NULL
) {
759 provctx
= ossl_provider_ctx(
760 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
761 return ctx
->op
.ciph
.cipher
->settable_ctx_params(provctx
);
763 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
764 && ctx
->keymgmt
!= NULL
)
765 return EVP_KEYMGMT_gen_settable_params(ctx
->keymgmt
);
766 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
767 && ctx
->op
.encap
.kem
!= NULL
768 && ctx
->op
.encap
.kem
->settable_ctx_params
!= NULL
) {
769 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
770 return ctx
->op
.encap
.kem
->settable_ctx_params(provctx
);
776 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
778 * Return 1 on success, 0 or negative for errors.
780 * In particular they return -2 if any of the params is not supported.
782 * They are not available in FIPS_MODULE as they depend on
783 * - EVP_PKEY_CTX_{get,set}_params()
784 * - EVP_PKEY_CTX_{gettable,settable}_params()
787 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
791 if (ctx
== NULL
|| params
== NULL
)
794 for (p
= params
; p
->key
!= NULL
; p
++) {
795 /* Check the ctx actually understands this parameter */
796 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx
),
801 return EVP_PKEY_CTX_set_params(ctx
, params
);
804 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
808 if (ctx
== NULL
|| params
== NULL
)
811 for (p
= params
; p
->key
!= NULL
; p
++ ) {
812 /* Check the ctx actually understands this parameter */
813 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx
),
818 return EVP_PKEY_CTX_get_params(ctx
, params
);
821 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
823 OSSL_PARAM sig_md_params
[2], *p
= sig_md_params
;
824 /* 80 should be big enough */
828 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
829 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
830 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
834 /* TODO(3.0): Remove this eventually when no more legacy */
835 if (ctx
->op
.sig
.sigprovctx
== NULL
)
836 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
837 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
839 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
842 *p
= OSSL_PARAM_construct_end();
844 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
847 tmp
= evp_get_digestbyname_ex(ctx
->libctx
, name
);
856 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
,
857 int fallback
, const char *param
, int op
,
860 OSSL_PARAM md_params
[2], *p
= md_params
;
863 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
864 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
865 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
869 /* TODO(3.0): Remove this eventually when no more legacy */
871 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, 0, (void *)(md
));
876 name
= EVP_MD_name(md
);
879 *p
++ = OSSL_PARAM_construct_utf8_string(param
,
881 * Cast away the const. This is read
882 * only so should be safe
885 *p
= OSSL_PARAM_construct_end();
887 return EVP_PKEY_CTX_set_params(ctx
, md_params
);
890 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
892 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.sig
.sigprovctx
== NULL
,
893 OSSL_SIGNATURE_PARAM_DIGEST
,
894 EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
);
897 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
899 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
900 OSSL_KDF_PARAM_DIGEST
,
901 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_TLS_MD
);
904 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX
*ctx
, int fallback
,
905 const char *param
, int op
, int ctrl
,
906 const unsigned char *data
,
909 OSSL_PARAM octet_string_params
[2], *p
= octet_string_params
;
911 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
912 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
913 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
917 /* TODO(3.0): Remove this eventually when no more legacy */
919 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, datalen
, (void *)(data
));
922 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_LENGTH
);
926 *p
++ = OSSL_PARAM_construct_octet_string(param
,
928 * Cast away the const. This is read
929 * only so should be safe
931 (unsigned char *)data
,
933 *p
= OSSL_PARAM_construct_end();
935 return EVP_PKEY_CTX_set_params(ctx
, octet_string_params
);
938 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX
*ctx
,
939 const unsigned char *sec
, int seclen
)
941 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
942 OSSL_KDF_PARAM_SECRET
,
944 EVP_PKEY_CTRL_TLS_SECRET
,
948 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX
*ctx
,
949 const unsigned char *seed
, int seedlen
)
951 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
954 EVP_PKEY_CTRL_TLS_SEED
,
958 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
960 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
961 OSSL_KDF_PARAM_DIGEST
,
962 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_HKDF_MD
);
965 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX
*ctx
,
966 const unsigned char *salt
, int saltlen
)
968 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
971 EVP_PKEY_CTRL_HKDF_SALT
,
975 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX
*ctx
,
976 const unsigned char *key
, int keylen
)
978 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
981 EVP_PKEY_CTRL_HKDF_KEY
,
985 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX
*ctx
,
986 const unsigned char *info
, int infolen
)
988 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
991 EVP_PKEY_CTRL_HKDF_INFO
,
995 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX
*ctx
, int mode
)
997 OSSL_PARAM int_params
[2], *p
= int_params
;
999 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1000 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1001 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1005 /* TODO(3.0): Remove this eventually when no more legacy */
1006 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1007 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_DERIVE
,
1008 EVP_PKEY_CTRL_HKDF_MODE
, mode
, NULL
);
1012 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1016 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
1017 *p
= OSSL_PARAM_construct_end();
1019 return EVP_PKEY_CTX_set_params(ctx
, int_params
);
1022 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX
*ctx
, const char *pass
,
1025 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1026 OSSL_KDF_PARAM_PASSWORD
,
1029 (const unsigned char *)pass
, passlen
);
1032 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX
*ctx
,
1033 const unsigned char *salt
, int saltlen
)
1035 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1036 OSSL_KDF_PARAM_SALT
,
1038 EVP_PKEY_CTRL_SCRYPT_SALT
,
1042 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX
*ctx
, const char *param
,
1043 int op
, int ctrl
, uint64_t val
)
1045 OSSL_PARAM uint64_params
[2], *p
= uint64_params
;
1047 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1048 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1049 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1053 /* TODO(3.0): Remove this eventually when no more legacy */
1054 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1055 return EVP_PKEY_CTX_ctrl_uint64(ctx
, -1, op
, ctrl
, val
);
1057 *p
++ = OSSL_PARAM_construct_uint64(param
, &val
);
1058 *p
= OSSL_PARAM_construct_end();
1060 return EVP_PKEY_CTX_set_params(ctx
, uint64_params
);
1063 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX
*ctx
, uint64_t n
)
1065 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_N
,
1066 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_N
,
1070 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX
*ctx
, uint64_t r
)
1072 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_R
,
1073 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_R
,
1077 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX
*ctx
, uint64_t p
)
1079 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_P
,
1080 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_P
,
1084 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX
*ctx
,
1085 uint64_t maxmem_bytes
)
1087 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_MAXMEM
,
1089 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
,
1093 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX
*ctx
, const unsigned char *key
,
1096 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.keymgmt
.genctx
== NULL
,
1097 OSSL_PKEY_PARAM_PRIV_KEY
,
1099 EVP_PKEY_CTRL_SET_MAC_KEY
,
1103 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX
*ctx
, const char *op
)
1105 OSSL_PARAM params
[2], *p
= params
;
1107 if (ctx
== NULL
|| op
== NULL
) {
1108 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1111 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
1112 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1115 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION
,
1117 *p
= OSSL_PARAM_construct_end();
1118 return EVP_PKEY_CTX_set_params(ctx
, params
);
1121 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1123 OSSL_PARAM params
[2], *p
= params
;
1126 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1127 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1128 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1132 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID
,
1134 * Cast away the const. This is
1135 * read only so should be safe
1137 (void *)id
, (size_t)len
);
1138 *p
++ = OSSL_PARAM_construct_end();
1140 ret
= evp_pkey_ctx_set_params_strict(ctx
, params
);
1142 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1146 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1148 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1149 EVP_PKEY_CTRL_SET1_ID
, (int)len
, (void*)(id
));
1152 static int get1_id_data(EVP_PKEY_CTX
*ctx
, void *id
, size_t *id_len
)
1155 void *tmp_id
= NULL
;
1156 OSSL_PARAM params
[2], *p
= params
;
1158 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1159 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1160 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1164 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID
,
1166 *p
++ = OSSL_PARAM_construct_end();
1168 ret
= evp_pkey_ctx_get_params_strict(ctx
, params
);
1170 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1171 } else if (ret
> 0) {
1172 size_t tmp_id_len
= params
[0].return_size
;
1175 memcpy(id
, tmp_id
, tmp_id_len
);
1177 *id_len
= tmp_id_len
;
1182 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX
*ctx
, void *id
)
1184 return get1_id_data(ctx
, id
, NULL
);
1187 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1189 return get1_id_data(ctx
, NULL
, id_len
);
1192 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX
*ctx
, void *id
)
1194 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET1_ID
, 0, (void*)id
);
1197 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1199 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1200 EVP_PKEY_CTRL_GET1_ID_LEN
, 0, (void*)id_len
);
1203 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1204 int cmd
, int p1
, void *p2
)
1207 case EVP_PKEY_CTRL_SET1_ID
:
1208 return evp_pkey_ctx_set1_id_prov(ctx
, p2
, p1
);
1209 case EVP_PKEY_CTRL_GET1_ID
:
1210 return evp_pkey_ctx_get1_id_prov(ctx
, p2
);
1211 case EVP_PKEY_CTRL_GET1_ID_LEN
:
1212 return evp_pkey_ctx_get1_id_len_prov(ctx
, p2
);
1215 if (keytype
== EVP_PKEY_DHX
) {
1217 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
1218 return EVP_PKEY_CTX_set_dh_kdf_type(ctx
, p1
);
1219 case EVP_PKEY_CTRL_DH_KDF_MD
:
1220 return EVP_PKEY_CTX_set_dh_kdf_md(ctx
, p2
);
1221 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
1222 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx
, p1
);
1223 case EVP_PKEY_CTRL_DH_KDF_UKM
:
1224 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx
, p2
, p1
);
1225 case EVP_PKEY_CTRL_DH_KDF_OID
:
1226 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx
, p2
);
1227 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
1228 return EVP_PKEY_CTX_get_dh_kdf_md(ctx
, p2
);
1229 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
1230 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx
, p2
);
1231 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
1232 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx
, p2
);
1233 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
1234 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx
, p2
);
1237 if (keytype
== EVP_PKEY_DH
) {
1239 case EVP_PKEY_CTRL_DH_PAD
:
1240 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
1241 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
1242 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, p1
);
1243 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
1244 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, p1
);
1245 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
1246 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, p1
);
1247 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
1248 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, p1
);
1249 case EVP_PKEY_CTRL_DH_RFC5114
:
1250 return EVP_PKEY_CTX_set_dh_rfc5114(ctx
, p1
);
1253 if (keytype
== EVP_PKEY_DSA
) {
1255 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS
:
1256 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx
, p1
);
1257 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS
:
1258 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx
, p1
);
1259 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD
:
1260 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx
, p2
);
1263 if (keytype
== EVP_PKEY_EC
) {
1265 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
1266 return evp_pkey_ctx_set_ec_param_enc_prov(ctx
, p1
);
1267 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
1268 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, p1
);
1269 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
1271 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx
);
1272 } else if (p1
< -1 || p1
> 1) {
1273 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1276 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, p1
);
1278 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
1280 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx
);
1282 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx
, p1
);
1284 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
1285 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx
, p2
);
1286 case EVP_PKEY_CTRL_EC_KDF_MD
:
1287 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, p2
);
1288 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
1289 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx
, p2
);
1290 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
1291 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx
, p1
);
1292 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
1293 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx
, p2
);
1294 case EVP_PKEY_CTRL_EC_KDF_UKM
:
1295 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx
, p2
, p1
);
1298 if (keytype
== EVP_PKEY_RSA
) {
1300 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
1301 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1302 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
1303 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1304 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
1305 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1306 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
1307 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, p2
, p1
);
1308 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
1309 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx
, (unsigned char **)p2
);
1310 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
1311 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, p1
);
1312 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
1313 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, p2
);
1314 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
1315 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, p1
);
1319 * keytype == -1 is used when several key types share the same structure,
1320 * or for generic controls that are the same across multiple key types.
1322 if (keytype
== -1) {
1323 if (optype
== EVP_PKEY_OP_DERIVE
) {
1326 case EVP_PKEY_CTRL_TLS_MD
:
1327 return EVP_PKEY_CTX_set_tls1_prf_md(ctx
, p2
);
1328 case EVP_PKEY_CTRL_TLS_SECRET
:
1329 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx
, p2
, p1
);
1330 case EVP_PKEY_CTRL_TLS_SEED
:
1331 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx
, p2
, p1
);
1334 case EVP_PKEY_CTRL_HKDF_MD
:
1335 return EVP_PKEY_CTX_set_hkdf_md(ctx
, p2
);
1336 case EVP_PKEY_CTRL_HKDF_SALT
:
1337 return EVP_PKEY_CTX_set1_hkdf_salt(ctx
, p2
, p1
);
1338 case EVP_PKEY_CTRL_HKDF_KEY
:
1339 return EVP_PKEY_CTX_set1_hkdf_key(ctx
, p2
, p1
);
1340 case EVP_PKEY_CTRL_HKDF_INFO
:
1341 return EVP_PKEY_CTX_add1_hkdf_info(ctx
, p2
, p1
);
1342 case EVP_PKEY_CTRL_HKDF_MODE
:
1343 return EVP_PKEY_CTX_hkdf_mode(ctx
, p1
);
1346 case EVP_PKEY_CTRL_PASS
:
1347 return EVP_PKEY_CTX_set1_pbe_pass(ctx
, p2
, p1
);
1348 case EVP_PKEY_CTRL_SCRYPT_SALT
:
1349 return EVP_PKEY_CTX_set1_scrypt_salt(ctx
, p2
, p1
);
1350 case EVP_PKEY_CTRL_SCRYPT_N
:
1351 return EVP_PKEY_CTX_set_scrypt_N(ctx
, p1
);
1352 case EVP_PKEY_CTRL_SCRYPT_R
:
1353 return EVP_PKEY_CTX_set_scrypt_r(ctx
, p1
);
1354 case EVP_PKEY_CTRL_SCRYPT_P
:
1355 return EVP_PKEY_CTX_set_scrypt_p(ctx
, p1
);
1356 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
:
1357 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx
, p1
);
1359 } else if (optype
== EVP_PKEY_OP_KEYGEN
) {
1360 OSSL_PARAM params
[2], *p
= params
;
1363 case EVP_PKEY_CTRL_CIPHER
:
1365 char *ciphname
= (char *)EVP_CIPHER_name(p2
);
1367 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
1369 *p
= OSSL_PARAM_construct_end();
1371 return EVP_PKEY_CTX_set_params(ctx
, params
);
1373 case EVP_PKEY_CTRL_SET_MAC_KEY
:
1375 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
1377 *p
= OSSL_PARAM_construct_end();
1379 return EVP_PKEY_CTX_set_params(ctx
, params
);
1384 case EVP_PKEY_CTRL_MD
:
1385 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
1386 case EVP_PKEY_CTRL_GET_MD
:
1387 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
1388 case EVP_PKEY_CTRL_RSA_PADDING
:
1389 return EVP_PKEY_CTX_set_rsa_padding(ctx
, p1
);
1390 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
1391 return EVP_PKEY_CTX_get_rsa_padding(ctx
, p2
);
1392 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
1393 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1394 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
1395 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, p1
);
1396 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
1397 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx
, p2
);
1398 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
1399 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
1400 # ifndef OPENSSL_NO_CMS
1401 case EVP_PKEY_CTRL_CMS_DECRYPT
:
1402 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
1404 /* TODO (3.0) Temporary hack, this should probe */
1405 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx
), "RSASSA-PSS"))
1407 ERR_raise(ERR_LIB_EVP
,
1408 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
1414 * GOST CMS format is different for different cipher algorithms.
1415 * Most of other algorithms don't have such a difference
1416 * so this ctrl is just ignored.
1418 if (cmd
== EVP_PKEY_CTRL_CIPHER
)
1424 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1425 int cmd
, int p1
, void *p2
)
1430 * If the method has a |digest_custom| function, we can relax the
1431 * operation type check, since this can be called before the operation
1434 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->digest_custom
== NULL
) {
1435 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
1436 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_OPERATION_SET
);
1440 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
1441 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1446 switch (evp_pkey_ctx_state(ctx
)) {
1447 case EVP_PKEY_STATE_PROVIDER
:
1448 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1449 case EVP_PKEY_STATE_UNKNOWN
:
1450 case EVP_PKEY_STATE_LEGACY
:
1451 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
1452 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1455 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
1458 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
1461 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1467 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1468 int cmd
, int p1
, void *p2
)
1473 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1476 /* If unsupported, we don't want that reported here */
1478 ret
= evp_pkey_ctx_store_cached_data(ctx
, keytype
, optype
,
1483 ERR_clear_last_mark();
1485 * If there was an error, there was an error.
1486 * If the operation isn't initialized yet, we also return, as
1487 * the saved values will be used then anyway.
1489 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1492 return evp_pkey_ctx_ctrl_int(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1495 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1496 int cmd
, uint64_t value
)
1498 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
1501 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
1504 if (strcmp(name
, "md") == 0)
1505 name
= OSSL_ALG_PARAM_DIGEST
;
1506 else if (strcmp(name
, "rsa_padding_mode") == 0)
1507 name
= OSSL_ASYM_CIPHER_PARAM_PAD_MODE
;
1508 else if (strcmp(name
, "rsa_mgf1_md") == 0)
1509 name
= OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
;
1510 else if (strcmp(name
, "rsa_oaep_md") == 0)
1511 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
;
1512 else if (strcmp(name
, "rsa_oaep_label") == 0)
1513 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
;
1514 else if (strcmp(name
, "rsa_pss_saltlen") == 0)
1515 name
= OSSL_SIGNATURE_PARAM_PSS_SALTLEN
;
1516 else if (strcmp(name
, "rsa_keygen_bits") == 0)
1517 name
= OSSL_PKEY_PARAM_RSA_BITS
;
1518 else if (strcmp(name
, "rsa_keygen_pubexp") == 0)
1519 name
= OSSL_PKEY_PARAM_RSA_E
;
1520 else if (strcmp(name
, "rsa_keygen_primes") == 0)
1521 name
= OSSL_PKEY_PARAM_RSA_PRIMES
;
1522 else if (strcmp(name
, "rsa_pss_keygen_md") == 0)
1523 name
= OSSL_PKEY_PARAM_RSA_DIGEST
;
1524 else if (strcmp(name
, "rsa_pss_keygen_mgf1_md") == 0)
1525 name
= OSSL_PKEY_PARAM_RSA_MGF1_DIGEST
;
1526 else if (strcmp(name
, "rsa_pss_keygen_saltlen") == 0)
1527 name
= OSSL_PKEY_PARAM_RSA_PSS_SALTLEN
;
1528 else if (strcmp(name
, "dsa_paramgen_bits") == 0)
1529 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1530 else if (strcmp(name
, "dsa_paramgen_q_bits") == 0)
1531 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1532 else if (strcmp(name
, "dsa_paramgen_md") == 0)
1533 name
= OSSL_PKEY_PARAM_FFC_DIGEST
;
1534 else if (strcmp(name
, "dh_paramgen_generator") == 0)
1535 name
= OSSL_PKEY_PARAM_DH_GENERATOR
;
1536 else if (strcmp(name
, "dh_paramgen_prime_len") == 0)
1537 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1538 else if (strcmp(name
, "dh_paramgen_subprime_len") == 0)
1539 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1540 else if (strcmp(name
, "dh_paramgen_type") == 0) {
1541 name
= OSSL_PKEY_PARAM_FFC_TYPE
;
1542 value
= dh_gen_type_id2name(atoi(value
));
1543 } else if (strcmp(name
, "dh_param") == 0)
1544 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1545 else if (strcmp(name
, "dh_rfc5114") == 0) {
1546 int num
= atoi(value
);
1548 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1550 ossl_ffc_named_group_get_name(ossl_ffc_uid_to_dh_named_group(num
));
1551 } else if (strcmp(name
, "dh_pad") == 0)
1552 name
= OSSL_EXCHANGE_PARAM_PAD
;
1553 else if (strcmp(name
, "ec_paramgen_curve") == 0)
1554 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1555 else if (strcmp(name
, "ecdh_cofactor_mode") == 0)
1556 name
= OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
;
1557 else if (strcmp(name
, "ecdh_kdf_md") == 0)
1558 name
= OSSL_EXCHANGE_PARAM_KDF_DIGEST
;
1559 else if (strcmp(name
, "ec_param_enc") == 0)
1560 name
= OSSL_PKEY_PARAM_EC_ENCODING
;
1561 else if (strcmp(name
, "N") == 0)
1562 name
= OSSL_KDF_PARAM_SCRYPT_N
;
1566 * TODO(3.0) reduce the code above to only translate known legacy
1567 * string to the corresponding core name (see core_names.h), but
1568 * otherwise leave it to this code block to do the actual work.
1570 const OSSL_PARAM
*settable
= EVP_PKEY_CTX_settable_params(ctx
);
1571 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1575 if (!OSSL_PARAM_allocate_from_text(¶ms
[0], settable
, name
, value
,
1576 strlen(value
), &exists
)) {
1578 ERR_raise_data(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
,
1579 "name=%s,value=%s", name
, value
);
1584 if (EVP_PKEY_CTX_set_params(ctx
, params
))
1586 OPENSSL_free(params
[0].data
);
1591 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX
*ctx
,
1592 const char *name
, const char *value
)
1597 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1601 switch (evp_pkey_ctx_state(ctx
)) {
1602 case EVP_PKEY_STATE_PROVIDER
:
1603 return legacy_ctrl_str_to_param(ctx
, name
, value
);
1604 case EVP_PKEY_STATE_UNKNOWN
:
1605 case EVP_PKEY_STATE_LEGACY
:
1606 if (ctx
== NULL
|| ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl_str
== NULL
) {
1607 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1610 if (strcmp(name
, "digest") == 0)
1611 ret
= EVP_PKEY_CTX_md(ctx
,
1612 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1613 EVP_PKEY_CTRL_MD
, value
);
1615 ret
= ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
1622 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
1623 const char *name
, const char *value
)
1627 /* If unsupported, we don't want that reported here */
1629 ret
= evp_pkey_ctx_store_cached_data(ctx
, -1, -1, -1,
1630 name
, value
, strlen(value
) + 1);
1634 ERR_clear_last_mark();
1636 * If there was an error, there was an error.
1637 * If the operation isn't initialized yet, we also return, as
1638 * the saved values will be used then anyway.
1640 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1644 return evp_pkey_ctx_ctrl_str_int(ctx
, name
, value
);
1647 static int decode_cmd(int cmd
, const char *name
)
1651 * The consequence of the assertion not being true is that this
1652 * function will return -1, which will cause the calling functions
1653 * to signal that the command is unsupported... in non-debug mode.
1655 if (ossl_assert(name
!= NULL
))
1656 if (strcmp(name
, "distid") == 0 || strcmp(name
, "hexdistid") == 0)
1657 cmd
= EVP_PKEY_CTRL_SET1_ID
;
1663 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
1664 int keytype
, int optype
,
1665 int cmd
, const char *name
,
1666 const void *data
, size_t data_len
)
1668 if ((keytype
!= -1 && ctx
->pmeth
->pkey_id
!= keytype
)
1669 || ((optype
!= -1) && !(ctx
->operation
& optype
))) {
1670 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1674 cmd
= decode_cmd(cmd
, name
);
1676 case EVP_PKEY_CTRL_SET1_ID
:
1677 evp_pkey_ctx_free_cached_data(ctx
, cmd
, name
);
1679 ctx
->cached_parameters
.dist_id_name
= OPENSSL_strdup(name
);
1680 if (ctx
->cached_parameters
.dist_id_name
== NULL
) {
1681 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1686 ctx
->cached_parameters
.dist_id
= OPENSSL_memdup(data
, data_len
);
1687 if (ctx
->cached_parameters
.dist_id
== NULL
) {
1688 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1692 ctx
->cached_parameters
.dist_id_set
= 1;
1693 ctx
->cached_parameters
.dist_id_len
= data_len
;
1697 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1701 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
1702 int cmd
, const char *name
)
1704 cmd
= decode_cmd(cmd
, name
);
1706 case EVP_PKEY_CTRL_SET1_ID
:
1707 OPENSSL_free(ctx
->cached_parameters
.dist_id
);
1708 OPENSSL_free(ctx
->cached_parameters
.dist_id_name
);
1709 ctx
->cached_parameters
.dist_id
= NULL
;
1710 ctx
->cached_parameters
.dist_id_name
= NULL
;
1715 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
)
1717 evp_pkey_ctx_free_cached_data(ctx
, EVP_PKEY_CTRL_SET1_ID
, NULL
);
1720 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
)
1724 if (ret
&& ctx
->cached_parameters
.dist_id_set
) {
1725 const char *name
= ctx
->cached_parameters
.dist_id_name
;
1726 const void *val
= ctx
->cached_parameters
.dist_id
;
1727 size_t len
= ctx
->cached_parameters
.dist_id_len
;
1730 ret
= evp_pkey_ctx_ctrl_str_int(ctx
, name
, val
);
1732 ret
= evp_pkey_ctx_ctrl_int(ctx
, -1, ctx
->operation
,
1733 EVP_PKEY_CTRL_SET1_ID
,
1734 (int)len
, (void *)val
);
1740 OSSL_LIB_CTX
*EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX
*ctx
)
1745 const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX
*ctx
)
1747 return ctx
->propquery
;
1750 /* Utility functions to send a string of hex string to a ctrl */
1752 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
1759 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
1762 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
1768 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1771 if (binlen
<= INT_MAX
)
1772 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
1777 /* Pass a message digest to a ctrl */
1778 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
1782 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
1783 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_DIGEST
);
1786 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
1789 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
1791 return ctx
->operation
;
1794 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
1796 ctx
->keygen_info
= dat
;
1797 ctx
->keygen_info_count
= datlen
;
1800 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
1805 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
1810 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
1815 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
1817 return ctx
->peerkey
;
1820 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
1822 ctx
->app_data
= data
;
1825 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
1827 return ctx
->app_data
;
1830 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
1831 int (*init
) (EVP_PKEY_CTX
*ctx
))
1836 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
1837 int (*copy
) (EVP_PKEY_CTX
*dst
,
1838 const EVP_PKEY_CTX
*src
))
1843 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
1844 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
1846 pmeth
->cleanup
= cleanup
;
1849 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
1850 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
1851 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
1854 pmeth
->paramgen_init
= paramgen_init
;
1855 pmeth
->paramgen
= paramgen
;
1858 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
1859 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
1860 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
1863 pmeth
->keygen_init
= keygen_init
;
1864 pmeth
->keygen
= keygen
;
1867 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
1868 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
1869 int (*sign
) (EVP_PKEY_CTX
*ctx
,
1870 unsigned char *sig
, size_t *siglen
,
1871 const unsigned char *tbs
,
1874 pmeth
->sign_init
= sign_init
;
1878 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
1879 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
1880 int (*verify
) (EVP_PKEY_CTX
*ctx
,
1881 const unsigned char *sig
,
1883 const unsigned char *tbs
,
1886 pmeth
->verify_init
= verify_init
;
1887 pmeth
->verify
= verify
;
1890 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
1891 int (*verify_recover_init
) (EVP_PKEY_CTX
1893 int (*verify_recover
) (EVP_PKEY_CTX
1902 pmeth
->verify_recover_init
= verify_recover_init
;
1903 pmeth
->verify_recover
= verify_recover
;
1906 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
1907 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
1909 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
1914 pmeth
->signctx_init
= signctx_init
;
1915 pmeth
->signctx
= signctx
;
1918 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
1919 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1921 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
1922 const unsigned char *sig
,
1926 pmeth
->verifyctx_init
= verifyctx_init
;
1927 pmeth
->verifyctx
= verifyctx
;
1930 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
1931 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
1932 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
1935 const unsigned char *in
,
1938 pmeth
->encrypt_init
= encrypt_init
;
1939 pmeth
->encrypt
= encryptfn
;
1942 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
1943 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
1944 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
1947 const unsigned char *in
,
1950 pmeth
->decrypt_init
= decrypt_init
;
1951 pmeth
->decrypt
= decrypt
;
1954 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
1955 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
1956 int (*derive
) (EVP_PKEY_CTX
*ctx
,
1960 pmeth
->derive_init
= derive_init
;
1961 pmeth
->derive
= derive
;
1964 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
1965 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1967 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
1972 pmeth
->ctrl_str
= ctrl_str
;
1975 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD
*pmeth
,
1976 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
1977 const unsigned char *tbs
, size_t tbslen
))
1979 pmeth
->digestsign
= digestsign
;
1982 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD
*pmeth
,
1983 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
1984 size_t siglen
, const unsigned char *tbs
,
1987 pmeth
->digestverify
= digestverify
;
1990 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
1991 int (*check
) (EVP_PKEY
*pkey
))
1993 pmeth
->check
= check
;
1996 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
1997 int (*check
) (EVP_PKEY
*pkey
))
1999 pmeth
->public_check
= check
;
2002 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
2003 int (*check
) (EVP_PKEY
*pkey
))
2005 pmeth
->param_check
= check
;
2008 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2009 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
2012 pmeth
->digest_custom
= digest_custom
;
2015 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
2016 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
2018 *pinit
= pmeth
->init
;
2021 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
2022 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
2023 const EVP_PKEY_CTX
*src
))
2025 *pcopy
= pmeth
->copy
;
2028 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
2029 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
2031 *pcleanup
= pmeth
->cleanup
;
2034 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
2035 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
2036 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
2040 *pparamgen_init
= pmeth
->paramgen_init
;
2042 *pparamgen
= pmeth
->paramgen
;
2045 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
2046 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
2047 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
2051 *pkeygen_init
= pmeth
->keygen_init
;
2053 *pkeygen
= pmeth
->keygen
;
2056 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
2057 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
2058 int (**psign
) (EVP_PKEY_CTX
*ctx
,
2059 unsigned char *sig
, size_t *siglen
,
2060 const unsigned char *tbs
,
2064 *psign_init
= pmeth
->sign_init
;
2066 *psign
= pmeth
->sign
;
2069 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
2070 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
2071 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
2072 const unsigned char *sig
,
2074 const unsigned char *tbs
,
2078 *pverify_init
= pmeth
->verify_init
;
2080 *pverify
= pmeth
->verify
;
2083 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
2084 int (**pverify_recover_init
) (EVP_PKEY_CTX
2086 int (**pverify_recover
) (EVP_PKEY_CTX
2095 if (pverify_recover_init
)
2096 *pverify_recover_init
= pmeth
->verify_recover_init
;
2097 if (pverify_recover
)
2098 *pverify_recover
= pmeth
->verify_recover
;
2101 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
2102 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
2104 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
2110 *psignctx_init
= pmeth
->signctx_init
;
2112 *psignctx
= pmeth
->signctx
;
2115 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
2116 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
2118 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
2119 const unsigned char *sig
,
2123 if (pverifyctx_init
)
2124 *pverifyctx_init
= pmeth
->verifyctx_init
;
2126 *pverifyctx
= pmeth
->verifyctx
;
2129 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
2130 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
2131 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
2134 const unsigned char *in
,
2138 *pencrypt_init
= pmeth
->encrypt_init
;
2140 *pencryptfn
= pmeth
->encrypt
;
2143 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
2144 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
2145 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
2148 const unsigned char *in
,
2152 *pdecrypt_init
= pmeth
->decrypt_init
;
2154 *pdecrypt
= pmeth
->decrypt
;
2157 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
2158 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
2159 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
2164 *pderive_init
= pmeth
->derive_init
;
2166 *pderive
= pmeth
->derive
;
2169 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
2170 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2172 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
2177 *pctrl
= pmeth
->ctrl
;
2179 *pctrl_str
= pmeth
->ctrl_str
;
2182 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD
*pmeth
,
2183 int (**digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2184 const unsigned char *tbs
, size_t tbslen
))
2187 *digestsign
= pmeth
->digestsign
;
2190 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD
*pmeth
,
2191 int (**digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2192 size_t siglen
, const unsigned char *tbs
,
2196 *digestverify
= pmeth
->digestverify
;
2199 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
2200 int (**pcheck
) (EVP_PKEY
*pkey
))
2203 *pcheck
= pmeth
->check
;
2206 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
2207 int (**pcheck
) (EVP_PKEY
*pkey
))
2210 *pcheck
= pmeth
->public_check
;
2213 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
2214 int (**pcheck
) (EVP_PKEY
*pkey
))
2217 *pcheck
= pmeth
->param_check
;
2220 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2221 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
2224 if (pdigest_custom
!= NULL
)
2225 *pdigest_custom
= pmeth
->digest_custom
;
2228 #endif /* FIPS_MODULE */