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 "internal/asn1_int.h"
20 #include "internal/evp_int.h"
21 #include "internal/numbers.h"
22 #include "internal/provider.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
;
99 if (app_pkey_methods
) {
101 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
103 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
105 ret
= OBJ_bsearch_pmeth_func(&t
, standard_methods
,
106 sizeof(standard_methods
) /
113 static EVP_PKEY_CTX
*int_ctx_new(EVP_PKEY
*pkey
, ENGINE
*e
, int id
)
116 const EVP_PKEY_METHOD
*pmeth
= NULL
;
119 * When using providers, the context is bound to the algo implementation
122 if (pkey
== NULL
&& e
== NULL
&& id
== -1)
125 /* TODO(3.0) Legacy code should be removed when all is provider based */
132 #ifndef OPENSSL_NO_ENGINE
133 if (e
== NULL
&& pkey
!= NULL
)
134 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
135 /* Try to find an ENGINE which implements this method */
137 if (!ENGINE_init(e
)) {
138 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
142 e
= ENGINE_get_pkey_meth_engine(id
);
146 * If an ENGINE handled this method look it up. Otherwise use internal
150 pmeth
= ENGINE_get_pkey_meth(e
, id
);
153 pmeth
= EVP_PKEY_meth_find(id
);
156 #ifndef OPENSSL_NO_ENGINE
159 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
165 ret
= OPENSSL_zalloc(sizeof(*ret
));
167 #ifndef OPENSSL_NO_ENGINE
170 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
175 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
178 EVP_PKEY_up_ref(pkey
);
180 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
181 if (pmeth
->init(ret
) <= 0) {
183 EVP_PKEY_CTX_free(ret
);
191 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
193 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
194 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
195 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
196 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
197 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
198 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
199 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
200 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
204 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
206 EVP_PKEY_METHOD
*pmeth
;
208 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
210 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
215 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
219 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
220 const EVP_PKEY_METHOD
*meth
)
223 *ppkey_id
= meth
->pkey_id
;
225 *pflags
= meth
->flags
;
228 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
231 dst
->init
= src
->init
;
232 dst
->copy
= src
->copy
;
233 dst
->cleanup
= src
->cleanup
;
235 dst
->paramgen_init
= src
->paramgen_init
;
236 dst
->paramgen
= src
->paramgen
;
238 dst
->keygen_init
= src
->keygen_init
;
239 dst
->keygen
= src
->keygen
;
241 dst
->sign_init
= src
->sign_init
;
242 dst
->sign
= src
->sign
;
244 dst
->verify_init
= src
->verify_init
;
245 dst
->verify
= src
->verify
;
247 dst
->verify_recover_init
= src
->verify_recover_init
;
248 dst
->verify_recover
= src
->verify_recover
;
250 dst
->signctx_init
= src
->signctx_init
;
251 dst
->signctx
= src
->signctx
;
253 dst
->verifyctx_init
= src
->verifyctx_init
;
254 dst
->verifyctx
= src
->verifyctx
;
256 dst
->encrypt_init
= src
->encrypt_init
;
257 dst
->encrypt
= src
->encrypt
;
259 dst
->decrypt_init
= src
->decrypt_init
;
260 dst
->decrypt
= src
->decrypt
;
262 dst
->derive_init
= src
->derive_init
;
263 dst
->derive
= src
->derive
;
265 dst
->ctrl
= src
->ctrl
;
266 dst
->ctrl_str
= src
->ctrl_str
;
268 dst
->check
= src
->check
;
271 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
273 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
277 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
279 return int_ctx_new(pkey
, e
, -1);
282 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
284 return int_ctx_new(NULL
, e
, id
);
287 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
291 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
292 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
293 && pctx
->op
.kex
.exchprovctx
== NULL
)
294 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
295 && pctx
->op
.sig
.sigprovctx
== NULL
)))
297 #ifndef OPENSSL_NO_ENGINE
298 /* Make sure it's safe to copy a pkey context using an ENGINE */
299 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
300 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
304 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
306 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
310 if (pctx
->pkey
!= NULL
)
311 EVP_PKEY_up_ref(pctx
->pkey
);
312 rctx
->pkey
= pctx
->pkey
;
313 rctx
->operation
= pctx
->operation
;
315 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
316 if (pctx
->op
.kex
.exchange
!= NULL
) {
317 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
318 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
323 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
324 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
326 rctx
->op
.kex
.exchprovctx
327 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
328 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
329 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
335 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
336 if (pctx
->op
.sig
.signature
!= NULL
) {
337 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
338 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
)) {
343 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
344 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
346 rctx
->op
.sig
.sigprovctx
347 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
348 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
349 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
357 rctx
->pmeth
= pctx
->pmeth
;
358 #ifndef OPENSSL_NO_ENGINE
359 rctx
->engine
= pctx
->engine
;
363 EVP_PKEY_up_ref(pctx
->peerkey
);
364 rctx
->peerkey
= pctx
->peerkey
;
366 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
370 EVP_PKEY_CTX_free(rctx
);
375 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
377 if (app_pkey_methods
== NULL
) {
378 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
379 if (app_pkey_methods
== NULL
){
380 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
384 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
385 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
388 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
392 void evp_app_cleanup_int(void)
394 if (app_pkey_methods
!= NULL
)
395 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
398 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
400 const EVP_PKEY_METHOD
*ret
;
402 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
404 return ret
== NULL
? 0 : 1;
407 size_t EVP_PKEY_meth_get_count(void)
409 size_t rv
= OSSL_NELEM(standard_methods
);
411 if (app_pkey_methods
)
412 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
416 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
418 if (idx
< OSSL_NELEM(standard_methods
))
419 return (standard_methods
[idx
])();
420 if (app_pkey_methods
== NULL
)
422 idx
-= OSSL_NELEM(standard_methods
);
423 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
425 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
428 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
432 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
433 ctx
->pmeth
->cleanup(ctx
);
435 evp_pkey_ctx_free_old_ops(ctx
);
437 EVP_PKEY_free(ctx
->pkey
);
438 EVP_PKEY_free(ctx
->peerkey
);
439 #ifndef OPENSSL_NO_ENGINE
440 ENGINE_finish(ctx
->engine
);
445 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
447 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
448 && ctx
->op
.sig
.sigprovctx
!= NULL
449 && ctx
->op
.sig
.signature
!= NULL
450 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
451 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
456 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
458 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
459 && ctx
->op
.sig
.signature
!= NULL
460 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
)
461 return ctx
->op
.sig
.signature
->gettable_ctx_params();
466 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
468 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
469 && ctx
->op
.kex
.exchprovctx
!= NULL
470 && ctx
->op
.kex
.exchange
!= NULL
471 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
472 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
474 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
475 && ctx
->op
.sig
.sigprovctx
!= NULL
476 && ctx
->op
.sig
.signature
!= NULL
477 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
478 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
483 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
485 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
486 && ctx
->op
.kex
.exchange
!= NULL
487 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
)
488 return ctx
->op
.kex
.exchange
->settable_ctx_params();
489 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
490 && ctx
->op
.sig
.signature
!= NULL
491 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
)
492 return ctx
->op
.sig
.signature
->settable_ctx_params();
497 #ifndef OPENSSL_NO_DH
498 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
500 OSSL_PARAM dh_pad_params
[2];
501 unsigned int upad
= pad
;
503 /* We use EVP_PKEY_CTX_ctrl return values */
504 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
505 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
509 /* TODO(3.0): Remove this eventually when no more legacy */
510 if (ctx
->op
.kex
.exchprovctx
== NULL
)
511 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
512 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
514 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
515 dh_pad_params
[1] = OSSL_PARAM_construct_end();
517 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
521 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
523 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
524 /* 80 should be big enough */
528 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
529 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
530 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
534 /* TODO(3.0): Remove this eventually when no more legacy */
535 if (ctx
->op
.sig
.sigprovctx
== NULL
)
536 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
537 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
539 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
542 *p
++ = OSSL_PARAM_construct_end();
544 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
547 tmp
= EVP_get_digestbyname(name
);
556 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
558 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
562 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
563 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
564 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
568 /* TODO(3.0): Remove this eventually when no more legacy */
569 if (ctx
->op
.sig
.sigprovctx
== NULL
)
570 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
571 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
577 mdsize
= EVP_MD_size(md
);
578 name
= EVP_MD_name(md
);
581 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
583 * Cast away the const. This is read
584 * only so should be safe
588 *p
++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
,
590 *p
++ = OSSL_PARAM_construct_end();
592 return EVP_PKEY_CTX_set_params(ctx
, sig_md_params
);
595 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
596 int cmd
, int p1
, void *p2
)
599 #ifndef OPENSSL_NO_DH
600 case EVP_PKEY_CTRL_DH_PAD
:
601 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
603 case EVP_PKEY_CTRL_MD
:
604 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
605 case EVP_PKEY_CTRL_GET_MD
:
606 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
611 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
612 int cmd
, int p1
, void *p2
)
617 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
621 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
622 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
623 && ctx
->op
.sig
.sigprovctx
!= NULL
))
624 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
626 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
627 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
630 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
633 /* Skip the operation checks since this is called in a very early stage */
634 if (ctx
->pmeth
->digest_custom
!= NULL
)
637 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
638 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_NO_OPERATION_SET
);
642 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
643 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_INVALID_OPERATION
);
648 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
651 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
656 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
657 int cmd
, uint64_t value
)
659 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
662 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
665 #ifndef OPENSSL_NO_DH
666 if (strcmp(name
, "dh_pad") == 0) {
670 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
673 if (strcmp(name
, "digest") == 0) {
677 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
) || ctx
->op
.sig
.signature
== NULL
)
679 md
= EVP_MD_fetch(ossl_provider_library_context(ctx
->op
.sig
.signature
->prov
),
683 ret
= EVP_PKEY_CTX_set_signature_md(ctx
, md
);
684 EVP_MD_meth_free(md
);
691 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
692 const char *name
, const char *value
)
695 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
699 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
700 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
701 && ctx
->op
.sig
.sigprovctx
!= NULL
))
702 return legacy_ctrl_str_to_param(ctx
, name
, value
);
704 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl_str
) {
705 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
708 if (strcmp(name
, "digest") == 0)
709 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
,
711 return ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
714 /* Utility functions to send a string of hex string to a ctrl */
716 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
723 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
726 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
732 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
735 if (binlen
<= INT_MAX
)
736 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
741 /* Pass a message digest to a ctrl */
742 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
746 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
747 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
750 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
753 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
755 return ctx
->operation
;
758 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
760 ctx
->keygen_info
= dat
;
761 ctx
->keygen_info_count
= datlen
;
764 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
769 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
774 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
779 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
784 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
786 ctx
->app_data
= data
;
789 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
791 return ctx
->app_data
;
794 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
795 int (*init
) (EVP_PKEY_CTX
*ctx
))
800 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
801 int (*copy
) (EVP_PKEY_CTX
*dst
,
802 const EVP_PKEY_CTX
*src
))
807 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
808 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
810 pmeth
->cleanup
= cleanup
;
813 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
814 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
815 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
818 pmeth
->paramgen_init
= paramgen_init
;
819 pmeth
->paramgen
= paramgen
;
822 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
823 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
824 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
827 pmeth
->keygen_init
= keygen_init
;
828 pmeth
->keygen
= keygen
;
831 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
832 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
833 int (*sign
) (EVP_PKEY_CTX
*ctx
,
834 unsigned char *sig
, size_t *siglen
,
835 const unsigned char *tbs
,
838 pmeth
->sign_init
= sign_init
;
842 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
843 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
844 int (*verify
) (EVP_PKEY_CTX
*ctx
,
845 const unsigned char *sig
,
847 const unsigned char *tbs
,
850 pmeth
->verify_init
= verify_init
;
851 pmeth
->verify
= verify
;
854 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
855 int (*verify_recover_init
) (EVP_PKEY_CTX
857 int (*verify_recover
) (EVP_PKEY_CTX
866 pmeth
->verify_recover_init
= verify_recover_init
;
867 pmeth
->verify_recover
= verify_recover
;
870 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
871 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
873 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
878 pmeth
->signctx_init
= signctx_init
;
879 pmeth
->signctx
= signctx
;
882 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
883 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
885 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
886 const unsigned char *sig
,
890 pmeth
->verifyctx_init
= verifyctx_init
;
891 pmeth
->verifyctx
= verifyctx
;
894 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
895 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
896 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
899 const unsigned char *in
,
902 pmeth
->encrypt_init
= encrypt_init
;
903 pmeth
->encrypt
= encryptfn
;
906 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
907 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
908 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
911 const unsigned char *in
,
914 pmeth
->decrypt_init
= decrypt_init
;
915 pmeth
->decrypt
= decrypt
;
918 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
919 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
920 int (*derive
) (EVP_PKEY_CTX
*ctx
,
924 pmeth
->derive_init
= derive_init
;
925 pmeth
->derive
= derive
;
928 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
929 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
931 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
936 pmeth
->ctrl_str
= ctrl_str
;
939 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
940 int (*check
) (EVP_PKEY
*pkey
))
942 pmeth
->check
= check
;
945 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
946 int (*check
) (EVP_PKEY
*pkey
))
948 pmeth
->public_check
= check
;
951 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
952 int (*check
) (EVP_PKEY
*pkey
))
954 pmeth
->param_check
= check
;
957 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
958 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
961 pmeth
->digest_custom
= digest_custom
;
964 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
965 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
967 *pinit
= pmeth
->init
;
970 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
971 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
972 const EVP_PKEY_CTX
*src
))
974 *pcopy
= pmeth
->copy
;
977 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
978 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
980 *pcleanup
= pmeth
->cleanup
;
983 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
984 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
985 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
989 *pparamgen_init
= pmeth
->paramgen_init
;
991 *pparamgen
= pmeth
->paramgen
;
994 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
995 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
996 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
1000 *pkeygen_init
= pmeth
->keygen_init
;
1002 *pkeygen
= pmeth
->keygen
;
1005 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
1006 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
1007 int (**psign
) (EVP_PKEY_CTX
*ctx
,
1008 unsigned char *sig
, size_t *siglen
,
1009 const unsigned char *tbs
,
1013 *psign_init
= pmeth
->sign_init
;
1015 *psign
= pmeth
->sign
;
1018 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
1019 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
1020 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
1021 const unsigned char *sig
,
1023 const unsigned char *tbs
,
1027 *pverify_init
= pmeth
->verify_init
;
1029 *pverify
= pmeth
->verify
;
1032 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
1033 int (**pverify_recover_init
) (EVP_PKEY_CTX
1035 int (**pverify_recover
) (EVP_PKEY_CTX
1044 if (pverify_recover_init
)
1045 *pverify_recover_init
= pmeth
->verify_recover_init
;
1046 if (pverify_recover
)
1047 *pverify_recover
= pmeth
->verify_recover
;
1050 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
1051 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
1053 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
1059 *psignctx_init
= pmeth
->signctx_init
;
1061 *psignctx
= pmeth
->signctx
;
1064 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
1065 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1067 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
1068 const unsigned char *sig
,
1072 if (pverifyctx_init
)
1073 *pverifyctx_init
= pmeth
->verifyctx_init
;
1075 *pverifyctx
= pmeth
->verifyctx
;
1078 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
1079 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
1080 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
1083 const unsigned char *in
,
1087 *pencrypt_init
= pmeth
->encrypt_init
;
1089 *pencryptfn
= pmeth
->encrypt
;
1092 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
1093 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
1094 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
1097 const unsigned char *in
,
1101 *pdecrypt_init
= pmeth
->decrypt_init
;
1103 *pdecrypt
= pmeth
->decrypt
;
1106 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
1107 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
1108 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
1113 *pderive_init
= pmeth
->derive_init
;
1115 *pderive
= pmeth
->derive
;
1118 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
1119 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1121 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
1126 *pctrl
= pmeth
->ctrl
;
1128 *pctrl_str
= pmeth
->ctrl_str
;
1131 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
1132 int (**pcheck
) (EVP_PKEY
*pkey
))
1135 *pcheck
= pmeth
->check
;
1138 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
1139 int (**pcheck
) (EVP_PKEY
*pkey
))
1142 *pcheck
= pmeth
->public_check
;
1145 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
1146 int (**pcheck
) (EVP_PKEY
*pkey
))
1149 *pcheck
= pmeth
->param_check
;
1152 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1153 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
1156 if (pdigest_custom
!= NULL
)
1157 *pdigest_custom
= pmeth
->digest_custom
;