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
78 # ifndef OPENSSL_NO_SM2
83 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
85 static int pmeth_func_cmp(const EVP_PKEY_METHOD
*const *a
, pmeth_fn
const *b
)
87 return ((*a
)->pkey_id
- ((**b
)())->pkey_id
);
90 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
92 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
93 const EVP_PKEY_METHOD
*const *b
)
95 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
98 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
102 const EVP_PKEY_METHOD
*t
= &tmp
;
105 if (app_pkey_methods
) {
107 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
109 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
111 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
112 sizeof(standard_methods
) /
114 if (ret
== NULL
|| *ret
== NULL
)
119 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
121 EVP_PKEY_METHOD
*pmeth
;
123 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
125 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
130 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
134 /* Three possible states: */
135 # define EVP_PKEY_STATE_UNKNOWN 0
136 # define EVP_PKEY_STATE_LEGACY 1
137 # define EVP_PKEY_STATE_PROVIDER 2
139 static int evp_pkey_ctx_state(EVP_PKEY_CTX
*ctx
)
141 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
142 return EVP_PKEY_STATE_UNKNOWN
;
144 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
145 && ctx
->op
.kex
.exchprovctx
!= NULL
)
146 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
147 && ctx
->op
.sig
.sigprovctx
!= NULL
)
148 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
149 && ctx
->op
.ciph
.ciphprovctx
!= NULL
)
150 || (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
151 && ctx
->op
.keymgmt
.genctx
!= NULL
))
152 return EVP_PKEY_STATE_PROVIDER
;
154 return EVP_PKEY_STATE_LEGACY
;
157 static void help_get_legacy_alg_type_from_keymgmt(const char *keytype
,
162 if (*type
== NID_undef
)
163 *type
= evp_pkey_name2type(keytype
);
166 static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT
*keymgmt
)
168 int type
= NID_undef
;
170 EVP_KEYMGMT_names_do_all(keymgmt
, help_get_legacy_alg_type_from_keymgmt
,
174 #endif /* FIPS_MODULE */
176 static int is_legacy_alg(int id
, const char *keytype
)
179 /* Certain EVP_PKEY keytypes are only available in legacy form */
181 id
= evp_pkey_name2type(keytype
);
185 * TODO(3.0): Remove SM2 when they are converted to have provider
198 static EVP_PKEY_CTX
*int_ctx_new(OPENSSL_CTX
*libctx
,
199 EVP_PKEY
*pkey
, ENGINE
*e
,
200 const char *keytype
, const char *propquery
,
204 EVP_PKEY_CTX
*ret
= NULL
;
205 const EVP_PKEY_METHOD
*pmeth
= NULL
;
206 EVP_KEYMGMT
*keymgmt
= NULL
;
209 * If the given |pkey| is provided, we extract the keytype from its
210 * keymgmt and skip over the legacy code.
212 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
213 /* If we have an engine, something went wrong somewhere... */
214 if (!ossl_assert(e
== NULL
))
216 keytype
= evp_first_name(pkey
->keymgmt
->prov
, pkey
->keymgmt
->name_id
);
222 * TODO(3.0) This legacy code section should be removed when we stop
229 else if (keytype
!= NULL
)
230 id
= evp_pkey_name2type(keytype
);
234 /* If no ID was found here, we can only resort to find a keymgmt */
239 * Here, we extract what information we can for the purpose of
240 * supporting usage with implementations from providers, to make
241 * for a smooth transition from legacy stuff to provider based stuff.
243 * If an engine is given, this is entirely legacy, and we should not
244 * pretend anything else, so we only set the name when no engine is
245 * given. If both are already given, someone made a mistake, and
246 * since that can only happen internally, it's safe to make an
249 if (!ossl_assert(e
== NULL
|| keytype
== NULL
))
252 keytype
= OBJ_nid2sn(id
);
254 # ifndef OPENSSL_NO_ENGINE
255 if (e
== NULL
&& pkey
!= NULL
)
256 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
257 /* Try to find an ENGINE which implements this method */
259 if (!ENGINE_init(e
)) {
260 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
264 e
= ENGINE_get_pkey_meth_engine(id
);
268 * If an ENGINE handled this method look it up. Otherwise use internal
272 pmeth
= ENGINE_get_pkey_meth(e
, id
);
275 pmeth
= EVP_PKEY_meth_find(id
);
278 #endif /* FIPS_MODULE */
281 * If there's no engine and there's a name, we try fetching a provider
284 if (e
== NULL
&& keytype
!= NULL
) {
285 int legacy
= is_legacy_alg(id
, keytype
);
287 /* This could fail so ignore errors */
291 keymgmt
= EVP_KEYMGMT_fetch(libctx
, keytype
, propquery
);
294 else if (keymgmt
== NULL
)
295 return NULL
; /* EVP_KEYMGMT_fetch() recorded an error */
299 * Chase down the legacy NID, as that might be needed for diverse
300 * purposes, such as ensure that EVP_PKEY_type() can return sensible
301 * values, or that there's a better chance to "downgrade" a key when
302 * needed. We go through all keymgmt names, because the keytype
303 * that's passed to this function doesn't necessarily translate
305 * TODO: Remove this when #legacy keys are gone.
307 if (keymgmt
!= NULL
) {
308 int tmp_id
= get_legacy_alg_type_from_keymgmt(keymgmt
);
310 if (tmp_id
!= NID_undef
) {
315 * It really really shouldn't differ. If it still does,
316 * something is very wrong.
318 if (!ossl_assert(id
== tmp_id
)) {
319 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_INTERNAL_ERROR
);
320 EVP_KEYMGMT_free(keymgmt
);
329 if (pmeth
== NULL
&& keymgmt
== NULL
) {
330 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
332 ret
= OPENSSL_zalloc(sizeof(*ret
));
334 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
337 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
338 if ((ret
== NULL
|| pmeth
== NULL
) && e
!= NULL
)
343 EVP_KEYMGMT_free(keymgmt
);
347 ret
->libctx
= libctx
;
348 ret
->propquery
= propquery
;
349 ret
->keytype
= keytype
;
350 ret
->keymgmt
= keymgmt
;
351 ret
->legacy_keytype
= id
; /* TODO: Remove when #legacy key are gone */
354 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
357 EVP_PKEY_up_ref(pkey
);
359 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
360 if (pmeth
->init(ret
) <= 0) {
362 EVP_PKEY_CTX_free(ret
);
370 /*- All methods below can also be used in FIPS_MODULE */
372 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_name(OPENSSL_CTX
*libctx
,
374 const char *propquery
)
376 return int_ctx_new(libctx
, NULL
, NULL
, name
, propquery
, -1);
379 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX
*libctx
, EVP_PKEY
*pkey
,
380 const char *propquery
)
382 return int_ctx_new(libctx
, pkey
, NULL
, NULL
, propquery
, -1);
385 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
387 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
388 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
389 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
390 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
391 ctx
->op
.sig
.sigprovctx
= NULL
;
392 ctx
->op
.sig
.signature
= NULL
;
393 } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
394 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
395 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
396 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
397 ctx
->op
.kex
.exchprovctx
= NULL
;
398 ctx
->op
.kex
.exchange
= NULL
;
399 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)) {
400 if (ctx
->op
.ciph
.ciphprovctx
!= NULL
&& ctx
->op
.ciph
.cipher
!= NULL
)
401 ctx
->op
.ciph
.cipher
->freectx(ctx
->op
.ciph
.ciphprovctx
);
402 EVP_ASYM_CIPHER_free(ctx
->op
.ciph
.cipher
);
403 ctx
->op
.ciph
.ciphprovctx
= NULL
;
404 ctx
->op
.ciph
.cipher
= NULL
;
405 } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
406 if (ctx
->op
.keymgmt
.genctx
!= NULL
&& ctx
->keymgmt
!= NULL
)
407 evp_keymgmt_gen_cleanup(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
);
411 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
415 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
416 ctx
->pmeth
->cleanup(ctx
);
418 evp_pkey_ctx_free_old_ops(ctx
);
420 evp_pkey_ctx_free_all_cached_data(ctx
);
422 EVP_KEYMGMT_free(ctx
->keymgmt
);
424 EVP_PKEY_free(ctx
->pkey
);
425 EVP_PKEY_free(ctx
->peerkey
);
426 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
427 ENGINE_finish(ctx
->engine
);
434 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
435 const EVP_PKEY_METHOD
*meth
)
438 *ppkey_id
= meth
->pkey_id
;
440 *pflags
= meth
->flags
;
443 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
445 int pkey_id
= dst
->pkey_id
;
446 int flags
= dst
->flags
;
450 /* We only copy the function pointers so restore the other values */
451 dst
->pkey_id
= pkey_id
;
455 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
457 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
461 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
463 return int_ctx_new(NULL
, pkey
, e
, NULL
, NULL
, -1);
466 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
468 return int_ctx_new(NULL
, NULL
, e
, NULL
, NULL
, id
);
471 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
475 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
476 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
477 && pctx
->op
.kex
.exchprovctx
== NULL
)
478 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
479 && pctx
->op
.sig
.sigprovctx
== NULL
)))
481 # ifndef OPENSSL_NO_ENGINE
482 /* Make sure it's safe to copy a pkey context using an ENGINE */
483 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
484 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
488 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
490 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
494 if (pctx
->pkey
!= NULL
)
495 EVP_PKEY_up_ref(pctx
->pkey
);
496 rctx
->pkey
= pctx
->pkey
;
497 rctx
->operation
= pctx
->operation
;
498 rctx
->libctx
= pctx
->libctx
;
499 rctx
->keytype
= pctx
->keytype
;
500 rctx
->propquery
= pctx
->propquery
;
502 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
503 if (pctx
->op
.kex
.exchange
!= NULL
) {
504 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
505 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
510 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
511 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
513 rctx
->op
.kex
.exchprovctx
514 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
515 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
516 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
522 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
523 if (pctx
->op
.sig
.signature
!= NULL
) {
524 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
525 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
)) {
530 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
531 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
533 rctx
->op
.sig
.sigprovctx
534 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
535 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
536 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
542 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx
)) {
543 if (pctx
->op
.ciph
.cipher
!= NULL
) {
544 rctx
->op
.ciph
.cipher
= pctx
->op
.ciph
.cipher
;
545 if (!EVP_ASYM_CIPHER_up_ref(rctx
->op
.ciph
.cipher
)) {
550 if (pctx
->op
.ciph
.ciphprovctx
!= NULL
) {
551 if (!ossl_assert(pctx
->op
.ciph
.cipher
!= NULL
))
553 rctx
->op
.ciph
.ciphprovctx
554 = pctx
->op
.ciph
.cipher
->dupctx(pctx
->op
.ciph
.ciphprovctx
);
555 if (rctx
->op
.ciph
.ciphprovctx
== NULL
) {
556 EVP_ASYM_CIPHER_free(rctx
->op
.ciph
.cipher
);
564 rctx
->pmeth
= pctx
->pmeth
;
565 # ifndef OPENSSL_NO_ENGINE
566 rctx
->engine
= pctx
->engine
;
570 EVP_PKEY_up_ref(pctx
->peerkey
);
571 rctx
->peerkey
= pctx
->peerkey
;
573 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
577 EVP_PKEY_CTX_free(rctx
);
582 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
584 if (app_pkey_methods
== NULL
) {
585 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
586 if (app_pkey_methods
== NULL
){
587 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
591 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
592 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
595 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
599 void evp_app_cleanup_int(void)
601 if (app_pkey_methods
!= NULL
)
602 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
605 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
607 const EVP_PKEY_METHOD
*ret
;
609 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
611 return ret
== NULL
? 0 : 1;
614 size_t EVP_PKEY_meth_get_count(void)
616 size_t rv
= OSSL_NELEM(standard_methods
);
618 if (app_pkey_methods
)
619 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
623 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
625 if (idx
< OSSL_NELEM(standard_methods
))
626 return (standard_methods
[idx
])();
627 if (app_pkey_methods
== NULL
)
629 idx
-= OSSL_NELEM(standard_methods
);
630 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
632 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
636 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
638 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
639 && ctx
->op
.kex
.exchprovctx
!= NULL
640 && ctx
->op
.kex
.exchange
!= NULL
641 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
642 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
644 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
645 && ctx
->op
.sig
.sigprovctx
!= NULL
646 && ctx
->op
.sig
.signature
!= NULL
647 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
648 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
650 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
651 && ctx
->op
.ciph
.ciphprovctx
!= NULL
652 && ctx
->op
.ciph
.cipher
!= NULL
653 && ctx
->op
.ciph
.cipher
->set_ctx_params
!= NULL
)
654 return ctx
->op
.ciph
.cipher
->set_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
656 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
657 && ctx
->op
.keymgmt
.genctx
!= NULL
658 && ctx
->keymgmt
!= NULL
659 && ctx
->keymgmt
->gen_set_params
!= NULL
)
660 return evp_keymgmt_gen_set_params(ctx
->keymgmt
, ctx
->op
.keymgmt
.genctx
,
665 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
667 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
668 && ctx
->op
.kex
.exchprovctx
!= NULL
669 && ctx
->op
.kex
.exchange
!= NULL
670 && ctx
->op
.kex
.exchange
->get_ctx_params
!= NULL
)
671 return ctx
->op
.kex
.exchange
->get_ctx_params(ctx
->op
.kex
.exchprovctx
,
673 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
674 && ctx
->op
.sig
.sigprovctx
!= NULL
675 && ctx
->op
.sig
.signature
!= NULL
676 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
677 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
679 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
680 && ctx
->op
.ciph
.ciphprovctx
!= NULL
681 && ctx
->op
.ciph
.cipher
!= NULL
682 && ctx
->op
.ciph
.cipher
->get_ctx_params
!= NULL
)
683 return ctx
->op
.ciph
.cipher
->get_ctx_params(ctx
->op
.ciph
.ciphprovctx
,
689 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
693 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
694 && ctx
->op
.kex
.exchange
!= NULL
695 && ctx
->op
.kex
.exchange
->gettable_ctx_params
!= NULL
) {
696 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
697 return ctx
->op
.kex
.exchange
->gettable_ctx_params(provctx
);
699 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
700 && ctx
->op
.sig
.signature
!= NULL
701 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
) {
702 provctx
= ossl_provider_ctx(
703 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
704 return ctx
->op
.sig
.signature
->gettable_ctx_params(provctx
);
706 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
707 && ctx
->op
.ciph
.cipher
!= NULL
708 && ctx
->op
.ciph
.cipher
->gettable_ctx_params
!= NULL
) {
709 provctx
= ossl_provider_ctx(
710 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
711 return ctx
->op
.ciph
.cipher
->gettable_ctx_params(provctx
);
716 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
720 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
721 && ctx
->op
.kex
.exchange
!= NULL
722 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
) {
723 provctx
= ossl_provider_ctx(EVP_KEYEXCH_provider(ctx
->op
.kex
.exchange
));
724 return ctx
->op
.kex
.exchange
->settable_ctx_params(provctx
);
726 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
727 && ctx
->op
.sig
.signature
!= NULL
728 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
) {
729 provctx
= ossl_provider_ctx(
730 EVP_SIGNATURE_provider(ctx
->op
.sig
.signature
));
731 return ctx
->op
.sig
.signature
->settable_ctx_params(provctx
);
733 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx
)
734 && ctx
->op
.ciph
.cipher
!= NULL
735 && ctx
->op
.ciph
.cipher
->settable_ctx_params
!= NULL
) {
736 provctx
= ossl_provider_ctx(
737 EVP_ASYM_CIPHER_provider(ctx
->op
.ciph
.cipher
));
738 return ctx
->op
.ciph
.cipher
->settable_ctx_params(provctx
);
740 if (EVP_PKEY_CTX_IS_GEN_OP(ctx
)
741 && ctx
->keymgmt
!= NULL
)
742 return EVP_KEYMGMT_gen_settable_params(ctx
->keymgmt
);
748 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
750 * Return 1 on success, 0 or negative for errors.
752 * In particular they return -2 if any of the params is not supported.
754 * They are not available in FIPS_MODULE as they depend on
755 * - EVP_PKEY_CTX_{get,set}_params()
756 * - EVP_PKEY_CTX_{gettable,settable}_params()
759 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
763 if (ctx
== NULL
|| params
== NULL
)
766 for (p
= params
; p
->key
!= NULL
; p
++) {
767 /* Check the ctx actually understands this parameter */
768 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx
),
773 return EVP_PKEY_CTX_set_params(ctx
, params
);
776 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
780 if (ctx
== NULL
|| params
== NULL
)
783 for (p
= params
; p
->key
!= NULL
; p
++ ) {
784 /* Check the ctx actually understands this parameter */
785 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx
),
790 return EVP_PKEY_CTX_get_params(ctx
, params
);
793 # ifndef OPENSSL_NO_DH
794 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
796 OSSL_PARAM dh_pad_params
[2];
797 unsigned int upad
= pad
;
799 /* We use EVP_PKEY_CTX_ctrl return values */
800 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
801 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
805 /* TODO(3.0): Remove this eventually when no more legacy */
806 if (ctx
->op
.kex
.exchprovctx
== NULL
)
807 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
808 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
810 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
811 dh_pad_params
[1] = OSSL_PARAM_construct_end();
813 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
817 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
819 OSSL_PARAM sig_md_params
[2], *p
= sig_md_params
;
820 /* 80 should be big enough */
824 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
825 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
826 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
830 /* TODO(3.0): Remove this eventually when no more legacy */
831 if (ctx
->op
.sig
.sigprovctx
== NULL
)
832 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
833 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
835 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
838 *p
= OSSL_PARAM_construct_end();
840 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
843 tmp
= evp_get_digestbyname_ex(ctx
->libctx
, name
);
852 static int evp_pkey_ctx_set_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
,
853 int fallback
, const char *param
, int op
,
856 OSSL_PARAM md_params
[2], *p
= md_params
;
859 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
860 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
861 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
865 /* TODO(3.0): Remove this eventually when no more legacy */
867 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, 0, (void *)(md
));
872 name
= EVP_MD_name(md
);
875 *p
++ = OSSL_PARAM_construct_utf8_string(param
,
877 * Cast away the const. This is read
878 * only so should be safe
881 *p
= OSSL_PARAM_construct_end();
883 return EVP_PKEY_CTX_set_params(ctx
, md_params
);
886 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
888 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.sig
.sigprovctx
== NULL
,
889 OSSL_SIGNATURE_PARAM_DIGEST
,
890 EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
);
893 int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
895 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
896 OSSL_KDF_PARAM_DIGEST
,
897 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_TLS_MD
);
900 static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX
*ctx
, int fallback
,
901 const char *param
, int op
, int ctrl
,
902 const unsigned char *data
,
905 OSSL_PARAM octet_string_params
[2], *p
= octet_string_params
;
907 if (ctx
== NULL
|| (ctx
->operation
& op
) == 0) {
908 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
909 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
913 /* TODO(3.0): Remove this eventually when no more legacy */
915 return EVP_PKEY_CTX_ctrl(ctx
, -1, op
, ctrl
, datalen
, (void *)(data
));
918 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_LENGTH
);
922 *p
++ = OSSL_PARAM_construct_octet_string(param
,
924 * Cast away the const. This is read
925 * only so should be safe
927 (unsigned char *)data
,
929 *p
= OSSL_PARAM_construct_end();
931 return EVP_PKEY_CTX_set_params(ctx
, octet_string_params
);
934 int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX
*ctx
,
935 const unsigned char *sec
, int seclen
)
937 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
938 OSSL_KDF_PARAM_SECRET
,
940 EVP_PKEY_CTRL_TLS_SECRET
,
944 int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX
*ctx
,
945 const unsigned char *seed
, int seedlen
)
947 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
950 EVP_PKEY_CTRL_TLS_SEED
,
954 int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
956 return evp_pkey_ctx_set_md(ctx
, md
, ctx
->op
.kex
.exchprovctx
== NULL
,
957 OSSL_KDF_PARAM_DIGEST
,
958 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_HKDF_MD
);
961 int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX
*ctx
,
962 const unsigned char *salt
, int saltlen
)
964 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
967 EVP_PKEY_CTRL_HKDF_SALT
,
971 int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX
*ctx
,
972 const unsigned char *key
, int keylen
)
974 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
977 EVP_PKEY_CTRL_HKDF_KEY
,
981 int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX
*ctx
,
982 const unsigned char *info
, int infolen
)
984 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
987 EVP_PKEY_CTRL_HKDF_INFO
,
991 int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX
*ctx
, int mode
)
993 OSSL_PARAM int_params
[2], *p
= int_params
;
995 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
996 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
997 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1001 /* TODO(3.0): Remove this eventually when no more legacy */
1002 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1003 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_DERIVE
,
1004 EVP_PKEY_CTRL_HKDF_MODE
, mode
, NULL
);
1008 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_VALUE
);
1012 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
1013 *p
= OSSL_PARAM_construct_end();
1015 return EVP_PKEY_CTX_set_params(ctx
, int_params
);
1018 int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX
*ctx
, const char *pass
,
1021 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1022 OSSL_KDF_PARAM_PASSWORD
,
1025 (const unsigned char *)pass
, passlen
);
1028 int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX
*ctx
,
1029 const unsigned char *salt
, int saltlen
)
1031 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.kex
.exchprovctx
== NULL
,
1032 OSSL_KDF_PARAM_SALT
,
1034 EVP_PKEY_CTRL_SCRYPT_SALT
,
1038 static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX
*ctx
, const char *param
,
1039 int op
, int ctrl
, uint64_t val
)
1041 OSSL_PARAM uint64_params
[2], *p
= uint64_params
;
1043 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
1044 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1045 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1049 /* TODO(3.0): Remove this eventually when no more legacy */
1050 if (ctx
->op
.kex
.exchprovctx
== NULL
)
1051 return EVP_PKEY_CTX_ctrl_uint64(ctx
, -1, op
, ctrl
, val
);
1053 *p
++ = OSSL_PARAM_construct_uint64(param
, &val
);
1054 *p
= OSSL_PARAM_construct_end();
1056 return EVP_PKEY_CTX_set_params(ctx
, uint64_params
);
1059 int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX
*ctx
, uint64_t n
)
1061 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_N
,
1062 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_N
,
1066 int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX
*ctx
, uint64_t r
)
1068 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_R
,
1069 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_R
,
1073 int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX
*ctx
, uint64_t p
)
1075 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_P
,
1076 EVP_PKEY_OP_DERIVE
, EVP_PKEY_CTRL_SCRYPT_P
,
1080 int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX
*ctx
,
1081 uint64_t maxmem_bytes
)
1083 return evp_pkey_ctx_set_uint64(ctx
, OSSL_KDF_PARAM_SCRYPT_MAXMEM
,
1085 EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
,
1089 int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX
*ctx
, const unsigned char *key
,
1092 return evp_pkey_ctx_set1_octet_string(ctx
, ctx
->op
.keymgmt
.genctx
== NULL
,
1093 OSSL_PKEY_PARAM_PRIV_KEY
,
1095 EVP_PKEY_CTRL_SET_MAC_KEY
,
1099 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1101 OSSL_PARAM params
[2], *p
= params
;
1104 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1105 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1106 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1110 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DIST_ID
,
1112 * Cast away the const. This is
1113 * read only so should be safe
1115 (void *)id
, (size_t)len
);
1116 *p
++ = OSSL_PARAM_construct_end();
1118 ret
= evp_pkey_ctx_set_params_strict(ctx
, params
);
1120 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1124 int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX
*ctx
, const void *id
, int len
)
1126 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1127 EVP_PKEY_CTRL_SET1_ID
, (int)len
, (void*)(id
));
1130 static int get1_id_data(EVP_PKEY_CTX
*ctx
, void *id
, size_t *id_len
)
1133 void *tmp_id
= NULL
;
1134 OSSL_PARAM params
[2], *p
= params
;
1136 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
1137 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1138 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1142 *p
++ = OSSL_PARAM_construct_octet_ptr(OSSL_PKEY_PARAM_DIST_ID
,
1144 *p
++ = OSSL_PARAM_construct_end();
1146 ret
= evp_pkey_ctx_get_params_strict(ctx
, params
);
1148 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1149 } else if (ret
> 0) {
1150 size_t tmp_id_len
= params
[0].return_size
;
1153 memcpy(id
, tmp_id
, tmp_id_len
);
1155 *id_len
= tmp_id_len
;
1160 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX
*ctx
, void *id
)
1162 return get1_id_data(ctx
, id
, NULL
);
1165 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1167 return get1_id_data(ctx
, NULL
, id_len
);
1170 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX
*ctx
, void *id
)
1172 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1, EVP_PKEY_CTRL_GET1_ID
, 0, (void*)id
);
1175 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX
*ctx
, size_t *id_len
)
1177 return EVP_PKEY_CTX_ctrl(ctx
, -1, -1,
1178 EVP_PKEY_CTRL_GET1_ID_LEN
, 0, (void*)id_len
);
1181 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1182 int cmd
, int p1
, void *p2
)
1185 case EVP_PKEY_CTRL_SET1_ID
:
1186 return evp_pkey_ctx_set1_id_prov(ctx
, p2
, p1
);
1187 case EVP_PKEY_CTRL_GET1_ID
:
1188 return evp_pkey_ctx_get1_id_prov(ctx
, p2
);
1189 case EVP_PKEY_CTRL_GET1_ID_LEN
:
1190 return evp_pkey_ctx_get1_id_len_prov(ctx
, p2
);
1193 # ifndef OPENSSL_NO_DH
1194 if (keytype
== EVP_PKEY_DHX
) {
1196 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
1197 return EVP_PKEY_CTX_set_dh_kdf_type(ctx
, p1
);
1198 case EVP_PKEY_CTRL_DH_KDF_MD
:
1199 return EVP_PKEY_CTX_set_dh_kdf_md(ctx
, p2
);
1200 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
1201 return EVP_PKEY_CTX_set_dh_kdf_outlen(ctx
, p1
);
1202 case EVP_PKEY_CTRL_DH_KDF_UKM
:
1203 return EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx
, p2
, p1
);
1204 case EVP_PKEY_CTRL_DH_KDF_OID
:
1205 return EVP_PKEY_CTX_set0_dh_kdf_oid(ctx
, p2
);
1206 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
1207 return EVP_PKEY_CTX_get_dh_kdf_md(ctx
, p2
);
1208 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
1209 return EVP_PKEY_CTX_get_dh_kdf_outlen(ctx
, p2
);
1210 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
1211 return EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx
, p2
);
1212 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
1213 return EVP_PKEY_CTX_get0_dh_kdf_oid(ctx
, p2
);
1216 if (keytype
== EVP_PKEY_DH
) {
1218 case EVP_PKEY_CTRL_DH_PAD
:
1219 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
1220 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
1221 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, p1
);
1222 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
1223 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, p1
);
1224 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
1225 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, p1
);
1226 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
1227 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, p1
);
1228 case EVP_PKEY_CTRL_DH_RFC5114
:
1229 return EVP_PKEY_CTX_set_dh_rfc5114(ctx
, p1
);
1233 # ifndef OPENSSL_NO_DSA
1234 if (keytype
== EVP_PKEY_DSA
) {
1236 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS
:
1237 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx
, p1
);
1238 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS
:
1239 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx
, p1
);
1240 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD
:
1241 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx
, p2
);
1245 # ifndef OPENSSL_NO_EC
1246 if (keytype
== EVP_PKEY_EC
) {
1248 case EVP_PKEY_CTRL_EC_PARAM_ENC
:
1249 return evp_pkey_ctx_set_ec_param_enc_prov(ctx
, p1
);
1250 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
:
1251 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx
, p1
);
1252 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR
:
1254 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx
);
1255 } else if (p1
< -1 || p1
> 1) {
1256 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1259 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx
, p1
);
1261 case EVP_PKEY_CTRL_EC_KDF_TYPE
:
1263 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx
);
1265 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx
, p1
);
1267 case EVP_PKEY_CTRL_GET_EC_KDF_MD
:
1268 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx
, p2
);
1269 case EVP_PKEY_CTRL_EC_KDF_MD
:
1270 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx
, p2
);
1271 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN
:
1272 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx
, p2
);
1273 case EVP_PKEY_CTRL_EC_KDF_OUTLEN
:
1274 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx
, p1
);
1275 case EVP_PKEY_CTRL_GET_EC_KDF_UKM
:
1276 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx
, p2
);
1277 case EVP_PKEY_CTRL_EC_KDF_UKM
:
1278 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx
, p2
, p1
);
1282 if (keytype
== EVP_PKEY_RSA
) {
1284 case EVP_PKEY_CTRL_RSA_OAEP_MD
:
1285 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1286 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD
:
1287 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1288 case EVP_PKEY_CTRL_RSA_MGF1_MD
:
1289 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx
, p2
);
1290 case EVP_PKEY_CTRL_RSA_OAEP_LABEL
:
1291 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx
, p2
, p1
);
1292 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL
:
1293 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx
, (unsigned char **)p2
);
1294 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS
:
1295 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, p1
);
1296 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP
:
1297 return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx
, p2
);
1298 case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES
:
1299 return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx
, p1
);
1303 * keytype == -1 is used when several key types share the same structure,
1304 * or for generic controls that are the same across multiple key types.
1306 if (keytype
== -1) {
1307 if (optype
== EVP_PKEY_OP_DERIVE
) {
1310 case EVP_PKEY_CTRL_TLS_MD
:
1311 return EVP_PKEY_CTX_set_tls1_prf_md(ctx
, p2
);
1312 case EVP_PKEY_CTRL_TLS_SECRET
:
1313 return EVP_PKEY_CTX_set1_tls1_prf_secret(ctx
, p2
, p1
);
1314 case EVP_PKEY_CTRL_TLS_SEED
:
1315 return EVP_PKEY_CTX_add1_tls1_prf_seed(ctx
, p2
, p1
);
1318 case EVP_PKEY_CTRL_HKDF_MD
:
1319 return EVP_PKEY_CTX_set_hkdf_md(ctx
, p2
);
1320 case EVP_PKEY_CTRL_HKDF_SALT
:
1321 return EVP_PKEY_CTX_set1_hkdf_salt(ctx
, p2
, p1
);
1322 case EVP_PKEY_CTRL_HKDF_KEY
:
1323 return EVP_PKEY_CTX_set1_hkdf_key(ctx
, p2
, p1
);
1324 case EVP_PKEY_CTRL_HKDF_INFO
:
1325 return EVP_PKEY_CTX_add1_hkdf_info(ctx
, p2
, p1
);
1326 case EVP_PKEY_CTRL_HKDF_MODE
:
1327 return EVP_PKEY_CTX_hkdf_mode(ctx
, p1
);
1330 case EVP_PKEY_CTRL_PASS
:
1331 return EVP_PKEY_CTX_set1_pbe_pass(ctx
, p2
, p1
);
1332 case EVP_PKEY_CTRL_SCRYPT_SALT
:
1333 return EVP_PKEY_CTX_set1_scrypt_salt(ctx
, p2
, p1
);
1334 case EVP_PKEY_CTRL_SCRYPT_N
:
1335 return EVP_PKEY_CTX_set_scrypt_N(ctx
, p1
);
1336 case EVP_PKEY_CTRL_SCRYPT_R
:
1337 return EVP_PKEY_CTX_set_scrypt_r(ctx
, p1
);
1338 case EVP_PKEY_CTRL_SCRYPT_P
:
1339 return EVP_PKEY_CTX_set_scrypt_p(ctx
, p1
);
1340 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES
:
1341 return EVP_PKEY_CTX_set_scrypt_maxmem_bytes(ctx
, p1
);
1343 } else if (optype
== EVP_PKEY_OP_KEYGEN
) {
1344 OSSL_PARAM params
[2], *p
= params
;
1347 case EVP_PKEY_CTRL_CIPHER
:
1349 char *ciphname
= (char *)EVP_CIPHER_name(p2
);
1351 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
1353 *p
= OSSL_PARAM_construct_end();
1355 return EVP_PKEY_CTX_set_params(ctx
, params
);
1357 case EVP_PKEY_CTRL_SET_MAC_KEY
:
1359 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
1361 *p
= OSSL_PARAM_construct_end();
1363 return EVP_PKEY_CTX_set_params(ctx
, params
);
1368 case EVP_PKEY_CTRL_MD
:
1369 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
1370 case EVP_PKEY_CTRL_GET_MD
:
1371 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
1372 case EVP_PKEY_CTRL_RSA_PADDING
:
1373 return EVP_PKEY_CTX_set_rsa_padding(ctx
, p1
);
1374 case EVP_PKEY_CTRL_GET_RSA_PADDING
:
1375 return EVP_PKEY_CTX_get_rsa_padding(ctx
, p2
);
1376 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD
:
1377 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx
, p2
);
1378 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN
:
1379 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx
, p1
);
1380 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN
:
1381 return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx
, p2
);
1382 case EVP_PKEY_CTRL_PKCS7_ENCRYPT
:
1383 case EVP_PKEY_CTRL_PKCS7_DECRYPT
:
1384 # ifndef OPENSSL_NO_CMS
1385 case EVP_PKEY_CTRL_CMS_DECRYPT
:
1386 case EVP_PKEY_CTRL_CMS_ENCRYPT
:
1388 /* TODO (3.0) Temporary hack, this should probe */
1389 if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx
), "RSASSA-PSS"))
1391 ERR_raise(ERR_LIB_EVP
,
1392 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
1398 * GOST CMS format is different for different cipher algorithms.
1399 * Most of other algorithms don't have such a difference
1400 * so this ctrl is just ignored.
1402 if (cmd
== EVP_PKEY_CTRL_CIPHER
)
1408 static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1409 int cmd
, int p1
, void *p2
)
1414 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1419 * If the method has a |digest_custom| function, we can relax the
1420 * operation type check, since this can be called before the operation
1423 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->digest_custom
== NULL
) {
1424 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
1425 EVPerr(0, EVP_R_NO_OPERATION_SET
);
1429 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
1430 EVPerr(0, EVP_R_INVALID_OPERATION
);
1435 switch (evp_pkey_ctx_state(ctx
)) {
1436 case EVP_PKEY_STATE_PROVIDER
:
1437 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1438 case EVP_PKEY_STATE_UNKNOWN
:
1439 case EVP_PKEY_STATE_LEGACY
:
1440 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
1441 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1444 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
1447 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
1450 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1456 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1457 int cmd
, int p1
, void *p2
)
1461 /* If unsupported, we don't want that reported here */
1463 ret
= evp_pkey_ctx_store_cached_data(ctx
, keytype
, optype
,
1468 ERR_clear_last_mark();
1470 * If there was an error, there was an error.
1471 * If the operation isn't initialized yet, we also return, as
1472 * the saved values will be used then anyway.
1474 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1478 return evp_pkey_ctx_ctrl_int(ctx
, keytype
, optype
, cmd
, p1
, p2
);
1481 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
1482 int cmd
, uint64_t value
)
1484 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
1487 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
1490 if (strcmp(name
, "md") == 0)
1491 name
= OSSL_ALG_PARAM_DIGEST
;
1492 else if (strcmp(name
, "rsa_padding_mode") == 0)
1493 name
= OSSL_ASYM_CIPHER_PARAM_PAD_MODE
;
1494 else if (strcmp(name
, "rsa_mgf1_md") == 0)
1495 name
= OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
;
1496 else if (strcmp(name
, "rsa_oaep_md") == 0)
1497 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
;
1498 else if (strcmp(name
, "rsa_oaep_label") == 0)
1499 name
= OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
;
1500 else if (strcmp(name
, "rsa_pss_saltlen") == 0)
1501 name
= OSSL_SIGNATURE_PARAM_PSS_SALTLEN
;
1502 else if (strcmp(name
, "rsa_keygen_bits") == 0)
1503 name
= OSSL_PKEY_PARAM_RSA_BITS
;
1504 else if (strcmp(name
, "rsa_keygen_pubexp") == 0)
1505 name
= OSSL_PKEY_PARAM_RSA_E
;
1506 else if (strcmp(name
, "rsa_keygen_primes") == 0)
1507 name
= OSSL_PKEY_PARAM_RSA_PRIMES
;
1508 else if (strcmp(name
, "rsa_pss_keygen_md") == 0)
1509 name
= OSSL_PKEY_PARAM_RSA_DIGEST
;
1510 else if (strcmp(name
, "rsa_pss_keygen_mgf1_md") == 0)
1511 name
= OSSL_PKEY_PARAM_RSA_MGF1_DIGEST
;
1512 else if (strcmp(name
, "rsa_pss_keygen_saltlen") == 0)
1513 name
= OSSL_PKEY_PARAM_RSA_PSS_SALTLEN
;
1514 # ifndef OPENSSL_NO_DSA
1515 else if (strcmp(name
, "dsa_paramgen_bits") == 0)
1516 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1517 else if (strcmp(name
, "dsa_paramgen_q_bits") == 0)
1518 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1519 else if (strcmp(name
, "dsa_paramgen_md") == 0)
1520 name
= OSSL_PKEY_PARAM_FFC_DIGEST
;
1522 # ifndef OPENSSL_NO_DH
1523 else if (strcmp(name
, "dh_paramgen_generator") == 0)
1524 name
= OSSL_PKEY_PARAM_DH_GENERATOR
;
1525 else if (strcmp(name
, "dh_paramgen_prime_len") == 0)
1526 name
= OSSL_PKEY_PARAM_FFC_PBITS
;
1527 else if (strcmp(name
, "dh_paramgen_subprime_len") == 0)
1528 name
= OSSL_PKEY_PARAM_FFC_QBITS
;
1529 else if (strcmp(name
, "dh_paramgen_type") == 0) {
1530 name
= OSSL_PKEY_PARAM_FFC_TYPE
;
1531 value
= dh_gen_type_id2name(atoi(value
));
1532 } else if (strcmp(name
, "dh_param") == 0)
1533 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1534 else if (strcmp(name
, "dh_rfc5114") == 0) {
1535 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1536 value
= ffc_named_group_from_uid(atoi(value
));
1537 } else if (strcmp(name
, "dh_pad") == 0)
1538 name
= OSSL_EXCHANGE_PARAM_PAD
;
1540 # ifndef OPENSSL_NO_EC
1541 else if (strcmp(name
, "ec_paramgen_curve") == 0)
1542 name
= OSSL_PKEY_PARAM_GROUP_NAME
;
1543 else if (strcmp(name
, "ecdh_cofactor_mode") == 0)
1544 name
= OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE
;
1545 else if (strcmp(name
, "ecdh_kdf_md") == 0)
1546 name
= OSSL_EXCHANGE_PARAM_KDF_DIGEST
;
1547 else if (strcmp(name
, "ec_param_enc") == 0)
1548 name
= OSSL_PKEY_PARAM_EC_ENCODING
;
1550 else if (strcmp(name
, "N") == 0)
1551 name
= OSSL_KDF_PARAM_SCRYPT_N
;
1555 * TODO(3.0) reduce the code above to only translate known legacy
1556 * string to the corresponding core name (see core_names.h), but
1557 * otherwise leave it to this code block to do the actual work.
1559 const OSSL_PARAM
*settable
= EVP_PKEY_CTX_settable_params(ctx
);
1560 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1564 if (!OSSL_PARAM_allocate_from_text(¶ms
[0], settable
, name
, value
,
1565 strlen(value
), &exists
)) {
1567 ERR_raise_data(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
,
1568 "name=%s,value=%s", name
, value
);
1573 if (EVP_PKEY_CTX_set_params(ctx
, params
))
1575 OPENSSL_free(params
[0].data
);
1580 static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX
*ctx
,
1581 const char *name
, const char *value
)
1586 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1590 switch (evp_pkey_ctx_state(ctx
)) {
1591 case EVP_PKEY_STATE_PROVIDER
:
1592 return legacy_ctrl_str_to_param(ctx
, name
, value
);
1593 case EVP_PKEY_STATE_UNKNOWN
:
1594 case EVP_PKEY_STATE_LEGACY
:
1595 if (ctx
== NULL
|| ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl_str
== NULL
) {
1596 EVPerr(0, EVP_R_COMMAND_NOT_SUPPORTED
);
1599 if (strcmp(name
, "digest") == 0)
1600 ret
= EVP_PKEY_CTX_md(ctx
,
1601 EVP_PKEY_OP_TYPE_SIG
| EVP_PKEY_OP_TYPE_CRYPT
,
1602 EVP_PKEY_CTRL_MD
, value
);
1604 ret
= ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
1611 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
1612 const char *name
, const char *value
)
1616 /* If unsupported, we don't want that reported here */
1618 ret
= evp_pkey_ctx_store_cached_data(ctx
, -1, -1, -1,
1619 name
, value
, strlen(value
) + 1);
1623 ERR_clear_last_mark();
1625 * If there was an error, there was an error.
1626 * If the operation isn't initialized yet, we also return, as
1627 * the saved values will be used then anyway.
1629 if (ret
< 1 || ctx
->operation
== EVP_PKEY_OP_UNDEFINED
)
1633 return evp_pkey_ctx_ctrl_str_int(ctx
, name
, value
);
1636 static int decode_cmd(int cmd
, const char *name
)
1640 * The consequence of the assertion not being true is that this
1641 * function will return -1, which will cause the calling functions
1642 * to signal that the command is unsupported... in non-debug mode.
1644 if (ossl_assert(name
!= NULL
))
1645 if (strcmp(name
, "distid") == 0 || strcmp(name
, "hexdistid") == 0)
1646 cmd
= EVP_PKEY_CTRL_SET1_ID
;
1652 static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX
*ctx
,
1653 int keytype
, int optype
,
1654 int cmd
, const char *name
,
1655 const void *data
, size_t data_len
)
1657 if ((keytype
!= -1 && ctx
->pmeth
->pkey_id
!= keytype
)
1658 || ((optype
!= -1) && !(ctx
->operation
& optype
))) {
1659 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
1663 cmd
= decode_cmd(cmd
, name
);
1665 case EVP_PKEY_CTRL_SET1_ID
:
1666 evp_pkey_ctx_free_cached_data(ctx
, cmd
, name
);
1668 ctx
->cached_parameters
.dist_id_name
= OPENSSL_strdup(name
);
1669 if (ctx
->cached_parameters
.dist_id_name
== NULL
) {
1670 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1675 ctx
->cached_parameters
.dist_id
= OPENSSL_memdup(data
, data_len
);
1676 if (ctx
->cached_parameters
.dist_id
== NULL
) {
1677 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1681 ctx
->cached_parameters
.dist_id_set
= 1;
1682 ctx
->cached_parameters
.dist_id_len
= data_len
;
1686 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1690 static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX
*ctx
,
1691 int cmd
, const char *name
)
1693 cmd
= decode_cmd(cmd
, name
);
1695 case EVP_PKEY_CTRL_SET1_ID
:
1696 OPENSSL_free(ctx
->cached_parameters
.dist_id
);
1697 OPENSSL_free(ctx
->cached_parameters
.dist_id_name
);
1698 ctx
->cached_parameters
.dist_id
= NULL
;
1699 ctx
->cached_parameters
.dist_id_name
= NULL
;
1704 static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX
*ctx
)
1706 evp_pkey_ctx_free_cached_data(ctx
, EVP_PKEY_CTRL_SET1_ID
, NULL
);
1709 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
)
1713 if (ret
&& ctx
->cached_parameters
.dist_id_set
) {
1714 const char *name
= ctx
->cached_parameters
.dist_id_name
;
1715 const void *val
= ctx
->cached_parameters
.dist_id
;
1716 size_t len
= ctx
->cached_parameters
.dist_id_len
;
1719 ret
= evp_pkey_ctx_ctrl_str_int(ctx
, name
, val
);
1721 ret
= evp_pkey_ctx_ctrl_int(ctx
, -1, ctx
->operation
,
1722 EVP_PKEY_CTRL_SET1_ID
,
1723 (int)len
, (void *)val
);
1729 /* Utility functions to send a string of hex string to a ctrl */
1731 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
1738 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
1741 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
1747 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1750 if (binlen
<= INT_MAX
)
1751 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
1756 /* Pass a message digest to a ctrl */
1757 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
1761 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
1762 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
1765 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
1768 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
1770 return ctx
->operation
;
1773 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
1775 ctx
->keygen_info
= dat
;
1776 ctx
->keygen_info_count
= datlen
;
1779 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
1784 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
1789 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
1794 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
1796 return ctx
->peerkey
;
1799 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
1801 ctx
->app_data
= data
;
1804 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
1806 return ctx
->app_data
;
1809 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
1810 int (*init
) (EVP_PKEY_CTX
*ctx
))
1815 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
1816 int (*copy
) (EVP_PKEY_CTX
*dst
,
1817 const EVP_PKEY_CTX
*src
))
1822 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
1823 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
1825 pmeth
->cleanup
= cleanup
;
1828 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
1829 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
1830 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
1833 pmeth
->paramgen_init
= paramgen_init
;
1834 pmeth
->paramgen
= paramgen
;
1837 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
1838 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
1839 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
1842 pmeth
->keygen_init
= keygen_init
;
1843 pmeth
->keygen
= keygen
;
1846 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
1847 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
1848 int (*sign
) (EVP_PKEY_CTX
*ctx
,
1849 unsigned char *sig
, size_t *siglen
,
1850 const unsigned char *tbs
,
1853 pmeth
->sign_init
= sign_init
;
1857 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
1858 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
1859 int (*verify
) (EVP_PKEY_CTX
*ctx
,
1860 const unsigned char *sig
,
1862 const unsigned char *tbs
,
1865 pmeth
->verify_init
= verify_init
;
1866 pmeth
->verify
= verify
;
1869 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
1870 int (*verify_recover_init
) (EVP_PKEY_CTX
1872 int (*verify_recover
) (EVP_PKEY_CTX
1881 pmeth
->verify_recover_init
= verify_recover_init
;
1882 pmeth
->verify_recover
= verify_recover
;
1885 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
1886 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
1888 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
1893 pmeth
->signctx_init
= signctx_init
;
1894 pmeth
->signctx
= signctx
;
1897 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
1898 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1900 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
1901 const unsigned char *sig
,
1905 pmeth
->verifyctx_init
= verifyctx_init
;
1906 pmeth
->verifyctx
= verifyctx
;
1909 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
1910 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
1911 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
1914 const unsigned char *in
,
1917 pmeth
->encrypt_init
= encrypt_init
;
1918 pmeth
->encrypt
= encryptfn
;
1921 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
1922 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
1923 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
1926 const unsigned char *in
,
1929 pmeth
->decrypt_init
= decrypt_init
;
1930 pmeth
->decrypt
= decrypt
;
1933 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
1934 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
1935 int (*derive
) (EVP_PKEY_CTX
*ctx
,
1939 pmeth
->derive_init
= derive_init
;
1940 pmeth
->derive
= derive
;
1943 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
1944 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1946 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
1951 pmeth
->ctrl_str
= ctrl_str
;
1954 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD
*pmeth
,
1955 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
1956 const unsigned char *tbs
, size_t tbslen
))
1958 pmeth
->digestsign
= digestsign
;
1961 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD
*pmeth
,
1962 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
1963 size_t siglen
, const unsigned char *tbs
,
1966 pmeth
->digestverify
= digestverify
;
1969 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
1970 int (*check
) (EVP_PKEY
*pkey
))
1972 pmeth
->check
= check
;
1975 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
1976 int (*check
) (EVP_PKEY
*pkey
))
1978 pmeth
->public_check
= check
;
1981 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
1982 int (*check
) (EVP_PKEY
*pkey
))
1984 pmeth
->param_check
= check
;
1987 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1988 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
1991 pmeth
->digest_custom
= digest_custom
;
1994 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
1995 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
1997 *pinit
= pmeth
->init
;
2000 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
2001 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
2002 const EVP_PKEY_CTX
*src
))
2004 *pcopy
= pmeth
->copy
;
2007 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
2008 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
2010 *pcleanup
= pmeth
->cleanup
;
2013 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
2014 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
2015 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
2019 *pparamgen_init
= pmeth
->paramgen_init
;
2021 *pparamgen
= pmeth
->paramgen
;
2024 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
2025 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
2026 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
2030 *pkeygen_init
= pmeth
->keygen_init
;
2032 *pkeygen
= pmeth
->keygen
;
2035 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
2036 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
2037 int (**psign
) (EVP_PKEY_CTX
*ctx
,
2038 unsigned char *sig
, size_t *siglen
,
2039 const unsigned char *tbs
,
2043 *psign_init
= pmeth
->sign_init
;
2045 *psign
= pmeth
->sign
;
2048 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
2049 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
2050 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
2051 const unsigned char *sig
,
2053 const unsigned char *tbs
,
2057 *pverify_init
= pmeth
->verify_init
;
2059 *pverify
= pmeth
->verify
;
2062 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
2063 int (**pverify_recover_init
) (EVP_PKEY_CTX
2065 int (**pverify_recover
) (EVP_PKEY_CTX
2074 if (pverify_recover_init
)
2075 *pverify_recover_init
= pmeth
->verify_recover_init
;
2076 if (pverify_recover
)
2077 *pverify_recover
= pmeth
->verify_recover
;
2080 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
2081 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
2083 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
2089 *psignctx_init
= pmeth
->signctx_init
;
2091 *psignctx
= pmeth
->signctx
;
2094 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
2095 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
2097 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
2098 const unsigned char *sig
,
2102 if (pverifyctx_init
)
2103 *pverifyctx_init
= pmeth
->verifyctx_init
;
2105 *pverifyctx
= pmeth
->verifyctx
;
2108 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
2109 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
2110 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
2113 const unsigned char *in
,
2117 *pencrypt_init
= pmeth
->encrypt_init
;
2119 *pencryptfn
= pmeth
->encrypt
;
2122 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
2123 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
2124 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
2127 const unsigned char *in
,
2131 *pdecrypt_init
= pmeth
->decrypt_init
;
2133 *pdecrypt
= pmeth
->decrypt
;
2136 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
2137 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
2138 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
2143 *pderive_init
= pmeth
->derive_init
;
2145 *pderive
= pmeth
->derive
;
2148 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
2149 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
2151 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
2156 *pctrl
= pmeth
->ctrl
;
2158 *pctrl_str
= pmeth
->ctrl_str
;
2161 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD
*pmeth
,
2162 int (**digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
2163 const unsigned char *tbs
, size_t tbslen
))
2166 *digestsign
= pmeth
->digestsign
;
2169 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD
*pmeth
,
2170 int (**digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
2171 size_t siglen
, const unsigned char *tbs
,
2175 *digestverify
= pmeth
->digestverify
;
2178 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
2179 int (**pcheck
) (EVP_PKEY
*pkey
))
2182 *pcheck
= pmeth
->check
;
2185 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
2186 int (**pcheck
) (EVP_PKEY
*pkey
))
2189 *pcheck
= pmeth
->public_check
;
2192 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
2193 int (**pcheck
) (EVP_PKEY
*pkey
))
2196 *pcheck
= pmeth
->param_check
;
2199 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
2200 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
2203 if (pdigest_custom
!= NULL
)
2204 *pdigest_custom
= pmeth
->digest_custom
;
2207 #endif /* FIPS_MODULE */