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(OPENSSL_CTX *libctx,
115 EVP_PKEY *pkey, ENGINE *e,
116 const char *name, const char *propquery,
120 const EVP_PKEY_METHOD *pmeth = NULL;
123 * When using providers, the context is bound to the algo implementation
126 if (pkey == NULL && e == NULL && id == -1)
129 /* TODO(3.0) Legacy code should be removed when all is provider based */
138 * Here, we extract what information we can for the purpose of
139 * supporting usage with implementations from providers, to make
140 * for a smooth transition from legacy stuff to provider based stuff.
142 * If an engine is given, this is entirely legacy, and we should not
143 * pretend anything else, so we only set the name when no engine is
144 * given. If both are already given, someone made a mistake, and
145 * since that can only happen internally, it's safe to make an
148 if (!ossl_assert(e == NULL || name == NULL))
151 name = OBJ_nid2sn(id);
154 * We were called using legacy data, or an EVP_PKEY, but an EVP_PKEY
155 * isn't tied to a specific library context, so we fall back to the
156 * default library context.
157 * TODO(v3.0): an EVP_PKEY that doesn't originate from a leagacy key
158 * structure only has the pkeys[] cache, where the first element is
159 * considered the "origin". Investigate if that could be a suitable
160 * way to find a library context.
164 #ifndef OPENSSL_NO_ENGINE
165 if (e == NULL && pkey != NULL)
166 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
167 /* Try to find an ENGINE which implements this method */
169 if (!ENGINE_init(e)) {
170 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
174 e = ENGINE_get_pkey_meth_engine(id);
178 * If an ENGINE handled this method look it up. Otherwise use internal
182 pmeth = ENGINE_get_pkey_meth(e, id);
185 pmeth = EVP_PKEY_meth_find(id);
188 #ifndef OPENSSL_NO_ENGINE
191 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
197 ret = OPENSSL_zalloc(sizeof(*ret));
199 #ifndef OPENSSL_NO_ENGINE
202 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
205 ret->libctx = libctx;
206 ret->algorithm = name;
207 ret->propquery = propquery;
210 ret->operation = EVP_PKEY_OP_UNDEFINED;
213 EVP_PKEY_up_ref(pkey);
215 if (pmeth != NULL && pmeth->init != NULL) {
216 if (pmeth->init(ret) <= 0) {
218 EVP_PKEY_CTX_free(ret);
226 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
228 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
229 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
230 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
231 EVP_KEYEXCH_free(ctx->op.kex.exchange);
232 ctx->op.kex.exchprovctx = NULL;
233 ctx->op.kex.exchange = NULL;
234 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
235 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
236 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
237 EVP_SIGNATURE_free(ctx->op.sig.signature);
238 ctx->op.sig.sigprovctx = NULL;
239 ctx->op.sig.signature = NULL;
243 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
245 EVP_PKEY_METHOD *pmeth;
247 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
249 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
254 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
258 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
259 const EVP_PKEY_METHOD *meth)
262 *ppkey_id = meth->pkey_id;
264 *pflags = meth->flags;
267 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
270 dst->init = src->init;
271 dst->copy = src->copy;
272 dst->cleanup = src->cleanup;
274 dst->paramgen_init = src->paramgen_init;
275 dst->paramgen = src->paramgen;
277 dst->keygen_init = src->keygen_init;
278 dst->keygen = src->keygen;
280 dst->sign_init = src->sign_init;
281 dst->sign = src->sign;
283 dst->verify_init = src->verify_init;
284 dst->verify = src->verify;
286 dst->verify_recover_init = src->verify_recover_init;
287 dst->verify_recover = src->verify_recover;
289 dst->signctx_init = src->signctx_init;
290 dst->signctx = src->signctx;
292 dst->verifyctx_init = src->verifyctx_init;
293 dst->verifyctx = src->verifyctx;
295 dst->encrypt_init = src->encrypt_init;
296 dst->encrypt = src->encrypt;
298 dst->decrypt_init = src->decrypt_init;
299 dst->decrypt = src->decrypt;
301 dst->derive_init = src->derive_init;
302 dst->derive = src->derive;
304 dst->ctrl = src->ctrl;
305 dst->ctrl_str = src->ctrl_str;
307 dst->check = src->check;
310 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
312 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
316 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
318 return int_ctx_new(NULL, pkey, e, NULL, NULL, -1);
321 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
323 return int_ctx_new(NULL, NULL, e, NULL, NULL, id);
326 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx,
328 const char *propquery)
330 return int_ctx_new(libctx, NULL, NULL, name, propquery, -1);
333 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
337 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
338 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
339 && pctx->op.kex.exchprovctx == NULL)
340 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
341 && pctx->op.sig.sigprovctx == NULL)))
343 #ifndef OPENSSL_NO_ENGINE
344 /* Make sure it's safe to copy a pkey context using an ENGINE */
345 if (pctx->engine && !ENGINE_init(pctx->engine)) {
346 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
350 rctx = OPENSSL_zalloc(sizeof(*rctx));
352 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
356 if (pctx->pkey != NULL)
357 EVP_PKEY_up_ref(pctx->pkey);
358 rctx->pkey = pctx->pkey;
359 rctx->operation = pctx->operation;
360 rctx->libctx = pctx->libctx;
361 rctx->algorithm = pctx->algorithm;
362 rctx->propquery = pctx->propquery;
364 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
365 if (pctx->op.kex.exchange != NULL) {
366 rctx->op.kex.exchange = pctx->op.kex.exchange;
367 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
372 if (pctx->op.kex.exchprovctx != NULL) {
373 if (!ossl_assert(pctx->op.kex.exchange != NULL))
375 rctx->op.kex.exchprovctx
376 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
377 if (rctx->op.kex.exchprovctx == NULL) {
378 EVP_KEYEXCH_free(rctx->op.kex.exchange);
384 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
385 if (pctx->op.sig.signature != NULL) {
386 rctx->op.sig.signature = pctx->op.sig.signature;
387 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
392 if (pctx->op.sig.sigprovctx != NULL) {
393 if (!ossl_assert(pctx->op.sig.signature != NULL))
395 rctx->op.sig.sigprovctx
396 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
397 if (rctx->op.sig.sigprovctx == NULL) {
398 EVP_SIGNATURE_free(rctx->op.sig.signature);
406 rctx->pmeth = pctx->pmeth;
407 #ifndef OPENSSL_NO_ENGINE
408 rctx->engine = pctx->engine;
412 EVP_PKEY_up_ref(pctx->peerkey);
413 rctx->peerkey = pctx->peerkey;
415 if (pctx->pmeth->copy(rctx, pctx) > 0)
419 EVP_PKEY_CTX_free(rctx);
424 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
426 if (app_pkey_methods == NULL) {
427 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
428 if (app_pkey_methods == NULL){
429 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
433 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
434 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
437 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
441 void evp_app_cleanup_int(void)
443 if (app_pkey_methods != NULL)
444 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
447 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
449 const EVP_PKEY_METHOD *ret;
451 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
453 return ret == NULL ? 0 : 1;
456 size_t EVP_PKEY_meth_get_count(void)
458 size_t rv = OSSL_NELEM(standard_methods);
460 if (app_pkey_methods)
461 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
465 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
467 if (idx < OSSL_NELEM(standard_methods))
468 return (standard_methods[idx])();
469 if (app_pkey_methods == NULL)
471 idx -= OSSL_NELEM(standard_methods);
472 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
474 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
477 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
481 if (ctx->pmeth && ctx->pmeth->cleanup)
482 ctx->pmeth->cleanup(ctx);
484 evp_pkey_ctx_free_old_ops(ctx);
485 EVP_KEYMGMT_free(ctx->keymgmt);
487 EVP_PKEY_free(ctx->pkey);
488 EVP_PKEY_free(ctx->peerkey);
489 #ifndef OPENSSL_NO_ENGINE
490 ENGINE_finish(ctx->engine);
495 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
497 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
498 && ctx->op.sig.sigprovctx != NULL
499 && ctx->op.sig.signature != NULL
500 && ctx->op.sig.signature->get_ctx_params != NULL)
501 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
506 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
508 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
509 && ctx->op.sig.signature != NULL
510 && ctx->op.sig.signature->gettable_ctx_params != NULL)
511 return ctx->op.sig.signature->gettable_ctx_params();
516 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
518 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
519 && ctx->op.kex.exchprovctx != NULL
520 && ctx->op.kex.exchange != NULL
521 && ctx->op.kex.exchange->set_ctx_params != NULL)
522 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
524 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
525 && ctx->op.sig.sigprovctx != NULL
526 && ctx->op.sig.signature != NULL
527 && ctx->op.sig.signature->set_ctx_params != NULL)
528 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
533 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
535 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
536 && ctx->op.kex.exchange != NULL
537 && ctx->op.kex.exchange->settable_ctx_params != NULL)
538 return ctx->op.kex.exchange->settable_ctx_params();
539 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
540 && ctx->op.sig.signature != NULL
541 && ctx->op.sig.signature->settable_ctx_params != NULL)
542 return ctx->op.sig.signature->settable_ctx_params();
547 #ifndef OPENSSL_NO_DH
548 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
550 OSSL_PARAM dh_pad_params[2];
551 unsigned int upad = pad;
553 /* We use EVP_PKEY_CTX_ctrl return values */
554 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
555 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
559 /* TODO(3.0): Remove this eventually when no more legacy */
560 if (ctx->op.kex.exchprovctx == NULL)
561 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
562 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
564 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
565 dh_pad_params[1] = OSSL_PARAM_construct_end();
567 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
571 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
573 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
574 /* 80 should be big enough */
578 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
579 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
580 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
584 /* TODO(3.0): Remove this eventually when no more legacy */
585 if (ctx->op.sig.sigprovctx == NULL)
586 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
587 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
589 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
592 *p++ = OSSL_PARAM_construct_end();
594 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
597 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
606 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
608 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
612 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
613 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
614 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
618 /* TODO(3.0): Remove this eventually when no more legacy */
619 if (ctx->op.sig.sigprovctx == NULL)
620 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
621 EVP_PKEY_CTRL_MD, 0, (void *)(md));
627 mdsize = EVP_MD_size(md);
628 name = EVP_MD_name(md);
631 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
633 * Cast away the const. This is read
634 * only so should be safe
638 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
640 *p++ = OSSL_PARAM_construct_end();
642 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
645 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
646 int cmd, int p1, void *p2)
649 #ifndef OPENSSL_NO_DH
650 case EVP_PKEY_CTRL_DH_PAD:
651 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
653 case EVP_PKEY_CTRL_MD:
654 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
655 case EVP_PKEY_CTRL_GET_MD:
656 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
661 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
662 int cmd, int p1, void *p2)
667 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
671 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
672 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
673 && ctx->op.sig.sigprovctx != NULL))
674 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
676 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
677 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
680 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
683 /* Skip the operation checks since this is called in a very early stage */
684 if (ctx->pmeth->digest_custom != NULL)
687 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
688 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
692 if ((optype != -1) && !(ctx->operation & optype)) {
693 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
698 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
701 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
706 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
707 int cmd, uint64_t value)
709 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
712 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
715 #ifndef OPENSSL_NO_DH
716 if (strcmp(name, "dh_pad") == 0) {
720 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
723 if (strcmp(name, "digest") == 0) {
727 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
729 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
733 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
734 EVP_MD_meth_free(md);
741 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
742 const char *name, const char *value)
745 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
749 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
750 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
751 && ctx->op.sig.sigprovctx != NULL))
752 return legacy_ctrl_str_to_param(ctx, name, value);
754 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
755 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
758 if (strcmp(name, "digest") == 0)
759 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
761 return ctx->pmeth->ctrl_str(ctx, name, value);
764 /* Utility functions to send a string of hex string to a ctrl */
766 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
773 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
776 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
782 bin = OPENSSL_hexstr2buf(hex, &binlen);
785 if (binlen <= INT_MAX)
786 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
791 /* Pass a message digest to a ctrl */
792 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
796 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
797 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
800 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
803 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
805 return ctx->operation;
808 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
810 ctx->keygen_info = dat;
811 ctx->keygen_info_count = datlen;
814 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
819 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
824 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
829 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
834 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
836 ctx->app_data = data;
839 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
841 return ctx->app_data;
844 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
845 int (*init) (EVP_PKEY_CTX *ctx))
850 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
851 int (*copy) (EVP_PKEY_CTX *dst,
852 const EVP_PKEY_CTX *src))
857 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
858 void (*cleanup) (EVP_PKEY_CTX *ctx))
860 pmeth->cleanup = cleanup;
863 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
864 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
865 int (*paramgen) (EVP_PKEY_CTX *ctx,
868 pmeth->paramgen_init = paramgen_init;
869 pmeth->paramgen = paramgen;
872 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
873 int (*keygen_init) (EVP_PKEY_CTX *ctx),
874 int (*keygen) (EVP_PKEY_CTX *ctx,
877 pmeth->keygen_init = keygen_init;
878 pmeth->keygen = keygen;
881 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
882 int (*sign_init) (EVP_PKEY_CTX *ctx),
883 int (*sign) (EVP_PKEY_CTX *ctx,
884 unsigned char *sig, size_t *siglen,
885 const unsigned char *tbs,
888 pmeth->sign_init = sign_init;
892 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
893 int (*verify_init) (EVP_PKEY_CTX *ctx),
894 int (*verify) (EVP_PKEY_CTX *ctx,
895 const unsigned char *sig,
897 const unsigned char *tbs,
900 pmeth->verify_init = verify_init;
901 pmeth->verify = verify;
904 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
905 int (*verify_recover_init) (EVP_PKEY_CTX
907 int (*verify_recover) (EVP_PKEY_CTX
916 pmeth->verify_recover_init = verify_recover_init;
917 pmeth->verify_recover = verify_recover;
920 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
921 int (*signctx_init) (EVP_PKEY_CTX *ctx,
923 int (*signctx) (EVP_PKEY_CTX *ctx,
928 pmeth->signctx_init = signctx_init;
929 pmeth->signctx = signctx;
932 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
933 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
935 int (*verifyctx) (EVP_PKEY_CTX *ctx,
936 const unsigned char *sig,
940 pmeth->verifyctx_init = verifyctx_init;
941 pmeth->verifyctx = verifyctx;
944 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
945 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
946 int (*encryptfn) (EVP_PKEY_CTX *ctx,
949 const unsigned char *in,
952 pmeth->encrypt_init = encrypt_init;
953 pmeth->encrypt = encryptfn;
956 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
957 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
958 int (*decrypt) (EVP_PKEY_CTX *ctx,
961 const unsigned char *in,
964 pmeth->decrypt_init = decrypt_init;
965 pmeth->decrypt = decrypt;
968 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
969 int (*derive_init) (EVP_PKEY_CTX *ctx),
970 int (*derive) (EVP_PKEY_CTX *ctx,
974 pmeth->derive_init = derive_init;
975 pmeth->derive = derive;
978 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
979 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
981 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
986 pmeth->ctrl_str = ctrl_str;
989 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
990 int (*check) (EVP_PKEY *pkey))
992 pmeth->check = check;
995 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
996 int (*check) (EVP_PKEY *pkey))
998 pmeth->public_check = check;
1001 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1002 int (*check) (EVP_PKEY *pkey))
1004 pmeth->param_check = check;
1007 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1008 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1011 pmeth->digest_custom = digest_custom;
1014 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1015 int (**pinit) (EVP_PKEY_CTX *ctx))
1017 *pinit = pmeth->init;
1020 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1021 int (**pcopy) (EVP_PKEY_CTX *dst,
1022 const EVP_PKEY_CTX *src))
1024 *pcopy = pmeth->copy;
1027 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1028 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1030 *pcleanup = pmeth->cleanup;
1033 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1034 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1035 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1039 *pparamgen_init = pmeth->paramgen_init;
1041 *pparamgen = pmeth->paramgen;
1044 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1045 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1046 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1050 *pkeygen_init = pmeth->keygen_init;
1052 *pkeygen = pmeth->keygen;
1055 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1056 int (**psign_init) (EVP_PKEY_CTX *ctx),
1057 int (**psign) (EVP_PKEY_CTX *ctx,
1058 unsigned char *sig, size_t *siglen,
1059 const unsigned char *tbs,
1063 *psign_init = pmeth->sign_init;
1065 *psign = pmeth->sign;
1068 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1069 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1070 int (**pverify) (EVP_PKEY_CTX *ctx,
1071 const unsigned char *sig,
1073 const unsigned char *tbs,
1077 *pverify_init = pmeth->verify_init;
1079 *pverify = pmeth->verify;
1082 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1083 int (**pverify_recover_init) (EVP_PKEY_CTX
1085 int (**pverify_recover) (EVP_PKEY_CTX
1094 if (pverify_recover_init)
1095 *pverify_recover_init = pmeth->verify_recover_init;
1096 if (pverify_recover)
1097 *pverify_recover = pmeth->verify_recover;
1100 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1101 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1103 int (**psignctx) (EVP_PKEY_CTX *ctx,
1109 *psignctx_init = pmeth->signctx_init;
1111 *psignctx = pmeth->signctx;
1114 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1115 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1117 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1118 const unsigned char *sig,
1122 if (pverifyctx_init)
1123 *pverifyctx_init = pmeth->verifyctx_init;
1125 *pverifyctx = pmeth->verifyctx;
1128 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1129 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1130 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1133 const unsigned char *in,
1137 *pencrypt_init = pmeth->encrypt_init;
1139 *pencryptfn = pmeth->encrypt;
1142 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1143 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1144 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1147 const unsigned char *in,
1151 *pdecrypt_init = pmeth->decrypt_init;
1153 *pdecrypt = pmeth->decrypt;
1156 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1157 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1158 int (**pderive) (EVP_PKEY_CTX *ctx,
1163 *pderive_init = pmeth->derive_init;
1165 *pderive = pmeth->derive;
1168 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1169 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1171 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1176 *pctrl = pmeth->ctrl;
1178 *pctrl_str = pmeth->ctrl_str;
1181 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1182 int (**pcheck) (EVP_PKEY *pkey))
1185 *pcheck = pmeth->check;
1188 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1189 int (**pcheck) (EVP_PKEY *pkey))
1192 *pcheck = pmeth->public_check;
1195 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1196 int (**pcheck) (EVP_PKEY *pkey))
1199 *pcheck = pmeth->param_check;
1202 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1203 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1206 if (pdigest_custom != NULL)
1207 *pdigest_custom = pmeth->digest_custom;