2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
18 #include "internal/numbers.h"
20 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
22 static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
24 /* This array needs to be in order of NIDs */
25 static const EVP_PKEY_METHOD *standard_methods[] = {
26 #ifndef OPENSSL_NO_RSA
32 #ifndef OPENSSL_NO_DSA
39 #ifndef OPENSSL_NO_CMAC
42 #ifndef OPENSSL_NO_RSA
48 #ifndef OPENSSL_NO_SCRYPT
57 #ifndef OPENSSL_NO_POLY1305
60 #ifndef OPENSSL_NO_SIPHASH
67 #ifndef OPENSSL_NO_SM2
72 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
75 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
76 const EVP_PKEY_METHOD *const *b)
78 return ((*a)->pkey_id - (*b)->pkey_id);
81 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
84 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
87 const EVP_PKEY_METHOD *t = &tmp, **ret;
89 if (app_pkey_methods) {
91 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
93 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
95 ret = OBJ_bsearch_pmeth(&t, standard_methods,
96 sizeof(standard_methods) /
97 sizeof(EVP_PKEY_METHOD *));
103 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
106 const EVP_PKEY_METHOD *pmeth;
111 #ifndef OPENSSL_NO_ENGINE
112 if (e == NULL && pkey != NULL)
113 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
114 /* Try to find an ENGINE which implements this method */
116 if (!ENGINE_init(e)) {
117 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
121 e = ENGINE_get_pkey_meth_engine(id);
125 * If an ENGINE handled this method look it up. Otherwise use internal
129 pmeth = ENGINE_get_pkey_meth(e, id);
132 pmeth = EVP_PKEY_meth_find(id);
135 #ifndef OPENSSL_NO_ENGINE
138 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
142 ret = OPENSSL_zalloc(sizeof(*ret));
144 #ifndef OPENSSL_NO_ENGINE
147 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
152 ret->operation = EVP_PKEY_OP_UNDEFINED;
155 EVP_PKEY_up_ref(pkey);
158 if (pmeth->init(ret) <= 0) {
160 EVP_PKEY_CTX_free(ret);
168 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
170 EVP_PKEY_METHOD *pmeth;
172 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
174 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
179 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
183 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
184 const EVP_PKEY_METHOD *meth)
187 *ppkey_id = meth->pkey_id;
189 *pflags = meth->flags;
192 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
195 dst->init = src->init;
196 dst->copy = src->copy;
197 dst->cleanup = src->cleanup;
199 dst->paramgen_init = src->paramgen_init;
200 dst->paramgen = src->paramgen;
202 dst->keygen_init = src->keygen_init;
203 dst->keygen = src->keygen;
205 dst->sign_init = src->sign_init;
206 dst->sign = src->sign;
208 dst->verify_init = src->verify_init;
209 dst->verify = src->verify;
211 dst->verify_recover_init = src->verify_recover_init;
212 dst->verify_recover = src->verify_recover;
214 dst->signctx_init = src->signctx_init;
215 dst->signctx = src->signctx;
217 dst->verifyctx_init = src->verifyctx_init;
218 dst->verifyctx = src->verifyctx;
220 dst->encrypt_init = src->encrypt_init;
221 dst->encrypt = src->encrypt;
223 dst->decrypt_init = src->decrypt_init;
224 dst->decrypt = src->decrypt;
226 dst->derive_init = src->derive_init;
227 dst->derive = src->derive;
229 dst->ctrl = src->ctrl;
230 dst->ctrl_str = src->ctrl_str;
232 dst->check = src->check;
235 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
237 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
241 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
243 return int_ctx_new(pkey, e, -1);
246 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
248 return int_ctx_new(NULL, e, id);
251 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
254 if (!pctx->pmeth || !pctx->pmeth->copy)
256 #ifndef OPENSSL_NO_ENGINE
257 /* Make sure it's safe to copy a pkey context using an ENGINE */
258 if (pctx->engine && !ENGINE_init(pctx->engine)) {
259 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
263 rctx = OPENSSL_malloc(sizeof(*rctx));
265 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
269 rctx->pmeth = pctx->pmeth;
270 #ifndef OPENSSL_NO_ENGINE
271 rctx->engine = pctx->engine;
275 EVP_PKEY_up_ref(pctx->pkey);
277 rctx->pkey = pctx->pkey;
280 EVP_PKEY_up_ref(pctx->peerkey);
282 rctx->peerkey = pctx->peerkey;
285 rctx->app_data = NULL;
286 rctx->operation = pctx->operation;
288 if (pctx->pmeth->copy(rctx, pctx) > 0)
292 EVP_PKEY_CTX_free(rctx);
297 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
299 if (app_pkey_methods == NULL) {
300 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
301 if (app_pkey_methods == NULL){
302 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
306 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
307 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
310 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
314 void evp_app_cleanup_int(void)
316 if (app_pkey_methods != NULL)
317 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
320 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
322 const EVP_PKEY_METHOD *ret;
324 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
326 return ret == NULL ? 0 : 1;
329 size_t EVP_PKEY_meth_get_count(void)
331 size_t rv = OSSL_NELEM(standard_methods);
333 if (app_pkey_methods)
334 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
338 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
340 if (idx < OSSL_NELEM(standard_methods))
341 return standard_methods[idx];
342 if (app_pkey_methods == NULL)
344 idx -= OSSL_NELEM(standard_methods);
345 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
347 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
350 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
354 if (ctx->pmeth && ctx->pmeth->cleanup)
355 ctx->pmeth->cleanup(ctx);
356 EVP_PKEY_free(ctx->pkey);
357 EVP_PKEY_free(ctx->peerkey);
358 #ifndef OPENSSL_NO_ENGINE
359 ENGINE_finish(ctx->engine);
364 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
365 int cmd, int p1, void *p2)
368 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
369 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
372 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
375 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
376 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
380 if ((optype != -1) && !(ctx->operation & optype)) {
381 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
385 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
388 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
394 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
395 int cmd, uint64_t value)
397 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
400 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
401 const char *name, const char *value)
403 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
404 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
407 if (strcmp(name, "digest") == 0)
408 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
410 return ctx->pmeth->ctrl_str(ctx, name, value);
413 /* Utility functions to send a string of hex string to a ctrl */
415 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
422 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
425 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
431 bin = OPENSSL_hexstr2buf(hex, &binlen);
434 if (binlen <= INT_MAX)
435 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
440 /* Pass a message digest to a ctrl */
441 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
445 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
446 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
449 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
452 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
454 return ctx->operation;
457 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
459 ctx->keygen_info = dat;
460 ctx->keygen_info_count = datlen;
463 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
468 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
473 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
478 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
483 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
485 ctx->app_data = data;
488 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
490 return ctx->app_data;
493 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
494 int (*init) (EVP_PKEY_CTX *ctx))
499 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
500 int (*copy) (EVP_PKEY_CTX *dst,
506 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
507 void (*cleanup) (EVP_PKEY_CTX *ctx))
509 pmeth->cleanup = cleanup;
512 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
513 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
514 int (*paramgen) (EVP_PKEY_CTX *ctx,
517 pmeth->paramgen_init = paramgen_init;
518 pmeth->paramgen = paramgen;
521 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
522 int (*keygen_init) (EVP_PKEY_CTX *ctx),
523 int (*keygen) (EVP_PKEY_CTX *ctx,
526 pmeth->keygen_init = keygen_init;
527 pmeth->keygen = keygen;
530 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
531 int (*sign_init) (EVP_PKEY_CTX *ctx),
532 int (*sign) (EVP_PKEY_CTX *ctx,
533 unsigned char *sig, size_t *siglen,
534 const unsigned char *tbs,
537 pmeth->sign_init = sign_init;
541 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
542 int (*verify_init) (EVP_PKEY_CTX *ctx),
543 int (*verify) (EVP_PKEY_CTX *ctx,
544 const unsigned char *sig,
546 const unsigned char *tbs,
549 pmeth->verify_init = verify_init;
550 pmeth->verify = verify;
553 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
554 int (*verify_recover_init) (EVP_PKEY_CTX
556 int (*verify_recover) (EVP_PKEY_CTX
565 pmeth->verify_recover_init = verify_recover_init;
566 pmeth->verify_recover = verify_recover;
569 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
570 int (*signctx_init) (EVP_PKEY_CTX *ctx,
572 int (*signctx) (EVP_PKEY_CTX *ctx,
577 pmeth->signctx_init = signctx_init;
578 pmeth->signctx = signctx;
581 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
582 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
584 int (*verifyctx) (EVP_PKEY_CTX *ctx,
585 const unsigned char *sig,
589 pmeth->verifyctx_init = verifyctx_init;
590 pmeth->verifyctx = verifyctx;
593 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
594 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
595 int (*encryptfn) (EVP_PKEY_CTX *ctx,
598 const unsigned char *in,
601 pmeth->encrypt_init = encrypt_init;
602 pmeth->encrypt = encryptfn;
605 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
606 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
607 int (*decrypt) (EVP_PKEY_CTX *ctx,
610 const unsigned char *in,
613 pmeth->decrypt_init = decrypt_init;
614 pmeth->decrypt = decrypt;
617 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
618 int (*derive_init) (EVP_PKEY_CTX *ctx),
619 int (*derive) (EVP_PKEY_CTX *ctx,
623 pmeth->derive_init = derive_init;
624 pmeth->derive = derive;
627 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
628 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
630 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
635 pmeth->ctrl_str = ctrl_str;
638 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
639 int (*check) (EVP_PKEY *pkey))
641 pmeth->check = check;
644 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
645 int (*check) (EVP_PKEY *pkey))
647 pmeth->public_check = check;
650 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
651 int (*check) (EVP_PKEY *pkey))
653 pmeth->param_check = check;
656 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
657 int (**pinit) (EVP_PKEY_CTX *ctx))
659 *pinit = pmeth->init;
662 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
663 int (**pcopy) (EVP_PKEY_CTX *dst,
666 *pcopy = pmeth->copy;
669 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
670 void (**pcleanup) (EVP_PKEY_CTX *ctx))
672 *pcleanup = pmeth->cleanup;
675 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
676 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
677 int (**pparamgen) (EVP_PKEY_CTX *ctx,
681 *pparamgen_init = pmeth->paramgen_init;
683 *pparamgen = pmeth->paramgen;
686 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
687 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
688 int (**pkeygen) (EVP_PKEY_CTX *ctx,
692 *pkeygen_init = pmeth->keygen_init;
694 *pkeygen = pmeth->keygen;
697 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
698 int (**psign_init) (EVP_PKEY_CTX *ctx),
699 int (**psign) (EVP_PKEY_CTX *ctx,
700 unsigned char *sig, size_t *siglen,
701 const unsigned char *tbs,
705 *psign_init = pmeth->sign_init;
707 *psign = pmeth->sign;
710 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
711 int (**pverify_init) (EVP_PKEY_CTX *ctx),
712 int (**pverify) (EVP_PKEY_CTX *ctx,
713 const unsigned char *sig,
715 const unsigned char *tbs,
719 *pverify_init = pmeth->verify_init;
721 *pverify = pmeth->verify;
724 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
725 int (**pverify_recover_init) (EVP_PKEY_CTX
727 int (**pverify_recover) (EVP_PKEY_CTX
736 if (pverify_recover_init)
737 *pverify_recover_init = pmeth->verify_recover_init;
739 *pverify_recover = pmeth->verify_recover;
742 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
743 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
745 int (**psignctx) (EVP_PKEY_CTX *ctx,
751 *psignctx_init = pmeth->signctx_init;
753 *psignctx = pmeth->signctx;
756 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
757 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
759 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
760 const unsigned char *sig,
765 *pverifyctx_init = pmeth->verifyctx_init;
767 *pverifyctx = pmeth->verifyctx;
770 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
771 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
772 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
775 const unsigned char *in,
779 *pencrypt_init = pmeth->encrypt_init;
781 *pencryptfn = pmeth->encrypt;
784 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
785 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
786 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
789 const unsigned char *in,
793 *pdecrypt_init = pmeth->decrypt_init;
795 *pdecrypt = pmeth->decrypt;
798 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
799 int (**pderive_init) (EVP_PKEY_CTX *ctx),
800 int (**pderive) (EVP_PKEY_CTX *ctx,
805 *pderive_init = pmeth->derive_init;
807 *pderive = pmeth->derive;
810 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
811 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
813 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
818 *pctrl = pmeth->ctrl;
820 *pctrl_str = pmeth->ctrl_str;
823 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
824 int (**pcheck) (EVP_PKEY *pkey))
827 *pcheck = pmeth->check;
830 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
831 int (**pcheck) (EVP_PKEY *pkey))
834 *pcheck = pmeth->public_check;
837 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
838 int (**pcheck) (EVP_PKEY *pkey))
841 *pcheck = pmeth->param_check;