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 static const EVP_PKEY_METHOD
*evp_pkey_meth_find_added_by_application(int type
)
93 if (app_pkey_methods
!= NULL
) {
98 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
100 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
105 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
109 const EVP_PKEY_METHOD
*t
;
111 if ((t
= evp_pkey_meth_find_added_by_application(type
)) != NULL
)
116 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
117 OSSL_NELEM(standard_methods
));
118 if (ret
== NULL
|| *ret
== NULL
)
123 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
125 EVP_PKEY_METHOD
*pmeth
;
127 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
129 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
134 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
138 /* Three possible states: */
139 # define EVP_PKEY_STATE_UNKNOWN 0
140 # define EVP_PKEY_STATE_LEGACY 1
141 # define EVP_PKEY_STATE_PROVIDER 2
143 static int evp_pkey_ctx_state(EVP_PKEY_CTX
*ctx
)
145 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
146 return EVP_PKEY_STATE_UNKNOWN
;
148 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
149 && ctx
->op
.kex
.exchprovctx
!= NULL
)
150 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
151 && ctx
->op
.sig
.sigprovctx
!= NULL
)
152 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
153 && ctx
->op
.ciph
.ciphprovctx
!= NULL
)
154 || (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
155 && ctx
->op
.keymgmt
.genctx
!= NULL
)
156 || (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
157 && ctx
->op
.encap
.kemprovctx
!= NULL
))
158 return EVP_PKEY_STATE_PROVIDER
;
160 return EVP_PKEY_STATE_LEGACY
;
163 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype
,
168 if (*type
== NID_undef
)
169 *type
= evp_pkey_name2type(keytype
);
172 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT
*keymgmt
)
174 int type
= NID_undef
;
176 EVP_KEYMGMT_names_do_all(keymgmt
, help_get_legacy_alg_type_from_keymgmt
,
180 #endif /* FIPS_MODULE */
182 static EVP_PKEY_CTX
*int_ctx_new(OSSL_LIB_CTX
*libctx
,
183 EVP_PKEY
*pkey
, ENGINE
*e
,
184 const char *keytype
, const char *propquery
,
188 EVP_PKEY_CTX
*ret
= NULL
;
189 const EVP_PKEY_METHOD
*pmeth
= NULL
;
190 EVP_KEYMGMT
*keymgmt
= NULL
;
193 * If the given |pkey| is provided, we extract the keytype from its
194 * keymgmt and skip over the legacy code.
196 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
197 /* If we have an engine, something went wrong somewhere... */
198 if (!ossl_assert(e
== NULL
))
200 keytype
= evp_first_name(pkey
->keymgmt
->prov
, pkey
->keymgmt
->name_id
);
206 * TODO(3.0) This legacy code section should be removed when we stop
213 else if (keytype
!= NULL
)
214 id
= evp_pkey_name2type(keytype
);
218 /* If no ID was found here, we can only resort to find a keymgmt */
223 * Here, we extract what information we can for the purpose of
224 * supporting usage with implementations from providers, to make
225 * for a smooth transition from legacy stuff to provider based stuff.
227 * If an engine is given, this is entirely legacy, and we should not
228 * pretend anything else, so we only set the name when no engine is
229 * given. If both are already given, someone made a mistake, and
230 * since that can only happen internally, it's safe to make an
233 if (!ossl_assert(e
== NULL
|| keytype
== NULL
))
236 keytype
= OBJ_nid2sn(id
);
238 # ifndef OPENSSL_NO_ENGINE
239 if (e
== NULL
&& pkey
!= NULL
)
240 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
241 /* Try to find an ENGINE which implements this method */
243 if (!ENGINE_init(e
)) {
244 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
248 e
= ENGINE_get_pkey_meth_engine(id
);
252 * If an ENGINE handled this method look it up. Otherwise use internal
256 pmeth
= ENGINE_get_pkey_meth(e
, id
);
259 pmeth
= evp_pkey_meth_find_added_by_application(id
);
262 #endif /* FIPS_MODULE */
265 * If there's no engine and there's a name, we try fetching a provider
268 if (e
== NULL
&& keytype
!= NULL
) {
269 keymgmt
= EVP_KEYMGMT_fetch(libctx
, keytype
, propquery
);
271 return NULL
; /* EVP_KEYMGMT_fetch() recorded an error */
275 * Chase down the legacy NID, as that might be needed for diverse
276 * purposes, such as ensure that EVP_PKEY_type() can return sensible
277 * values, or that there's a better chance to "downgrade" a key when
278 * needed. We go through all keymgmt names, because the keytype
279 * that's passed to this function doesn't necessarily translate
281 * TODO: Remove this when #legacy keys are gone.
283 if (keymgmt
!= NULL
) {
284 int tmp_id
= get_legacy_alg_type_from_keymgmt(keymgmt
);
286 if (tmp_id
!= NID_undef
) {
291 * It really really shouldn't differ. If it still does,
292 * something is very wrong.
294 if (!ossl_assert(id
== tmp_id
)) {
295 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
296 EVP_KEYMGMT_free(keymgmt
);
305 if (pmeth
== NULL
&& keymgmt
== NULL
) {
306 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
308 ret
= OPENSSL_zalloc(sizeof(*ret
));
310 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
313 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
314 if ((ret
== NULL
|| pmeth
== NULL
) && e
!= NULL
)
319 EVP_KEYMGMT_free(keymgmt
);
322 if (propquery
!= NULL
) {
323 ret
->propquery
= OPENSSL_strdup(propquery
);
324 if (ret
->propquery
== NULL
) {
325 EVP_KEYMGMT_free(keymgmt
);
329 ret
->libctx
= libctx
;
330 ret
->keytype
= keytype
;
331 ret
->keymgmt
= keymgmt
;
332 ret
->legacy_keytype
= id
; /* TODO: Remove when #legacy key are gone */
335 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
338 EVP_PKEY_up_ref(pkey
);
340 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
341 if (pmeth
->init(ret
) <= 0) {
343 EVP_PKEY_CTX_free(ret
);
351 /*- All methods below can also be used in FIPS_MODULE */
353 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX
*libctx
,
355 const char *propquery
)
357 return int_ctx_new(libctx
, NULL
, NULL
, name
, propquery
, -1);
360 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX
*libctx
, EVP_PKEY
*pkey
,
361 const char *propquery
)
363 return int_ctx_new(libctx
, pkey
, NULL
, NULL
, propquery
, -1);
366 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
368 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
369 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
370 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
371 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
372 ctx
->op
.sig
.sigprovctx
= NULL
;
373 ctx
->op
.sig
.signature
= NULL
;
374 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
375 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
376 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
377 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
378 ctx
->op
.kex
.exchprovctx
= NULL
;
379 ctx
->op
.kex
.exchange
= NULL
;
380 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
381 if (ctx
->op
.encap
.kemprovctx
!= NULL
&& ctx
->op
.encap
.kem
!= NULL
)
382 ctx
->op
.encap
.kem
->freectx(ctx
->op
.encap
.kemprovctx
);
383 EVP_KEM_free(ctx
->op
.encap
.kem
);
384 ctx
->op
.encap
.kemprovctx
= NULL
;
385 ctx
->op
.encap
.kem
= NULL
;
387 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
388 if (ctx
->op
.ciph
.ciphprovctx
!= NULL
&& ctx
->op
.ciph
.cipher
!= NULL
)
389 ctx
->op
.ciph
.cipher
->freectx(ctx
->op
.ciph
.ciphprovctx
);
390 EVP_ASYM_CIPHER_free(ctx
->op
.ciph
.cipher
);
391 ctx
->op
.ciph
.ciphprovctx
= NULL
;
392 ctx
->op
.ciph
.cipher
= NULL
;
393 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
394 if (ctx
->op
.keymgmt
.genctx
!= NULL
&& ctx
->keymgmt
!= NULL
)
395 evp_keymgmt_gen_cleanup(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
);
399 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
403 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
404 ctx
->pmeth
->cleanup(ctx
);
406 evp_pkey_ctx_free_old_ops(ctx
);
408 evp_pkey_ctx_free_all_cached_data(ctx
);
410 EVP_KEYMGMT_free(ctx
->keymgmt
);
412 OPENSSL_free(ctx
->propquery
);
413 EVP_PKEY_free(ctx
->pkey
);
414 EVP_PKEY_free(ctx
->peerkey
);
415 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
416 ENGINE_finish(ctx
->engine
);
418 BN_free(ctx
->rsa_pubexp
);
424 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
425 const EVP_PKEY_METHOD
*meth
)
428 *ppkey_id
= meth
->pkey_id
;
430 *pflags
= meth
->flags
;
433 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
435 int pkey_id
= dst
->pkey_id
;
436 int flags
= dst
->flags
;
440 /* We only copy the function pointers so restore the other values */
441 dst
->pkey_id
= pkey_id
;
445 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
447 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
451 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
453 return int_ctx_new(NULL
, pkey
, e
, NULL
, NULL
, -1);
456 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
458 return int_ctx_new(NULL
, NULL
, e
, NULL
, NULL
, id
);
461 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
465 # ifndef OPENSSL_NO_ENGINE
466 /* Make sure it's safe to copy a pkey context using an ENGINE */
467 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
468 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
472 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
474 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
478 if (pctx
->pkey
!= NULL
)
479 EVP_PKEY_up_ref(pctx
->pkey
);
480 rctx
->pkey
= pctx
->pkey
;
481 rctx
->operation
= pctx
->operation
;
482 rctx
->libctx
= pctx
->libctx
;
483 rctx
->keytype
= pctx
->keytype
;
484 rctx
->propquery
= NULL
;
485 if (pctx
->propquery
!= NULL
) {
486 rctx
->propquery
= OPENSSL_strdup(pctx
->propquery
);
487 if (rctx
->propquery
== NULL
)
490 rctx
->legacy_keytype
= pctx
->legacy_keytype
;
492 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
493 if (pctx
->op
.kex
.exchange
!= NULL
) {
494 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
495 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
))
498 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
499 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
501 rctx
->op
.kex
.exchprovctx
502 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
503 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
504 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
509 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
510 if (pctx
->op
.sig
.signature
!= NULL
) {
511 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
512 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
))
515 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
516 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
518 rctx
->op
.sig
.sigprovctx
519 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
520 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
521 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
526 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx
)) {
527 if (pctx
->op
.ciph
.cipher
!= NULL
) {
528 rctx
->op
.ciph
.cipher
= pctx
->op
.ciph
.cipher
;
529 if (!EVP_ASYM_CIPHER_up_ref(rctx
->op
.ciph
.cipher
))
532 if (pctx
->op
.ciph
.ciphprovctx
!= NULL
) {
533 if (!ossl_assert(pctx
->op
.ciph
.cipher
!= NULL
))
535 rctx
->op
.ciph
.ciphprovctx
536 = pctx
->op
.ciph
.cipher
->dupctx(pctx
->op
.ciph
.ciphprovctx
);
537 if (rctx
->op
.ciph
.ciphprovctx
== NULL
) {
538 EVP_ASYM_CIPHER_free(rctx
->op
.ciph
.cipher
);
543 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx
)) {
544 if (pctx
->op
.encap
.kem
!= NULL
) {
545 rctx
->op
.encap
.kem
= pctx
->op
.encap
.kem
;
546 if (!EVP_KEM_up_ref(rctx
->op
.encap
.kem
))
549 if (pctx
->op
.encap
.kemprovctx
!= NULL
) {
550 if (!ossl_assert(pctx
->op
.encap
.kem
!= NULL
))
552 rctx
->op
.encap
.kemprovctx
553 = pctx
->op
.encap
.kem
->dupctx(pctx
->op
.encap
.kemprovctx
);
554 if (rctx
->op
.encap
.kemprovctx
== NULL
) {
555 EVP_KEM_free(rctx
->op
.encap
.kem
);
560 } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx
)) {
561 /* Not supported - This would need a gen_dupctx() to work */
565 rctx
->pmeth
= pctx
->pmeth
;
566 # ifndef OPENSSL_NO_ENGINE
567 rctx
->engine
= pctx
->engine
;
570 if (pctx
->peerkey
!= NULL
)
571 EVP_PKEY_up_ref(pctx
->peerkey
);
572 rctx
->peerkey
= pctx
->peerkey
;
574 if (pctx
->pmeth
== NULL
) {
575 if (rctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
576 EVP_KEYMGMT
*tmp_keymgmt
= pctx
->keymgmt
;
579 provkey
= evp_pkey_export_to_provider(pctx
->pkey
, pctx
->libctx
,
580 &tmp_keymgmt
, pctx
->propquery
);
583 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
))
585 EVP_KEYMGMT_free(rctx
->keymgmt
);
586 rctx
->keymgmt
= tmp_keymgmt
;
589 } else if (pctx
->pmeth
->copy(rctx
, pctx
) > 0) {
594 EVP_PKEY_CTX_free(rctx
);
598 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
600 if (app_pkey_methods
== NULL
) {
601 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
602 if (app_pkey_methods
== NULL
){
603 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
607 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
608 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
611 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
615 void evp_app_cleanup_int(void)
617 if (app_pkey_methods
!= NULL
)
618 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
621 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
623 const EVP_PKEY_METHOD
*ret
;
625 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
627 return ret
== NULL
? 0 : 1;
630 size_t EVP_PKEY_meth_get_count(void)
632 size_t rv
= OSSL_NELEM(standard_methods
);
634 if (app_pkey_methods
)
635 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
639 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
641 if (idx
< OSSL_NELEM(standard_methods
))
642 return (standard_methods
[idx
])();
643 if (app_pkey_methods
== NULL
)
645 idx
-= OSSL_NELEM(standard_methods
);
646 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
648 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
652 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
654 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
655 && ctx
->op
.kex
.exchprovctx
!= NULL
656 && ctx
->op
.kex
.exchange
!= NULL
657 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
658 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
660 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
661 && ctx
->op
.sig
.sigprovctx
!= NULL
662 && ctx
->op
.sig
.signature
!= NULL
663 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
664 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
666 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
667 && ctx
->op
.ciph
.ciphprovctx
!= NULL
668 && ctx
->op
.ciph
.cipher
!= NULL
669 && ctx
->op
.ciph
.cipher
->set_ctx_params
!= NULL
)
670 return ctx
->op
.ciph
.cipher
->set_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
672 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
673 && ctx
->op
.keymgmt
.genctx
!= NULL
674 && ctx
->keymgmt
!= NULL
675 && ctx
->keymgmt
->gen_set_params
!= NULL
)
676 return evp_keymgmt_gen_set_params(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
,
678 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
679 && ctx
->op
.encap
.kemprovctx
!= NULL
680 && ctx
->op
.encap
.kem
!= NULL
681 && ctx
->op
.encap
.kem
->set_ctx_params
!= NULL
)
682 return ctx
->op
.encap
.kem
->set_ctx_params(ctx
->op
.encap
.kemprovctx
,
687 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
689 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
690 && ctx
->op
.kex
.exchprovctx
!= NULL
691 && ctx
->op
.kex
.exchange
!= NULL
692 && ctx
->op
.kex
.exchange
->get_ctx_params
!= NULL
)
693 return ctx
->op
.kex
.exchange
->get_ctx_params(ctx
->op
.kex
.exchprovctx
,
695 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
696 && ctx
->op
.sig
.sigprovctx
!= NULL
697 && ctx
->op
.sig
.signature
!= NULL
698 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
699 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
701 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
702 && ctx
->op
.ciph
.ciphprovctx
!= NULL
703 && ctx
->op
.ciph
.cipher
!= NULL
704 && ctx
->op
.ciph
.cipher
->get_ctx_params
!= NULL
)
705 return ctx
->op
.ciph
.cipher
->get_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
707 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
708 && ctx
->op
.encap
.kemprovctx
!= NULL
709 && ctx
->op
.encap
.kem
!= NULL
710 && ctx
->op
.encap
.kem
->get_ctx_params
!= NULL
)
711 return ctx
->op
.encap
.kem
->get_ctx_params(ctx
->op
.encap
.kemprovctx
,
717 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
721 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
722 && ctx
->op
.kex
.exchange
!= NULL
723 && ctx
->op
.kex
.exchange
->gettable_ctx_params
!= NULL
) {
724 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
725 return ctx
->op
.kex
.exchange
->gettable_ctx_params(provctx
);
727 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
728 && ctx
->op
.sig
.signature
!= NULL
729 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
) {
730 provctx
= ossl_provider_ctx(
731 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
732 return ctx
->op
.sig
.signature
->gettable_ctx_params(provctx
);
734 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
735 && ctx
->op
.ciph
.cipher
!= NULL
736 && ctx
->op
.ciph
.cipher
->gettable_ctx_params
!= NULL
) {
737 provctx
= ossl_provider_ctx(
738 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
739 return ctx
->op
.ciph
.cipher
->gettable_ctx_params(provctx
);
741 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
742 && ctx
->op
.encap
.kem
!= NULL
743 && ctx
->op
.encap
.kem
->gettable_ctx_params
!= NULL
) {
744 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
745 return ctx
->op
.encap
.kem
->gettable_ctx_params(provctx
);
750 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
754 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
755 && ctx
->op
.kex
.exchange
!= NULL
756 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
) {
757 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
758 return ctx
->op
.kex
.exchange
->settable_ctx_params(provctx
);
760 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
761 && ctx
->op
.sig
.signature
!= NULL
762 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
) {
763 provctx
= ossl_provider_ctx(
764 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
765 return ctx
->op
.sig
.signature
->settable_ctx_params(provctx
);
767 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
768 && ctx
->op
.ciph
.cipher
!= NULL
769 && ctx
->op
.ciph
.cipher
->settable_ctx_params
!= NULL
) {
770 provctx
= ossl_provider_ctx(
771 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
772 return ctx
->op
.ciph
.cipher
->settable_ctx_params(provctx
);
774 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
775 && ctx
->keymgmt
!= NULL
)
776 return EVP_KEYMGMT_gen_settable_params(ctx
->keymgmt
);
777 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
778 && ctx
->op
.encap
.kem
!= NULL
779 && ctx
->op
.encap
.kem
->settable_ctx_params
!= NULL
) {
780 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
781 return ctx
->op
.encap
.kem
->settable_ctx_params(provctx
);
787 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
789 * Return 1 on success, 0 or negative for errors.
791 * In particular they return -2 if any of the params is not supported.
793 * They are not available in FIPS_MODULE as they depend on
794 * - EVP_PKEY_CTX_{get,set}_params()
795 * - EVP_PKEY_CTX_{gettable,settable}_params()
798 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
802 if (ctx
== NULL
|| params
== NULL
)
805 for (p
= params
; p
->key
!= NULL
; p
++) {
806 /* Check the ctx actually understands this parameter */
807 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx
),
812 return EVP_PKEY_CTX_set_params(ctx
, params
);
815 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
819 if (ctx
== NULL
|| params
== NULL
)
822 for (p
= params
; p
->key
!= NULL
; p
++ ) {
823 /* Check the ctx actually understands this parameter */
824 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx
),
829 return EVP_PKEY_CTX_get_params(ctx
, params
);
832 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
834 OSSL_PARAM sig_md_params
[2], *p
= sig_md_params
;
835 /* 80 should be big enough */
839 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
840 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
841 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
845 /* TODO(3.0): Remove this eventually when no more legacy */
846 if (ctx
->op
.sig
.sigprovctx
== NULL
)
847 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
848 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
850 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
853 *p
= OSSL_PARAM_construct_end();
855 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
858 tmp
= evp_get_digestbyname_ex(ctx
->libctx
, name
);
867 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
,
868 int fallback
, const char *param
, int op
,
871 OSSL_PARAM md_params
[2], *p
= md_params
;
874 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
875 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
876 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
880 /* TODO(3.0): Remove this eventually when no more legacy */
882 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, 0, (void *)(md
));
887 name
= EVP_MD_name(md
);
890 *p
++ = OSSL_PARAM_construct_utf8_string(param
,
892 * Cast away the const. This is read
893 * only so should be safe
896 *p
= OSSL_PARAM_construct_end();
898 return EVP_PKEY_CTX_set_params(ctx
, md_params
);
901 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
903 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.sig
.sigprovctx
== NULL
,
904 OSSL_SIGNATURE_PARAM_DIGEST
,
905 EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
);
908 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
910 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
911 OSSL_KDF_PARAM_DIGEST
,
912 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_TLS_MD
);
915 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX
*ctx
, int fallback
,
916 const char *param
, int op
, int ctrl
,
917 const unsigned char *data
,
920 OSSL_PARAM octet_string_params
[2], *p
= octet_string_params
;
922 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
923 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
924 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
928 /* TODO(3.0): Remove this eventually when no more legacy */
930 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, datalen
, (void *)(data
));
933 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_LENGTH
);
937 *p
++ = OSSL_PARAM_construct_octet_string(param
,
939 * Cast away the const. This is read
940 * only so should be safe
942 (unsigned char *)data
,
944 *p
= OSSL_PARAM_construct_end();
946 return EVP_PKEY_CTX_set_params(ctx
, octet_string_params
);
949 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX
*ctx
,
950 const unsigned char *sec
, int seclen
)
952 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
953 OSSL_KDF_PARAM_SECRET
,
955 EVP_PKEY_CTRL_TLS_SECRET
,
959 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX
*ctx
,
960 const unsigned char *seed
, int seedlen
)
962 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
965 EVP_PKEY_CTRL_TLS_SEED
,
969 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
971 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
972 OSSL_KDF_PARAM_DIGEST
,
973 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_HKDF_MD
);
976 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX
*ctx
,
977 const unsigned char *salt
, int saltlen
)
979 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
982 EVP_PKEY_CTRL_HKDF_SALT
,
986 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX
*ctx
,
987 const unsigned char *key
, int keylen
)
989 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
992 EVP_PKEY_CTRL_HKDF_KEY
,
996 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX
*ctx
,
997 const unsigned char *info
, int infolen
)
999 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1000 OSSL_KDF_PARAM_INFO
,
1002 EVP_PKEY_CTRL_HKDF_INFO
,
1006 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX
*ctx
, int mode
)
1008 OSSL_PARAM int_params
[2], *p
= int_params
;
1010 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1011 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1012 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1016 /* TODO(3.0): Remove this eventually when no more legacy */
1017 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1018 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_DERIVE
,
1019 EVP_PKEY_CTRL_HKDF_MODE
, mode
, NULL
);
1023 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1027 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
1028 *p
= OSSL_PARAM_construct_end();
1030 return EVP_PKEY_CTX_set_params(ctx
, int_params
);
1033 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX
*ctx
, const char *pass
,
1036 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1037 OSSL_KDF_PARAM_PASSWORD
,
1040 (const unsigned char *)pass
, passlen
);
1043 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX
*ctx
,
1044 const unsigned char *salt
, int saltlen
)
1046 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1047 OSSL_KDF_PARAM_SALT
,
1049 EVP_PKEY_CTRL_SCRYPT_SALT
,
1053 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX
*ctx
, const char *param
,
1054 int op
, int ctrl
, uint64_t val
)
1056 OSSL_PARAM uint64_params
[2], *p
= uint64_params
;
1058 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1059 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1060 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1064 /* TODO(3.0): Remove this eventually when no more legacy */
1065 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1066 return EVP_PKEY_CTX_ctrl_uint64(ctx
, -1, op
, ctrl
, val
);
1068 *p
++ = OSSL_PARAM_construct_uint64(param
, &val
);
1069 *p
= OSSL_PARAM_construct_end();
1071 return EVP_PKEY_CTX_set_params(ctx
, uint64_params
);
1074 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX
*ctx
, uint64_t n
)
1076 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_N
,
1077 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_N
,
1081 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX
*ctx
, uint64_t r
)
1083 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_R
,
1084 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_R
,
1088 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX
*ctx
, uint64_t p
)
1090 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_P
,
1091 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_P
,
1095 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX
*ctx
,
1096 uint64_t maxmem_bytes
)
1098 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_MAXMEM
,
1100 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
,
1104 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX
*ctx
, const unsigned char *key
,
1107 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.keymgmt
.genctx
== NULL
,
1108 OSSL_PKEY_PARAM_PRIV_KEY
,
1110 EVP_PKEY_CTRL_SET_MAC_KEY
,
1114 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX
*ctx
, const char *op
)
1116 OSSL_PARAM params
[2], *p
= params
;
1118 if (ctx
== NULL
|| op
== NULL
) {
1119 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1122 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
1123 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1126 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION
,
1128 *p
= OSSL_PARAM_construct_end();
1129 return EVP_PKEY_CTX_set_params(ctx
, params
);
1132 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1134 OSSL_PARAM params
[2], *p
= params
;
1137 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1138 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1139 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1143 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID
,
1145 * Cast away the const. This is
1146 * read only so should be safe
1148 (void *)id
, (size_t)len
);
1149 *p
++ = OSSL_PARAM_construct_end();
1151 ret
= evp_pkey_ctx_set_params_strict(ctx
, params
);
1153 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1157 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1159 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1160 EVP_PKEY_CTRL_SET1_ID
, (int)len
, (void*)(id
));
1163 static int get1_id_data(EVP_PKEY_CTX
*ctx
, void *id
, size_t *id_len
)
1166 void *tmp_id
= NULL
;
1167 OSSL_PARAM params
[2], *p
= params
;
1169 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1170 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1171 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1175 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID
,
1177 *p
++ = OSSL_PARAM_construct_end();
1179 ret
= evp_pkey_ctx_get_params_strict(ctx
, params
);
1181 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1182 } else if (ret
> 0) {
1183 size_t tmp_id_len
= params
[0].return_size
;
1186 memcpy(id
, tmp_id
, tmp_id_len
);
1188 *id_len
= tmp_id_len
;
1193 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX
*ctx
, void *id
)
1195 return get1_id_data(ctx
, id
, NULL
);
1198 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1200 return get1_id_data(ctx
, NULL
, id_len
);
1203 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX
*ctx
, void *id
)
1205 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET1_ID
, 0, (void*)id
);
1208 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1210 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1211 EVP_PKEY_CTRL_GET1_ID_LEN
, 0, (void*)id_len
);
1214 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1215 int cmd
, int p1
, void *p2
)
1218 case EVP_PKEY_CTRL_SET1_ID
:
1219 return evp_pkey_ctx_set1_id_prov(ctx
, p2
, p1
);
1220 case EVP_PKEY_CTRL_GET1_ID
:
1221 return evp_pkey_ctx_get1_id_prov(ctx
, p2
);
1222 case EVP_PKEY_CTRL_GET1_ID_LEN
:
1223 return evp_pkey_ctx_get1_id_len_prov(ctx
, p2
);
1226 if (keytype
== EVP_PKEY_DHX
) {
1228 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
1229 return EVP_PKEY_CTX_set_dh_kdf_type(ctx
, p1
);
1230 case EVP_PKEY_CTRL_DH_KDF_MD
:
1231 return EVP_PKEY_CTX_set_dh_kdf_md(ctx
, p2
);
1232 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
1233 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx
, p1
);
1234 case EVP_PKEY_CTRL_DH_KDF_UKM
:
1235 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx
, p2
, p1
);
1236 case EVP_PKEY_CTRL_DH_KDF_OID
:
1237 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx
, p2
);
1238 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
1239 return EVP_PKEY_CTX_get_dh_kdf_md(ctx
, p2
);
1240 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
1241 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx
, p2
);
1242 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
1243 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx
, p2
);
1244 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
1245 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx
, p2
);
1248 if (keytype
== EVP_PKEY_DH
) {
1250 case EVP_PKEY_CTRL_DH_PAD
:
1251 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
1252 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
1253 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, p1
);
1254 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
1255 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, p1
);
1256 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
1257 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, p1
);
1258 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
1259 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, p1
);
1260 case EVP_PKEY_CTRL_DH_RFC5114
:
1261 return EVP_PKEY_CTX_set_dh_rfc5114(ctx
, p1
);
1264 if (keytype
== EVP_PKEY_DSA
) {
1266 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS
:
1267 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx
, p1
);
1268 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS
:
1269 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx
, p1
);
1270 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD
:
1271 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx
, p2
);
1274 if (keytype
== EVP_PKEY_EC
) {
1276 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
1277 return evp_pkey_ctx_set_ec_param_enc_prov(ctx
, p1
);
1278 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
1279 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, p1
);
1280 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
1282 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx
);
1283 } else if (p1
< -1 || p1
> 1) {
1284 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1287 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, p1
);
1289 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
1291 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx
);
1293 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx
, p1
);
1295 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
1296 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx
, p2
);
1297 case EVP_PKEY_CTRL_EC_KDF_MD
:
1298 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, p2
);
1299 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
1300 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx
, p2
);
1301 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
1302 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx
, p1
);
1303 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
1304 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx
, p2
);
1305 case EVP_PKEY_CTRL_EC_KDF_UKM
:
1306 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx
, p2
, p1
);
1309 if (keytype
== EVP_PKEY_RSA
) {
1311 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
1312 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1313 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
1314 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1315 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
1316 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1317 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
1318 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, p2
, p1
);
1319 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
1320 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx
, (unsigned char **)p2
);
1321 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
1322 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, p1
);
1323 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
1324 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, p2
);
1325 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
1326 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, p1
);
1330 if (keytype
== EVP_PKEY_RSA_PSS
) {
1332 case EVP_PKEY_CTRL_MD
:
1333 return EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx
, p2
);
1338 * keytype == -1 is used when several key types share the same structure,
1339 * or for generic controls that are the same across multiple key types.
1341 if (keytype
== -1) {
1342 if (optype
== EVP_PKEY_OP_DERIVE
) {
1345 case EVP_PKEY_CTRL_TLS_MD
:
1346 return EVP_PKEY_CTX_set_tls1_prf_md(ctx
, p2
);
1347 case EVP_PKEY_CTRL_TLS_SECRET
:
1348 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx
, p2
, p1
);
1349 case EVP_PKEY_CTRL_TLS_SEED
:
1350 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx
, p2
, p1
);
1353 case EVP_PKEY_CTRL_HKDF_MD
:
1354 return EVP_PKEY_CTX_set_hkdf_md(ctx
, p2
);
1355 case EVP_PKEY_CTRL_HKDF_SALT
:
1356 return EVP_PKEY_CTX_set1_hkdf_salt(ctx
, p2
, p1
);
1357 case EVP_PKEY_CTRL_HKDF_KEY
:
1358 return EVP_PKEY_CTX_set1_hkdf_key(ctx
, p2
, p1
);
1359 case EVP_PKEY_CTRL_HKDF_INFO
:
1360 return EVP_PKEY_CTX_add1_hkdf_info(ctx
, p2
, p1
);
1361 case EVP_PKEY_CTRL_HKDF_MODE
:
1362 return EVP_PKEY_CTX_hkdf_mode(ctx
, p1
);
1365 case EVP_PKEY_CTRL_PASS
:
1366 return EVP_PKEY_CTX_set1_pbe_pass(ctx
, p2
, p1
);
1367 case EVP_PKEY_CTRL_SCRYPT_SALT
:
1368 return EVP_PKEY_CTX_set1_scrypt_salt(ctx
, p2
, p1
);
1369 case EVP_PKEY_CTRL_SCRYPT_N
:
1370 return EVP_PKEY_CTX_set_scrypt_N(ctx
, p1
);
1371 case EVP_PKEY_CTRL_SCRYPT_R
:
1372 return EVP_PKEY_CTX_set_scrypt_r(ctx
, p1
);
1373 case EVP_PKEY_CTRL_SCRYPT_P
:
1374 return EVP_PKEY_CTX_set_scrypt_p(ctx
, p1
);
1375 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
:
1376 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx
, p1
);
1378 } else if (optype
== EVP_PKEY_OP_KEYGEN
) {
1379 OSSL_PARAM params
[2], *p
= params
;
1382 case EVP_PKEY_CTRL_CIPHER
:
1384 char *ciphname
= (char *)EVP_CIPHER_name(p2
);
1386 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
1388 *p
= OSSL_PARAM_construct_end();
1390 return EVP_PKEY_CTX_set_params(ctx
, params
);
1392 case EVP_PKEY_CTRL_SET_MAC_KEY
:
1394 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
1396 *p
= OSSL_PARAM_construct_end();
1398 return EVP_PKEY_CTX_set_params(ctx
, params
);
1403 case EVP_PKEY_CTRL_MD
:
1404 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
1405 case EVP_PKEY_CTRL_GET_MD
:
1406 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
1407 case EVP_PKEY_CTRL_RSA_PADDING
:
1408 return EVP_PKEY_CTX_set_rsa_padding(ctx
, p1
);
1409 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
1410 return EVP_PKEY_CTX_get_rsa_padding(ctx
, p2
);
1411 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
1412 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1413 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
1414 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, p1
);
1415 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
1416 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx
, p2
);
1417 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
1418 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
1419 # ifndef OPENSSL_NO_CMS
1420 case EVP_PKEY_CTRL_CMS_DECRYPT
:
1421 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
1423 /* TODO (3.0) Temporary hack, this should probe */
1424 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx
), "RSASSA-PSS"))
1426 ERR_raise(ERR_LIB_EVP
,
1427 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
1433 * GOST CMS format is different for different cipher algorithms.
1434 * Most of other algorithms don't have such a difference
1435 * so this ctrl is just ignored.
1437 if (cmd
== EVP_PKEY_CTRL_CIPHER
)
1443 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1444 int cmd
, int p1
, void *p2
)
1449 * If the method has a |digest_custom| function, we can relax the
1450 * operation type check, since this can be called before the operation
1453 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->digest_custom
== NULL
) {
1454 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
1455 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_OPERATION_SET
);
1459 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
1460 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1465 switch (evp_pkey_ctx_state(ctx
)) {
1466 case EVP_PKEY_STATE_PROVIDER
:
1467 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1468 case EVP_PKEY_STATE_UNKNOWN
:
1469 case EVP_PKEY_STATE_LEGACY
:
1470 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
1471 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1474 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
1477 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
1480 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1486 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1487 int cmd
, int p1
, void *p2
)
1492 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1495 /* If unsupported, we don't want that reported here */
1497 ret
= evp_pkey_ctx_store_cached_data(ctx
, keytype
, optype
,
1502 ERR_clear_last_mark();
1504 * If there was an error, there was an error.
1505 * If the operation isn't initialized yet, we also return, as
1506 * the saved values will be used then anyway.
1508 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1511 return evp_pkey_ctx_ctrl_int(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1514 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1515 int cmd
, uint64_t value
)
1517 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
1520 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
1523 if (strcmp(name
, "md") == 0)
1524 name
= OSSL_ALG_PARAM_DIGEST
;
1525 else if (strcmp(name
, "rsa_padding_mode") == 0)
1526 name
= OSSL_ASYM_CIPHER_PARAM_PAD_MODE
;
1527 else if (strcmp(name
, "rsa_mgf1_md") == 0)
1528 name
= OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
;
1529 else if (strcmp(name
, "rsa_oaep_md") == 0)
1530 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
;
1531 else if (strcmp(name
, "rsa_oaep_label") == 0)
1532 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
;
1533 else if (strcmp(name
, "rsa_pss_saltlen") == 0)
1534 name
= OSSL_SIGNATURE_PARAM_PSS_SALTLEN
;
1535 else if (strcmp(name
, "rsa_keygen_bits") == 0)
1536 name
= OSSL_PKEY_PARAM_RSA_BITS
;
1537 else if (strcmp(name
, "rsa_keygen_pubexp") == 0)
1538 name
= OSSL_PKEY_PARAM_RSA_E
;
1539 else if (strcmp(name
, "rsa_keygen_primes") == 0)
1540 name
= OSSL_PKEY_PARAM_RSA_PRIMES
;
1541 else if (strcmp(name
, "rsa_pss_keygen_md") == 0)
1542 name
= OSSL_PKEY_PARAM_RSA_DIGEST
;
1543 else if (strcmp(name
, "rsa_pss_keygen_mgf1_md") == 0)
1544 name
= OSSL_PKEY_PARAM_RSA_MGF1_DIGEST
;
1545 else if (strcmp(name
, "rsa_pss_keygen_saltlen") == 0)
1546 name
= OSSL_PKEY_PARAM_RSA_PSS_SALTLEN
;
1547 else if (strcmp(name
, "dsa_paramgen_bits") == 0)
1548 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1549 else if (strcmp(name
, "dsa_paramgen_q_bits") == 0)
1550 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1551 else if (strcmp(name
, "dsa_paramgen_md") == 0)
1552 name
= OSSL_PKEY_PARAM_FFC_DIGEST
;
1553 else if (strcmp(name
, "dh_paramgen_generator") == 0)
1554 name
= OSSL_PKEY_PARAM_DH_GENERATOR
;
1555 else if (strcmp(name
, "dh_paramgen_prime_len") == 0)
1556 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1557 else if (strcmp(name
, "dh_paramgen_subprime_len") == 0)
1558 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1559 else if (strcmp(name
, "dh_paramgen_type") == 0) {
1560 name
= OSSL_PKEY_PARAM_FFC_TYPE
;
1561 value
= dh_gen_type_id2name(atoi(value
));
1562 } else if (strcmp(name
, "dh_param") == 0)
1563 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1564 else if (strcmp(name
, "dh_rfc5114") == 0) {
1565 int num
= atoi(value
);
1567 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1569 ossl_ffc_named_group_get_name(ossl_ffc_uid_to_dh_named_group(num
));
1570 } else if (strcmp(name
, "dh_pad") == 0)
1571 name
= OSSL_EXCHANGE_PARAM_PAD
;
1572 else if (strcmp(name
, "ec_paramgen_curve") == 0)
1573 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1574 else if (strcmp(name
, "ecdh_cofactor_mode") == 0)
1575 name
= OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
;
1576 else if (strcmp(name
, "ecdh_kdf_md") == 0)
1577 name
= OSSL_EXCHANGE_PARAM_KDF_DIGEST
;
1578 else if (strcmp(name
, "ec_param_enc") == 0)
1579 name
= OSSL_PKEY_PARAM_EC_ENCODING
;
1580 else if (strcmp(name
, "N") == 0)
1581 name
= OSSL_KDF_PARAM_SCRYPT_N
;
1585 * TODO(3.0) reduce the code above to only translate known legacy
1586 * string to the corresponding core name (see core_names.h), but
1587 * otherwise leave it to this code block to do the actual work.
1589 const OSSL_PARAM
*settable
= EVP_PKEY_CTX_settable_params(ctx
);
1590 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1594 if (!OSSL_PARAM_allocate_from_text(¶ms
[0], settable
, name
, value
,
1595 strlen(value
), &exists
)) {
1597 ERR_raise_data(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
,
1598 "name=%s,value=%s", name
, value
);
1603 if (EVP_PKEY_CTX_set_params(ctx
, params
))
1605 OPENSSL_free(params
[0].data
);
1610 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX
*ctx
,
1611 const char *name
, const char *value
)
1616 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1620 switch (evp_pkey_ctx_state(ctx
)) {
1621 case EVP_PKEY_STATE_PROVIDER
:
1622 return legacy_ctrl_str_to_param(ctx
, name
, value
);
1623 case EVP_PKEY_STATE_UNKNOWN
:
1624 case EVP_PKEY_STATE_LEGACY
:
1625 if (ctx
== NULL
|| ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl_str
== NULL
) {
1626 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1629 if (strcmp(name
, "digest") == 0)
1630 ret
= EVP_PKEY_CTX_md(ctx
,
1631 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1632 EVP_PKEY_CTRL_MD
, value
);
1634 ret
= ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
1641 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
1642 const char *name
, const char *value
)
1646 /* If unsupported, we don't want that reported here */
1648 ret
= evp_pkey_ctx_store_cached_data(ctx
, -1, -1, -1,
1649 name
, value
, strlen(value
) + 1);
1653 ERR_clear_last_mark();
1655 * If there was an error, there was an error.
1656 * If the operation isn't initialized yet, we also return, as
1657 * the saved values will be used then anyway.
1659 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1663 return evp_pkey_ctx_ctrl_str_int(ctx
, name
, value
);
1666 static int decode_cmd(int cmd
, const char *name
)
1670 * The consequence of the assertion not being true is that this
1671 * function will return -1, which will cause the calling functions
1672 * to signal that the command is unsupported... in non-debug mode.
1674 if (ossl_assert(name
!= NULL
))
1675 if (strcmp(name
, "distid") == 0 || strcmp(name
, "hexdistid") == 0)
1676 cmd
= EVP_PKEY_CTRL_SET1_ID
;
1682 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
1683 int keytype
, int optype
,
1684 int cmd
, const char *name
,
1685 const void *data
, size_t data_len
)
1687 if (keytype
!= -1) {
1688 switch (evp_pkey_ctx_state(ctx
)) {
1689 case EVP_PKEY_STATE_PROVIDER
:
1690 if (ctx
->keymgmt
== NULL
) {
1691 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1694 if (!EVP_KEYMGMT_is_a(ctx
->keymgmt
,
1695 evp_pkey_type2name(keytype
))) {
1696 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1700 case EVP_PKEY_STATE_UNKNOWN
:
1701 case EVP_PKEY_STATE_LEGACY
:
1702 if (ctx
->pmeth
== NULL
) {
1703 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1706 if (ctx
->pmeth
->pkey_id
!= keytype
) {
1707 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1713 if (optype
!= -1 && (ctx
->operation
& optype
) == 0) {
1714 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1718 cmd
= decode_cmd(cmd
, name
);
1720 case EVP_PKEY_CTRL_SET1_ID
:
1721 evp_pkey_ctx_free_cached_data(ctx
, cmd
, name
);
1723 ctx
->cached_parameters
.dist_id_name
= OPENSSL_strdup(name
);
1724 if (ctx
->cached_parameters
.dist_id_name
== NULL
) {
1725 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1730 ctx
->cached_parameters
.dist_id
= OPENSSL_memdup(data
, data_len
);
1731 if (ctx
->cached_parameters
.dist_id
== NULL
) {
1732 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1736 ctx
->cached_parameters
.dist_id_set
= 1;
1737 ctx
->cached_parameters
.dist_id_len
= data_len
;
1741 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1745 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
1746 int cmd
, const char *name
)
1748 cmd
= decode_cmd(cmd
, name
);
1750 case EVP_PKEY_CTRL_SET1_ID
:
1751 OPENSSL_free(ctx
->cached_parameters
.dist_id
);
1752 OPENSSL_free(ctx
->cached_parameters
.dist_id_name
);
1753 ctx
->cached_parameters
.dist_id
= NULL
;
1754 ctx
->cached_parameters
.dist_id_name
= NULL
;
1759 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
)
1761 evp_pkey_ctx_free_cached_data(ctx
, EVP_PKEY_CTRL_SET1_ID
, NULL
);
1764 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
)
1768 if (ret
&& ctx
->cached_parameters
.dist_id_set
) {
1769 const char *name
= ctx
->cached_parameters
.dist_id_name
;
1770 const void *val
= ctx
->cached_parameters
.dist_id
;
1771 size_t len
= ctx
->cached_parameters
.dist_id_len
;
1774 ret
= evp_pkey_ctx_ctrl_str_int(ctx
, name
, val
);
1776 ret
= evp_pkey_ctx_ctrl_int(ctx
, -1, ctx
->operation
,
1777 EVP_PKEY_CTRL_SET1_ID
,
1778 (int)len
, (void *)val
);
1784 OSSL_LIB_CTX
*EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX
*ctx
)
1789 const char *EVP_PKEY_CTX_get0_propq(EVP_PKEY_CTX
*ctx
)
1791 return ctx
->propquery
;
1794 /* Utility functions to send a string of hex string to a ctrl */
1796 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
1803 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
1806 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
1812 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1815 if (binlen
<= INT_MAX
)
1816 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
1821 /* Pass a message digest to a ctrl */
1822 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
1826 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
1827 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_DIGEST
);
1830 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
1833 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
1835 return ctx
->operation
;
1838 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
1840 ctx
->keygen_info
= dat
;
1841 ctx
->keygen_info_count
= datlen
;
1844 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
1849 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
1854 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
1859 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
1861 return ctx
->peerkey
;
1864 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
1866 ctx
->app_data
= data
;
1869 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
1871 return ctx
->app_data
;
1874 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
1875 int (*init
) (EVP_PKEY_CTX
*ctx
))
1880 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
1881 int (*copy
) (EVP_PKEY_CTX
*dst
,
1882 const EVP_PKEY_CTX
*src
))
1887 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
1888 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
1890 pmeth
->cleanup
= cleanup
;
1893 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
1894 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
1895 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
1898 pmeth
->paramgen_init
= paramgen_init
;
1899 pmeth
->paramgen
= paramgen
;
1902 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
1903 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
1904 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
1907 pmeth
->keygen_init
= keygen_init
;
1908 pmeth
->keygen
= keygen
;
1911 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
1912 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
1913 int (*sign
) (EVP_PKEY_CTX
*ctx
,
1914 unsigned char *sig
, size_t *siglen
,
1915 const unsigned char *tbs
,
1918 pmeth
->sign_init
= sign_init
;
1922 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
1923 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
1924 int (*verify
) (EVP_PKEY_CTX
*ctx
,
1925 const unsigned char *sig
,
1927 const unsigned char *tbs
,
1930 pmeth
->verify_init
= verify_init
;
1931 pmeth
->verify
= verify
;
1934 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
1935 int (*verify_recover_init
) (EVP_PKEY_CTX
1937 int (*verify_recover
) (EVP_PKEY_CTX
1946 pmeth
->verify_recover_init
= verify_recover_init
;
1947 pmeth
->verify_recover
= verify_recover
;
1950 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
1951 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
1953 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
1958 pmeth
->signctx_init
= signctx_init
;
1959 pmeth
->signctx
= signctx
;
1962 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
1963 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1965 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
1966 const unsigned char *sig
,
1970 pmeth
->verifyctx_init
= verifyctx_init
;
1971 pmeth
->verifyctx
= verifyctx
;
1974 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
1975 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
1976 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
1979 const unsigned char *in
,
1982 pmeth
->encrypt_init
= encrypt_init
;
1983 pmeth
->encrypt
= encryptfn
;
1986 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
1987 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
1988 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
1991 const unsigned char *in
,
1994 pmeth
->decrypt_init
= decrypt_init
;
1995 pmeth
->decrypt
= decrypt
;
1998 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
1999 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
2000 int (*derive
) (EVP_PKEY_CTX
*ctx
,
2004 pmeth
->derive_init
= derive_init
;
2005 pmeth
->derive
= derive
;
2008 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
2009 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2011 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
2016 pmeth
->ctrl_str
= ctrl_str
;
2019 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD
*pmeth
,
2020 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2021 const unsigned char *tbs
, size_t tbslen
))
2023 pmeth
->digestsign
= digestsign
;
2026 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD
*pmeth
,
2027 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2028 size_t siglen
, const unsigned char *tbs
,
2031 pmeth
->digestverify
= digestverify
;
2034 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
2035 int (*check
) (EVP_PKEY
*pkey
))
2037 pmeth
->check
= check
;
2040 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
2041 int (*check
) (EVP_PKEY
*pkey
))
2043 pmeth
->public_check
= check
;
2046 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
2047 int (*check
) (EVP_PKEY
*pkey
))
2049 pmeth
->param_check
= check
;
2052 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2053 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
2056 pmeth
->digest_custom
= digest_custom
;
2059 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
2060 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
2062 *pinit
= pmeth
->init
;
2065 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
2066 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
2067 const EVP_PKEY_CTX
*src
))
2069 *pcopy
= pmeth
->copy
;
2072 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
2073 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
2075 *pcleanup
= pmeth
->cleanup
;
2078 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
2079 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
2080 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
2084 *pparamgen_init
= pmeth
->paramgen_init
;
2086 *pparamgen
= pmeth
->paramgen
;
2089 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
2090 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
2091 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
2095 *pkeygen_init
= pmeth
->keygen_init
;
2097 *pkeygen
= pmeth
->keygen
;
2100 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
2101 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
2102 int (**psign
) (EVP_PKEY_CTX
*ctx
,
2103 unsigned char *sig
, size_t *siglen
,
2104 const unsigned char *tbs
,
2108 *psign_init
= pmeth
->sign_init
;
2110 *psign
= pmeth
->sign
;
2113 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
2114 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
2115 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
2116 const unsigned char *sig
,
2118 const unsigned char *tbs
,
2122 *pverify_init
= pmeth
->verify_init
;
2124 *pverify
= pmeth
->verify
;
2127 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
2128 int (**pverify_recover_init
) (EVP_PKEY_CTX
2130 int (**pverify_recover
) (EVP_PKEY_CTX
2139 if (pverify_recover_init
)
2140 *pverify_recover_init
= pmeth
->verify_recover_init
;
2141 if (pverify_recover
)
2142 *pverify_recover
= pmeth
->verify_recover
;
2145 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
2146 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
2148 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
2154 *psignctx_init
= pmeth
->signctx_init
;
2156 *psignctx
= pmeth
->signctx
;
2159 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
2160 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
2162 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
2163 const unsigned char *sig
,
2167 if (pverifyctx_init
)
2168 *pverifyctx_init
= pmeth
->verifyctx_init
;
2170 *pverifyctx
= pmeth
->verifyctx
;
2173 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
2174 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
2175 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
2178 const unsigned char *in
,
2182 *pencrypt_init
= pmeth
->encrypt_init
;
2184 *pencryptfn
= pmeth
->encrypt
;
2187 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
2188 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
2189 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
2192 const unsigned char *in
,
2196 *pdecrypt_init
= pmeth
->decrypt_init
;
2198 *pdecrypt
= pmeth
->decrypt
;
2201 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
2202 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
2203 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
2208 *pderive_init
= pmeth
->derive_init
;
2210 *pderive
= pmeth
->derive
;
2213 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
2214 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2216 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
2221 *pctrl
= pmeth
->ctrl
;
2223 *pctrl_str
= pmeth
->ctrl_str
;
2226 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD
*pmeth
,
2227 int (**digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2228 const unsigned char *tbs
, size_t tbslen
))
2231 *digestsign
= pmeth
->digestsign
;
2234 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD
*pmeth
,
2235 int (**digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2236 size_t siglen
, const unsigned char *tbs
,
2240 *digestverify
= pmeth
->digestverify
;
2243 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
2244 int (**pcheck
) (EVP_PKEY
*pkey
))
2247 *pcheck
= pmeth
->check
;
2250 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
2251 int (**pcheck
) (EVP_PKEY
*pkey
))
2254 *pcheck
= pmeth
->public_check
;
2257 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
2258 int (**pcheck
) (EVP_PKEY
*pkey
))
2261 *pcheck
= pmeth
->param_check
;
2264 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2265 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
2268 if (pdigest_custom
!= NULL
)
2269 *pdigest_custom
= pmeth
->digest_custom
;
2272 #endif /* FIPS_MODULE */