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
69 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
72 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
73 const EVP_PKEY_METHOD *const *b)
75 return ((*a)->pkey_id - (*b)->pkey_id);
78 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
81 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
84 const EVP_PKEY_METHOD *t = &tmp, **ret;
86 if (app_pkey_methods) {
88 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
90 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
92 ret = OBJ_bsearch_pmeth(&t, standard_methods,
93 sizeof(standard_methods) /
94 sizeof(EVP_PKEY_METHOD *));
100 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
103 const EVP_PKEY_METHOD *pmeth;
105 if (!pkey || !pkey->ameth)
107 id = pkey->ameth->pkey_id;
109 #ifndef OPENSSL_NO_ENGINE
110 if (e == NULL && pkey != NULL)
111 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
112 /* Try to find an ENGINE which implements this method */
114 if (!ENGINE_init(e)) {
115 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
119 e = ENGINE_get_pkey_meth_engine(id);
123 * If an ENGINE handled this method look it up. Otherwise use internal
127 pmeth = ENGINE_get_pkey_meth(e, id);
130 pmeth = EVP_PKEY_meth_find(id);
133 #ifndef OPENSSL_NO_ENGINE
136 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
140 ret = OPENSSL_zalloc(sizeof(*ret));
142 #ifndef OPENSSL_NO_ENGINE
145 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
150 ret->operation = EVP_PKEY_OP_UNDEFINED;
153 EVP_PKEY_up_ref(pkey);
156 if (pmeth->init(ret) <= 0) {
158 EVP_PKEY_CTX_free(ret);
166 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
168 EVP_PKEY_METHOD *pmeth;
170 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
172 EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
177 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
181 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
182 const EVP_PKEY_METHOD *meth)
185 *ppkey_id = meth->pkey_id;
187 *pflags = meth->flags;
190 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
193 dst->init = src->init;
194 dst->copy = src->copy;
195 dst->cleanup = src->cleanup;
197 dst->paramgen_init = src->paramgen_init;
198 dst->paramgen = src->paramgen;
200 dst->keygen_init = src->keygen_init;
201 dst->keygen = src->keygen;
203 dst->sign_init = src->sign_init;
204 dst->sign = src->sign;
206 dst->verify_init = src->verify_init;
207 dst->verify = src->verify;
209 dst->verify_recover_init = src->verify_recover_init;
210 dst->verify_recover = src->verify_recover;
212 dst->signctx_init = src->signctx_init;
213 dst->signctx = src->signctx;
215 dst->verifyctx_init = src->verifyctx_init;
216 dst->verifyctx = src->verifyctx;
218 dst->encrypt_init = src->encrypt_init;
219 dst->encrypt = src->encrypt;
221 dst->decrypt_init = src->decrypt_init;
222 dst->decrypt = src->decrypt;
224 dst->derive_init = src->derive_init;
225 dst->derive = src->derive;
227 dst->ctrl = src->ctrl;
228 dst->ctrl_str = src->ctrl_str;
230 dst->check = src->check;
233 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
235 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
239 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
241 return int_ctx_new(pkey, e, -1);
244 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
246 return int_ctx_new(NULL, e, id);
249 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
252 if (!pctx->pmeth || !pctx->pmeth->copy)
254 #ifndef OPENSSL_NO_ENGINE
255 /* Make sure it's safe to copy a pkey context using an ENGINE */
256 if (pctx->engine && !ENGINE_init(pctx->engine)) {
257 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
261 rctx = OPENSSL_malloc(sizeof(*rctx));
263 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
267 rctx->pmeth = pctx->pmeth;
268 #ifndef OPENSSL_NO_ENGINE
269 rctx->engine = pctx->engine;
273 EVP_PKEY_up_ref(pctx->pkey);
275 rctx->pkey = pctx->pkey;
278 EVP_PKEY_up_ref(pctx->peerkey);
280 rctx->peerkey = pctx->peerkey;
283 rctx->app_data = NULL;
284 rctx->operation = pctx->operation;
286 if (pctx->pmeth->copy(rctx, pctx) > 0)
290 EVP_PKEY_CTX_free(rctx);
295 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
297 if (app_pkey_methods == NULL) {
298 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
299 if (app_pkey_methods == NULL){
300 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
304 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
305 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
308 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
312 void evp_app_cleanup_int(void)
314 if (app_pkey_methods != NULL)
315 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
318 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
320 const EVP_PKEY_METHOD *ret;
322 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
324 return ret == NULL ? 0 : 1;
327 size_t EVP_PKEY_meth_get_count(void)
329 size_t rv = OSSL_NELEM(standard_methods);
331 if (app_pkey_methods)
332 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
336 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
338 if (idx < OSSL_NELEM(standard_methods))
339 return standard_methods[idx];
340 if (app_pkey_methods == NULL)
342 idx -= OSSL_NELEM(standard_methods);
343 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
345 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
348 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
352 if (ctx->pmeth && ctx->pmeth->cleanup)
353 ctx->pmeth->cleanup(ctx);
354 EVP_PKEY_free(ctx->pkey);
355 EVP_PKEY_free(ctx->peerkey);
356 #ifndef OPENSSL_NO_ENGINE
357 ENGINE_finish(ctx->engine);
362 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
363 int cmd, int p1, void *p2)
366 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
367 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
370 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
373 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
374 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
378 if ((optype != -1) && !(ctx->operation & optype)) {
379 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
383 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
386 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
392 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
393 int cmd, uint64_t value)
395 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
398 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
399 const char *name, const char *value)
401 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
402 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
405 if (strcmp(name, "digest") == 0)
406 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
408 return ctx->pmeth->ctrl_str(ctx, name, value);
411 /* Utility functions to send a string of hex string to a ctrl */
413 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
420 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
423 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
429 bin = OPENSSL_hexstr2buf(hex, &binlen);
432 if (binlen <= INT_MAX)
433 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
438 /* Pass a message digest to a ctrl */
439 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
443 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
444 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
447 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
450 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
452 return ctx->operation;
455 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
457 ctx->keygen_info = dat;
458 ctx->keygen_info_count = datlen;
461 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
466 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
471 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
476 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
481 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
483 ctx->app_data = data;
486 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
488 return ctx->app_data;
491 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
492 int (*init) (EVP_PKEY_CTX *ctx))
497 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
498 int (*copy) (EVP_PKEY_CTX *dst,
504 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
505 void (*cleanup) (EVP_PKEY_CTX *ctx))
507 pmeth->cleanup = cleanup;
510 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
511 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
512 int (*paramgen) (EVP_PKEY_CTX *ctx,
515 pmeth->paramgen_init = paramgen_init;
516 pmeth->paramgen = paramgen;
519 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
520 int (*keygen_init) (EVP_PKEY_CTX *ctx),
521 int (*keygen) (EVP_PKEY_CTX *ctx,
524 pmeth->keygen_init = keygen_init;
525 pmeth->keygen = keygen;
528 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
529 int (*sign_init) (EVP_PKEY_CTX *ctx),
530 int (*sign) (EVP_PKEY_CTX *ctx,
531 unsigned char *sig, size_t *siglen,
532 const unsigned char *tbs,
535 pmeth->sign_init = sign_init;
539 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
540 int (*verify_init) (EVP_PKEY_CTX *ctx),
541 int (*verify) (EVP_PKEY_CTX *ctx,
542 const unsigned char *sig,
544 const unsigned char *tbs,
547 pmeth->verify_init = verify_init;
548 pmeth->verify = verify;
551 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
552 int (*verify_recover_init) (EVP_PKEY_CTX
554 int (*verify_recover) (EVP_PKEY_CTX
563 pmeth->verify_recover_init = verify_recover_init;
564 pmeth->verify_recover = verify_recover;
567 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
568 int (*signctx_init) (EVP_PKEY_CTX *ctx,
570 int (*signctx) (EVP_PKEY_CTX *ctx,
575 pmeth->signctx_init = signctx_init;
576 pmeth->signctx = signctx;
579 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
580 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
582 int (*verifyctx) (EVP_PKEY_CTX *ctx,
583 const unsigned char *sig,
587 pmeth->verifyctx_init = verifyctx_init;
588 pmeth->verifyctx = verifyctx;
591 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
592 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
593 int (*encryptfn) (EVP_PKEY_CTX *ctx,
596 const unsigned char *in,
599 pmeth->encrypt_init = encrypt_init;
600 pmeth->encrypt = encryptfn;
603 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
604 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
605 int (*decrypt) (EVP_PKEY_CTX *ctx,
608 const unsigned char *in,
611 pmeth->decrypt_init = decrypt_init;
612 pmeth->decrypt = decrypt;
615 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
616 int (*derive_init) (EVP_PKEY_CTX *ctx),
617 int (*derive) (EVP_PKEY_CTX *ctx,
621 pmeth->derive_init = derive_init;
622 pmeth->derive = derive;
625 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
626 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
628 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
633 pmeth->ctrl_str = ctrl_str;
636 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
637 int (*check) (EVP_PKEY *pkey))
639 pmeth->check = check;
642 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
643 int (*check) (EVP_PKEY *pkey))
645 pmeth->public_check = check;
648 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
649 int (*check) (EVP_PKEY *pkey))
651 pmeth->param_check = check;
654 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
655 int (**pinit) (EVP_PKEY_CTX *ctx))
657 *pinit = pmeth->init;
660 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
661 int (**pcopy) (EVP_PKEY_CTX *dst,
664 *pcopy = pmeth->copy;
667 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
668 void (**pcleanup) (EVP_PKEY_CTX *ctx))
670 *pcleanup = pmeth->cleanup;
673 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
674 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
675 int (**pparamgen) (EVP_PKEY_CTX *ctx,
679 *pparamgen_init = pmeth->paramgen_init;
681 *pparamgen = pmeth->paramgen;
684 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
685 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
686 int (**pkeygen) (EVP_PKEY_CTX *ctx,
690 *pkeygen_init = pmeth->keygen_init;
692 *pkeygen = pmeth->keygen;
695 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
696 int (**psign_init) (EVP_PKEY_CTX *ctx),
697 int (**psign) (EVP_PKEY_CTX *ctx,
698 unsigned char *sig, size_t *siglen,
699 const unsigned char *tbs,
703 *psign_init = pmeth->sign_init;
705 *psign = pmeth->sign;
708 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
709 int (**pverify_init) (EVP_PKEY_CTX *ctx),
710 int (**pverify) (EVP_PKEY_CTX *ctx,
711 const unsigned char *sig,
713 const unsigned char *tbs,
717 *pverify_init = pmeth->verify_init;
719 *pverify = pmeth->verify;
722 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
723 int (**pverify_recover_init) (EVP_PKEY_CTX
725 int (**pverify_recover) (EVP_PKEY_CTX
734 if (pverify_recover_init)
735 *pverify_recover_init = pmeth->verify_recover_init;
737 *pverify_recover = pmeth->verify_recover;
740 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
741 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
743 int (**psignctx) (EVP_PKEY_CTX *ctx,
749 *psignctx_init = pmeth->signctx_init;
751 *psignctx = pmeth->signctx;
754 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
755 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
757 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
758 const unsigned char *sig,
763 *pverifyctx_init = pmeth->verifyctx_init;
765 *pverifyctx = pmeth->verifyctx;
768 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
769 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
770 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
773 const unsigned char *in,
777 *pencrypt_init = pmeth->encrypt_init;
779 *pencryptfn = pmeth->encrypt;
782 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
783 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
784 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
787 const unsigned char *in,
791 *pdecrypt_init = pmeth->decrypt_init;
793 *pdecrypt = pmeth->decrypt;
796 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
797 int (**pderive_init) (EVP_PKEY_CTX *ctx),
798 int (**pderive) (EVP_PKEY_CTX *ctx,
803 *pderive_init = pmeth->derive_init;
805 *pderive = pmeth->derive;
808 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
809 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
811 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
816 *pctrl = pmeth->ctrl;
818 *pctrl_str = pmeth->ctrl_str;
821 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
822 int (**pcheck) (EVP_PKEY *pkey))
825 *pcheck = pmeth->check;
828 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
829 int (**pcheck) (EVP_PKEY *pkey))
832 *pcheck = pmeth->public_check;
835 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
836 int (**pcheck) (EVP_PKEY *pkey))
839 *pcheck = pmeth->param_check;