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 ctx->op.kex.exchprovctx = NULL;
205 ctx->op.kex.exchange = NULL;
206 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
207 if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
208 ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
209 EVP_SIGNATURE_free(ctx->op.sig.signature);
210 ctx->op.sig.sigprovctx = NULL;
211 ctx->op.sig.signature = NULL;
215 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
217 EVP_PKEY_METHOD *pmeth;
219 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
221 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
226 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
230 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
231 const EVP_PKEY_METHOD *meth)
234 *ppkey_id = meth->pkey_id;
236 *pflags = meth->flags;
239 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
242 dst->init = src->init;
243 dst->copy = src->copy;
244 dst->cleanup = src->cleanup;
246 dst->paramgen_init = src->paramgen_init;
247 dst->paramgen = src->paramgen;
249 dst->keygen_init = src->keygen_init;
250 dst->keygen = src->keygen;
252 dst->sign_init = src->sign_init;
253 dst->sign = src->sign;
255 dst->verify_init = src->verify_init;
256 dst->verify = src->verify;
258 dst->verify_recover_init = src->verify_recover_init;
259 dst->verify_recover = src->verify_recover;
261 dst->signctx_init = src->signctx_init;
262 dst->signctx = src->signctx;
264 dst->verifyctx_init = src->verifyctx_init;
265 dst->verifyctx = src->verifyctx;
267 dst->encrypt_init = src->encrypt_init;
268 dst->encrypt = src->encrypt;
270 dst->decrypt_init = src->decrypt_init;
271 dst->decrypt = src->decrypt;
273 dst->derive_init = src->derive_init;
274 dst->derive = src->derive;
276 dst->ctrl = src->ctrl;
277 dst->ctrl_str = src->ctrl_str;
279 dst->check = src->check;
282 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
284 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
288 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
290 return int_ctx_new(pkey, e, NULL, NULL, -1);
293 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
295 return int_ctx_new(NULL, e, NULL, NULL, id);
298 EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(const char *name,
299 const char *propquery)
301 return int_ctx_new(NULL, NULL, name, propquery, -1);
304 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
308 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
309 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
310 && pctx->op.kex.exchprovctx == NULL)
311 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
312 && pctx->op.sig.sigprovctx == NULL)))
314 #ifndef OPENSSL_NO_ENGINE
315 /* Make sure it's safe to copy a pkey context using an ENGINE */
316 if (pctx->engine && !ENGINE_init(pctx->engine)) {
317 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
321 rctx = OPENSSL_zalloc(sizeof(*rctx));
323 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
327 if (pctx->pkey != NULL)
328 EVP_PKEY_up_ref(pctx->pkey);
329 rctx->pkey = pctx->pkey;
330 rctx->operation = pctx->operation;
331 rctx->algorithm = pctx->algorithm;
332 rctx->propquery = pctx->propquery;
334 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
335 if (pctx->op.kex.exchange != NULL) {
336 rctx->op.kex.exchange = pctx->op.kex.exchange;
337 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
342 if (pctx->op.kex.exchprovctx != NULL) {
343 if (!ossl_assert(pctx->op.kex.exchange != NULL))
345 rctx->op.kex.exchprovctx
346 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
347 if (rctx->op.kex.exchprovctx == NULL) {
348 EVP_KEYEXCH_free(rctx->op.kex.exchange);
354 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
355 if (pctx->op.sig.signature != NULL) {
356 rctx->op.sig.signature = pctx->op.sig.signature;
357 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
362 if (pctx->op.sig.sigprovctx != NULL) {
363 if (!ossl_assert(pctx->op.sig.signature != NULL))
365 rctx->op.sig.sigprovctx
366 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
367 if (rctx->op.sig.sigprovctx == NULL) {
368 EVP_SIGNATURE_free(rctx->op.sig.signature);
376 rctx->pmeth = pctx->pmeth;
377 #ifndef OPENSSL_NO_ENGINE
378 rctx->engine = pctx->engine;
382 EVP_PKEY_up_ref(pctx->peerkey);
383 rctx->peerkey = pctx->peerkey;
385 if (pctx->pmeth->copy(rctx, pctx) > 0)
389 EVP_PKEY_CTX_free(rctx);
394 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
396 if (app_pkey_methods == NULL) {
397 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
398 if (app_pkey_methods == NULL){
399 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
403 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
404 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
407 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
411 void evp_app_cleanup_int(void)
413 if (app_pkey_methods != NULL)
414 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
417 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
419 const EVP_PKEY_METHOD *ret;
421 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
423 return ret == NULL ? 0 : 1;
426 size_t EVP_PKEY_meth_get_count(void)
428 size_t rv = OSSL_NELEM(standard_methods);
430 if (app_pkey_methods)
431 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
435 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
437 if (idx < OSSL_NELEM(standard_methods))
438 return (standard_methods[idx])();
439 if (app_pkey_methods == NULL)
441 idx -= OSSL_NELEM(standard_methods);
442 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
444 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
447 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
451 if (ctx->pmeth && ctx->pmeth->cleanup)
452 ctx->pmeth->cleanup(ctx);
454 evp_pkey_ctx_free_old_ops(ctx);
456 EVP_PKEY_free(ctx->pkey);
457 EVP_PKEY_free(ctx->peerkey);
458 #ifndef OPENSSL_NO_ENGINE
459 ENGINE_finish(ctx->engine);
464 int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
466 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
467 && ctx->op.sig.sigprovctx != NULL
468 && ctx->op.sig.signature != NULL
469 && ctx->op.sig.signature->get_ctx_params != NULL)
470 return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
475 const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
477 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
478 && ctx->op.sig.signature != NULL
479 && ctx->op.sig.signature->gettable_ctx_params != NULL)
480 return ctx->op.sig.signature->gettable_ctx_params();
485 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
487 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
488 && ctx->op.kex.exchprovctx != NULL
489 && ctx->op.kex.exchange != NULL
490 && ctx->op.kex.exchange->set_ctx_params != NULL)
491 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
493 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
494 && ctx->op.sig.sigprovctx != NULL
495 && ctx->op.sig.signature != NULL
496 && ctx->op.sig.signature->set_ctx_params != NULL)
497 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
502 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
504 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
505 && ctx->op.kex.exchange != NULL
506 && ctx->op.kex.exchange->settable_ctx_params != NULL)
507 return ctx->op.kex.exchange->settable_ctx_params();
508 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
509 && ctx->op.sig.signature != NULL
510 && ctx->op.sig.signature->settable_ctx_params != NULL)
511 return ctx->op.sig.signature->settable_ctx_params();
516 #ifndef OPENSSL_NO_DH
517 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
519 OSSL_PARAM dh_pad_params[2];
520 unsigned int upad = pad;
522 /* We use EVP_PKEY_CTX_ctrl return values */
523 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
524 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
528 /* TODO(3.0): Remove this eventually when no more legacy */
529 if (ctx->op.kex.exchprovctx == NULL)
530 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
531 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
533 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
534 dh_pad_params[1] = OSSL_PARAM_construct_end();
536 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
540 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
542 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
543 /* 80 should be big enough */
547 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
548 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
549 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
553 /* TODO(3.0): Remove this eventually when no more legacy */
554 if (ctx->op.sig.sigprovctx == NULL)
555 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
556 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
558 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
561 *p++ = OSSL_PARAM_construct_end();
563 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
566 tmp = EVP_get_digestbyname(name);
575 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
577 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
581 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
582 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
583 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
587 /* TODO(3.0): Remove this eventually when no more legacy */
588 if (ctx->op.sig.sigprovctx == NULL)
589 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
590 EVP_PKEY_CTRL_MD, 0, (void *)(md));
596 mdsize = EVP_MD_size(md);
597 name = EVP_MD_name(md);
600 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
602 * Cast away the const. This is read
603 * only so should be safe
607 *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
609 *p++ = OSSL_PARAM_construct_end();
611 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
614 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
615 int cmd, int p1, void *p2)
618 #ifndef OPENSSL_NO_DH
619 case EVP_PKEY_CTRL_DH_PAD:
620 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
622 case EVP_PKEY_CTRL_MD:
623 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
624 case EVP_PKEY_CTRL_GET_MD:
625 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
630 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
631 int cmd, int p1, void *p2)
636 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
640 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
641 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
642 && ctx->op.sig.sigprovctx != NULL))
643 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
645 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
646 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
649 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
652 /* Skip the operation checks since this is called in a very early stage */
653 if (ctx->pmeth->digest_custom != NULL)
656 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
657 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
661 if ((optype != -1) && !(ctx->operation & optype)) {
662 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
667 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
670 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
675 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
676 int cmd, uint64_t value)
678 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
681 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
684 #ifndef OPENSSL_NO_DH
685 if (strcmp(name, "dh_pad") == 0) {
689 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
692 if (strcmp(name, "digest") == 0) {
696 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
698 md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
702 ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
703 EVP_MD_meth_free(md);
710 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
711 const char *name, const char *value)
714 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
718 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
719 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
720 && ctx->op.sig.sigprovctx != NULL))
721 return legacy_ctrl_str_to_param(ctx, name, value);
723 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
724 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
727 if (strcmp(name, "digest") == 0)
728 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
730 return ctx->pmeth->ctrl_str(ctx, name, value);
733 /* Utility functions to send a string of hex string to a ctrl */
735 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
742 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
745 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
751 bin = OPENSSL_hexstr2buf(hex, &binlen);
754 if (binlen <= INT_MAX)
755 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
760 /* Pass a message digest to a ctrl */
761 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
765 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
766 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
769 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
772 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
774 return ctx->operation;
777 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
779 ctx->keygen_info = dat;
780 ctx->keygen_info_count = datlen;
783 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
788 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
793 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
798 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
803 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
805 ctx->app_data = data;
808 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
810 return ctx->app_data;
813 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
814 int (*init) (EVP_PKEY_CTX *ctx))
819 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
820 int (*copy) (EVP_PKEY_CTX *dst,
821 const EVP_PKEY_CTX *src))
826 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
827 void (*cleanup) (EVP_PKEY_CTX *ctx))
829 pmeth->cleanup = cleanup;
832 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
833 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
834 int (*paramgen) (EVP_PKEY_CTX *ctx,
837 pmeth->paramgen_init = paramgen_init;
838 pmeth->paramgen = paramgen;
841 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
842 int (*keygen_init) (EVP_PKEY_CTX *ctx),
843 int (*keygen) (EVP_PKEY_CTX *ctx,
846 pmeth->keygen_init = keygen_init;
847 pmeth->keygen = keygen;
850 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
851 int (*sign_init) (EVP_PKEY_CTX *ctx),
852 int (*sign) (EVP_PKEY_CTX *ctx,
853 unsigned char *sig, size_t *siglen,
854 const unsigned char *tbs,
857 pmeth->sign_init = sign_init;
861 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
862 int (*verify_init) (EVP_PKEY_CTX *ctx),
863 int (*verify) (EVP_PKEY_CTX *ctx,
864 const unsigned char *sig,
866 const unsigned char *tbs,
869 pmeth->verify_init = verify_init;
870 pmeth->verify = verify;
873 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
874 int (*verify_recover_init) (EVP_PKEY_CTX
876 int (*verify_recover) (EVP_PKEY_CTX
885 pmeth->verify_recover_init = verify_recover_init;
886 pmeth->verify_recover = verify_recover;
889 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
890 int (*signctx_init) (EVP_PKEY_CTX *ctx,
892 int (*signctx) (EVP_PKEY_CTX *ctx,
897 pmeth->signctx_init = signctx_init;
898 pmeth->signctx = signctx;
901 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
902 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
904 int (*verifyctx) (EVP_PKEY_CTX *ctx,
905 const unsigned char *sig,
909 pmeth->verifyctx_init = verifyctx_init;
910 pmeth->verifyctx = verifyctx;
913 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
914 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
915 int (*encryptfn) (EVP_PKEY_CTX *ctx,
918 const unsigned char *in,
921 pmeth->encrypt_init = encrypt_init;
922 pmeth->encrypt = encryptfn;
925 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
926 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
927 int (*decrypt) (EVP_PKEY_CTX *ctx,
930 const unsigned char *in,
933 pmeth->decrypt_init = decrypt_init;
934 pmeth->decrypt = decrypt;
937 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
938 int (*derive_init) (EVP_PKEY_CTX *ctx),
939 int (*derive) (EVP_PKEY_CTX *ctx,
943 pmeth->derive_init = derive_init;
944 pmeth->derive = derive;
947 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
948 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
950 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
955 pmeth->ctrl_str = ctrl_str;
958 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
959 int (*check) (EVP_PKEY *pkey))
961 pmeth->check = check;
964 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
965 int (*check) (EVP_PKEY *pkey))
967 pmeth->public_check = check;
970 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
971 int (*check) (EVP_PKEY *pkey))
973 pmeth->param_check = check;
976 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
977 int (*digest_custom) (EVP_PKEY_CTX *ctx,
980 pmeth->digest_custom = digest_custom;
983 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
984 int (**pinit) (EVP_PKEY_CTX *ctx))
986 *pinit = pmeth->init;
989 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
990 int (**pcopy) (EVP_PKEY_CTX *dst,
991 const EVP_PKEY_CTX *src))
993 *pcopy = pmeth->copy;
996 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
997 void (**pcleanup) (EVP_PKEY_CTX *ctx))
999 *pcleanup = pmeth->cleanup;
1002 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1003 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1004 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1008 *pparamgen_init = pmeth->paramgen_init;
1010 *pparamgen = pmeth->paramgen;
1013 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1014 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1015 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1019 *pkeygen_init = pmeth->keygen_init;
1021 *pkeygen = pmeth->keygen;
1024 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1025 int (**psign_init) (EVP_PKEY_CTX *ctx),
1026 int (**psign) (EVP_PKEY_CTX *ctx,
1027 unsigned char *sig, size_t *siglen,
1028 const unsigned char *tbs,
1032 *psign_init = pmeth->sign_init;
1034 *psign = pmeth->sign;
1037 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1038 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1039 int (**pverify) (EVP_PKEY_CTX *ctx,
1040 const unsigned char *sig,
1042 const unsigned char *tbs,
1046 *pverify_init = pmeth->verify_init;
1048 *pverify = pmeth->verify;
1051 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1052 int (**pverify_recover_init) (EVP_PKEY_CTX
1054 int (**pverify_recover) (EVP_PKEY_CTX
1063 if (pverify_recover_init)
1064 *pverify_recover_init = pmeth->verify_recover_init;
1065 if (pverify_recover)
1066 *pverify_recover = pmeth->verify_recover;
1069 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1070 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1072 int (**psignctx) (EVP_PKEY_CTX *ctx,
1078 *psignctx_init = pmeth->signctx_init;
1080 *psignctx = pmeth->signctx;
1083 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1084 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1086 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1087 const unsigned char *sig,
1091 if (pverifyctx_init)
1092 *pverifyctx_init = pmeth->verifyctx_init;
1094 *pverifyctx = pmeth->verifyctx;
1097 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1098 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1099 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1102 const unsigned char *in,
1106 *pencrypt_init = pmeth->encrypt_init;
1108 *pencryptfn = pmeth->encrypt;
1111 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1112 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1113 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1116 const unsigned char *in,
1120 *pdecrypt_init = pmeth->decrypt_init;
1122 *pdecrypt = pmeth->decrypt;
1125 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1126 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1127 int (**pderive) (EVP_PKEY_CTX *ctx,
1132 *pderive_init = pmeth->derive_init;
1134 *pderive = pmeth->derive;
1137 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1138 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1140 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1145 *pctrl = pmeth->ctrl;
1147 *pctrl_str = pmeth->ctrl_str;
1150 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1151 int (**pcheck) (EVP_PKEY *pkey))
1154 *pcheck = pmeth->check;
1157 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1158 int (**pcheck) (EVP_PKEY *pkey))
1161 *pcheck = pmeth->public_check;
1164 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1165 int (**pcheck) (EVP_PKEY *pkey))
1168 *pcheck = pmeth->param_check;
1171 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1172 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1175 if (pdigest_custom != NULL)
1176 *pdigest_custom = pmeth->digest_custom;