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
128 pmeth = ENGINE_get_pkey_meth(e, id);
131 pmeth = EVP_PKEY_meth_find(id);
134 #ifndef OPENSSL_NO_ENGINE
137 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
141 ret = OPENSSL_zalloc(sizeof(*ret));
143 #ifndef OPENSSL_NO_ENGINE
146 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
151 ret->operation = EVP_PKEY_OP_UNDEFINED;
154 EVP_PKEY_up_ref(pkey);
157 if (pmeth->init(ret) <= 0) {
159 EVP_PKEY_CTX_free(ret);
167 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
169 EVP_PKEY_METHOD *pmeth;
171 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
176 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
180 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
181 const EVP_PKEY_METHOD *meth)
184 *ppkey_id = meth->pkey_id;
186 *pflags = meth->flags;
189 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
192 dst->init = src->init;
193 dst->copy = src->copy;
194 dst->cleanup = src->cleanup;
196 dst->paramgen_init = src->paramgen_init;
197 dst->paramgen = src->paramgen;
199 dst->keygen_init = src->keygen_init;
200 dst->keygen = src->keygen;
202 dst->sign_init = src->sign_init;
203 dst->sign = src->sign;
205 dst->verify_init = src->verify_init;
206 dst->verify = src->verify;
208 dst->verify_recover_init = src->verify_recover_init;
209 dst->verify_recover = src->verify_recover;
211 dst->signctx_init = src->signctx_init;
212 dst->signctx = src->signctx;
214 dst->verifyctx_init = src->verifyctx_init;
215 dst->verifyctx = src->verifyctx;
217 dst->encrypt_init = src->encrypt_init;
218 dst->encrypt = src->encrypt;
220 dst->decrypt_init = src->decrypt_init;
221 dst->decrypt = src->decrypt;
223 dst->derive_init = src->derive_init;
224 dst->derive = src->derive;
226 dst->ctrl = src->ctrl;
227 dst->ctrl_str = src->ctrl_str;
229 dst->check = src->check;
232 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
234 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
238 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
240 return int_ctx_new(pkey, e, -1);
243 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
245 return int_ctx_new(NULL, e, id);
248 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
251 if (!pctx->pmeth || !pctx->pmeth->copy)
253 #ifndef OPENSSL_NO_ENGINE
254 /* Make sure it's safe to copy a pkey context using an ENGINE */
255 if (pctx->engine && !ENGINE_init(pctx->engine)) {
256 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
260 rctx = OPENSSL_malloc(sizeof(*rctx));
264 rctx->pmeth = pctx->pmeth;
265 #ifndef OPENSSL_NO_ENGINE
266 rctx->engine = pctx->engine;
270 EVP_PKEY_up_ref(pctx->pkey);
272 rctx->pkey = pctx->pkey;
275 EVP_PKEY_up_ref(pctx->peerkey);
277 rctx->peerkey = pctx->peerkey;
280 rctx->app_data = NULL;
281 rctx->operation = pctx->operation;
283 if (pctx->pmeth->copy(rctx, pctx) > 0)
287 EVP_PKEY_CTX_free(rctx);
292 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
294 if (app_pkey_methods == NULL) {
295 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
296 if (app_pkey_methods == NULL)
299 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
301 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
305 void evp_app_cleanup_int(void)
307 if (app_pkey_methods != NULL)
308 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
311 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
313 const EVP_PKEY_METHOD *ret;
315 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
317 return ret == NULL ? 0 : 1;
320 size_t EVP_PKEY_meth_get_count(void)
322 size_t rv = OSSL_NELEM(standard_methods);
324 if (app_pkey_methods)
325 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
329 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
331 if (idx < OSSL_NELEM(standard_methods))
332 return standard_methods[idx];
333 if (app_pkey_methods == NULL)
335 idx -= OSSL_NELEM(standard_methods);
336 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
338 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
341 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
345 if (ctx->pmeth && ctx->pmeth->cleanup)
346 ctx->pmeth->cleanup(ctx);
347 EVP_PKEY_free(ctx->pkey);
348 EVP_PKEY_free(ctx->peerkey);
349 #ifndef OPENSSL_NO_ENGINE
350 ENGINE_finish(ctx->engine);
355 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
356 int cmd, int p1, void *p2)
359 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
360 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
363 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
366 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
367 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
371 if ((optype != -1) && !(ctx->operation & optype)) {
372 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
376 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
379 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
385 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
386 int cmd, uint64_t value)
388 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
391 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
392 const char *name, const char *value)
394 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
395 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
398 if (strcmp(name, "digest") == 0)
399 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
401 return ctx->pmeth->ctrl_str(ctx, name, value);
404 /* Utility functions to send a string of hex string to a ctrl */
406 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
413 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
416 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
422 bin = OPENSSL_hexstr2buf(hex, &binlen);
425 if (binlen <= INT_MAX)
426 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
431 /* Pass a message digest to a ctrl */
432 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
436 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
437 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
440 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
443 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
445 return ctx->operation;
448 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
450 ctx->keygen_info = dat;
451 ctx->keygen_info_count = datlen;
454 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
459 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
464 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
469 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
474 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
476 ctx->app_data = data;
479 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
481 return ctx->app_data;
484 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
485 int (*init) (EVP_PKEY_CTX *ctx))
490 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
491 int (*copy) (EVP_PKEY_CTX *dst,
497 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
498 void (*cleanup) (EVP_PKEY_CTX *ctx))
500 pmeth->cleanup = cleanup;
503 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
504 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
505 int (*paramgen) (EVP_PKEY_CTX *ctx,
508 pmeth->paramgen_init = paramgen_init;
509 pmeth->paramgen = paramgen;
512 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
513 int (*keygen_init) (EVP_PKEY_CTX *ctx),
514 int (*keygen) (EVP_PKEY_CTX *ctx,
517 pmeth->keygen_init = keygen_init;
518 pmeth->keygen = keygen;
521 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
522 int (*sign_init) (EVP_PKEY_CTX *ctx),
523 int (*sign) (EVP_PKEY_CTX *ctx,
524 unsigned char *sig, size_t *siglen,
525 const unsigned char *tbs,
528 pmeth->sign_init = sign_init;
532 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
533 int (*verify_init) (EVP_PKEY_CTX *ctx),
534 int (*verify) (EVP_PKEY_CTX *ctx,
535 const unsigned char *sig,
537 const unsigned char *tbs,
540 pmeth->verify_init = verify_init;
541 pmeth->verify = verify;
544 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
545 int (*verify_recover_init) (EVP_PKEY_CTX
547 int (*verify_recover) (EVP_PKEY_CTX
556 pmeth->verify_recover_init = verify_recover_init;
557 pmeth->verify_recover = verify_recover;
560 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
561 int (*signctx_init) (EVP_PKEY_CTX *ctx,
563 int (*signctx) (EVP_PKEY_CTX *ctx,
568 pmeth->signctx_init = signctx_init;
569 pmeth->signctx = signctx;
572 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
573 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
575 int (*verifyctx) (EVP_PKEY_CTX *ctx,
576 const unsigned char *sig,
580 pmeth->verifyctx_init = verifyctx_init;
581 pmeth->verifyctx = verifyctx;
584 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
585 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
586 int (*encryptfn) (EVP_PKEY_CTX *ctx,
589 const unsigned char *in,
592 pmeth->encrypt_init = encrypt_init;
593 pmeth->encrypt = encryptfn;
596 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
597 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
598 int (*decrypt) (EVP_PKEY_CTX *ctx,
601 const unsigned char *in,
604 pmeth->decrypt_init = decrypt_init;
605 pmeth->decrypt = decrypt;
608 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
609 int (*derive_init) (EVP_PKEY_CTX *ctx),
610 int (*derive) (EVP_PKEY_CTX *ctx,
614 pmeth->derive_init = derive_init;
615 pmeth->derive = derive;
618 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
619 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
621 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
626 pmeth->ctrl_str = ctrl_str;
629 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
630 int (*check) (EVP_PKEY *pkey))
632 pmeth->check = check;
635 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
636 int (*check) (EVP_PKEY *pkey))
638 pmeth->public_check = check;
641 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
642 int (*check) (EVP_PKEY *pkey))
644 pmeth->param_check = check;
647 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
648 int (**pinit) (EVP_PKEY_CTX *ctx))
650 *pinit = pmeth->init;
653 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
654 int (**pcopy) (EVP_PKEY_CTX *dst,
657 *pcopy = pmeth->copy;
660 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
661 void (**pcleanup) (EVP_PKEY_CTX *ctx))
663 *pcleanup = pmeth->cleanup;
666 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
667 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
668 int (**pparamgen) (EVP_PKEY_CTX *ctx,
672 *pparamgen_init = pmeth->paramgen_init;
674 *pparamgen = pmeth->paramgen;
677 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
678 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
679 int (**pkeygen) (EVP_PKEY_CTX *ctx,
683 *pkeygen_init = pmeth->keygen_init;
685 *pkeygen = pmeth->keygen;
688 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
689 int (**psign_init) (EVP_PKEY_CTX *ctx),
690 int (**psign) (EVP_PKEY_CTX *ctx,
691 unsigned char *sig, size_t *siglen,
692 const unsigned char *tbs,
696 *psign_init = pmeth->sign_init;
698 *psign = pmeth->sign;
701 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
702 int (**pverify_init) (EVP_PKEY_CTX *ctx),
703 int (**pverify) (EVP_PKEY_CTX *ctx,
704 const unsigned char *sig,
706 const unsigned char *tbs,
710 *pverify_init = pmeth->verify_init;
712 *pverify = pmeth->verify;
715 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
716 int (**pverify_recover_init) (EVP_PKEY_CTX
718 int (**pverify_recover) (EVP_PKEY_CTX
727 if (pverify_recover_init)
728 *pverify_recover_init = pmeth->verify_recover_init;
730 *pverify_recover = pmeth->verify_recover;
733 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
734 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
736 int (**psignctx) (EVP_PKEY_CTX *ctx,
742 *psignctx_init = pmeth->signctx_init;
744 *psignctx = pmeth->signctx;
747 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
748 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
750 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
751 const unsigned char *sig,
756 *pverifyctx_init = pmeth->verifyctx_init;
758 *pverifyctx = pmeth->verifyctx;
761 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
762 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
763 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
766 const unsigned char *in,
770 *pencrypt_init = pmeth->encrypt_init;
772 *pencryptfn = pmeth->encrypt;
775 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
776 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
777 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
780 const unsigned char *in,
784 *pdecrypt_init = pmeth->decrypt_init;
786 *pdecrypt = pmeth->decrypt;
789 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
790 int (**pderive_init) (EVP_PKEY_CTX *ctx),
791 int (**pderive) (EVP_PKEY_CTX *ctx,
796 *pderive_init = pmeth->derive_init;
798 *pderive = pmeth->derive;
801 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
802 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
804 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
809 *pctrl = pmeth->ctrl;
811 *pctrl_str = pmeth->ctrl_str;
814 void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
815 int (**pcheck) (EVP_PKEY *pkey))
818 *pcheck = pmeth->check;
821 void EVP_PKEY_meth_get_public_check(EVP_PKEY_METHOD *pmeth,
822 int (**pcheck) (EVP_PKEY *pkey))
825 *pcheck = pmeth->public_check;
828 void EVP_PKEY_meth_get_param_check(EVP_PKEY_METHOD *pmeth,
829 int (**pcheck) (EVP_PKEY *pkey))
832 *pcheck = pmeth->param_check;