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 */
135 name = OBJ_nid2sn(id);
137 #ifndef OPENSSL_NO_ENGINE
138 if (e == NULL && pkey != NULL)
139 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
140 /* Try to find an ENGINE which implements this method */
142 if (!ENGINE_init(e)) {
143 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
147 e = ENGINE_get_pkey_meth_engine(id);
151 * If an ENGINE handled this method look it up. Otherwise use internal
155 pmeth = ENGINE_get_pkey_meth(e, id);
158 pmeth = EVP_PKEY_meth_find(id);
161 #ifndef OPENSSL_NO_ENGINE
164 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
170 ret = OPENSSL_zalloc(sizeof(*ret));
172 #ifndef OPENSSL_NO_ENGINE
175 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
178 ret->algorithm = name;
179 ret->propquery = propquery;
182 ret->operation = EVP_PKEY_OP_UNDEFINED;
185 EVP_PKEY_up_ref(pkey);
187 if (pmeth != NULL && pmeth->init != NULL) {
188 if (pmeth->init(ret) <= 0) {
190 EVP_PKEY_CTX_free(ret);
198 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
200 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
201 if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
202 ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
203 EVP_KEYEXCH_free(ctx->op.kex.exchange);
204 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
205 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
206 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
207 EVP_SIGNATURE_free(ctx->op.sig.signature);
211 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
213 EVP_PKEY_METHOD *pmeth;
215 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
217 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
222 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
226 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
227 const EVP_PKEY_METHOD *meth)
230 *ppkey_id = meth->pkey_id;
232 *pflags = meth->flags;
235 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
238 dst->init = src->init;
239 dst->copy = src->copy;
240 dst->cleanup = src->cleanup;
242 dst->paramgen_init = src->paramgen_init;
243 dst->paramgen = src->paramgen;
245 dst->keygen_init = src->keygen_init;
246 dst->keygen = src->keygen;
248 dst->sign_init = src->sign_init;
249 dst->sign = src->sign;
251 dst->verify_init = src->verify_init;
252 dst->verify = src->verify;
254 dst->verify_recover_init = src->verify_recover_init;
255 dst->verify_recover = src->verify_recover;
257 dst->signctx_init = src->signctx_init;
258 dst->signctx = src->signctx;
260 dst->verifyctx_init = src->verifyctx_init;
261 dst->verifyctx = src->verifyctx;
263 dst->encrypt_init = src->encrypt_init;
264 dst->encrypt = src->encrypt;
266 dst->decrypt_init = src->decrypt_init;
267 dst->decrypt = src->decrypt;
269 dst->derive_init = src->derive_init;
270 dst->derive = src->derive;
272 dst->ctrl = src->ctrl;
273 dst->ctrl_str = src->ctrl_str;
275 dst->check = src->check;
278 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
280 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
284 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
286 return int_ctx_new(pkey, e, NULL, NULL, -1);
289 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
291 return int_ctx_new(NULL, e, NULL, NULL, id);
294 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(const char *name,
295 const char *propquery)
297 return int_ctx_new(NULL, NULL, name, propquery, -1);
300 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
304 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
305 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
306 && pctx->op.kex.exchprovctx == NULL)
307 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
308 && pctx->op.sig.sigprovctx == NULL)))
310 #ifndef OPENSSL_NO_ENGINE
311 /* Make sure it's safe to copy a pkey context using an ENGINE */
312 if (pctx->engine && !ENGINE_init(pctx->engine)) {
313 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
317 rctx = OPENSSL_zalloc(sizeof(*rctx));
319 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
323 if (pctx->pkey != NULL)
324 EVP_PKEY_up_ref(pctx->pkey);
325 rctx->pkey = pctx->pkey;
326 rctx->operation = pctx->operation;
327 rctx->algorithm = pctx->algorithm;
328 rctx->propquery = pctx->propquery;
330 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
331 if (pctx->op.kex.exchange != NULL) {
332 rctx->op.kex.exchange = pctx->op.kex.exchange;
333 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
338 if (pctx->op.kex.exchprovctx != NULL) {
339 if (!ossl_assert(pctx->op.kex.exchange != NULL))
341 rctx->op.kex.exchprovctx
342 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
343 if (rctx->op.kex.exchprovctx == NULL) {
344 EVP_KEYEXCH_free(rctx->op.kex.exchange);
350 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
351 if (pctx->op.sig.signature != NULL) {
352 rctx->op.sig.signature = pctx->op.sig.signature;
353 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
358 if (pctx->op.sig.sigprovctx != NULL) {
359 if (!ossl_assert(pctx->op.sig.signature != NULL))
361 rctx->op.sig.sigprovctx
362 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
363 if (rctx->op.sig.sigprovctx == NULL) {
364 EVP_SIGNATURE_free(rctx->op.sig.signature);
372 rctx->pmeth = pctx->pmeth;
373 #ifndef OPENSSL_NO_ENGINE
374 rctx->engine = pctx->engine;
378 EVP_PKEY_up_ref(pctx->peerkey);
379 rctx->peerkey = pctx->peerkey;
381 if (pctx->pmeth->copy(rctx, pctx) > 0)
385 EVP_PKEY_CTX_free(rctx);
390 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
392 if (app_pkey_methods == NULL) {
393 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
394 if (app_pkey_methods == NULL){
395 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
399 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
400 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
403 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
407 void evp_app_cleanup_int(void)
409 if (app_pkey_methods != NULL)
410 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
413 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
415 const EVP_PKEY_METHOD *ret;
417 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
419 return ret == NULL ? 0 : 1;
422 size_t EVP_PKEY_meth_get_count(void)
424 size_t rv = OSSL_NELEM(standard_methods);
426 if (app_pkey_methods)
427 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
431 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
433 if (idx < OSSL_NELEM(standard_methods))
434 return (standard_methods[idx])();
435 if (app_pkey_methods == NULL)
437 idx -= OSSL_NELEM(standard_methods);
438 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
440 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
443 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
447 if (ctx->pmeth && ctx->pmeth->cleanup)
448 ctx->pmeth->cleanup(ctx);
450 evp_pkey_ctx_free_old_ops(ctx);
452 EVP_PKEY_free(ctx->pkey);
453 EVP_PKEY_free(ctx->peerkey);
454 #ifndef OPENSSL_NO_ENGINE
455 ENGINE_finish(ctx->engine);
460 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
462 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
463 && ctx->op.sig.sigprovctx != NULL
464 && ctx->op.sig.signature != NULL
465 && ctx->op.sig.signature->get_ctx_params != NULL)
466 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
471 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
473 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
474 && ctx->op.sig.signature != NULL
475 && ctx->op.sig.signature->gettable_ctx_params != NULL)
476 return ctx->op.sig.signature->gettable_ctx_params();
481 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
483 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
484 && ctx->op.kex.exchprovctx != NULL
485 && ctx->op.kex.exchange != NULL
486 && ctx->op.kex.exchange->set_ctx_params != NULL)
487 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
489 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
490 && ctx->op.sig.sigprovctx != NULL
491 && ctx->op.sig.signature != NULL
492 && ctx->op.sig.signature->set_ctx_params != NULL)
493 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
498 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
500 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
501 && ctx->op.kex.exchange != NULL
502 && ctx->op.kex.exchange->settable_ctx_params != NULL)
503 return ctx->op.kex.exchange->settable_ctx_params();
504 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
505 && ctx->op.sig.signature != NULL
506 && ctx->op.sig.signature->settable_ctx_params != NULL)
507 return ctx->op.sig.signature->settable_ctx_params();
512 #ifndef OPENSSL_NO_DH
513 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
515 OSSL_PARAM dh_pad_params[2];
516 unsigned int upad = pad;
518 /* We use EVP_PKEY_CTX_ctrl return values */
519 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
520 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
524 /* TODO(3.0): Remove this eventually when no more legacy */
525 if (ctx->op.kex.exchprovctx == NULL)
526 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
527 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
529 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
530 dh_pad_params[1] = OSSL_PARAM_construct_end();
532 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
536 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
538 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
539 /* 80 should be big enough */
543 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
544 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
545 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
549 /* TODO(3.0): Remove this eventually when no more legacy */
550 if (ctx->op.sig.sigprovctx == NULL)
551 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
552 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
554 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
557 *p++ = OSSL_PARAM_construct_end();
559 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
562 tmp = EVP_get_digestbyname(name);
571 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
573 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
577 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
578 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
579 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
583 /* TODO(3.0): Remove this eventually when no more legacy */
584 if (ctx->op.sig.sigprovctx == NULL)
585 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
586 EVP_PKEY_CTRL_MD, 0, (void *)(md));
592 mdsize = EVP_MD_size(md);
593 name = EVP_MD_name(md);
596 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
598 * Cast away the const. This is read
599 * only so should be safe
603 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
605 *p++ = OSSL_PARAM_construct_end();
607 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
610 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
611 int cmd, int p1, void *p2)
614 #ifndef OPENSSL_NO_DH
615 case EVP_PKEY_CTRL_DH_PAD:
616 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
618 case EVP_PKEY_CTRL_MD:
619 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
620 case EVP_PKEY_CTRL_GET_MD:
621 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
626 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
627 int cmd, int p1, void *p2)
632 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
636 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
637 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
638 && ctx->op.sig.sigprovctx != NULL))
639 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
641 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
642 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
645 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
648 /* Skip the operation checks since this is called in a very early stage */
649 if (ctx->pmeth->digest_custom != NULL)
652 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
653 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
657 if ((optype != -1) && !(ctx->operation & optype)) {
658 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
663 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
666 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
671 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
672 int cmd, uint64_t value)
674 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
677 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
680 #ifndef OPENSSL_NO_DH
681 if (strcmp(name, "dh_pad") == 0) {
685 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
688 if (strcmp(name, "digest") == 0) {
692 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
694 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
698 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
699 EVP_MD_meth_free(md);
706 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
707 const char *name, const char *value)
710 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
714 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
715 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
716 && ctx->op.sig.sigprovctx != NULL))
717 return legacy_ctrl_str_to_param(ctx, name, value);
719 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
720 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
723 if (strcmp(name, "digest") == 0)
724 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
726 return ctx->pmeth->ctrl_str(ctx, name, value);
729 /* Utility functions to send a string of hex string to a ctrl */
731 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
738 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
741 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
747 bin = OPENSSL_hexstr2buf(hex, &binlen);
750 if (binlen <= INT_MAX)
751 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
756 /* Pass a message digest to a ctrl */
757 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
761 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
762 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
765 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
768 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
770 return ctx->operation;
773 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
775 ctx->keygen_info = dat;
776 ctx->keygen_info_count = datlen;
779 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
784 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
789 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
794 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
799 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
801 ctx->app_data = data;
804 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
806 return ctx->app_data;
809 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
810 int (*init) (EVP_PKEY_CTX *ctx))
815 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
816 int (*copy) (EVP_PKEY_CTX *dst,
817 const EVP_PKEY_CTX *src))
822 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
823 void (*cleanup) (EVP_PKEY_CTX *ctx))
825 pmeth->cleanup = cleanup;
828 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
829 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
830 int (*paramgen) (EVP_PKEY_CTX *ctx,
833 pmeth->paramgen_init = paramgen_init;
834 pmeth->paramgen = paramgen;
837 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
838 int (*keygen_init) (EVP_PKEY_CTX *ctx),
839 int (*keygen) (EVP_PKEY_CTX *ctx,
842 pmeth->keygen_init = keygen_init;
843 pmeth->keygen = keygen;
846 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
847 int (*sign_init) (EVP_PKEY_CTX *ctx),
848 int (*sign) (EVP_PKEY_CTX *ctx,
849 unsigned char *sig, size_t *siglen,
850 const unsigned char *tbs,
853 pmeth->sign_init = sign_init;
857 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
858 int (*verify_init) (EVP_PKEY_CTX *ctx),
859 int (*verify) (EVP_PKEY_CTX *ctx,
860 const unsigned char *sig,
862 const unsigned char *tbs,
865 pmeth->verify_init = verify_init;
866 pmeth->verify = verify;
869 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
870 int (*verify_recover_init) (EVP_PKEY_CTX
872 int (*verify_recover) (EVP_PKEY_CTX
881 pmeth->verify_recover_init = verify_recover_init;
882 pmeth->verify_recover = verify_recover;
885 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
886 int (*signctx_init) (EVP_PKEY_CTX *ctx,
888 int (*signctx) (EVP_PKEY_CTX *ctx,
893 pmeth->signctx_init = signctx_init;
894 pmeth->signctx = signctx;
897 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
898 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
900 int (*verifyctx) (EVP_PKEY_CTX *ctx,
901 const unsigned char *sig,
905 pmeth->verifyctx_init = verifyctx_init;
906 pmeth->verifyctx = verifyctx;
909 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
910 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
911 int (*encryptfn) (EVP_PKEY_CTX *ctx,
914 const unsigned char *in,
917 pmeth->encrypt_init = encrypt_init;
918 pmeth->encrypt = encryptfn;
921 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
922 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
923 int (*decrypt) (EVP_PKEY_CTX *ctx,
926 const unsigned char *in,
929 pmeth->decrypt_init = decrypt_init;
930 pmeth->decrypt = decrypt;
933 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
934 int (*derive_init) (EVP_PKEY_CTX *ctx),
935 int (*derive) (EVP_PKEY_CTX *ctx,
939 pmeth->derive_init = derive_init;
940 pmeth->derive = derive;
943 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
944 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
946 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
951 pmeth->ctrl_str = ctrl_str;
954 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
955 int (*check) (EVP_PKEY *pkey))
957 pmeth->check = check;
960 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
961 int (*check) (EVP_PKEY *pkey))
963 pmeth->public_check = check;
966 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
967 int (*check) (EVP_PKEY *pkey))
969 pmeth->param_check = check;
972 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
973 int (*digest_custom) (EVP_PKEY_CTX *ctx,
976 pmeth->digest_custom = digest_custom;
979 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
980 int (**pinit) (EVP_PKEY_CTX *ctx))
982 *pinit = pmeth->init;
985 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
986 int (**pcopy) (EVP_PKEY_CTX *dst,
987 const EVP_PKEY_CTX *src))
989 *pcopy = pmeth->copy;
992 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
993 void (**pcleanup) (EVP_PKEY_CTX *ctx))
995 *pcleanup = pmeth->cleanup;
998 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
999 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1000 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1004 *pparamgen_init = pmeth->paramgen_init;
1006 *pparamgen = pmeth->paramgen;
1009 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1010 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1011 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1015 *pkeygen_init = pmeth->keygen_init;
1017 *pkeygen = pmeth->keygen;
1020 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1021 int (**psign_init) (EVP_PKEY_CTX *ctx),
1022 int (**psign) (EVP_PKEY_CTX *ctx,
1023 unsigned char *sig, size_t *siglen,
1024 const unsigned char *tbs,
1028 *psign_init = pmeth->sign_init;
1030 *psign = pmeth->sign;
1033 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1034 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1035 int (**pverify) (EVP_PKEY_CTX *ctx,
1036 const unsigned char *sig,
1038 const unsigned char *tbs,
1042 *pverify_init = pmeth->verify_init;
1044 *pverify = pmeth->verify;
1047 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1048 int (**pverify_recover_init) (EVP_PKEY_CTX
1050 int (**pverify_recover) (EVP_PKEY_CTX
1059 if (pverify_recover_init)
1060 *pverify_recover_init = pmeth->verify_recover_init;
1061 if (pverify_recover)
1062 *pverify_recover = pmeth->verify_recover;
1065 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1066 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1068 int (**psignctx) (EVP_PKEY_CTX *ctx,
1074 *psignctx_init = pmeth->signctx_init;
1076 *psignctx = pmeth->signctx;
1079 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1080 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1082 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1083 const unsigned char *sig,
1087 if (pverifyctx_init)
1088 *pverifyctx_init = pmeth->verifyctx_init;
1090 *pverifyctx = pmeth->verifyctx;
1093 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1094 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1095 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1098 const unsigned char *in,
1102 *pencrypt_init = pmeth->encrypt_init;
1104 *pencryptfn = pmeth->encrypt;
1107 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1108 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1109 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1112 const unsigned char *in,
1116 *pdecrypt_init = pmeth->decrypt_init;
1118 *pdecrypt = pmeth->decrypt;
1121 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1122 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1123 int (**pderive) (EVP_PKEY_CTX *ctx,
1128 *pderive_init = pmeth->derive_init;
1130 *pderive = pmeth->derive;
1133 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1134 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1136 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1141 *pctrl = pmeth->ctrl;
1143 *pctrl_str = pmeth->ctrl_str;
1146 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1147 int (**pcheck) (EVP_PKEY *pkey))
1150 *pcheck = pmeth->check;
1153 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1154 int (**pcheck) (EVP_PKEY *pkey))
1157 *pcheck = pmeth->public_check;
1160 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1161 int (**pcheck) (EVP_PKEY *pkey))
1164 *pcheck = pmeth->param_check;
1167 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1168 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1171 if (pdigest_custom != NULL)
1172 *pdigest_custom = pmeth->digest_custom;