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
,
115 const char *name
, const char *propquery
,
119 const EVP_PKEY_METHOD
*pmeth
= NULL
;
122 * When using providers, the context is bound to the algo implementation
125 if (pkey
== NULL
&& e
== NULL
&& id
== -1)
128 /* TODO(3.0) Legacy code should be removed when all is provider based */
137 * Here, we extract what information we can for the purpose of
138 * supporting usage with implementations from providers, to make
139 * for a smooth transition from legacy stuff to provider based stuff.
141 * If an engine is given, this is entirely legacy, and we should not
142 * pretend anything else, so we only set the name when no engine is
143 * given. If both are already given, someone made a mistake, and
144 * since that can only happen internally, it's safe to make an
147 if (!ossl_assert(e
== NULL
|| name
== NULL
))
150 name
= OBJ_nid2sn(id
);
153 #ifndef OPENSSL_NO_ENGINE
154 if (e
== NULL
&& pkey
!= NULL
)
155 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
156 /* Try to find an ENGINE which implements this method */
158 if (!ENGINE_init(e
)) {
159 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
163 e
= ENGINE_get_pkey_meth_engine(id
);
167 * If an ENGINE handled this method look it up. Otherwise use internal
171 pmeth
= ENGINE_get_pkey_meth(e
, id
);
174 pmeth
= EVP_PKEY_meth_find(id
);
177 #ifndef OPENSSL_NO_ENGINE
180 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
186 ret
= OPENSSL_zalloc(sizeof(*ret
));
188 #ifndef OPENSSL_NO_ENGINE
191 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
194 ret
->algorithm
= name
;
195 ret
->propquery
= propquery
;
198 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
201 EVP_PKEY_up_ref(pkey
);
203 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
204 if (pmeth
->init(ret
) <= 0) {
206 EVP_PKEY_CTX_free(ret
);
214 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
216 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
217 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
218 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
219 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
220 ctx
->op
.kex
.exchprovctx
= NULL
;
221 ctx
->op
.kex
.exchange
= NULL
;
222 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
223 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
224 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
225 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
226 ctx
->op
.sig
.sigprovctx
= NULL
;
227 ctx
->op
.sig
.signature
= NULL
;
231 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
233 EVP_PKEY_METHOD
*pmeth
;
235 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
237 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
242 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
246 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
247 const EVP_PKEY_METHOD
*meth
)
250 *ppkey_id
= meth
->pkey_id
;
252 *pflags
= meth
->flags
;
255 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
258 dst
->init
= src
->init
;
259 dst
->copy
= src
->copy
;
260 dst
->cleanup
= src
->cleanup
;
262 dst
->paramgen_init
= src
->paramgen_init
;
263 dst
->paramgen
= src
->paramgen
;
265 dst
->keygen_init
= src
->keygen_init
;
266 dst
->keygen
= src
->keygen
;
268 dst
->sign_init
= src
->sign_init
;
269 dst
->sign
= src
->sign
;
271 dst
->verify_init
= src
->verify_init
;
272 dst
->verify
= src
->verify
;
274 dst
->verify_recover_init
= src
->verify_recover_init
;
275 dst
->verify_recover
= src
->verify_recover
;
277 dst
->signctx_init
= src
->signctx_init
;
278 dst
->signctx
= src
->signctx
;
280 dst
->verifyctx_init
= src
->verifyctx_init
;
281 dst
->verifyctx
= src
->verifyctx
;
283 dst
->encrypt_init
= src
->encrypt_init
;
284 dst
->encrypt
= src
->encrypt
;
286 dst
->decrypt_init
= src
->decrypt_init
;
287 dst
->decrypt
= src
->decrypt
;
289 dst
->derive_init
= src
->derive_init
;
290 dst
->derive
= src
->derive
;
292 dst
->ctrl
= src
->ctrl
;
293 dst
->ctrl_str
= src
->ctrl_str
;
295 dst
->check
= src
->check
;
298 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
300 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
304 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
306 return int_ctx_new(pkey
, e
, NULL
, NULL
, -1);
309 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
311 return int_ctx_new(NULL
, e
, NULL
, NULL
, id
);
314 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_provided(const char *name
,
315 const char *propquery
)
317 return int_ctx_new(NULL
, NULL
, name
, propquery
, -1);
320 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
324 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
325 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
326 && pctx
->op
.kex
.exchprovctx
== NULL
)
327 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
328 && pctx
->op
.sig
.sigprovctx
== NULL
)))
330 #ifndef OPENSSL_NO_ENGINE
331 /* Make sure it's safe to copy a pkey context using an ENGINE */
332 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
333 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
337 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
339 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
343 if (pctx
->pkey
!= NULL
)
344 EVP_PKEY_up_ref(pctx
->pkey
);
345 rctx
->pkey
= pctx
->pkey
;
346 rctx
->operation
= pctx
->operation
;
347 rctx
->algorithm
= pctx
->algorithm
;
348 rctx
->propquery
= pctx
->propquery
;
350 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
351 if (pctx
->op
.kex
.exchange
!= NULL
) {
352 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
353 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
358 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
359 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
361 rctx
->op
.kex
.exchprovctx
362 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
363 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
364 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
370 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
371 if (pctx
->op
.sig
.signature
!= NULL
) {
372 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
373 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
)) {
378 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
379 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
381 rctx
->op
.sig
.sigprovctx
382 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
383 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
384 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
392 rctx
->pmeth
= pctx
->pmeth
;
393 #ifndef OPENSSL_NO_ENGINE
394 rctx
->engine
= pctx
->engine
;
398 EVP_PKEY_up_ref(pctx
->peerkey
);
399 rctx
->peerkey
= pctx
->peerkey
;
401 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
405 EVP_PKEY_CTX_free(rctx
);
410 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
412 if (app_pkey_methods
== NULL
) {
413 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
414 if (app_pkey_methods
== NULL
){
415 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
419 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
420 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
423 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
427 void evp_app_cleanup_int(void)
429 if (app_pkey_methods
!= NULL
)
430 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
433 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
435 const EVP_PKEY_METHOD
*ret
;
437 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
439 return ret
== NULL
? 0 : 1;
442 size_t EVP_PKEY_meth_get_count(void)
444 size_t rv
= OSSL_NELEM(standard_methods
);
446 if (app_pkey_methods
)
447 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
451 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
453 if (idx
< OSSL_NELEM(standard_methods
))
454 return (standard_methods
[idx
])();
455 if (app_pkey_methods
== NULL
)
457 idx
-= OSSL_NELEM(standard_methods
);
458 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
460 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
463 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
467 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
468 ctx
->pmeth
->cleanup(ctx
);
470 evp_pkey_ctx_free_old_ops(ctx
);
472 EVP_PKEY_free(ctx
->pkey
);
473 EVP_PKEY_free(ctx
->peerkey
);
474 #ifndef OPENSSL_NO_ENGINE
475 ENGINE_finish(ctx
->engine
);
480 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
482 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
483 && ctx
->op
.sig
.sigprovctx
!= NULL
484 && ctx
->op
.sig
.signature
!= NULL
485 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
486 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
491 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
493 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
494 && ctx
->op
.sig
.signature
!= NULL
495 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
)
496 return ctx
->op
.sig
.signature
->gettable_ctx_params();
501 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
503 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
504 && ctx
->op
.kex
.exchprovctx
!= NULL
505 && ctx
->op
.kex
.exchange
!= NULL
506 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
507 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
509 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
510 && ctx
->op
.sig
.sigprovctx
!= NULL
511 && ctx
->op
.sig
.signature
!= NULL
512 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
513 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
518 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
520 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
521 && ctx
->op
.kex
.exchange
!= NULL
522 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
)
523 return ctx
->op
.kex
.exchange
->settable_ctx_params();
524 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
525 && ctx
->op
.sig
.signature
!= NULL
526 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
)
527 return ctx
->op
.sig
.signature
->settable_ctx_params();
532 #ifndef OPENSSL_NO_DH
533 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
535 OSSL_PARAM dh_pad_params
[2];
536 unsigned int upad
= pad
;
538 /* We use EVP_PKEY_CTX_ctrl return values */
539 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
540 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
544 /* TODO(3.0): Remove this eventually when no more legacy */
545 if (ctx
->op
.kex
.exchprovctx
== NULL
)
546 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
547 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
549 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
550 dh_pad_params
[1] = OSSL_PARAM_construct_end();
552 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
556 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
558 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
559 /* 80 should be big enough */
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_GET_MD
, 0, (void *)(md
));
574 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
577 *p
++ = OSSL_PARAM_construct_end();
579 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
582 tmp
= EVP_get_digestbyname(name
);
591 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
593 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
597 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
598 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
599 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
603 /* TODO(3.0): Remove this eventually when no more legacy */
604 if (ctx
->op
.sig
.sigprovctx
== NULL
)
605 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
606 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
612 mdsize
= EVP_MD_size(md
);
613 name
= EVP_MD_name(md
);
616 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
618 * Cast away the const. This is read
619 * only so should be safe
623 *p
++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
,
625 *p
++ = OSSL_PARAM_construct_end();
627 return EVP_PKEY_CTX_set_params(ctx
, sig_md_params
);
630 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
631 int cmd
, int p1
, void *p2
)
634 #ifndef OPENSSL_NO_DH
635 case EVP_PKEY_CTRL_DH_PAD
:
636 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
638 case EVP_PKEY_CTRL_MD
:
639 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
640 case EVP_PKEY_CTRL_GET_MD
:
641 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
646 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
647 int cmd
, int p1
, void *p2
)
652 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
656 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
657 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
658 && ctx
->op
.sig
.sigprovctx
!= NULL
))
659 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
661 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
662 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
665 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
668 /* Skip the operation checks since this is called in a very early stage */
669 if (ctx
->pmeth
->digest_custom
!= NULL
)
672 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
673 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_NO_OPERATION_SET
);
677 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
678 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_INVALID_OPERATION
);
683 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
686 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
691 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
692 int cmd
, uint64_t value
)
694 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
697 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
700 #ifndef OPENSSL_NO_DH
701 if (strcmp(name
, "dh_pad") == 0) {
705 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
708 if (strcmp(name
, "digest") == 0) {
712 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
) || ctx
->op
.sig
.signature
== NULL
)
714 md
= EVP_MD_fetch(ossl_provider_library_context(ctx
->op
.sig
.signature
->prov
),
718 ret
= EVP_PKEY_CTX_set_signature_md(ctx
, md
);
719 EVP_MD_meth_free(md
);
726 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
727 const char *name
, const char *value
)
730 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
734 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
735 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
736 && ctx
->op
.sig
.sigprovctx
!= NULL
))
737 return legacy_ctrl_str_to_param(ctx
, name
, value
);
739 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl_str
) {
740 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
743 if (strcmp(name
, "digest") == 0)
744 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
,
746 return ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
749 /* Utility functions to send a string of hex string to a ctrl */
751 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
758 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
761 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
767 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
770 if (binlen
<= INT_MAX
)
771 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
776 /* Pass a message digest to a ctrl */
777 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
781 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
782 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
785 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
788 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
790 return ctx
->operation
;
793 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
795 ctx
->keygen_info
= dat
;
796 ctx
->keygen_info_count
= datlen
;
799 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
804 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
809 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
814 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
819 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
821 ctx
->app_data
= data
;
824 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
826 return ctx
->app_data
;
829 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
830 int (*init
) (EVP_PKEY_CTX
*ctx
))
835 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
836 int (*copy
) (EVP_PKEY_CTX
*dst
,
837 const EVP_PKEY_CTX
*src
))
842 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
843 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
845 pmeth
->cleanup
= cleanup
;
848 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
849 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
850 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
853 pmeth
->paramgen_init
= paramgen_init
;
854 pmeth
->paramgen
= paramgen
;
857 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
858 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
859 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
862 pmeth
->keygen_init
= keygen_init
;
863 pmeth
->keygen
= keygen
;
866 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
867 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
868 int (*sign
) (EVP_PKEY_CTX
*ctx
,
869 unsigned char *sig
, size_t *siglen
,
870 const unsigned char *tbs
,
873 pmeth
->sign_init
= sign_init
;
877 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
878 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
879 int (*verify
) (EVP_PKEY_CTX
*ctx
,
880 const unsigned char *sig
,
882 const unsigned char *tbs
,
885 pmeth
->verify_init
= verify_init
;
886 pmeth
->verify
= verify
;
889 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
890 int (*verify_recover_init
) (EVP_PKEY_CTX
892 int (*verify_recover
) (EVP_PKEY_CTX
901 pmeth
->verify_recover_init
= verify_recover_init
;
902 pmeth
->verify_recover
= verify_recover
;
905 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
906 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
908 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
913 pmeth
->signctx_init
= signctx_init
;
914 pmeth
->signctx
= signctx
;
917 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
918 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
920 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
921 const unsigned char *sig
,
925 pmeth
->verifyctx_init
= verifyctx_init
;
926 pmeth
->verifyctx
= verifyctx
;
929 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
930 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
931 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
934 const unsigned char *in
,
937 pmeth
->encrypt_init
= encrypt_init
;
938 pmeth
->encrypt
= encryptfn
;
941 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
942 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
943 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
946 const unsigned char *in
,
949 pmeth
->decrypt_init
= decrypt_init
;
950 pmeth
->decrypt
= decrypt
;
953 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
954 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
955 int (*derive
) (EVP_PKEY_CTX
*ctx
,
959 pmeth
->derive_init
= derive_init
;
960 pmeth
->derive
= derive
;
963 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
964 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
966 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
971 pmeth
->ctrl_str
= ctrl_str
;
974 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
975 int (*check
) (EVP_PKEY
*pkey
))
977 pmeth
->check
= check
;
980 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
981 int (*check
) (EVP_PKEY
*pkey
))
983 pmeth
->public_check
= check
;
986 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
987 int (*check
) (EVP_PKEY
*pkey
))
989 pmeth
->param_check
= check
;
992 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
993 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
996 pmeth
->digest_custom
= digest_custom
;
999 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
1000 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
1002 *pinit
= pmeth
->init
;
1005 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
1006 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
1007 const EVP_PKEY_CTX
*src
))
1009 *pcopy
= pmeth
->copy
;
1012 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
1013 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
1015 *pcleanup
= pmeth
->cleanup
;
1018 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
1019 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
1020 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
1024 *pparamgen_init
= pmeth
->paramgen_init
;
1026 *pparamgen
= pmeth
->paramgen
;
1029 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
1030 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
1031 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
1035 *pkeygen_init
= pmeth
->keygen_init
;
1037 *pkeygen
= pmeth
->keygen
;
1040 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
1041 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
1042 int (**psign
) (EVP_PKEY_CTX
*ctx
,
1043 unsigned char *sig
, size_t *siglen
,
1044 const unsigned char *tbs
,
1048 *psign_init
= pmeth
->sign_init
;
1050 *psign
= pmeth
->sign
;
1053 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
1054 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
1055 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
1056 const unsigned char *sig
,
1058 const unsigned char *tbs
,
1062 *pverify_init
= pmeth
->verify_init
;
1064 *pverify
= pmeth
->verify
;
1067 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
1068 int (**pverify_recover_init
) (EVP_PKEY_CTX
1070 int (**pverify_recover
) (EVP_PKEY_CTX
1079 if (pverify_recover_init
)
1080 *pverify_recover_init
= pmeth
->verify_recover_init
;
1081 if (pverify_recover
)
1082 *pverify_recover
= pmeth
->verify_recover
;
1085 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
1086 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
1088 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
1094 *psignctx_init
= pmeth
->signctx_init
;
1096 *psignctx
= pmeth
->signctx
;
1099 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
1100 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1102 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
1103 const unsigned char *sig
,
1107 if (pverifyctx_init
)
1108 *pverifyctx_init
= pmeth
->verifyctx_init
;
1110 *pverifyctx
= pmeth
->verifyctx
;
1113 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
1114 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
1115 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
1118 const unsigned char *in
,
1122 *pencrypt_init
= pmeth
->encrypt_init
;
1124 *pencryptfn
= pmeth
->encrypt
;
1127 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
1128 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
1129 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
1132 const unsigned char *in
,
1136 *pdecrypt_init
= pmeth
->decrypt_init
;
1138 *pdecrypt
= pmeth
->decrypt
;
1141 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
1142 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
1143 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
1148 *pderive_init
= pmeth
->derive_init
;
1150 *pderive
= pmeth
->derive
;
1153 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
1154 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1156 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
1161 *pctrl
= pmeth
->ctrl
;
1163 *pctrl_str
= pmeth
->ctrl_str
;
1166 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
1167 int (**pcheck
) (EVP_PKEY
*pkey
))
1170 *pcheck
= pmeth
->check
;
1173 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
1174 int (**pcheck
) (EVP_PKEY
*pkey
))
1177 *pcheck
= pmeth
->public_check
;
1180 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
1181 int (**pcheck
) (EVP_PKEY
*pkey
))
1184 *pcheck
= pmeth
->param_check
;
1187 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1188 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
1191 if (pdigest_custom
!= NULL
)
1192 *pdigest_custom
= pmeth
->digest_custom
;