3 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/core_names.h>
17 #include <openssl/dh.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "internal/numbers.h"
22 #include "internal/provider.h"
23 #include "evp_local.h"
25 typedef const EVP_PKEY_METHOD
*(*pmeth_fn
)(void);
26 typedef int sk_cmp_fn_type(const char *const *a
, const char *const *b
);
28 static STACK_OF(EVP_PKEY_METHOD
) *app_pkey_methods
= NULL
;
30 /* This array needs to be in order of NIDs */
31 static pmeth_fn standard_methods
[] = {
32 #ifndef OPENSSL_NO_RSA
38 #ifndef OPENSSL_NO_DSA
45 #ifndef OPENSSL_NO_CMAC
48 #ifndef OPENSSL_NO_RSA
54 #ifndef OPENSSL_NO_SCRYPT
63 #ifndef OPENSSL_NO_POLY1305
66 #ifndef OPENSSL_NO_SIPHASH
73 #ifndef OPENSSL_NO_SM2
78 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
80 static int pmeth_func_cmp(const EVP_PKEY_METHOD
*const *a
, pmeth_fn
const *b
)
82 return ((*a
)->pkey_id
- ((**b
)())->pkey_id
);
85 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, pmeth_fn
, pmeth_func
);
87 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
88 const EVP_PKEY_METHOD
*const *b
)
90 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
93 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
97 const EVP_PKEY_METHOD
*t
= &tmp
;
100 if (app_pkey_methods
) {
102 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
104 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
106 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
107 sizeof(standard_methods
) /
109 if (ret
== NULL
|| *ret
== NULL
)
114 static EVP_PKEY_CTX
*int_ctx_new(EVP_PKEY
*pkey
, ENGINE
*e
, int id
)
117 const EVP_PKEY_METHOD
*pmeth
= NULL
;
120 * When using providers, the context is bound to the algo implementation
123 if (pkey
== NULL
&& e
== NULL
&& id
== -1)
126 /* TODO(3.0) Legacy code should be removed when all is provider based */
133 #ifndef OPENSSL_NO_ENGINE
134 if (e
== NULL
&& pkey
!= NULL
)
135 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
136 /* Try to find an ENGINE which implements this method */
138 if (!ENGINE_init(e
)) {
139 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
143 e
= ENGINE_get_pkey_meth_engine(id
);
147 * If an ENGINE handled this method look it up. Otherwise use internal
151 pmeth
= ENGINE_get_pkey_meth(e
, id
);
154 pmeth
= EVP_PKEY_meth_find(id
);
157 #ifndef OPENSSL_NO_ENGINE
160 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
166 ret
= OPENSSL_zalloc(sizeof(*ret
));
168 #ifndef OPENSSL_NO_ENGINE
171 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
176 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
179 EVP_PKEY_up_ref(pkey
);
181 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
182 if (pmeth
->init(ret
) <= 0) {
184 EVP_PKEY_CTX_free(ret
);
192 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
194 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
195 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
196 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
197 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
198 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
199 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
200 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
201 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
205 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
207 EVP_PKEY_METHOD
*pmeth
;
209 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
211 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
216 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
220 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
221 const EVP_PKEY_METHOD
*meth
)
224 *ppkey_id
= meth
->pkey_id
;
226 *pflags
= meth
->flags
;
229 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
232 dst
->init
= src
->init
;
233 dst
->copy
= src
->copy
;
234 dst
->cleanup
= src
->cleanup
;
236 dst
->paramgen_init
= src
->paramgen_init
;
237 dst
->paramgen
= src
->paramgen
;
239 dst
->keygen_init
= src
->keygen_init
;
240 dst
->keygen
= src
->keygen
;
242 dst
->sign_init
= src
->sign_init
;
243 dst
->sign
= src
->sign
;
245 dst
->verify_init
= src
->verify_init
;
246 dst
->verify
= src
->verify
;
248 dst
->verify_recover_init
= src
->verify_recover_init
;
249 dst
->verify_recover
= src
->verify_recover
;
251 dst
->signctx_init
= src
->signctx_init
;
252 dst
->signctx
= src
->signctx
;
254 dst
->verifyctx_init
= src
->verifyctx_init
;
255 dst
->verifyctx
= src
->verifyctx
;
257 dst
->encrypt_init
= src
->encrypt_init
;
258 dst
->encrypt
= src
->encrypt
;
260 dst
->decrypt_init
= src
->decrypt_init
;
261 dst
->decrypt
= src
->decrypt
;
263 dst
->derive_init
= src
->derive_init
;
264 dst
->derive
= src
->derive
;
266 dst
->ctrl
= src
->ctrl
;
267 dst
->ctrl_str
= src
->ctrl_str
;
269 dst
->check
= src
->check
;
272 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
274 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
278 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
280 return int_ctx_new(pkey
, e
, -1);
283 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
285 return int_ctx_new(NULL
, e
, id
);
288 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
292 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
293 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
294 && pctx
->op
.kex
.exchprovctx
== NULL
)
295 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
296 && pctx
->op
.sig
.sigprovctx
== NULL
)))
298 #ifndef OPENSSL_NO_ENGINE
299 /* Make sure it's safe to copy a pkey context using an ENGINE */
300 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
301 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
305 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
307 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
311 if (pctx
->pkey
!= NULL
)
312 EVP_PKEY_up_ref(pctx
->pkey
);
313 rctx
->pkey
= pctx
->pkey
;
314 rctx
->operation
= pctx
->operation
;
316 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
317 if (pctx
->op
.kex
.exchange
!= NULL
) {
318 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
319 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
324 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
325 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
327 rctx
->op
.kex
.exchprovctx
328 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
329 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
330 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
336 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
337 if (pctx
->op
.sig
.signature
!= NULL
) {
338 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
339 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
)) {
344 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
345 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
347 rctx
->op
.sig
.sigprovctx
348 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
349 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
350 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
358 rctx
->pmeth
= pctx
->pmeth
;
359 #ifndef OPENSSL_NO_ENGINE
360 rctx
->engine
= pctx
->engine
;
364 EVP_PKEY_up_ref(pctx
->peerkey
);
365 rctx
->peerkey
= pctx
->peerkey
;
367 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
371 EVP_PKEY_CTX_free(rctx
);
376 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
378 if (app_pkey_methods
== NULL
) {
379 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
380 if (app_pkey_methods
== NULL
){
381 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
385 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
386 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
389 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
393 void evp_app_cleanup_int(void)
395 if (app_pkey_methods
!= NULL
)
396 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
399 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
401 const EVP_PKEY_METHOD
*ret
;
403 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
405 return ret
== NULL
? 0 : 1;
408 size_t EVP_PKEY_meth_get_count(void)
410 size_t rv
= OSSL_NELEM(standard_methods
);
412 if (app_pkey_methods
)
413 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
417 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
419 if (idx
< OSSL_NELEM(standard_methods
))
420 return (standard_methods
[idx
])();
421 if (app_pkey_methods
== NULL
)
423 idx
-= OSSL_NELEM(standard_methods
);
424 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
426 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
429 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
433 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
434 ctx
->pmeth
->cleanup(ctx
);
436 evp_pkey_ctx_free_old_ops(ctx
);
438 EVP_PKEY_free(ctx
->pkey
);
439 EVP_PKEY_free(ctx
->peerkey
);
440 #ifndef OPENSSL_NO_ENGINE
441 ENGINE_finish(ctx
->engine
);
446 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
448 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
449 && ctx
->op
.sig
.sigprovctx
!= NULL
450 && ctx
->op
.sig
.signature
!= NULL
451 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
452 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
457 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
459 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
460 && ctx
->op
.sig
.signature
!= NULL
461 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
)
462 return ctx
->op
.sig
.signature
->gettable_ctx_params();
467 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
469 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
470 && ctx
->op
.kex
.exchprovctx
!= NULL
471 && ctx
->op
.kex
.exchange
!= NULL
472 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
473 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
475 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
476 && ctx
->op
.sig
.sigprovctx
!= NULL
477 && ctx
->op
.sig
.signature
!= NULL
478 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
479 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
484 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
486 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
487 && ctx
->op
.kex
.exchange
!= NULL
488 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
)
489 return ctx
->op
.kex
.exchange
->settable_ctx_params();
490 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
491 && ctx
->op
.sig
.signature
!= NULL
492 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
)
493 return ctx
->op
.sig
.signature
->settable_ctx_params();
498 #ifndef OPENSSL_NO_DH
499 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
501 OSSL_PARAM dh_pad_params
[2];
502 unsigned int upad
= pad
;
504 /* We use EVP_PKEY_CTX_ctrl return values */
505 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
506 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
510 /* TODO(3.0): Remove this eventually when no more legacy */
511 if (ctx
->op
.kex
.exchprovctx
== NULL
)
512 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
513 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
515 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
516 dh_pad_params
[1] = OSSL_PARAM_construct_end();
518 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
522 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
524 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
525 /* 80 should be big enough */
529 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
530 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
531 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
535 /* TODO(3.0): Remove this eventually when no more legacy */
536 if (ctx
->op
.sig
.sigprovctx
== NULL
)
537 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
538 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
540 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
543 *p
++ = OSSL_PARAM_construct_end();
545 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
548 tmp
= EVP_get_digestbyname(name
);
557 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
559 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
563 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
564 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
565 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
569 /* TODO(3.0): Remove this eventually when no more legacy */
570 if (ctx
->op
.sig
.sigprovctx
== NULL
)
571 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
572 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
578 mdsize
= EVP_MD_size(md
);
579 name
= EVP_MD_name(md
);
582 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
584 * Cast away the const. This is read
585 * only so should be safe
589 *p
++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
,
591 *p
++ = OSSL_PARAM_construct_end();
593 return EVP_PKEY_CTX_set_params(ctx
, sig_md_params
);
596 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
597 int cmd
, int p1
, void *p2
)
600 #ifndef OPENSSL_NO_DH
601 case EVP_PKEY_CTRL_DH_PAD
:
602 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
604 case EVP_PKEY_CTRL_MD
:
605 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
606 case EVP_PKEY_CTRL_GET_MD
:
607 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
612 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
613 int cmd
, int p1
, void *p2
)
618 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
622 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
623 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
624 && ctx
->op
.sig
.sigprovctx
!= NULL
))
625 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
627 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
628 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
631 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
634 /* Skip the operation checks since this is called in a very early stage */
635 if (ctx
->pmeth
->digest_custom
!= NULL
)
638 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
639 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_NO_OPERATION_SET
);
643 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
644 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_INVALID_OPERATION
);
649 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
652 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
657 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
658 int cmd
, uint64_t value
)
660 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
663 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
666 #ifndef OPENSSL_NO_DH
667 if (strcmp(name
, "dh_pad") == 0) {
671 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
674 if (strcmp(name
, "digest") == 0) {
678 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
) || ctx
->op
.sig
.signature
== NULL
)
680 md
= EVP_MD_fetch(ossl_provider_library_context(ctx
->op
.sig
.signature
->prov
),
684 ret
= EVP_PKEY_CTX_set_signature_md(ctx
, md
);
685 EVP_MD_meth_free(md
);
692 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
693 const char *name
, const char *value
)
696 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
700 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
701 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
702 && ctx
->op
.sig
.sigprovctx
!= NULL
))
703 return legacy_ctrl_str_to_param(ctx
, name
, value
);
705 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl_str
) {
706 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
709 if (strcmp(name
, "digest") == 0)
710 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
,
712 return ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
715 /* Utility functions to send a string of hex string to a ctrl */
717 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
724 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
727 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
733 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
736 if (binlen
<= INT_MAX
)
737 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
742 /* Pass a message digest to a ctrl */
743 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
747 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
748 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
751 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
754 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
756 return ctx
->operation
;
759 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
761 ctx
->keygen_info
= dat
;
762 ctx
->keygen_info_count
= datlen
;
765 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
770 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
775 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
780 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
785 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
787 ctx
->app_data
= data
;
790 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
792 return ctx
->app_data
;
795 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
796 int (*init
) (EVP_PKEY_CTX
*ctx
))
801 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
802 int (*copy
) (EVP_PKEY_CTX
*dst
,
803 const EVP_PKEY_CTX
*src
))
808 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
809 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
811 pmeth
->cleanup
= cleanup
;
814 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
815 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
816 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
819 pmeth
->paramgen_init
= paramgen_init
;
820 pmeth
->paramgen
= paramgen
;
823 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
824 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
825 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
828 pmeth
->keygen_init
= keygen_init
;
829 pmeth
->keygen
= keygen
;
832 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
833 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
834 int (*sign
) (EVP_PKEY_CTX
*ctx
,
835 unsigned char *sig
, size_t *siglen
,
836 const unsigned char *tbs
,
839 pmeth
->sign_init
= sign_init
;
843 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
844 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
845 int (*verify
) (EVP_PKEY_CTX
*ctx
,
846 const unsigned char *sig
,
848 const unsigned char *tbs
,
851 pmeth
->verify_init
= verify_init
;
852 pmeth
->verify
= verify
;
855 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
856 int (*verify_recover_init
) (EVP_PKEY_CTX
858 int (*verify_recover
) (EVP_PKEY_CTX
867 pmeth
->verify_recover_init
= verify_recover_init
;
868 pmeth
->verify_recover
= verify_recover
;
871 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
872 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
874 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
879 pmeth
->signctx_init
= signctx_init
;
880 pmeth
->signctx
= signctx
;
883 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
884 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
886 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
887 const unsigned char *sig
,
891 pmeth
->verifyctx_init
= verifyctx_init
;
892 pmeth
->verifyctx
= verifyctx
;
895 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
896 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
897 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
900 const unsigned char *in
,
903 pmeth
->encrypt_init
= encrypt_init
;
904 pmeth
->encrypt
= encryptfn
;
907 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
908 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
909 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
912 const unsigned char *in
,
915 pmeth
->decrypt_init
= decrypt_init
;
916 pmeth
->decrypt
= decrypt
;
919 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
920 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
921 int (*derive
) (EVP_PKEY_CTX
*ctx
,
925 pmeth
->derive_init
= derive_init
;
926 pmeth
->derive
= derive
;
929 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
930 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
932 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
937 pmeth
->ctrl_str
= ctrl_str
;
940 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
941 int (*check
) (EVP_PKEY
*pkey
))
943 pmeth
->check
= check
;
946 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
947 int (*check
) (EVP_PKEY
*pkey
))
949 pmeth
->public_check
= check
;
952 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
953 int (*check
) (EVP_PKEY
*pkey
))
955 pmeth
->param_check
= check
;
958 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
959 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
962 pmeth
->digest_custom
= digest_custom
;
965 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
966 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
968 *pinit
= pmeth
->init
;
971 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
972 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
973 const EVP_PKEY_CTX
*src
))
975 *pcopy
= pmeth
->copy
;
978 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
979 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
981 *pcleanup
= pmeth
->cleanup
;
984 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
985 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
986 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
990 *pparamgen_init
= pmeth
->paramgen_init
;
992 *pparamgen
= pmeth
->paramgen
;
995 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
996 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
997 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
1001 *pkeygen_init
= pmeth
->keygen_init
;
1003 *pkeygen
= pmeth
->keygen
;
1006 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
1007 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
1008 int (**psign
) (EVP_PKEY_CTX
*ctx
,
1009 unsigned char *sig
, size_t *siglen
,
1010 const unsigned char *tbs
,
1014 *psign_init
= pmeth
->sign_init
;
1016 *psign
= pmeth
->sign
;
1019 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
1020 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
1021 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
1022 const unsigned char *sig
,
1024 const unsigned char *tbs
,
1028 *pverify_init
= pmeth
->verify_init
;
1030 *pverify
= pmeth
->verify
;
1033 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
1034 int (**pverify_recover_init
) (EVP_PKEY_CTX
1036 int (**pverify_recover
) (EVP_PKEY_CTX
1045 if (pverify_recover_init
)
1046 *pverify_recover_init
= pmeth
->verify_recover_init
;
1047 if (pverify_recover
)
1048 *pverify_recover
= pmeth
->verify_recover
;
1051 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
1052 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
1054 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
1060 *psignctx_init
= pmeth
->signctx_init
;
1062 *psignctx
= pmeth
->signctx
;
1065 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
1066 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1068 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
1069 const unsigned char *sig
,
1073 if (pverifyctx_init
)
1074 *pverifyctx_init
= pmeth
->verifyctx_init
;
1076 *pverifyctx
= pmeth
->verifyctx
;
1079 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
1080 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
1081 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
1084 const unsigned char *in
,
1088 *pencrypt_init
= pmeth
->encrypt_init
;
1090 *pencryptfn
= pmeth
->encrypt
;
1093 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
1094 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
1095 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
1098 const unsigned char *in
,
1102 *pdecrypt_init
= pmeth
->decrypt_init
;
1104 *pdecrypt
= pmeth
->decrypt
;
1107 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
1108 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
1109 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
1114 *pderive_init
= pmeth
->derive_init
;
1116 *pderive
= pmeth
->derive
;
1119 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
1120 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1122 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
1127 *pctrl
= pmeth
->ctrl
;
1129 *pctrl_str
= pmeth
->ctrl_str
;
1132 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
1133 int (**pcheck
) (EVP_PKEY
*pkey
))
1136 *pcheck
= pmeth
->check
;
1139 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
1140 int (**pcheck
) (EVP_PKEY
*pkey
))
1143 *pcheck
= pmeth
->public_check
;
1146 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
1147 int (**pcheck
) (EVP_PKEY
*pkey
))
1150 *pcheck
= pmeth
->param_check
;
1153 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1154 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
1157 if (pdigest_custom
!= NULL
)
1158 *pdigest_custom
= pmeth
->digest_custom
;