2 * Copyright 2006-2017 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
56 #ifndef OPENSSL_NO_POLY1305
59 #ifndef OPENSSL_NO_SIPHASH
67 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
70 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
71 const EVP_PKEY_METHOD *const *b)
73 return ((*a)->pkey_id - (*b)->pkey_id);
76 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
79 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
82 const EVP_PKEY_METHOD *t = &tmp, **ret;
84 if (app_pkey_methods) {
86 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
88 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
90 ret = OBJ_bsearch_pmeth(&t, standard_methods,
91 sizeof(standard_methods) /
92 sizeof(EVP_PKEY_METHOD *));
98 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
101 const EVP_PKEY_METHOD *pmeth;
103 if (!pkey || !pkey->ameth)
105 id = pkey->ameth->pkey_id;
107 #ifndef OPENSSL_NO_ENGINE
108 if (pkey && pkey->engine)
110 /* Try to find an ENGINE which implements this method */
112 if (!ENGINE_init(e)) {
113 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
117 e = ENGINE_get_pkey_meth_engine(id);
120 * If an ENGINE handled this method look it up. Otherwise use internal
125 pmeth = ENGINE_get_pkey_meth(e, id);
128 pmeth = EVP_PKEY_meth_find(id);
131 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
135 ret = OPENSSL_zalloc(sizeof(*ret));
137 #ifndef OPENSSL_NO_ENGINE
140 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
145 ret->operation = EVP_PKEY_OP_UNDEFINED;
148 EVP_PKEY_up_ref(pkey);
151 if (pmeth->init(ret) <= 0) {
153 EVP_PKEY_CTX_free(ret);
161 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
163 EVP_PKEY_METHOD *pmeth;
165 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
170 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
174 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
175 const EVP_PKEY_METHOD *meth)
178 *ppkey_id = meth->pkey_id;
180 *pflags = meth->flags;
183 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
186 dst->init = src->init;
187 dst->copy = src->copy;
188 dst->cleanup = src->cleanup;
190 dst->paramgen_init = src->paramgen_init;
191 dst->paramgen = src->paramgen;
193 dst->keygen_init = src->keygen_init;
194 dst->keygen = src->keygen;
196 dst->sign_init = src->sign_init;
197 dst->sign = src->sign;
199 dst->verify_init = src->verify_init;
200 dst->verify = src->verify;
202 dst->verify_recover_init = src->verify_recover_init;
203 dst->verify_recover = src->verify_recover;
205 dst->signctx_init = src->signctx_init;
206 dst->signctx = src->signctx;
208 dst->verifyctx_init = src->verifyctx_init;
209 dst->verifyctx = src->verifyctx;
211 dst->encrypt_init = src->encrypt_init;
212 dst->encrypt = src->encrypt;
214 dst->decrypt_init = src->decrypt_init;
215 dst->decrypt = src->decrypt;
217 dst->derive_init = src->derive_init;
218 dst->derive = src->derive;
220 dst->ctrl = src->ctrl;
221 dst->ctrl_str = src->ctrl_str;
223 dst->check = src->check;
226 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
228 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
232 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
234 return int_ctx_new(pkey, e, -1);
237 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
239 return int_ctx_new(NULL, e, id);
242 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
245 if (!pctx->pmeth || !pctx->pmeth->copy)
247 #ifndef OPENSSL_NO_ENGINE
248 /* Make sure it's safe to copy a pkey context using an ENGINE */
249 if (pctx->engine && !ENGINE_init(pctx->engine)) {
250 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
254 rctx = OPENSSL_malloc(sizeof(*rctx));
258 rctx->pmeth = pctx->pmeth;
259 #ifndef OPENSSL_NO_ENGINE
260 rctx->engine = pctx->engine;
264 EVP_PKEY_up_ref(pctx->pkey);
266 rctx->pkey = pctx->pkey;
269 EVP_PKEY_up_ref(pctx->peerkey);
271 rctx->peerkey = pctx->peerkey;
274 rctx->app_data = NULL;
275 rctx->operation = pctx->operation;
277 if (pctx->pmeth->copy(rctx, pctx) > 0)
281 EVP_PKEY_CTX_free(rctx);
286 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
288 if (app_pkey_methods == NULL) {
289 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
290 if (app_pkey_methods == NULL)
293 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
295 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
299 void evp_app_cleanup_int(void)
301 if (app_pkey_methods != NULL)
302 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
305 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
307 const EVP_PKEY_METHOD *ret;
309 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
311 return ret == NULL ? 0 : 1;
314 size_t EVP_PKEY_meth_get_count(void)
316 size_t rv = OSSL_NELEM(standard_methods);
318 if (app_pkey_methods)
319 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
323 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
325 if (idx < OSSL_NELEM(standard_methods))
326 return standard_methods[idx];
327 if (app_pkey_methods == NULL)
329 idx -= OSSL_NELEM(standard_methods);
330 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
332 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
335 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
339 if (ctx->pmeth && ctx->pmeth->cleanup)
340 ctx->pmeth->cleanup(ctx);
341 EVP_PKEY_free(ctx->pkey);
342 EVP_PKEY_free(ctx->peerkey);
343 #ifndef OPENSSL_NO_ENGINE
344 ENGINE_finish(ctx->engine);
349 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
350 int cmd, int p1, void *p2)
353 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
354 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
357 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
360 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
361 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
365 if ((optype != -1) && !(ctx->operation & optype)) {
366 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
370 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
373 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
379 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
380 int cmd, uint64_t value)
382 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
385 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
386 const char *name, const char *value)
388 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
389 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
392 if (strcmp(name, "digest") == 0)
393 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
395 return ctx->pmeth->ctrl_str(ctx, name, value);
398 /* Utility functions to send a string of hex string to a ctrl */
400 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
407 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
410 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
416 bin = OPENSSL_hexstr2buf(hex, &binlen);
419 if (binlen <= INT_MAX)
420 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
425 /* Pass a message digest to a ctrl */
426 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
430 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
431 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
434 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
437 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
439 return ctx->operation;
442 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
444 ctx->keygen_info = dat;
445 ctx->keygen_info_count = datlen;
448 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
453 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
458 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
463 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
468 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
470 ctx->app_data = data;
473 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
475 return ctx->app_data;
478 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
479 int (*init) (EVP_PKEY_CTX *ctx))
484 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
485 int (*copy) (EVP_PKEY_CTX *dst,
491 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
492 void (*cleanup) (EVP_PKEY_CTX *ctx))
494 pmeth->cleanup = cleanup;
497 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
498 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
499 int (*paramgen) (EVP_PKEY_CTX *ctx,
502 pmeth->paramgen_init = paramgen_init;
503 pmeth->paramgen = paramgen;
506 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
507 int (*keygen_init) (EVP_PKEY_CTX *ctx),
508 int (*keygen) (EVP_PKEY_CTX *ctx,
511 pmeth->keygen_init = keygen_init;
512 pmeth->keygen = keygen;
515 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
516 int (*sign_init) (EVP_PKEY_CTX *ctx),
517 int (*sign) (EVP_PKEY_CTX *ctx,
518 unsigned char *sig, size_t *siglen,
519 const unsigned char *tbs,
522 pmeth->sign_init = sign_init;
526 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
527 int (*verify_init) (EVP_PKEY_CTX *ctx),
528 int (*verify) (EVP_PKEY_CTX *ctx,
529 const unsigned char *sig,
531 const unsigned char *tbs,
534 pmeth->verify_init = verify_init;
535 pmeth->verify = verify;
538 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
539 int (*verify_recover_init) (EVP_PKEY_CTX
541 int (*verify_recover) (EVP_PKEY_CTX
550 pmeth->verify_recover_init = verify_recover_init;
551 pmeth->verify_recover = verify_recover;
554 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
555 int (*signctx_init) (EVP_PKEY_CTX *ctx,
557 int (*signctx) (EVP_PKEY_CTX *ctx,
562 pmeth->signctx_init = signctx_init;
563 pmeth->signctx = signctx;
566 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
567 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
569 int (*verifyctx) (EVP_PKEY_CTX *ctx,
570 const unsigned char *sig,
574 pmeth->verifyctx_init = verifyctx_init;
575 pmeth->verifyctx = verifyctx;
578 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
579 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
580 int (*encryptfn) (EVP_PKEY_CTX *ctx,
583 const unsigned char *in,
586 pmeth->encrypt_init = encrypt_init;
587 pmeth->encrypt = encryptfn;
590 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
591 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
592 int (*decrypt) (EVP_PKEY_CTX *ctx,
595 const unsigned char *in,
598 pmeth->decrypt_init = decrypt_init;
599 pmeth->decrypt = decrypt;
602 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
603 int (*derive_init) (EVP_PKEY_CTX *ctx),
604 int (*derive) (EVP_PKEY_CTX *ctx,
608 pmeth->derive_init = derive_init;
609 pmeth->derive = derive;
612 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
613 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
615 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
620 pmeth->ctrl_str = ctrl_str;
623 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
624 int (*check) (EVP_PKEY *pkey))
626 pmeth->check = check;
629 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
630 int (**pinit) (EVP_PKEY_CTX *ctx))
632 *pinit = pmeth->init;
635 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
636 int (**pcopy) (EVP_PKEY_CTX *dst,
639 *pcopy = pmeth->copy;
642 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
643 void (**pcleanup) (EVP_PKEY_CTX *ctx))
645 *pcleanup = pmeth->cleanup;
648 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
649 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
650 int (**pparamgen) (EVP_PKEY_CTX *ctx,
654 *pparamgen_init = pmeth->paramgen_init;
656 *pparamgen = pmeth->paramgen;
659 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
660 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
661 int (**pkeygen) (EVP_PKEY_CTX *ctx,
665 *pkeygen_init = pmeth->keygen_init;
667 *pkeygen = pmeth->keygen;
670 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
671 int (**psign_init) (EVP_PKEY_CTX *ctx),
672 int (**psign) (EVP_PKEY_CTX *ctx,
673 unsigned char *sig, size_t *siglen,
674 const unsigned char *tbs,
678 *psign_init = pmeth->sign_init;
680 *psign = pmeth->sign;
683 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
684 int (**pverify_init) (EVP_PKEY_CTX *ctx),
685 int (**pverify) (EVP_PKEY_CTX *ctx,
686 const unsigned char *sig,
688 const unsigned char *tbs,
692 *pverify_init = pmeth->verify_init;
694 *pverify = pmeth->verify;
697 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
698 int (**pverify_recover_init) (EVP_PKEY_CTX
700 int (**pverify_recover) (EVP_PKEY_CTX
709 if (pverify_recover_init)
710 *pverify_recover_init = pmeth->verify_recover_init;
712 *pverify_recover = pmeth->verify_recover;
715 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
716 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
718 int (**psignctx) (EVP_PKEY_CTX *ctx,
724 *psignctx_init = pmeth->signctx_init;
726 *psignctx = pmeth->signctx;
729 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
730 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
732 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
733 const unsigned char *sig,
738 *pverifyctx_init = pmeth->verifyctx_init;
740 *pverifyctx = pmeth->verifyctx;
743 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
744 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
745 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
748 const unsigned char *in,
752 *pencrypt_init = pmeth->encrypt_init;
754 *pencryptfn = pmeth->encrypt;
757 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
758 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
759 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
762 const unsigned char *in,
766 *pdecrypt_init = pmeth->decrypt_init;
768 *pdecrypt = pmeth->decrypt;
771 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
772 int (**pderive_init) (EVP_PKEY_CTX *ctx),
773 int (**pderive) (EVP_PKEY_CTX *ctx,
778 *pderive_init = pmeth->derive_init;
780 *pderive = pmeth->derive;
783 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
784 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
786 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
791 *pctrl = pmeth->ctrl;
793 *pctrl_str = pmeth->ctrl_str;
796 void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
797 int (**pcheck) (EVP_PKEY *pkey))
800 *pcheck = pmeth->check;