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 int sk_cmp_fn_type(const char *const *a
, const char *const *b
);
27 static STACK_OF(EVP_PKEY_METHOD
) *app_pkey_methods
= NULL
;
29 /* This array needs to be in order of NIDs */
30 static const EVP_PKEY_METHOD
*standard_methods
[] = {
31 #ifndef OPENSSL_NO_RSA
37 #ifndef OPENSSL_NO_DSA
44 #ifndef OPENSSL_NO_CMAC
47 #ifndef OPENSSL_NO_RSA
53 #ifndef OPENSSL_NO_SCRYPT
62 #ifndef OPENSSL_NO_POLY1305
65 #ifndef OPENSSL_NO_SIPHASH
72 #ifndef OPENSSL_NO_SM2
77 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, const EVP_PKEY_METHOD
*,
80 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
81 const EVP_PKEY_METHOD
*const *b
)
83 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
86 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, const EVP_PKEY_METHOD
*,
89 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
92 const EVP_PKEY_METHOD
*t
= &tmp
, **ret
;
94 if (app_pkey_methods
) {
96 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
98 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
100 ret
= OBJ_bsearch_pmeth(&t
, standard_methods
,
101 sizeof(standard_methods
) /
102 sizeof(EVP_PKEY_METHOD
*));
108 static EVP_PKEY_CTX
*int_ctx_new(EVP_PKEY
*pkey
, ENGINE
*e
, int id
)
111 const EVP_PKEY_METHOD
*pmeth
= NULL
;
114 * When using providers, the context is bound to the algo implementation
117 if (pkey
== NULL
&& e
== NULL
&& id
== -1)
120 /* TODO(3.0) Legacy code should be removed when all is provider based */
127 #ifndef OPENSSL_NO_ENGINE
128 if (e
== NULL
&& pkey
!= NULL
)
129 e
= pkey
->pmeth_engine
!= NULL
? pkey
->pmeth_engine
: pkey
->engine
;
130 /* Try to find an ENGINE which implements this method */
132 if (!ENGINE_init(e
)) {
133 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
137 e
= ENGINE_get_pkey_meth_engine(id
);
141 * If an ENGINE handled this method look it up. Otherwise use internal
145 pmeth
= ENGINE_get_pkey_meth(e
, id
);
148 pmeth
= EVP_PKEY_meth_find(id
);
151 #ifndef OPENSSL_NO_ENGINE
154 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
160 ret
= OPENSSL_zalloc(sizeof(*ret
));
162 #ifndef OPENSSL_NO_ENGINE
165 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
170 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
173 EVP_PKEY_up_ref(pkey
);
175 if (pmeth
!= NULL
&& pmeth
->init
!= NULL
) {
176 if (pmeth
->init(ret
) <= 0) {
178 EVP_PKEY_CTX_free(ret
);
186 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX
*ctx
)
188 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
189 if (ctx
->op
.kex
.exchprovctx
!= NULL
&& ctx
->op
.kex
.exchange
!= NULL
)
190 ctx
->op
.kex
.exchange
->freectx(ctx
->op
.kex
.exchprovctx
);
191 EVP_KEYEXCH_free(ctx
->op
.kex
.exchange
);
192 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
193 if (ctx
->op
.sig
.sigprovctx
!= NULL
&& ctx
->op
.sig
.signature
!= NULL
)
194 ctx
->op
.sig
.signature
->freectx(ctx
->op
.sig
.sigprovctx
);
195 EVP_SIGNATURE_free(ctx
->op
.sig
.signature
);
199 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
201 EVP_PKEY_METHOD
*pmeth
;
203 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
205 EVPerr(EVP_F_EVP_PKEY_METH_NEW
, ERR_R_MALLOC_FAILURE
);
210 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
214 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
215 const EVP_PKEY_METHOD
*meth
)
218 *ppkey_id
= meth
->pkey_id
;
220 *pflags
= meth
->flags
;
223 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
226 dst
->init
= src
->init
;
227 dst
->copy
= src
->copy
;
228 dst
->cleanup
= src
->cleanup
;
230 dst
->paramgen_init
= src
->paramgen_init
;
231 dst
->paramgen
= src
->paramgen
;
233 dst
->keygen_init
= src
->keygen_init
;
234 dst
->keygen
= src
->keygen
;
236 dst
->sign_init
= src
->sign_init
;
237 dst
->sign
= src
->sign
;
239 dst
->verify_init
= src
->verify_init
;
240 dst
->verify
= src
->verify
;
242 dst
->verify_recover_init
= src
->verify_recover_init
;
243 dst
->verify_recover
= src
->verify_recover
;
245 dst
->signctx_init
= src
->signctx_init
;
246 dst
->signctx
= src
->signctx
;
248 dst
->verifyctx_init
= src
->verifyctx_init
;
249 dst
->verifyctx
= src
->verifyctx
;
251 dst
->encrypt_init
= src
->encrypt_init
;
252 dst
->encrypt
= src
->encrypt
;
254 dst
->decrypt_init
= src
->decrypt_init
;
255 dst
->decrypt
= src
->decrypt
;
257 dst
->derive_init
= src
->derive_init
;
258 dst
->derive
= src
->derive
;
260 dst
->ctrl
= src
->ctrl
;
261 dst
->ctrl_str
= src
->ctrl_str
;
263 dst
->check
= src
->check
;
266 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
268 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
272 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
274 return int_ctx_new(pkey
, e
, -1);
277 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
279 return int_ctx_new(NULL
, e
, id
);
282 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(const EVP_PKEY_CTX
*pctx
)
286 if (((pctx
->pmeth
== NULL
) || (pctx
->pmeth
->copy
== NULL
))
287 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)
288 && pctx
->op
.kex
.exchprovctx
== NULL
)
289 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)
290 && pctx
->op
.sig
.sigprovctx
== NULL
)))
292 #ifndef OPENSSL_NO_ENGINE
293 /* Make sure it's safe to copy a pkey context using an ENGINE */
294 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
295 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
299 rctx
= OPENSSL_zalloc(sizeof(*rctx
));
301 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_MALLOC_FAILURE
);
305 if (pctx
->pkey
!= NULL
)
306 EVP_PKEY_up_ref(pctx
->pkey
);
307 rctx
->pkey
= pctx
->pkey
;
308 rctx
->operation
= pctx
->operation
;
310 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx
)) {
311 if (pctx
->op
.kex
.exchange
!= NULL
) {
312 rctx
->op
.kex
.exchange
= pctx
->op
.kex
.exchange
;
313 if (!EVP_KEYEXCH_up_ref(rctx
->op
.kex
.exchange
)) {
318 if (pctx
->op
.kex
.exchprovctx
!= NULL
) {
319 if (!ossl_assert(pctx
->op
.kex
.exchange
!= NULL
))
321 rctx
->op
.kex
.exchprovctx
322 = pctx
->op
.kex
.exchange
->dupctx(pctx
->op
.kex
.exchprovctx
);
323 if (rctx
->op
.kex
.exchprovctx
== NULL
) {
324 EVP_KEYEXCH_free(rctx
->op
.kex
.exchange
);
330 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
331 if (pctx
->op
.sig
.signature
!= NULL
) {
332 rctx
->op
.sig
.signature
= pctx
->op
.sig
.signature
;
333 if (!EVP_SIGNATURE_up_ref(rctx
->op
.sig
.signature
)) {
338 if (pctx
->op
.sig
.sigprovctx
!= NULL
) {
339 if (!ossl_assert(pctx
->op
.sig
.signature
!= NULL
))
341 rctx
->op
.sig
.sigprovctx
342 = pctx
->op
.sig
.signature
->dupctx(pctx
->op
.sig
.sigprovctx
);
343 if (rctx
->op
.sig
.sigprovctx
== NULL
) {
344 EVP_SIGNATURE_free(rctx
->op
.sig
.signature
);
352 rctx
->pmeth
= pctx
->pmeth
;
353 #ifndef OPENSSL_NO_ENGINE
354 rctx
->engine
= pctx
->engine
;
358 EVP_PKEY_up_ref(pctx
->peerkey
);
359 rctx
->peerkey
= pctx
->peerkey
;
361 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
365 EVP_PKEY_CTX_free(rctx
);
370 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
372 if (app_pkey_methods
== NULL
) {
373 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
374 if (app_pkey_methods
== NULL
){
375 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
379 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
)) {
380 EVPerr(EVP_F_EVP_PKEY_METH_ADD0
, ERR_R_MALLOC_FAILURE
);
383 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
387 void evp_app_cleanup_int(void)
389 if (app_pkey_methods
!= NULL
)
390 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods
, EVP_PKEY_meth_free
);
393 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD
*pmeth
)
395 const EVP_PKEY_METHOD
*ret
;
397 ret
= sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods
, pmeth
);
399 return ret
== NULL
? 0 : 1;
402 size_t EVP_PKEY_meth_get_count(void)
404 size_t rv
= OSSL_NELEM(standard_methods
);
406 if (app_pkey_methods
)
407 rv
+= sk_EVP_PKEY_METHOD_num(app_pkey_methods
);
411 const EVP_PKEY_METHOD
*EVP_PKEY_meth_get0(size_t idx
)
413 if (idx
< OSSL_NELEM(standard_methods
))
414 return standard_methods
[idx
];
415 if (app_pkey_methods
== NULL
)
417 idx
-= OSSL_NELEM(standard_methods
);
418 if (idx
>= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods
))
420 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
423 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
427 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
428 ctx
->pmeth
->cleanup(ctx
);
430 evp_pkey_ctx_free_old_ops(ctx
);
432 EVP_PKEY_free(ctx
->pkey
);
433 EVP_PKEY_free(ctx
->peerkey
);
434 #ifndef OPENSSL_NO_ENGINE
435 ENGINE_finish(ctx
->engine
);
440 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
442 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
443 && ctx
->op
.sig
.sigprovctx
!= NULL
444 && ctx
->op
.sig
.signature
!= NULL
445 && ctx
->op
.sig
.signature
->get_ctx_params
!= NULL
)
446 return ctx
->op
.sig
.signature
->get_ctx_params(ctx
->op
.sig
.sigprovctx
,
451 const OSSL_PARAM
*EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX
*ctx
)
453 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
454 && ctx
->op
.sig
.signature
!= NULL
455 && ctx
->op
.sig
.signature
->gettable_ctx_params
!= NULL
)
456 return ctx
->op
.sig
.signature
->gettable_ctx_params();
461 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
)
463 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
464 && ctx
->op
.kex
.exchprovctx
!= NULL
465 && ctx
->op
.kex
.exchange
!= NULL
466 && ctx
->op
.kex
.exchange
->set_ctx_params
!= NULL
)
467 return ctx
->op
.kex
.exchange
->set_ctx_params(ctx
->op
.kex
.exchprovctx
,
469 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
470 && ctx
->op
.sig
.sigprovctx
!= NULL
471 && ctx
->op
.sig
.signature
!= NULL
472 && ctx
->op
.sig
.signature
->set_ctx_params
!= NULL
)
473 return ctx
->op
.sig
.signature
->set_ctx_params(ctx
->op
.sig
.sigprovctx
,
478 const OSSL_PARAM
*EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX
*ctx
)
480 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
481 && ctx
->op
.kex
.exchange
!= NULL
482 && ctx
->op
.kex
.exchange
->settable_ctx_params
!= NULL
)
483 return ctx
->op
.kex
.exchange
->settable_ctx_params();
484 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
485 && ctx
->op
.sig
.signature
!= NULL
486 && ctx
->op
.sig
.signature
->settable_ctx_params
!= NULL
)
487 return ctx
->op
.sig
.signature
->settable_ctx_params();
492 #ifndef OPENSSL_NO_DH
493 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX
*ctx
, int pad
)
495 OSSL_PARAM dh_pad_params
[2];
496 unsigned int upad
= pad
;
498 /* We use EVP_PKEY_CTX_ctrl return values */
499 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)) {
500 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
504 /* TODO(3.0): Remove this eventually when no more legacy */
505 if (ctx
->op
.kex
.exchprovctx
== NULL
)
506 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
, EVP_PKEY_OP_DERIVE
,
507 EVP_PKEY_CTRL_DH_PAD
, pad
, NULL
);
509 dh_pad_params
[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD
, &upad
);
510 dh_pad_params
[1] = OSSL_PARAM_construct_end();
512 return EVP_PKEY_CTX_set_params(ctx
, dh_pad_params
);
516 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
**md
)
518 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
519 /* 80 should be big enough */
523 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
524 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
525 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
529 /* TODO(3.0): Remove this eventually when no more legacy */
530 if (ctx
->op
.sig
.sigprovctx
== NULL
)
531 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
532 EVP_PKEY_CTRL_GET_MD
, 0, (void *)(md
));
534 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
537 *p
++ = OSSL_PARAM_construct_end();
539 if (!EVP_PKEY_CTX_get_params(ctx
, sig_md_params
))
542 tmp
= EVP_get_digestbyname(name
);
551 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
553 OSSL_PARAM sig_md_params
[3], *p
= sig_md_params
;
557 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)) {
558 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
559 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
563 /* TODO(3.0): Remove this eventually when no more legacy */
564 if (ctx
->op
.sig
.sigprovctx
== NULL
)
565 return EVP_PKEY_CTX_ctrl(ctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
566 EVP_PKEY_CTRL_MD
, 0, (void *)(md
));
572 mdsize
= EVP_MD_size(md
);
573 name
= EVP_MD_name(md
);
576 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
,
578 * Cast away the const. This is read
579 * only so should be safe
583 *p
++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE
,
585 *p
++ = OSSL_PARAM_construct_end();
587 return EVP_PKEY_CTX_set_params(ctx
, sig_md_params
);
590 static int legacy_ctrl_to_param(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
591 int cmd
, int p1
, void *p2
)
594 #ifndef OPENSSL_NO_DH
595 case EVP_PKEY_CTRL_DH_PAD
:
596 return EVP_PKEY_CTX_set_dh_pad(ctx
, p1
);
598 case EVP_PKEY_CTRL_MD
:
599 return EVP_PKEY_CTX_set_signature_md(ctx
, p2
);
600 case EVP_PKEY_CTRL_GET_MD
:
601 return EVP_PKEY_CTX_get_signature_md(ctx
, p2
);
606 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
607 int cmd
, int p1
, void *p2
)
612 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
616 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
617 || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx
)
618 && ctx
->op
.sig
.sigprovctx
!= NULL
))
619 return legacy_ctrl_to_param(ctx
, keytype
, optype
, cmd
, p1
, p2
);
621 if (ctx
->pmeth
== NULL
|| ctx
->pmeth
->ctrl
== NULL
) {
622 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
625 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
628 /* Skip the operation checks since this is called in a very early stage */
629 if (ctx
->pmeth
->digest_custom
!= NULL
)
632 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
633 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_NO_OPERATION_SET
);
637 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
638 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_INVALID_OPERATION
);
643 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
646 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
651 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
652 int cmd
, uint64_t value
)
654 return EVP_PKEY_CTX_ctrl(ctx
, keytype
, optype
, cmd
, 0, &value
);
657 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX
*ctx
, const char *name
,
660 #ifndef OPENSSL_NO_DH
661 if (strcmp(name
, "dh_pad") == 0) {
665 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
668 if (strcmp(name
, "digest") == 0) {
672 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
) || ctx
->op
.sig
.signature
== NULL
)
674 md
= EVP_MD_fetch(ossl_provider_library_context(ctx
->op
.sig
.signature
->prov
),
678 ret
= EVP_PKEY_CTX_set_signature_md(ctx
, md
);
679 EVP_MD_meth_free(md
);
686 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
687 const char *name
, const char *value
)
690 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
694 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx
) && ctx
->op
.kex
.exchprovctx
!= NULL
)
695 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
)
696 && ctx
->op
.sig
.sigprovctx
!= NULL
))
697 return legacy_ctrl_str_to_param(ctx
, name
, value
);
699 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl_str
) {
700 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
703 if (strcmp(name
, "digest") == 0)
704 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
,
706 return ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
709 /* Utility functions to send a string of hex string to a ctrl */
711 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
718 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
721 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
727 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
730 if (binlen
<= INT_MAX
)
731 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
736 /* Pass a message digest to a ctrl */
737 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
741 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
742 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
745 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
748 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
750 return ctx
->operation
;
753 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
755 ctx
->keygen_info
= dat
;
756 ctx
->keygen_info_count
= datlen
;
759 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
764 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX
*ctx
)
769 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
774 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
779 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
781 ctx
->app_data
= data
;
784 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
786 return ctx
->app_data
;
789 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
790 int (*init
) (EVP_PKEY_CTX
*ctx
))
795 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
796 int (*copy
) (EVP_PKEY_CTX
*dst
,
797 const EVP_PKEY_CTX
*src
))
802 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
803 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
805 pmeth
->cleanup
= cleanup
;
808 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
809 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
810 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
813 pmeth
->paramgen_init
= paramgen_init
;
814 pmeth
->paramgen
= paramgen
;
817 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
818 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
819 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
822 pmeth
->keygen_init
= keygen_init
;
823 pmeth
->keygen
= keygen
;
826 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
827 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
828 int (*sign
) (EVP_PKEY_CTX
*ctx
,
829 unsigned char *sig
, size_t *siglen
,
830 const unsigned char *tbs
,
833 pmeth
->sign_init
= sign_init
;
837 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
838 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
839 int (*verify
) (EVP_PKEY_CTX
*ctx
,
840 const unsigned char *sig
,
842 const unsigned char *tbs
,
845 pmeth
->verify_init
= verify_init
;
846 pmeth
->verify
= verify
;
849 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
850 int (*verify_recover_init
) (EVP_PKEY_CTX
852 int (*verify_recover
) (EVP_PKEY_CTX
861 pmeth
->verify_recover_init
= verify_recover_init
;
862 pmeth
->verify_recover
= verify_recover
;
865 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
866 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
868 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
873 pmeth
->signctx_init
= signctx_init
;
874 pmeth
->signctx
= signctx
;
877 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
878 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
880 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
881 const unsigned char *sig
,
885 pmeth
->verifyctx_init
= verifyctx_init
;
886 pmeth
->verifyctx
= verifyctx
;
889 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
890 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
891 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
894 const unsigned char *in
,
897 pmeth
->encrypt_init
= encrypt_init
;
898 pmeth
->encrypt
= encryptfn
;
901 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
902 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
903 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
906 const unsigned char *in
,
909 pmeth
->decrypt_init
= decrypt_init
;
910 pmeth
->decrypt
= decrypt
;
913 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
914 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
915 int (*derive
) (EVP_PKEY_CTX
*ctx
,
919 pmeth
->derive_init
= derive_init
;
920 pmeth
->derive
= derive
;
923 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
924 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
926 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
931 pmeth
->ctrl_str
= ctrl_str
;
934 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD
*pmeth
,
935 int (*check
) (EVP_PKEY
*pkey
))
937 pmeth
->check
= check
;
940 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD
*pmeth
,
941 int (*check
) (EVP_PKEY
*pkey
))
943 pmeth
->public_check
= check
;
946 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD
*pmeth
,
947 int (*check
) (EVP_PKEY
*pkey
))
949 pmeth
->param_check
= check
;
952 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD
*pmeth
,
953 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
,
956 pmeth
->digest_custom
= digest_custom
;
959 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD
*pmeth
,
960 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
962 *pinit
= pmeth
->init
;
965 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD
*pmeth
,
966 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
967 const EVP_PKEY_CTX
*src
))
969 *pcopy
= pmeth
->copy
;
972 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD
*pmeth
,
973 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
975 *pcleanup
= pmeth
->cleanup
;
978 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD
*pmeth
,
979 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
980 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
984 *pparamgen_init
= pmeth
->paramgen_init
;
986 *pparamgen
= pmeth
->paramgen
;
989 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD
*pmeth
,
990 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
991 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
995 *pkeygen_init
= pmeth
->keygen_init
;
997 *pkeygen
= pmeth
->keygen
;
1000 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD
*pmeth
,
1001 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
1002 int (**psign
) (EVP_PKEY_CTX
*ctx
,
1003 unsigned char *sig
, size_t *siglen
,
1004 const unsigned char *tbs
,
1008 *psign_init
= pmeth
->sign_init
;
1010 *psign
= pmeth
->sign
;
1013 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD
*pmeth
,
1014 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
1015 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
1016 const unsigned char *sig
,
1018 const unsigned char *tbs
,
1022 *pverify_init
= pmeth
->verify_init
;
1024 *pverify
= pmeth
->verify
;
1027 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD
*pmeth
,
1028 int (**pverify_recover_init
) (EVP_PKEY_CTX
1030 int (**pverify_recover
) (EVP_PKEY_CTX
1039 if (pverify_recover_init
)
1040 *pverify_recover_init
= pmeth
->verify_recover_init
;
1041 if (pverify_recover
)
1042 *pverify_recover
= pmeth
->verify_recover
;
1045 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD
*pmeth
,
1046 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
1048 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
1054 *psignctx_init
= pmeth
->signctx_init
;
1056 *psignctx
= pmeth
->signctx
;
1059 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD
*pmeth
,
1060 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
1062 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
1063 const unsigned char *sig
,
1067 if (pverifyctx_init
)
1068 *pverifyctx_init
= pmeth
->verifyctx_init
;
1070 *pverifyctx
= pmeth
->verifyctx
;
1073 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD
*pmeth
,
1074 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
1075 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
1078 const unsigned char *in
,
1082 *pencrypt_init
= pmeth
->encrypt_init
;
1084 *pencryptfn
= pmeth
->encrypt
;
1087 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD
*pmeth
,
1088 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
1089 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
1092 const unsigned char *in
,
1096 *pdecrypt_init
= pmeth
->decrypt_init
;
1098 *pdecrypt
= pmeth
->decrypt
;
1101 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD
*pmeth
,
1102 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
1103 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
1108 *pderive_init
= pmeth
->derive_init
;
1110 *pderive
= pmeth
->derive
;
1113 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD
*pmeth
,
1114 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
1116 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
1121 *pctrl
= pmeth
->ctrl
;
1123 *pctrl_str
= pmeth
->ctrl_str
;
1126 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD
*pmeth
,
1127 int (**pcheck
) (EVP_PKEY
*pkey
))
1130 *pcheck
= pmeth
->check
;
1133 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD
*pmeth
,
1134 int (**pcheck
) (EVP_PKEY
*pkey
))
1137 *pcheck
= pmeth
->public_check
;
1140 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD
*pmeth
,
1141 int (**pcheck
) (EVP_PKEY
*pkey
))
1144 *pcheck
= pmeth
->param_check
;
1147 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD
*pmeth
,
1148 int (**pdigest_custom
) (EVP_PKEY_CTX
*ctx
,
1151 if (pdigest_custom
!= NULL
)
1152 *pdigest_custom
= pmeth
->digest_custom
;