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 (e == NULL && pkey != NULL)
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);
121 * If an ENGINE handled this method look it up. Otherwise use internal
126 pmeth = ENGINE_get_pkey_meth(e, id);
129 pmeth = EVP_PKEY_meth_find(id);
132 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
136 ret = OPENSSL_zalloc(sizeof(*ret));
138 #ifndef OPENSSL_NO_ENGINE
141 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
146 ret->operation = EVP_PKEY_OP_UNDEFINED;
149 EVP_PKEY_up_ref(pkey);
152 if (pmeth->init(ret) <= 0) {
154 EVP_PKEY_CTX_free(ret);
162 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
164 EVP_PKEY_METHOD *pmeth;
166 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
171 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
175 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
176 const EVP_PKEY_METHOD *meth)
179 *ppkey_id = meth->pkey_id;
181 *pflags = meth->flags;
184 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
187 dst->init = src->init;
188 dst->copy = src->copy;
189 dst->cleanup = src->cleanup;
191 dst->paramgen_init = src->paramgen_init;
192 dst->paramgen = src->paramgen;
194 dst->keygen_init = src->keygen_init;
195 dst->keygen = src->keygen;
197 dst->sign_init = src->sign_init;
198 dst->sign = src->sign;
200 dst->verify_init = src->verify_init;
201 dst->verify = src->verify;
203 dst->verify_recover_init = src->verify_recover_init;
204 dst->verify_recover = src->verify_recover;
206 dst->signctx_init = src->signctx_init;
207 dst->signctx = src->signctx;
209 dst->verifyctx_init = src->verifyctx_init;
210 dst->verifyctx = src->verifyctx;
212 dst->encrypt_init = src->encrypt_init;
213 dst->encrypt = src->encrypt;
215 dst->decrypt_init = src->decrypt_init;
216 dst->decrypt = src->decrypt;
218 dst->derive_init = src->derive_init;
219 dst->derive = src->derive;
221 dst->ctrl = src->ctrl;
222 dst->ctrl_str = src->ctrl_str;
224 dst->check = src->check;
227 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
229 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
233 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
235 return int_ctx_new(pkey, e, -1);
238 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
240 return int_ctx_new(NULL, e, id);
243 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
246 if (!pctx->pmeth || !pctx->pmeth->copy)
248 #ifndef OPENSSL_NO_ENGINE
249 /* Make sure it's safe to copy a pkey context using an ENGINE */
250 if (pctx->engine && !ENGINE_init(pctx->engine)) {
251 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
255 rctx = OPENSSL_malloc(sizeof(*rctx));
259 rctx->pmeth = pctx->pmeth;
260 #ifndef OPENSSL_NO_ENGINE
261 rctx->engine = pctx->engine;
265 EVP_PKEY_up_ref(pctx->pkey);
267 rctx->pkey = pctx->pkey;
270 EVP_PKEY_up_ref(pctx->peerkey);
272 rctx->peerkey = pctx->peerkey;
275 rctx->app_data = NULL;
276 rctx->operation = pctx->operation;
278 if (pctx->pmeth->copy(rctx, pctx) > 0)
282 EVP_PKEY_CTX_free(rctx);
287 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
289 if (app_pkey_methods == NULL) {
290 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
291 if (app_pkey_methods == NULL)
294 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
296 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
300 void evp_app_cleanup_int(void)
302 if (app_pkey_methods != NULL)
303 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
306 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
308 const EVP_PKEY_METHOD *ret;
310 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
312 return ret == NULL ? 0 : 1;
315 size_t EVP_PKEY_meth_get_count(void)
317 size_t rv = OSSL_NELEM(standard_methods);
319 if (app_pkey_methods)
320 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
324 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
326 if (idx < OSSL_NELEM(standard_methods))
327 return standard_methods[idx];
328 if (app_pkey_methods == NULL)
330 idx -= OSSL_NELEM(standard_methods);
331 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
333 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
336 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
340 if (ctx->pmeth && ctx->pmeth->cleanup)
341 ctx->pmeth->cleanup(ctx);
342 EVP_PKEY_free(ctx->pkey);
343 EVP_PKEY_free(ctx->peerkey);
344 #ifndef OPENSSL_NO_ENGINE
345 ENGINE_finish(ctx->engine);
350 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
351 int cmd, int p1, void *p2)
354 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
355 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
358 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
361 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
362 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
366 if ((optype != -1) && !(ctx->operation & optype)) {
367 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
371 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
374 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
380 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
381 int cmd, uint64_t value)
383 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
386 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
387 const char *name, const char *value)
389 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
390 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
393 if (strcmp(name, "digest") == 0)
394 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
396 return ctx->pmeth->ctrl_str(ctx, name, value);
399 /* Utility functions to send a string of hex string to a ctrl */
401 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
408 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
411 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
417 bin = OPENSSL_hexstr2buf(hex, &binlen);
420 if (binlen <= INT_MAX)
421 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
426 /* Pass a message digest to a ctrl */
427 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
431 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
432 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
435 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
438 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
440 return ctx->operation;
443 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
445 ctx->keygen_info = dat;
446 ctx->keygen_info_count = datlen;
449 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
454 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
459 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
464 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
469 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
471 ctx->app_data = data;
474 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
476 return ctx->app_data;
479 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
480 int (*init) (EVP_PKEY_CTX *ctx))
485 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
486 int (*copy) (EVP_PKEY_CTX *dst,
492 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
493 void (*cleanup) (EVP_PKEY_CTX *ctx))
495 pmeth->cleanup = cleanup;
498 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
499 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
500 int (*paramgen) (EVP_PKEY_CTX *ctx,
503 pmeth->paramgen_init = paramgen_init;
504 pmeth->paramgen = paramgen;
507 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
508 int (*keygen_init) (EVP_PKEY_CTX *ctx),
509 int (*keygen) (EVP_PKEY_CTX *ctx,
512 pmeth->keygen_init = keygen_init;
513 pmeth->keygen = keygen;
516 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
517 int (*sign_init) (EVP_PKEY_CTX *ctx),
518 int (*sign) (EVP_PKEY_CTX *ctx,
519 unsigned char *sig, size_t *siglen,
520 const unsigned char *tbs,
523 pmeth->sign_init = sign_init;
527 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
528 int (*verify_init) (EVP_PKEY_CTX *ctx),
529 int (*verify) (EVP_PKEY_CTX *ctx,
530 const unsigned char *sig,
532 const unsigned char *tbs,
535 pmeth->verify_init = verify_init;
536 pmeth->verify = verify;
539 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
540 int (*verify_recover_init) (EVP_PKEY_CTX
542 int (*verify_recover) (EVP_PKEY_CTX
551 pmeth->verify_recover_init = verify_recover_init;
552 pmeth->verify_recover = verify_recover;
555 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
556 int (*signctx_init) (EVP_PKEY_CTX *ctx,
558 int (*signctx) (EVP_PKEY_CTX *ctx,
563 pmeth->signctx_init = signctx_init;
564 pmeth->signctx = signctx;
567 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
568 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
570 int (*verifyctx) (EVP_PKEY_CTX *ctx,
571 const unsigned char *sig,
575 pmeth->verifyctx_init = verifyctx_init;
576 pmeth->verifyctx = verifyctx;
579 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
580 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
581 int (*encryptfn) (EVP_PKEY_CTX *ctx,
584 const unsigned char *in,
587 pmeth->encrypt_init = encrypt_init;
588 pmeth->encrypt = encryptfn;
591 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
592 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
593 int (*decrypt) (EVP_PKEY_CTX *ctx,
596 const unsigned char *in,
599 pmeth->decrypt_init = decrypt_init;
600 pmeth->decrypt = decrypt;
603 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
604 int (*derive_init) (EVP_PKEY_CTX *ctx),
605 int (*derive) (EVP_PKEY_CTX *ctx,
609 pmeth->derive_init = derive_init;
610 pmeth->derive = derive;
613 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
614 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
616 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
621 pmeth->ctrl_str = ctrl_str;
624 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
625 int (*check) (EVP_PKEY *pkey))
627 pmeth->check = check;
630 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
631 int (**pinit) (EVP_PKEY_CTX *ctx))
633 *pinit = pmeth->init;
636 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
637 int (**pcopy) (EVP_PKEY_CTX *dst,
640 *pcopy = pmeth->copy;
643 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
644 void (**pcleanup) (EVP_PKEY_CTX *ctx))
646 *pcleanup = pmeth->cleanup;
649 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
650 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
651 int (**pparamgen) (EVP_PKEY_CTX *ctx,
655 *pparamgen_init = pmeth->paramgen_init;
657 *pparamgen = pmeth->paramgen;
660 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
661 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
662 int (**pkeygen) (EVP_PKEY_CTX *ctx,
666 *pkeygen_init = pmeth->keygen_init;
668 *pkeygen = pmeth->keygen;
671 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
672 int (**psign_init) (EVP_PKEY_CTX *ctx),
673 int (**psign) (EVP_PKEY_CTX *ctx,
674 unsigned char *sig, size_t *siglen,
675 const unsigned char *tbs,
679 *psign_init = pmeth->sign_init;
681 *psign = pmeth->sign;
684 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
685 int (**pverify_init) (EVP_PKEY_CTX *ctx),
686 int (**pverify) (EVP_PKEY_CTX *ctx,
687 const unsigned char *sig,
689 const unsigned char *tbs,
693 *pverify_init = pmeth->verify_init;
695 *pverify = pmeth->verify;
698 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
699 int (**pverify_recover_init) (EVP_PKEY_CTX
701 int (**pverify_recover) (EVP_PKEY_CTX
710 if (pverify_recover_init)
711 *pverify_recover_init = pmeth->verify_recover_init;
713 *pverify_recover = pmeth->verify_recover;
716 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
717 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
719 int (**psignctx) (EVP_PKEY_CTX *ctx,
725 *psignctx_init = pmeth->signctx_init;
727 *psignctx = pmeth->signctx;
730 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
731 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
733 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
734 const unsigned char *sig,
739 *pverifyctx_init = pmeth->verifyctx_init;
741 *pverifyctx = pmeth->verifyctx;
744 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
745 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
746 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
749 const unsigned char *in,
753 *pencrypt_init = pmeth->encrypt_init;
755 *pencryptfn = pmeth->encrypt;
758 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
759 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
760 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
763 const unsigned char *in,
767 *pdecrypt_init = pmeth->decrypt_init;
769 *pdecrypt = pmeth->decrypt;
772 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
773 int (**pderive_init) (EVP_PKEY_CTX *ctx),
774 int (**pderive) (EVP_PKEY_CTX *ctx,
779 *pderive_init = pmeth->derive_init;
781 *pderive = pmeth->derive;
784 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
785 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
787 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
792 *pctrl = pmeth->ctrl;
794 *pctrl_str = pmeth->ctrl_str;
797 void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
798 int (**pcheck) (EVP_PKEY *pkey))
801 *pcheck = pmeth->check;