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 static const EVP_PKEY_METHOD *standard_methods[] = {
25 #ifndef OPENSSL_NO_RSA
31 #ifndef OPENSSL_NO_DSA
38 #ifndef OPENSSL_NO_CMAC
41 #ifndef OPENSSL_NO_RSA
52 #ifndef OPENSSL_NO_POLY1305
55 #ifndef OPENSSL_NO_SIPHASH
63 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
66 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
67 const EVP_PKEY_METHOD *const *b)
69 return ((*a)->pkey_id - (*b)->pkey_id);
72 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
75 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
78 const EVP_PKEY_METHOD *t = &tmp, **ret;
80 if (app_pkey_methods) {
82 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
84 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
86 ret = OBJ_bsearch_pmeth(&t, standard_methods,
87 sizeof(standard_methods) /
88 sizeof(EVP_PKEY_METHOD *));
94 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
97 const EVP_PKEY_METHOD *pmeth;
99 if (!pkey || !pkey->ameth)
101 id = pkey->ameth->pkey_id;
103 #ifndef OPENSSL_NO_ENGINE
104 if (pkey && pkey->engine)
106 /* Try to find an ENGINE which implements this method */
108 if (!ENGINE_init(e)) {
109 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
113 e = ENGINE_get_pkey_meth_engine(id);
116 * If an ENGINE handled this method look it up. Otherwise use internal
121 pmeth = ENGINE_get_pkey_meth(e, id);
124 pmeth = EVP_PKEY_meth_find(id);
127 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
131 ret = OPENSSL_zalloc(sizeof(*ret));
133 #ifndef OPENSSL_NO_ENGINE
136 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
141 ret->operation = EVP_PKEY_OP_UNDEFINED;
144 EVP_PKEY_up_ref(pkey);
147 if (pmeth->init(ret) <= 0) {
149 EVP_PKEY_CTX_free(ret);
157 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
159 EVP_PKEY_METHOD *pmeth;
161 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
166 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
170 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
171 const EVP_PKEY_METHOD *meth)
174 *ppkey_id = meth->pkey_id;
176 *pflags = meth->flags;
179 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
182 dst->init = src->init;
183 dst->copy = src->copy;
184 dst->cleanup = src->cleanup;
186 dst->paramgen_init = src->paramgen_init;
187 dst->paramgen = src->paramgen;
189 dst->keygen_init = src->keygen_init;
190 dst->keygen = src->keygen;
192 dst->sign_init = src->sign_init;
193 dst->sign = src->sign;
195 dst->verify_init = src->verify_init;
196 dst->verify = src->verify;
198 dst->verify_recover_init = src->verify_recover_init;
199 dst->verify_recover = src->verify_recover;
201 dst->signctx_init = src->signctx_init;
202 dst->signctx = src->signctx;
204 dst->verifyctx_init = src->verifyctx_init;
205 dst->verifyctx = src->verifyctx;
207 dst->encrypt_init = src->encrypt_init;
208 dst->encrypt = src->encrypt;
210 dst->decrypt_init = src->decrypt_init;
211 dst->decrypt = src->decrypt;
213 dst->derive_init = src->derive_init;
214 dst->derive = src->derive;
216 dst->ctrl = src->ctrl;
217 dst->ctrl_str = src->ctrl_str;
220 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
222 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
226 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
228 return int_ctx_new(pkey, e, -1);
231 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
233 return int_ctx_new(NULL, e, id);
236 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
239 if (!pctx->pmeth || !pctx->pmeth->copy)
241 #ifndef OPENSSL_NO_ENGINE
242 /* Make sure it's safe to copy a pkey context using an ENGINE */
243 if (pctx->engine && !ENGINE_init(pctx->engine)) {
244 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
248 rctx = OPENSSL_malloc(sizeof(*rctx));
252 rctx->pmeth = pctx->pmeth;
253 #ifndef OPENSSL_NO_ENGINE
254 rctx->engine = pctx->engine;
258 EVP_PKEY_up_ref(pctx->pkey);
260 rctx->pkey = pctx->pkey;
263 EVP_PKEY_up_ref(pctx->peerkey);
265 rctx->peerkey = pctx->peerkey;
268 rctx->app_data = NULL;
269 rctx->operation = pctx->operation;
271 if (pctx->pmeth->copy(rctx, pctx) > 0)
275 EVP_PKEY_CTX_free(rctx);
280 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
282 if (app_pkey_methods == NULL) {
283 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
284 if (app_pkey_methods == NULL)
287 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
289 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
293 size_t EVP_PKEY_meth_get_count(void)
295 size_t rv = OSSL_NELEM(standard_methods);
297 if (app_pkey_methods)
298 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
302 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
304 if (idx < OSSL_NELEM(standard_methods))
305 return standard_methods[idx];
306 if (app_pkey_methods == NULL)
308 idx -= OSSL_NELEM(standard_methods);
309 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
311 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
314 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
318 if (ctx->pmeth && ctx->pmeth->cleanup)
319 ctx->pmeth->cleanup(ctx);
320 EVP_PKEY_free(ctx->pkey);
321 EVP_PKEY_free(ctx->peerkey);
322 #ifndef OPENSSL_NO_ENGINE
323 ENGINE_finish(ctx->engine);
328 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
329 int cmd, int p1, void *p2)
332 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
333 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
336 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
339 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
340 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
344 if ((optype != -1) && !(ctx->operation & optype)) {
345 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
349 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
352 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
358 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
359 const char *name, const char *value)
361 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
362 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
365 if (strcmp(name, "digest") == 0)
366 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
368 return ctx->pmeth->ctrl_str(ctx, name, value);
371 /* Utility functions to send a string of hex string to a ctrl */
373 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
380 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
383 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
389 bin = OPENSSL_hexstr2buf(hex, &binlen);
392 if (binlen <= INT_MAX)
393 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
398 /* Pass a message digest to a ctrl */
399 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
403 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
404 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
407 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
410 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
412 return ctx->operation;
415 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
417 ctx->keygen_info = dat;
418 ctx->keygen_info_count = datlen;
421 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
426 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
431 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
436 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
441 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
443 ctx->app_data = data;
446 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
448 return ctx->app_data;
451 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
452 int (*init) (EVP_PKEY_CTX *ctx))
457 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
458 int (*copy) (EVP_PKEY_CTX *dst,
464 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
465 void (*cleanup) (EVP_PKEY_CTX *ctx))
467 pmeth->cleanup = cleanup;
470 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
471 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
472 int (*paramgen) (EVP_PKEY_CTX *ctx,
475 pmeth->paramgen_init = paramgen_init;
476 pmeth->paramgen = paramgen;
479 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
480 int (*keygen_init) (EVP_PKEY_CTX *ctx),
481 int (*keygen) (EVP_PKEY_CTX *ctx,
484 pmeth->keygen_init = keygen_init;
485 pmeth->keygen = keygen;
488 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
489 int (*sign_init) (EVP_PKEY_CTX *ctx),
490 int (*sign) (EVP_PKEY_CTX *ctx,
491 unsigned char *sig, size_t *siglen,
492 const unsigned char *tbs,
495 pmeth->sign_init = sign_init;
499 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
500 int (*verify_init) (EVP_PKEY_CTX *ctx),
501 int (*verify) (EVP_PKEY_CTX *ctx,
502 const unsigned char *sig,
504 const unsigned char *tbs,
507 pmeth->verify_init = verify_init;
508 pmeth->verify = verify;
511 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
512 int (*verify_recover_init) (EVP_PKEY_CTX
514 int (*verify_recover) (EVP_PKEY_CTX
523 pmeth->verify_recover_init = verify_recover_init;
524 pmeth->verify_recover = verify_recover;
527 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
528 int (*signctx_init) (EVP_PKEY_CTX *ctx,
530 int (*signctx) (EVP_PKEY_CTX *ctx,
535 pmeth->signctx_init = signctx_init;
536 pmeth->signctx = signctx;
539 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
540 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
542 int (*verifyctx) (EVP_PKEY_CTX *ctx,
543 const unsigned char *sig,
547 pmeth->verifyctx_init = verifyctx_init;
548 pmeth->verifyctx = verifyctx;
551 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
552 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
553 int (*encryptfn) (EVP_PKEY_CTX *ctx,
556 const unsigned char *in,
559 pmeth->encrypt_init = encrypt_init;
560 pmeth->encrypt = encryptfn;
563 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
564 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
565 int (*decrypt) (EVP_PKEY_CTX *ctx,
568 const unsigned char *in,
571 pmeth->decrypt_init = decrypt_init;
572 pmeth->decrypt = decrypt;
575 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
576 int (*derive_init) (EVP_PKEY_CTX *ctx),
577 int (*derive) (EVP_PKEY_CTX *ctx,
581 pmeth->derive_init = derive_init;
582 pmeth->derive = derive;
585 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
586 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
588 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
593 pmeth->ctrl_str = ctrl_str;
596 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
597 int (**pinit) (EVP_PKEY_CTX *ctx))
599 *pinit = pmeth->init;
602 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
603 int (**pcopy) (EVP_PKEY_CTX *dst,
606 *pcopy = pmeth->copy;
609 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
610 void (**pcleanup) (EVP_PKEY_CTX *ctx))
612 *pcleanup = pmeth->cleanup;
615 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
616 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
617 int (**pparamgen) (EVP_PKEY_CTX *ctx,
621 *pparamgen_init = pmeth->paramgen_init;
623 *pparamgen = pmeth->paramgen;
626 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
627 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
628 int (**pkeygen) (EVP_PKEY_CTX *ctx,
632 *pkeygen_init = pmeth->keygen_init;
634 *pkeygen = pmeth->keygen;
637 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
638 int (**psign_init) (EVP_PKEY_CTX *ctx),
639 int (**psign) (EVP_PKEY_CTX *ctx,
640 unsigned char *sig, size_t *siglen,
641 const unsigned char *tbs,
645 *psign_init = pmeth->sign_init;
647 *psign = pmeth->sign;
650 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
651 int (**pverify_init) (EVP_PKEY_CTX *ctx),
652 int (**pverify) (EVP_PKEY_CTX *ctx,
653 const unsigned char *sig,
655 const unsigned char *tbs,
659 *pverify_init = pmeth->verify_init;
661 *pverify = pmeth->verify;
664 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
665 int (**pverify_recover_init) (EVP_PKEY_CTX
667 int (**pverify_recover) (EVP_PKEY_CTX
676 if (pverify_recover_init)
677 *pverify_recover_init = pmeth->verify_recover_init;
679 *pverify_recover = pmeth->verify_recover;
682 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
683 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
685 int (**psignctx) (EVP_PKEY_CTX *ctx,
691 *psignctx_init = pmeth->signctx_init;
693 *psignctx = pmeth->signctx;
696 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
697 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
699 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
700 const unsigned char *sig,
705 *pverifyctx_init = pmeth->verifyctx_init;
707 *pverifyctx = pmeth->verifyctx;
710 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
711 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
712 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
715 const unsigned char *in,
719 *pencrypt_init = pmeth->encrypt_init;
721 *pencryptfn = pmeth->encrypt;
724 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
725 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
726 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
729 const unsigned char *in,
733 *pdecrypt_init = pmeth->decrypt_init;
735 *pdecrypt = pmeth->decrypt;
738 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
739 int (**pderive_init) (EVP_PKEY_CTX *ctx),
740 int (**pderive) (EVP_PKEY_CTX *ctx,
745 *pderive_init = pmeth->derive_init;
747 *pderive = pmeth->derive;
750 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
751 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
753 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
758 *pctrl = pmeth->ctrl;
760 *pctrl_str = pmeth->ctrl_str;