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
[] = {
52 # ifndef OPENSSL_NO_RSA
55 # ifndef OPENSSL_NO_DH
58 # ifndef OPENSSL_NO_DSA
61 # ifndef OPENSSL_NO_EC
64 # ifndef OPENSSL_NO_RSA
67 # ifndef OPENSSL_NO_DH
70 # ifndef OPENSSL_NO_EC
74 # ifndef OPENSSL_NO_EC
80 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
82 static int pmeth_func_cmp(const EVP_PKEY_METHOD
*const *a
, pmeth_fn
const *b
)
84 return ((*a
)->pkey_id
- ((**b
)())->pkey_id
);
87 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
89 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
90 const EVP_PKEY_METHOD
*const *b
)
92 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
95 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
99 const EVP_PKEY_METHOD
*t
= &tmp
;
102 if (app_pkey_methods
) {
104 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
106 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
108 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
109 sizeof(standard_methods
) /
111 if (ret
== NULL
|| *ret
== NULL
)
116 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
118 EVP_PKEY_METHOD
*pmeth
;
120 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
122 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
127 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
131 /* Three possible states: */
132 # define EVP_PKEY_STATE_UNKNOWN 0
133 # define EVP_PKEY_STATE_LEGACY 1
134 # define EVP_PKEY_STATE_PROVIDER 2
136 static int evp_pkey_ctx_state(EVP_PKEY_CTX
*ctx
)
138 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
139 return EVP_PKEY_STATE_UNKNOWN
;
141 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
142 && ctx
->op
.kex
.exchprovctx
!= NULL
)
143 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
144 && ctx
->op
.sig
.sigprovctx
!= NULL
)
145 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
146 && ctx
->op
.ciph
.ciphprovctx
!= NULL
)
147 || (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
148 && ctx
->op
.keymgmt
.genctx
!= NULL
)
149 || (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
150 && ctx
->op
.encap
.kemprovctx
!= NULL
))
151 return EVP_PKEY_STATE_PROVIDER
;
153 return EVP_PKEY_STATE_LEGACY
;
156 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype
,
161 if (*type
== NID_undef
)
162 *type
= evp_pkey_name2type(keytype
);
165 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT
*keymgmt
)
167 int type
= NID_undef
;
169 EVP_KEYMGMT_names_do_all(keymgmt
, help_get_legacy_alg_type_from_keymgmt
,
173 #endif /* FIPS_MODULE */
175 static EVP_PKEY_CTX
*int_ctx_new(OPENSSL_CTX
*libctx
,
176 EVP_PKEY
*pkey
, ENGINE
*e
,
177 const char *keytype
, const char *propquery
,
181 EVP_PKEY_CTX
*ret
= NULL
;
182 const EVP_PKEY_METHOD
*pmeth
= NULL
;
183 EVP_KEYMGMT
*keymgmt
= NULL
;
186 * If the given |pkey| is provided, we extract the keytype from its
187 * keymgmt and skip over the legacy code.
189 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
190 /* If we have an engine, something went wrong somewhere... */
191 if (!ossl_assert(e
== NULL
))
193 keytype
= evp_first_name(pkey
->keymgmt
->prov
, pkey
->keymgmt
->name_id
);
199 * TODO(3.0) This legacy code section should be removed when we stop
206 else if (keytype
!= NULL
)
207 id
= evp_pkey_name2type(keytype
);
211 /* If no ID was found here, we can only resort to find a keymgmt */
216 * Here, we extract what information we can for the purpose of
217 * supporting usage with implementations from providers, to make
218 * for a smooth transition from legacy stuff to provider based stuff.
220 * If an engine is given, this is entirely legacy, and we should not
221 * pretend anything else, so we only set the name when no engine is
222 * given. If both are already given, someone made a mistake, and
223 * since that can only happen internally, it's safe to make an
226 if (!ossl_assert(e
== NULL
|| keytype
== NULL
))
229 keytype
= OBJ_nid2sn(id
);
231 # ifndef OPENSSL_NO_ENGINE
232 if (e
== NULL
&& pkey
!= NULL
)
233 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
234 /* Try to find an ENGINE which implements this method */
236 if (!ENGINE_init(e
)) {
237 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
241 e
= ENGINE_get_pkey_meth_engine(id
);
245 * If an ENGINE handled this method look it up. Otherwise use internal
249 pmeth
= ENGINE_get_pkey_meth(e
, id
);
252 pmeth
= EVP_PKEY_meth_find(id
);
255 #endif /* FIPS_MODULE */
258 * If there's no engine and there's a name, we try fetching a provider
261 if (e
== NULL
&& keytype
!= NULL
) {
262 keymgmt
= EVP_KEYMGMT_fetch(libctx
, keytype
, propquery
);
264 return NULL
; /* EVP_KEYMGMT_fetch() recorded an error */
268 * Chase down the legacy NID, as that might be needed for diverse
269 * purposes, such as ensure that EVP_PKEY_type() can return sensible
270 * values, or that there's a better chance to "downgrade" a key when
271 * needed. We go through all keymgmt names, because the keytype
272 * that's passed to this function doesn't necessarily translate
274 * TODO: Remove this when #legacy keys are gone.
276 if (keymgmt
!= NULL
) {
277 int tmp_id
= get_legacy_alg_type_from_keymgmt(keymgmt
);
279 if (tmp_id
!= NID_undef
) {
284 * It really really shouldn't differ. If it still does,
285 * something is very wrong.
287 if (!ossl_assert(id
== tmp_id
)) {
288 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_INTERNAL_ERROR
);
289 EVP_KEYMGMT_free(keymgmt
);
298 if (pmeth
== NULL
&& keymgmt
== NULL
) {
299 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
301 ret
= OPENSSL_zalloc(sizeof(*ret
));
303 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
306 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
307 if ((ret
== NULL
|| pmeth
== NULL
) && e
!= NULL
)
312 EVP_KEYMGMT_free(keymgmt
);
316 ret
->libctx
= libctx
;
317 ret
->propquery
= propquery
;
318 ret
->keytype
= keytype
;
319 ret
->keymgmt
= keymgmt
;
320 ret
->legacy_keytype
= id
; /* TODO: Remove when #legacy key are gone */
323 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
326 EVP_PKEY_up_ref(pkey
);
328 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
329 if (pmeth
->init(ret
) <= 0) {
331 EVP_PKEY_CTX_free(ret
);
339 /*- All methods below can also be used in FIPS_MODULE */
341 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_name(OPENSSL_CTX
*libctx
,
343 const char *propquery
)
345 return int_ctx_new(libctx
, NULL
, NULL
, name
, propquery
, -1);
348 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX
*libctx
, EVP_PKEY
*pkey
,
349 const char *propquery
)
351 return int_ctx_new(libctx
, pkey
, NULL
, NULL
, propquery
, -1);
354 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
356 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
357 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
358 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
359 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
360 ctx
->op
.sig
.sigprovctx
= NULL
;
361 ctx
->op
.sig
.signature
= NULL
;
362 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
363 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
364 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
365 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
366 ctx
->op
.kex
.exchprovctx
= NULL
;
367 ctx
->op
.kex
.exchange
= NULL
;
368 } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
369 if (ctx
->op
.encap
.kemprovctx
!= NULL
&& ctx
->op
.encap
.kem
!= NULL
)
370 ctx
->op
.encap
.kem
->freectx(ctx
->op
.encap
.kemprovctx
);
371 EVP_KEM_free(ctx
->op
.encap
.kem
);
372 ctx
->op
.encap
.kemprovctx
= NULL
;
373 ctx
->op
.encap
.kem
= NULL
;
375 else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
376 if (ctx
->op
.ciph
.ciphprovctx
!= NULL
&& ctx
->op
.ciph
.cipher
!= NULL
)
377 ctx
->op
.ciph
.cipher
->freectx(ctx
->op
.ciph
.ciphprovctx
);
378 EVP_ASYM_CIPHER_free(ctx
->op
.ciph
.cipher
);
379 ctx
->op
.ciph
.ciphprovctx
= NULL
;
380 ctx
->op
.ciph
.cipher
= NULL
;
381 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
382 if (ctx
->op
.keymgmt
.genctx
!= NULL
&& ctx
->keymgmt
!= NULL
)
383 evp_keymgmt_gen_cleanup(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
);
387 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
391 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
392 ctx
->pmeth
->cleanup(ctx
);
394 evp_pkey_ctx_free_old_ops(ctx
);
396 evp_pkey_ctx_free_all_cached_data(ctx
);
398 EVP_KEYMGMT_free(ctx
->keymgmt
);
400 EVP_PKEY_free(ctx
->pkey
);
401 EVP_PKEY_free(ctx
->peerkey
);
402 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
403 ENGINE_finish(ctx
->engine
);
410 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
411 const EVP_PKEY_METHOD
*meth
)
414 *ppkey_id
= meth
->pkey_id
;
416 *pflags
= meth
->flags
;
419 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
421 int pkey_id
= dst
->pkey_id
;
422 int flags
= dst
->flags
;
426 /* We only copy the function pointers so restore the other values */
427 dst
->pkey_id
= pkey_id
;
431 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
433 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
437 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
439 return int_ctx_new(NULL
, pkey
, e
, NULL
, NULL
, -1);
442 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
444 return int_ctx_new(NULL
, NULL
, e
, NULL
, NULL
, id
);
447 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
451 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
452 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
453 && pctx
->op
.kex
.exchprovctx
== NULL
)
454 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
455 && pctx
->op
.sig
.sigprovctx
== NULL
)))
457 # ifndef OPENSSL_NO_ENGINE
458 /* Make sure it's safe to copy a pkey context using an ENGINE */
459 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
460 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
464 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
466 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
470 if (pctx
->pkey
!= NULL
)
471 EVP_PKEY_up_ref(pctx
->pkey
);
472 rctx
->pkey
= pctx
->pkey
;
473 rctx
->operation
= pctx
->operation
;
474 rctx
->libctx
= pctx
->libctx
;
475 rctx
->keytype
= pctx
->keytype
;
476 rctx
->propquery
= pctx
->propquery
;
478 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
479 if (pctx
->op
.kex
.exchange
!= NULL
) {
480 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
481 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
486 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
487 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
489 rctx
->op
.kex
.exchprovctx
490 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
491 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
492 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
)) {
506 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
507 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
509 rctx
->op
.sig
.sigprovctx
510 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
511 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
512 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
518 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx
)) {
519 if (pctx
->op
.ciph
.cipher
!= NULL
) {
520 rctx
->op
.ciph
.cipher
= pctx
->op
.ciph
.cipher
;
521 if (!EVP_ASYM_CIPHER_up_ref(rctx
->op
.ciph
.cipher
)) {
526 if (pctx
->op
.ciph
.ciphprovctx
!= NULL
) {
527 if (!ossl_assert(pctx
->op
.ciph
.cipher
!= NULL
))
529 rctx
->op
.ciph
.ciphprovctx
530 = pctx
->op
.ciph
.cipher
->dupctx(pctx
->op
.ciph
.ciphprovctx
);
531 if (rctx
->op
.ciph
.ciphprovctx
== NULL
) {
532 EVP_ASYM_CIPHER_free(rctx
->op
.ciph
.cipher
);
538 } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx
)) {
539 if (pctx
->op
.encap
.kem
!= NULL
) {
540 rctx
->op
.encap
.kem
= pctx
->op
.encap
.kem
;
541 if (!EVP_KEM_up_ref(rctx
->op
.encap
.kem
)) {
546 if (pctx
->op
.encap
.kemprovctx
!= NULL
) {
547 if (!ossl_assert(pctx
->op
.encap
.kem
!= NULL
))
549 rctx
->op
.encap
.kemprovctx
550 = pctx
->op
.encap
.kem
->dupctx(pctx
->op
.encap
.kemprovctx
);
551 if (rctx
->op
.encap
.kemprovctx
== NULL
) {
552 EVP_KEM_free(rctx
->op
.encap
.kem
);
560 rctx
->pmeth
= pctx
->pmeth
;
561 # ifndef OPENSSL_NO_ENGINE
562 rctx
->engine
= pctx
->engine
;
566 EVP_PKEY_up_ref(pctx
->peerkey
);
567 rctx
->peerkey
= pctx
->peerkey
;
569 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
573 EVP_PKEY_CTX_free(rctx
);
578 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
580 if (app_pkey_methods
== NULL
) {
581 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
582 if (app_pkey_methods
== NULL
){
583 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
587 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
588 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
591 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
595 void evp_app_cleanup_int(void)
597 if (app_pkey_methods
!= NULL
)
598 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
601 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
603 const EVP_PKEY_METHOD
*ret
;
605 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
607 return ret
== NULL
? 0 : 1;
610 size_t EVP_PKEY_meth_get_count(void)
612 size_t rv
= OSSL_NELEM(standard_methods
);
614 if (app_pkey_methods
)
615 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
619 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
621 if (idx
< OSSL_NELEM(standard_methods
))
622 return (standard_methods
[idx
])();
623 if (app_pkey_methods
== NULL
)
625 idx
-= OSSL_NELEM(standard_methods
);
626 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
628 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
632 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
634 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
635 && ctx
->op
.kex
.exchprovctx
!= NULL
636 && ctx
->op
.kex
.exchange
!= NULL
637 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
638 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
640 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
641 && ctx
->op
.sig
.sigprovctx
!= NULL
642 && ctx
->op
.sig
.signature
!= NULL
643 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
644 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
646 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
647 && ctx
->op
.ciph
.ciphprovctx
!= NULL
648 && ctx
->op
.ciph
.cipher
!= NULL
649 && ctx
->op
.ciph
.cipher
->set_ctx_params
!= NULL
)
650 return ctx
->op
.ciph
.cipher
->set_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
652 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
653 && ctx
->op
.keymgmt
.genctx
!= NULL
654 && ctx
->keymgmt
!= NULL
655 && ctx
->keymgmt
->gen_set_params
!= NULL
)
656 return evp_keymgmt_gen_set_params(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
,
658 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
659 && ctx
->op
.encap
.kemprovctx
!= NULL
660 && ctx
->op
.encap
.kem
!= NULL
661 && ctx
->op
.encap
.kem
->set_ctx_params
!= NULL
)
662 return ctx
->op
.encap
.kem
->set_ctx_params(ctx
->op
.encap
.kemprovctx
,
667 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
669 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
670 && ctx
->op
.kex
.exchprovctx
!= NULL
671 && ctx
->op
.kex
.exchange
!= NULL
672 && ctx
->op
.kex
.exchange
->get_ctx_params
!= NULL
)
673 return ctx
->op
.kex
.exchange
->get_ctx_params(ctx
->op
.kex
.exchprovctx
,
675 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
676 && ctx
->op
.sig
.sigprovctx
!= NULL
677 && ctx
->op
.sig
.signature
!= NULL
678 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
679 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
681 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
682 && ctx
->op
.ciph
.ciphprovctx
!= NULL
683 && ctx
->op
.ciph
.cipher
!= NULL
684 && ctx
->op
.ciph
.cipher
->get_ctx_params
!= NULL
)
685 return ctx
->op
.ciph
.cipher
->get_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
687 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
688 && ctx
->op
.encap
.kemprovctx
!= NULL
689 && ctx
->op
.encap
.kem
!= NULL
690 && ctx
->op
.encap
.kem
->get_ctx_params
!= NULL
)
691 return ctx
->op
.encap
.kem
->get_ctx_params(ctx
->op
.encap
.kemprovctx
,
697 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
701 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
702 && ctx
->op
.kex
.exchange
!= NULL
703 && ctx
->op
.kex
.exchange
->gettable_ctx_params
!= NULL
) {
704 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
705 return ctx
->op
.kex
.exchange
->gettable_ctx_params(provctx
);
707 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
708 && ctx
->op
.sig
.signature
!= NULL
709 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
) {
710 provctx
= ossl_provider_ctx(
711 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
712 return ctx
->op
.sig
.signature
->gettable_ctx_params(provctx
);
714 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
715 && ctx
->op
.ciph
.cipher
!= NULL
716 && ctx
->op
.ciph
.cipher
->gettable_ctx_params
!= NULL
) {
717 provctx
= ossl_provider_ctx(
718 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
719 return ctx
->op
.ciph
.cipher
->gettable_ctx_params(provctx
);
721 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
722 && ctx
->op
.encap
.kem
!= NULL
723 && ctx
->op
.encap
.kem
->gettable_ctx_params
!= NULL
) {
724 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
725 return ctx
->op
.encap
.kem
->gettable_ctx_params(provctx
);
730 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
734 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
735 && ctx
->op
.kex
.exchange
!= NULL
736 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
) {
737 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
738 return ctx
->op
.kex
.exchange
->settable_ctx_params(provctx
);
740 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
741 && ctx
->op
.sig
.signature
!= NULL
742 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
) {
743 provctx
= ossl_provider_ctx(
744 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
745 return ctx
->op
.sig
.signature
->settable_ctx_params(provctx
);
747 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
748 && ctx
->op
.ciph
.cipher
!= NULL
749 && ctx
->op
.ciph
.cipher
->settable_ctx_params
!= NULL
) {
750 provctx
= ossl_provider_ctx(
751 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
752 return ctx
->op
.ciph
.cipher
->settable_ctx_params(provctx
);
754 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
755 && ctx
->keymgmt
!= NULL
)
756 return EVP_KEYMGMT_gen_settable_params(ctx
->keymgmt
);
757 if (EVP_PKEY_CTX_IS_KEM_OP(ctx
)
758 && ctx
->op
.encap
.kem
!= NULL
759 && ctx
->op
.encap
.kem
->settable_ctx_params
!= NULL
) {
760 provctx
= ossl_provider_ctx(EVP_KEM_provider(ctx
->op
.encap
.kem
));
761 return ctx
->op
.encap
.kem
->settable_ctx_params(provctx
);
767 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
769 * Return 1 on success, 0 or negative for errors.
771 * In particular they return -2 if any of the params is not supported.
773 * They are not available in FIPS_MODULE as they depend on
774 * - EVP_PKEY_CTX_{get,set}_params()
775 * - EVP_PKEY_CTX_{gettable,settable}_params()
778 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
782 if (ctx
== NULL
|| params
== NULL
)
785 for (p
= params
; p
->key
!= NULL
; p
++) {
786 /* Check the ctx actually understands this parameter */
787 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx
),
792 return EVP_PKEY_CTX_set_params(ctx
, params
);
795 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
799 if (ctx
== NULL
|| params
== NULL
)
802 for (p
= params
; p
->key
!= NULL
; p
++ ) {
803 /* Check the ctx actually understands this parameter */
804 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx
),
809 return EVP_PKEY_CTX_get_params(ctx
, params
);
812 # ifndef OPENSSL_NO_DH
813 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
815 OSSL_PARAM dh_pad_params
[2];
816 unsigned int upad
= pad
;
818 /* We use EVP_PKEY_CTX_ctrl return values */
819 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
820 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
824 /* TODO(3.0): Remove this eventually when no more legacy */
825 if (ctx
->op
.kex
.exchprovctx
== NULL
)
826 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
827 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
829 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
830 dh_pad_params
[1] = OSSL_PARAM_construct_end();
832 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
836 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
838 OSSL_PARAM sig_md_params
[2], *p
= sig_md_params
;
839 /* 80 should be big enough */
843 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
844 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
845 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
849 /* TODO(3.0): Remove this eventually when no more legacy */
850 if (ctx
->op
.sig
.sigprovctx
== NULL
)
851 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
852 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
854 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
857 *p
= OSSL_PARAM_construct_end();
859 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
862 tmp
= evp_get_digestbyname_ex(ctx
->libctx
, name
);
871 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
,
872 int fallback
, const char *param
, int op
,
875 OSSL_PARAM md_params
[2], *p
= md_params
;
878 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
879 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
880 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
884 /* TODO(3.0): Remove this eventually when no more legacy */
886 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, 0, (void *)(md
));
891 name
= EVP_MD_name(md
);
894 *p
++ = OSSL_PARAM_construct_utf8_string(param
,
896 * Cast away the const. This is read
897 * only so should be safe
900 *p
= OSSL_PARAM_construct_end();
902 return EVP_PKEY_CTX_set_params(ctx
, md_params
);
905 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
907 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.sig
.sigprovctx
== NULL
,
908 OSSL_SIGNATURE_PARAM_DIGEST
,
909 EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
);
912 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
914 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
915 OSSL_KDF_PARAM_DIGEST
,
916 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_TLS_MD
);
919 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX
*ctx
, int fallback
,
920 const char *param
, int op
, int ctrl
,
921 const unsigned char *data
,
924 OSSL_PARAM octet_string_params
[2], *p
= octet_string_params
;
926 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
927 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
928 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
932 /* TODO(3.0): Remove this eventually when no more legacy */
934 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, datalen
, (void *)(data
));
937 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_LENGTH
);
941 *p
++ = OSSL_PARAM_construct_octet_string(param
,
943 * Cast away the const. This is read
944 * only so should be safe
946 (unsigned char *)data
,
948 *p
= OSSL_PARAM_construct_end();
950 return EVP_PKEY_CTX_set_params(ctx
, octet_string_params
);
953 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX
*ctx
,
954 const unsigned char *sec
, int seclen
)
956 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
957 OSSL_KDF_PARAM_SECRET
,
959 EVP_PKEY_CTRL_TLS_SECRET
,
963 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX
*ctx
,
964 const unsigned char *seed
, int seedlen
)
966 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
969 EVP_PKEY_CTRL_TLS_SEED
,
973 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
975 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
976 OSSL_KDF_PARAM_DIGEST
,
977 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_HKDF_MD
);
980 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX
*ctx
,
981 const unsigned char *salt
, int saltlen
)
983 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
986 EVP_PKEY_CTRL_HKDF_SALT
,
990 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX
*ctx
,
991 const unsigned char *key
, int keylen
)
993 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
996 EVP_PKEY_CTRL_HKDF_KEY
,
1000 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX
*ctx
,
1001 const unsigned char *info
, int infolen
)
1003 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1004 OSSL_KDF_PARAM_INFO
,
1006 EVP_PKEY_CTRL_HKDF_INFO
,
1010 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX
*ctx
, int mode
)
1012 OSSL_PARAM int_params
[2], *p
= int_params
;
1014 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1015 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1016 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1020 /* TODO(3.0): Remove this eventually when no more legacy */
1021 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1022 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_DERIVE
,
1023 EVP_PKEY_CTRL_HKDF_MODE
, mode
, NULL
);
1027 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1031 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
1032 *p
= OSSL_PARAM_construct_end();
1034 return EVP_PKEY_CTX_set_params(ctx
, int_params
);
1037 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX
*ctx
, const char *pass
,
1040 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1041 OSSL_KDF_PARAM_PASSWORD
,
1044 (const unsigned char *)pass
, passlen
);
1047 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX
*ctx
,
1048 const unsigned char *salt
, int saltlen
)
1050 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1051 OSSL_KDF_PARAM_SALT
,
1053 EVP_PKEY_CTRL_SCRYPT_SALT
,
1057 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX
*ctx
, const char *param
,
1058 int op
, int ctrl
, uint64_t val
)
1060 OSSL_PARAM uint64_params
[2], *p
= uint64_params
;
1062 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1063 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1064 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1068 /* TODO(3.0): Remove this eventually when no more legacy */
1069 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1070 return EVP_PKEY_CTX_ctrl_uint64(ctx
, -1, op
, ctrl
, val
);
1072 *p
++ = OSSL_PARAM_construct_uint64(param
, &val
);
1073 *p
= OSSL_PARAM_construct_end();
1075 return EVP_PKEY_CTX_set_params(ctx
, uint64_params
);
1078 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX
*ctx
, uint64_t n
)
1080 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_N
,
1081 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_N
,
1085 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX
*ctx
, uint64_t r
)
1087 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_R
,
1088 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_R
,
1092 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX
*ctx
, uint64_t p
)
1094 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_P
,
1095 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_P
,
1099 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX
*ctx
,
1100 uint64_t maxmem_bytes
)
1102 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_MAXMEM
,
1104 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
,
1108 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX
*ctx
, const unsigned char *key
,
1111 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.keymgmt
.genctx
== NULL
,
1112 OSSL_PKEY_PARAM_PRIV_KEY
,
1114 EVP_PKEY_CTRL_SET_MAC_KEY
,
1118 int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX
*ctx
, const char *op
)
1120 OSSL_PARAM params
[2], *p
= params
;
1122 if (ctx
== NULL
|| op
== NULL
) {
1123 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1126 if (!EVP_PKEY_CTX_IS_KEM_OP(ctx
)) {
1127 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1130 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION
,
1132 *p
= OSSL_PARAM_construct_end();
1133 return EVP_PKEY_CTX_set_params(ctx
, params
);
1136 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1138 OSSL_PARAM params
[2], *p
= params
;
1141 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1142 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1143 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1147 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID
,
1149 * Cast away the const. This is
1150 * read only so should be safe
1152 (void *)id
, (size_t)len
);
1153 *p
++ = OSSL_PARAM_construct_end();
1155 ret
= evp_pkey_ctx_set_params_strict(ctx
, params
);
1157 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1161 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1163 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1164 EVP_PKEY_CTRL_SET1_ID
, (int)len
, (void*)(id
));
1167 static int get1_id_data(EVP_PKEY_CTX
*ctx
, void *id
, size_t *id_len
)
1170 void *tmp_id
= NULL
;
1171 OSSL_PARAM params
[2], *p
= params
;
1173 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1174 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1175 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1179 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID
,
1181 *p
++ = OSSL_PARAM_construct_end();
1183 ret
= evp_pkey_ctx_get_params_strict(ctx
, params
);
1185 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1186 } else if (ret
> 0) {
1187 size_t tmp_id_len
= params
[0].return_size
;
1190 memcpy(id
, tmp_id
, tmp_id_len
);
1192 *id_len
= tmp_id_len
;
1197 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX
*ctx
, void *id
)
1199 return get1_id_data(ctx
, id
, NULL
);
1202 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1204 return get1_id_data(ctx
, NULL
, id_len
);
1207 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX
*ctx
, void *id
)
1209 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET1_ID
, 0, (void*)id
);
1212 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1214 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1215 EVP_PKEY_CTRL_GET1_ID_LEN
, 0, (void*)id_len
);
1218 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1219 int cmd
, int p1
, void *p2
)
1222 case EVP_PKEY_CTRL_SET1_ID
:
1223 return evp_pkey_ctx_set1_id_prov(ctx
, p2
, p1
);
1224 case EVP_PKEY_CTRL_GET1_ID
:
1225 return evp_pkey_ctx_get1_id_prov(ctx
, p2
);
1226 case EVP_PKEY_CTRL_GET1_ID_LEN
:
1227 return evp_pkey_ctx_get1_id_len_prov(ctx
, p2
);
1230 # ifndef OPENSSL_NO_DH
1231 if (keytype
== EVP_PKEY_DHX
) {
1233 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
1234 return EVP_PKEY_CTX_set_dh_kdf_type(ctx
, p1
);
1235 case EVP_PKEY_CTRL_DH_KDF_MD
:
1236 return EVP_PKEY_CTX_set_dh_kdf_md(ctx
, p2
);
1237 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
1238 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx
, p1
);
1239 case EVP_PKEY_CTRL_DH_KDF_UKM
:
1240 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx
, p2
, p1
);
1241 case EVP_PKEY_CTRL_DH_KDF_OID
:
1242 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx
, p2
);
1243 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
1244 return EVP_PKEY_CTX_get_dh_kdf_md(ctx
, p2
);
1245 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
1246 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx
, p2
);
1247 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
1248 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx
, p2
);
1249 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
1250 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx
, p2
);
1253 if (keytype
== EVP_PKEY_DH
) {
1255 case EVP_PKEY_CTRL_DH_PAD
:
1256 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
1257 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
1258 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, p1
);
1259 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
1260 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, p1
);
1261 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
1262 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, p1
);
1263 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
1264 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, p1
);
1265 case EVP_PKEY_CTRL_DH_RFC5114
:
1266 return EVP_PKEY_CTX_set_dh_rfc5114(ctx
, p1
);
1270 # ifndef OPENSSL_NO_DSA
1271 if (keytype
== EVP_PKEY_DSA
) {
1273 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS
:
1274 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx
, p1
);
1275 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS
:
1276 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx
, p1
);
1277 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD
:
1278 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx
, p2
);
1282 # ifndef OPENSSL_NO_EC
1283 if (keytype
== EVP_PKEY_EC
) {
1285 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
1286 return evp_pkey_ctx_set_ec_param_enc_prov(ctx
, p1
);
1287 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
1288 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, p1
);
1289 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
1291 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx
);
1292 } else if (p1
< -1 || p1
> 1) {
1293 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1296 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, p1
);
1298 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
1300 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx
);
1302 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx
, p1
);
1304 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
1305 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx
, p2
);
1306 case EVP_PKEY_CTRL_EC_KDF_MD
:
1307 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, p2
);
1308 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
1309 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx
, p2
);
1310 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
1311 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx
, p1
);
1312 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
1313 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx
, p2
);
1314 case EVP_PKEY_CTRL_EC_KDF_UKM
:
1315 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx
, p2
, p1
);
1319 if (keytype
== EVP_PKEY_RSA
) {
1321 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
1322 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1323 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
1324 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1325 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
1326 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1327 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
1328 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, p2
, p1
);
1329 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
1330 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx
, (unsigned char **)p2
);
1331 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
1332 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, p1
);
1333 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
1334 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, p2
);
1335 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
1336 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, p1
);
1340 * keytype == -1 is used when several key types share the same structure,
1341 * or for generic controls that are the same across multiple key types.
1343 if (keytype
== -1) {
1344 if (optype
== EVP_PKEY_OP_DERIVE
) {
1347 case EVP_PKEY_CTRL_TLS_MD
:
1348 return EVP_PKEY_CTX_set_tls1_prf_md(ctx
, p2
);
1349 case EVP_PKEY_CTRL_TLS_SECRET
:
1350 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx
, p2
, p1
);
1351 case EVP_PKEY_CTRL_TLS_SEED
:
1352 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx
, p2
, p1
);
1355 case EVP_PKEY_CTRL_HKDF_MD
:
1356 return EVP_PKEY_CTX_set_hkdf_md(ctx
, p2
);
1357 case EVP_PKEY_CTRL_HKDF_SALT
:
1358 return EVP_PKEY_CTX_set1_hkdf_salt(ctx
, p2
, p1
);
1359 case EVP_PKEY_CTRL_HKDF_KEY
:
1360 return EVP_PKEY_CTX_set1_hkdf_key(ctx
, p2
, p1
);
1361 case EVP_PKEY_CTRL_HKDF_INFO
:
1362 return EVP_PKEY_CTX_add1_hkdf_info(ctx
, p2
, p1
);
1363 case EVP_PKEY_CTRL_HKDF_MODE
:
1364 return EVP_PKEY_CTX_hkdf_mode(ctx
, p1
);
1367 case EVP_PKEY_CTRL_PASS
:
1368 return EVP_PKEY_CTX_set1_pbe_pass(ctx
, p2
, p1
);
1369 case EVP_PKEY_CTRL_SCRYPT_SALT
:
1370 return EVP_PKEY_CTX_set1_scrypt_salt(ctx
, p2
, p1
);
1371 case EVP_PKEY_CTRL_SCRYPT_N
:
1372 return EVP_PKEY_CTX_set_scrypt_N(ctx
, p1
);
1373 case EVP_PKEY_CTRL_SCRYPT_R
:
1374 return EVP_PKEY_CTX_set_scrypt_r(ctx
, p1
);
1375 case EVP_PKEY_CTRL_SCRYPT_P
:
1376 return EVP_PKEY_CTX_set_scrypt_p(ctx
, p1
);
1377 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
:
1378 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx
, p1
);
1380 } else if (optype
== EVP_PKEY_OP_KEYGEN
) {
1381 OSSL_PARAM params
[2], *p
= params
;
1384 case EVP_PKEY_CTRL_CIPHER
:
1386 char *ciphname
= (char *)EVP_CIPHER_name(p2
);
1388 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
1390 *p
= OSSL_PARAM_construct_end();
1392 return EVP_PKEY_CTX_set_params(ctx
, params
);
1394 case EVP_PKEY_CTRL_SET_MAC_KEY
:
1396 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
1398 *p
= OSSL_PARAM_construct_end();
1400 return EVP_PKEY_CTX_set_params(ctx
, params
);
1405 case EVP_PKEY_CTRL_MD
:
1406 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
1407 case EVP_PKEY_CTRL_GET_MD
:
1408 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
1409 case EVP_PKEY_CTRL_RSA_PADDING
:
1410 return EVP_PKEY_CTX_set_rsa_padding(ctx
, p1
);
1411 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
1412 return EVP_PKEY_CTX_get_rsa_padding(ctx
, p2
);
1413 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
1414 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1415 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
1416 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, p1
);
1417 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
1418 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx
, p2
);
1419 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
1420 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
1421 # ifndef OPENSSL_NO_CMS
1422 case EVP_PKEY_CTRL_CMS_DECRYPT
:
1423 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
1425 /* TODO (3.0) Temporary hack, this should probe */
1426 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx
), "RSASSA-PSS"))
1428 ERR_raise(ERR_LIB_EVP
,
1429 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
1435 * GOST CMS format is different for different cipher algorithms.
1436 * Most of other algorithms don't have such a difference
1437 * so this ctrl is just ignored.
1439 if (cmd
== EVP_PKEY_CTRL_CIPHER
)
1445 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1446 int cmd
, int p1
, void *p2
)
1451 * If the method has a |digest_custom| function, we can relax the
1452 * operation type check, since this can be called before the operation
1455 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->digest_custom
== NULL
) {
1456 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
1457 EVPerr(0, EVP_R_NO_OPERATION_SET
);
1461 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
1462 EVPerr(0, EVP_R_INVALID_OPERATION
);
1467 switch (evp_pkey_ctx_state(ctx
)) {
1468 case EVP_PKEY_STATE_PROVIDER
:
1469 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1470 case EVP_PKEY_STATE_UNKNOWN
:
1471 case EVP_PKEY_STATE_LEGACY
:
1472 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
1473 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1476 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
1479 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
1482 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1488 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1489 int cmd
, int p1
, void *p2
)
1494 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1497 /* If unsupported, we don't want that reported here */
1499 ret
= evp_pkey_ctx_store_cached_data(ctx
, keytype
, optype
,
1504 ERR_clear_last_mark();
1506 * If there was an error, there was an error.
1507 * If the operation isn't initialized yet, we also return, as
1508 * the saved values will be used then anyway.
1510 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1513 return evp_pkey_ctx_ctrl_int(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1516 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1517 int cmd
, uint64_t value
)
1519 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
1522 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
1525 if (strcmp(name
, "md") == 0)
1526 name
= OSSL_ALG_PARAM_DIGEST
;
1527 else if (strcmp(name
, "rsa_padding_mode") == 0)
1528 name
= OSSL_ASYM_CIPHER_PARAM_PAD_MODE
;
1529 else if (strcmp(name
, "rsa_mgf1_md") == 0)
1530 name
= OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
;
1531 else if (strcmp(name
, "rsa_oaep_md") == 0)
1532 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
;
1533 else if (strcmp(name
, "rsa_oaep_label") == 0)
1534 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
;
1535 else if (strcmp(name
, "rsa_pss_saltlen") == 0)
1536 name
= OSSL_SIGNATURE_PARAM_PSS_SALTLEN
;
1537 else if (strcmp(name
, "rsa_keygen_bits") == 0)
1538 name
= OSSL_PKEY_PARAM_RSA_BITS
;
1539 else if (strcmp(name
, "rsa_keygen_pubexp") == 0)
1540 name
= OSSL_PKEY_PARAM_RSA_E
;
1541 else if (strcmp(name
, "rsa_keygen_primes") == 0)
1542 name
= OSSL_PKEY_PARAM_RSA_PRIMES
;
1543 else if (strcmp(name
, "rsa_pss_keygen_md") == 0)
1544 name
= OSSL_PKEY_PARAM_RSA_DIGEST
;
1545 else if (strcmp(name
, "rsa_pss_keygen_mgf1_md") == 0)
1546 name
= OSSL_PKEY_PARAM_RSA_MGF1_DIGEST
;
1547 else if (strcmp(name
, "rsa_pss_keygen_saltlen") == 0)
1548 name
= OSSL_PKEY_PARAM_RSA_PSS_SALTLEN
;
1549 # ifndef OPENSSL_NO_DSA
1550 else if (strcmp(name
, "dsa_paramgen_bits") == 0)
1551 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1552 else if (strcmp(name
, "dsa_paramgen_q_bits") == 0)
1553 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1554 else if (strcmp(name
, "dsa_paramgen_md") == 0)
1555 name
= OSSL_PKEY_PARAM_FFC_DIGEST
;
1557 # ifndef OPENSSL_NO_DH
1558 else if (strcmp(name
, "dh_paramgen_generator") == 0)
1559 name
= OSSL_PKEY_PARAM_DH_GENERATOR
;
1560 else if (strcmp(name
, "dh_paramgen_prime_len") == 0)
1561 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1562 else if (strcmp(name
, "dh_paramgen_subprime_len") == 0)
1563 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1564 else if (strcmp(name
, "dh_paramgen_type") == 0) {
1565 name
= OSSL_PKEY_PARAM_FFC_TYPE
;
1566 value
= dh_gen_type_id2name(atoi(value
));
1567 } else if (strcmp(name
, "dh_param") == 0)
1568 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1569 else if (strcmp(name
, "dh_rfc5114") == 0) {
1570 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1571 value
= ffc_named_group_from_uid(atoi(value
));
1572 } else if (strcmp(name
, "dh_pad") == 0)
1573 name
= OSSL_EXCHANGE_PARAM_PAD
;
1575 # ifndef OPENSSL_NO_EC
1576 else if (strcmp(name
, "ec_paramgen_curve") == 0)
1577 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1578 else if (strcmp(name
, "ecdh_cofactor_mode") == 0)
1579 name
= OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
;
1580 else if (strcmp(name
, "ecdh_kdf_md") == 0)
1581 name
= OSSL_EXCHANGE_PARAM_KDF_DIGEST
;
1582 else if (strcmp(name
, "ec_param_enc") == 0)
1583 name
= OSSL_PKEY_PARAM_EC_ENCODING
;
1585 else if (strcmp(name
, "N") == 0)
1586 name
= OSSL_KDF_PARAM_SCRYPT_N
;
1590 * TODO(3.0) reduce the code above to only translate known legacy
1591 * string to the corresponding core name (see core_names.h), but
1592 * otherwise leave it to this code block to do the actual work.
1594 const OSSL_PARAM
*settable
= EVP_PKEY_CTX_settable_params(ctx
);
1595 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1599 if (!OSSL_PARAM_allocate_from_text(¶ms
[0], settable
, name
, value
,
1600 strlen(value
), &exists
)) {
1602 ERR_raise_data(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
,
1603 "name=%s,value=%s", name
, value
);
1608 if (EVP_PKEY_CTX_set_params(ctx
, params
))
1610 OPENSSL_free(params
[0].data
);
1615 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX
*ctx
,
1616 const char *name
, const char *value
)
1621 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1625 switch (evp_pkey_ctx_state(ctx
)) {
1626 case EVP_PKEY_STATE_PROVIDER
:
1627 return legacy_ctrl_str_to_param(ctx
, name
, value
);
1628 case EVP_PKEY_STATE_UNKNOWN
:
1629 case EVP_PKEY_STATE_LEGACY
:
1630 if (ctx
== NULL
|| ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl_str
== NULL
) {
1631 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1634 if (strcmp(name
, "digest") == 0)
1635 ret
= EVP_PKEY_CTX_md(ctx
,
1636 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1637 EVP_PKEY_CTRL_MD
, value
);
1639 ret
= ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
1646 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
1647 const char *name
, const char *value
)
1651 /* If unsupported, we don't want that reported here */
1653 ret
= evp_pkey_ctx_store_cached_data(ctx
, -1, -1, -1,
1654 name
, value
, strlen(value
) + 1);
1658 ERR_clear_last_mark();
1660 * If there was an error, there was an error.
1661 * If the operation isn't initialized yet, we also return, as
1662 * the saved values will be used then anyway.
1664 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1668 return evp_pkey_ctx_ctrl_str_int(ctx
, name
, value
);
1671 static int decode_cmd(int cmd
, const char *name
)
1675 * The consequence of the assertion not being true is that this
1676 * function will return -1, which will cause the calling functions
1677 * to signal that the command is unsupported... in non-debug mode.
1679 if (ossl_assert(name
!= NULL
))
1680 if (strcmp(name
, "distid") == 0 || strcmp(name
, "hexdistid") == 0)
1681 cmd
= EVP_PKEY_CTRL_SET1_ID
;
1687 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
1688 int keytype
, int optype
,
1689 int cmd
, const char *name
,
1690 const void *data
, size_t data_len
)
1692 if ((keytype
!= -1 && ctx
->pmeth
->pkey_id
!= keytype
)
1693 || ((optype
!= -1) && !(ctx
->operation
& optype
))) {
1694 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1698 cmd
= decode_cmd(cmd
, name
);
1700 case EVP_PKEY_CTRL_SET1_ID
:
1701 evp_pkey_ctx_free_cached_data(ctx
, cmd
, name
);
1703 ctx
->cached_parameters
.dist_id_name
= OPENSSL_strdup(name
);
1704 if (ctx
->cached_parameters
.dist_id_name
== NULL
) {
1705 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1710 ctx
->cached_parameters
.dist_id
= OPENSSL_memdup(data
, data_len
);
1711 if (ctx
->cached_parameters
.dist_id
== NULL
) {
1712 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1716 ctx
->cached_parameters
.dist_id_set
= 1;
1717 ctx
->cached_parameters
.dist_id_len
= data_len
;
1721 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1725 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
1726 int cmd
, const char *name
)
1728 cmd
= decode_cmd(cmd
, name
);
1730 case EVP_PKEY_CTRL_SET1_ID
:
1731 OPENSSL_free(ctx
->cached_parameters
.dist_id
);
1732 OPENSSL_free(ctx
->cached_parameters
.dist_id_name
);
1733 ctx
->cached_parameters
.dist_id
= NULL
;
1734 ctx
->cached_parameters
.dist_id_name
= NULL
;
1739 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
)
1741 evp_pkey_ctx_free_cached_data(ctx
, EVP_PKEY_CTRL_SET1_ID
, NULL
);
1744 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
)
1748 if (ret
&& ctx
->cached_parameters
.dist_id_set
) {
1749 const char *name
= ctx
->cached_parameters
.dist_id_name
;
1750 const void *val
= ctx
->cached_parameters
.dist_id
;
1751 size_t len
= ctx
->cached_parameters
.dist_id_len
;
1754 ret
= evp_pkey_ctx_ctrl_str_int(ctx
, name
, val
);
1756 ret
= evp_pkey_ctx_ctrl_int(ctx
, -1, ctx
->operation
,
1757 EVP_PKEY_CTRL_SET1_ID
,
1758 (int)len
, (void *)val
);
1764 /* Utility functions to send a string of hex string to a ctrl */
1766 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
1773 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
1776 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
1782 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1785 if (binlen
<= INT_MAX
)
1786 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
1791 /* Pass a message digest to a ctrl */
1792 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
1796 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
1797 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
1800 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
1803 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
1805 return ctx
->operation
;
1808 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
1810 ctx
->keygen_info
= dat
;
1811 ctx
->keygen_info_count
= datlen
;
1814 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
1819 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
1824 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
1829 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
1831 return ctx
->peerkey
;
1834 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
1836 ctx
->app_data
= data
;
1839 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
1841 return ctx
->app_data
;
1844 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
1845 int (*init
) (EVP_PKEY_CTX
*ctx
))
1850 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
1851 int (*copy
) (EVP_PKEY_CTX
*dst
,
1852 const EVP_PKEY_CTX
*src
))
1857 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
1858 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
1860 pmeth
->cleanup
= cleanup
;
1863 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
1864 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
1865 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
1868 pmeth
->paramgen_init
= paramgen_init
;
1869 pmeth
->paramgen
= paramgen
;
1872 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
1873 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
1874 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
1877 pmeth
->keygen_init
= keygen_init
;
1878 pmeth
->keygen
= keygen
;
1881 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
1882 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
1883 int (*sign
) (EVP_PKEY_CTX
*ctx
,
1884 unsigned char *sig
, size_t *siglen
,
1885 const unsigned char *tbs
,
1888 pmeth
->sign_init
= sign_init
;
1892 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
1893 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
1894 int (*verify
) (EVP_PKEY_CTX
*ctx
,
1895 const unsigned char *sig
,
1897 const unsigned char *tbs
,
1900 pmeth
->verify_init
= verify_init
;
1901 pmeth
->verify
= verify
;
1904 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
1905 int (*verify_recover_init
) (EVP_PKEY_CTX
1907 int (*verify_recover
) (EVP_PKEY_CTX
1916 pmeth
->verify_recover_init
= verify_recover_init
;
1917 pmeth
->verify_recover
= verify_recover
;
1920 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
1921 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
1923 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
1928 pmeth
->signctx_init
= signctx_init
;
1929 pmeth
->signctx
= signctx
;
1932 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
1933 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1935 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
1936 const unsigned char *sig
,
1940 pmeth
->verifyctx_init
= verifyctx_init
;
1941 pmeth
->verifyctx
= verifyctx
;
1944 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
1945 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
1946 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
1949 const unsigned char *in
,
1952 pmeth
->encrypt_init
= encrypt_init
;
1953 pmeth
->encrypt
= encryptfn
;
1956 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
1957 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
1958 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
1961 const unsigned char *in
,
1964 pmeth
->decrypt_init
= decrypt_init
;
1965 pmeth
->decrypt
= decrypt
;
1968 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
1969 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
1970 int (*derive
) (EVP_PKEY_CTX
*ctx
,
1974 pmeth
->derive_init
= derive_init
;
1975 pmeth
->derive
= derive
;
1978 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
1979 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1981 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
1986 pmeth
->ctrl_str
= ctrl_str
;
1989 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD
*pmeth
,
1990 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
1991 const unsigned char *tbs
, size_t tbslen
))
1993 pmeth
->digestsign
= digestsign
;
1996 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD
*pmeth
,
1997 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
1998 size_t siglen
, const unsigned char *tbs
,
2001 pmeth
->digestverify
= digestverify
;
2004 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
2005 int (*check
) (EVP_PKEY
*pkey
))
2007 pmeth
->check
= check
;
2010 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
2011 int (*check
) (EVP_PKEY
*pkey
))
2013 pmeth
->public_check
= check
;
2016 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
2017 int (*check
) (EVP_PKEY
*pkey
))
2019 pmeth
->param_check
= check
;
2022 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2023 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
2026 pmeth
->digest_custom
= digest_custom
;
2029 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
2030 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
2032 *pinit
= pmeth
->init
;
2035 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
2036 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
2037 const EVP_PKEY_CTX
*src
))
2039 *pcopy
= pmeth
->copy
;
2042 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
2043 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
2045 *pcleanup
= pmeth
->cleanup
;
2048 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
2049 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
2050 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
2054 *pparamgen_init
= pmeth
->paramgen_init
;
2056 *pparamgen
= pmeth
->paramgen
;
2059 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
2060 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
2061 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
2065 *pkeygen_init
= pmeth
->keygen_init
;
2067 *pkeygen
= pmeth
->keygen
;
2070 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
2071 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
2072 int (**psign
) (EVP_PKEY_CTX
*ctx
,
2073 unsigned char *sig
, size_t *siglen
,
2074 const unsigned char *tbs
,
2078 *psign_init
= pmeth
->sign_init
;
2080 *psign
= pmeth
->sign
;
2083 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
2084 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
2085 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
2086 const unsigned char *sig
,
2088 const unsigned char *tbs
,
2092 *pverify_init
= pmeth
->verify_init
;
2094 *pverify
= pmeth
->verify
;
2097 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
2098 int (**pverify_recover_init
) (EVP_PKEY_CTX
2100 int (**pverify_recover
) (EVP_PKEY_CTX
2109 if (pverify_recover_init
)
2110 *pverify_recover_init
= pmeth
->verify_recover_init
;
2111 if (pverify_recover
)
2112 *pverify_recover
= pmeth
->verify_recover
;
2115 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
2116 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
2118 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
2124 *psignctx_init
= pmeth
->signctx_init
;
2126 *psignctx
= pmeth
->signctx
;
2129 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
2130 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
2132 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
2133 const unsigned char *sig
,
2137 if (pverifyctx_init
)
2138 *pverifyctx_init
= pmeth
->verifyctx_init
;
2140 *pverifyctx
= pmeth
->verifyctx
;
2143 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
2144 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
2145 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
2148 const unsigned char *in
,
2152 *pencrypt_init
= pmeth
->encrypt_init
;
2154 *pencryptfn
= pmeth
->encrypt
;
2157 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
2158 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
2159 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
2162 const unsigned char *in
,
2166 *pdecrypt_init
= pmeth
->decrypt_init
;
2168 *pdecrypt
= pmeth
->decrypt
;
2171 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
2172 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
2173 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
2178 *pderive_init
= pmeth
->derive_init
;
2180 *pderive
= pmeth
->derive
;
2183 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
2184 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2186 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
2191 *pctrl
= pmeth
->ctrl
;
2193 *pctrl_str
= pmeth
->ctrl_str
;
2196 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD
*pmeth
,
2197 int (**digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2198 const unsigned char *tbs
, size_t tbslen
))
2201 *digestsign
= pmeth
->digestsign
;
2204 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD
*pmeth
,
2205 int (**digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2206 size_t siglen
, const unsigned char *tbs
,
2210 *digestverify
= pmeth
->digestverify
;
2213 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
2214 int (**pcheck
) (EVP_PKEY
*pkey
))
2217 *pcheck
= pmeth
->check
;
2220 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
2221 int (**pcheck
) (EVP_PKEY
*pkey
))
2224 *pcheck
= pmeth
->public_check
;
2227 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
2228 int (**pcheck
) (EVP_PKEY
*pkey
))
2231 *pcheck
= pmeth
->param_check
;
2234 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2235 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
2238 if (pdigest_custom
!= NULL
)
2239 *pdigest_custom
= pmeth
->digest_custom
;
2242 #endif /* FIPS_MODULE */