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)
109 e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : 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);
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 #ifndef OPENSSL_NO_ENGINE
135 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
139 ret = OPENSSL_zalloc(sizeof(*ret));
141 #ifndef OPENSSL_NO_ENGINE
144 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
149 ret->operation = EVP_PKEY_OP_UNDEFINED;
152 EVP_PKEY_up_ref(pkey);
155 if (pmeth->init(ret) <= 0) {
157 EVP_PKEY_CTX_free(ret);
165 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
167 EVP_PKEY_METHOD *pmeth;
169 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
174 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
178 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
179 const EVP_PKEY_METHOD *meth)
182 *ppkey_id = meth->pkey_id;
184 *pflags = meth->flags;
187 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
190 dst->init = src->init;
191 dst->copy = src->copy;
192 dst->cleanup = src->cleanup;
194 dst->paramgen_init = src->paramgen_init;
195 dst->paramgen = src->paramgen;
197 dst->keygen_init = src->keygen_init;
198 dst->keygen = src->keygen;
200 dst->sign_init = src->sign_init;
201 dst->sign = src->sign;
203 dst->verify_init = src->verify_init;
204 dst->verify = src->verify;
206 dst->verify_recover_init = src->verify_recover_init;
207 dst->verify_recover = src->verify_recover;
209 dst->signctx_init = src->signctx_init;
210 dst->signctx = src->signctx;
212 dst->verifyctx_init = src->verifyctx_init;
213 dst->verifyctx = src->verifyctx;
215 dst->encrypt_init = src->encrypt_init;
216 dst->encrypt = src->encrypt;
218 dst->decrypt_init = src->decrypt_init;
219 dst->decrypt = src->decrypt;
221 dst->derive_init = src->derive_init;
222 dst->derive = src->derive;
224 dst->ctrl = src->ctrl;
225 dst->ctrl_str = src->ctrl_str;
227 dst->check = src->check;
230 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
232 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
236 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
238 return int_ctx_new(pkey, e, -1);
241 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
243 return int_ctx_new(NULL, e, id);
246 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
249 if (!pctx->pmeth || !pctx->pmeth->copy)
251 #ifndef OPENSSL_NO_ENGINE
252 /* Make sure it's safe to copy a pkey context using an ENGINE */
253 if (pctx->engine && !ENGINE_init(pctx->engine)) {
254 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
258 rctx = OPENSSL_malloc(sizeof(*rctx));
262 rctx->pmeth = pctx->pmeth;
263 #ifndef OPENSSL_NO_ENGINE
264 rctx->engine = pctx->engine;
268 EVP_PKEY_up_ref(pctx->pkey);
270 rctx->pkey = pctx->pkey;
273 EVP_PKEY_up_ref(pctx->peerkey);
275 rctx->peerkey = pctx->peerkey;
278 rctx->app_data = NULL;
279 rctx->operation = pctx->operation;
281 if (pctx->pmeth->copy(rctx, pctx) > 0)
285 EVP_PKEY_CTX_free(rctx);
290 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
292 if (app_pkey_methods == NULL) {
293 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
294 if (app_pkey_methods == NULL)
297 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
299 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
303 void evp_app_cleanup_int(void)
305 if (app_pkey_methods != NULL)
306 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
309 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
311 const EVP_PKEY_METHOD *ret;
313 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
315 return ret == NULL ? 0 : 1;
318 size_t EVP_PKEY_meth_get_count(void)
320 size_t rv = OSSL_NELEM(standard_methods);
322 if (app_pkey_methods)
323 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
327 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
329 if (idx < OSSL_NELEM(standard_methods))
330 return standard_methods[idx];
331 if (app_pkey_methods == NULL)
333 idx -= OSSL_NELEM(standard_methods);
334 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
336 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
339 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
343 if (ctx->pmeth && ctx->pmeth->cleanup)
344 ctx->pmeth->cleanup(ctx);
345 EVP_PKEY_free(ctx->pkey);
346 EVP_PKEY_free(ctx->peerkey);
347 #ifndef OPENSSL_NO_ENGINE
348 ENGINE_finish(ctx->engine);
353 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
354 int cmd, int p1, void *p2)
357 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
358 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
361 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
364 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
365 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
369 if ((optype != -1) && !(ctx->operation & optype)) {
370 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
374 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
377 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
383 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
384 int cmd, uint64_t value)
386 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
389 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
390 const char *name, const char *value)
392 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
393 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
396 if (strcmp(name, "digest") == 0)
397 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
399 return ctx->pmeth->ctrl_str(ctx, name, value);
402 /* Utility functions to send a string of hex string to a ctrl */
404 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
411 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
414 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
420 bin = OPENSSL_hexstr2buf(hex, &binlen);
423 if (binlen <= INT_MAX)
424 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
429 /* Pass a message digest to a ctrl */
430 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
434 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
435 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
438 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
441 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
443 return ctx->operation;
446 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
448 ctx->keygen_info = dat;
449 ctx->keygen_info_count = datlen;
452 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
457 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
462 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
467 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
472 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
474 ctx->app_data = data;
477 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
479 return ctx->app_data;
482 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
483 int (*init) (EVP_PKEY_CTX *ctx))
488 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
489 int (*copy) (EVP_PKEY_CTX *dst,
495 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
496 void (*cleanup) (EVP_PKEY_CTX *ctx))
498 pmeth->cleanup = cleanup;
501 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
502 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
503 int (*paramgen) (EVP_PKEY_CTX *ctx,
506 pmeth->paramgen_init = paramgen_init;
507 pmeth->paramgen = paramgen;
510 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
511 int (*keygen_init) (EVP_PKEY_CTX *ctx),
512 int (*keygen) (EVP_PKEY_CTX *ctx,
515 pmeth->keygen_init = keygen_init;
516 pmeth->keygen = keygen;
519 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
520 int (*sign_init) (EVP_PKEY_CTX *ctx),
521 int (*sign) (EVP_PKEY_CTX *ctx,
522 unsigned char *sig, size_t *siglen,
523 const unsigned char *tbs,
526 pmeth->sign_init = sign_init;
530 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
531 int (*verify_init) (EVP_PKEY_CTX *ctx),
532 int (*verify) (EVP_PKEY_CTX *ctx,
533 const unsigned char *sig,
535 const unsigned char *tbs,
538 pmeth->verify_init = verify_init;
539 pmeth->verify = verify;
542 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
543 int (*verify_recover_init) (EVP_PKEY_CTX
545 int (*verify_recover) (EVP_PKEY_CTX
554 pmeth->verify_recover_init = verify_recover_init;
555 pmeth->verify_recover = verify_recover;
558 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
559 int (*signctx_init) (EVP_PKEY_CTX *ctx,
561 int (*signctx) (EVP_PKEY_CTX *ctx,
566 pmeth->signctx_init = signctx_init;
567 pmeth->signctx = signctx;
570 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
571 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
573 int (*verifyctx) (EVP_PKEY_CTX *ctx,
574 const unsigned char *sig,
578 pmeth->verifyctx_init = verifyctx_init;
579 pmeth->verifyctx = verifyctx;
582 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
583 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
584 int (*encryptfn) (EVP_PKEY_CTX *ctx,
587 const unsigned char *in,
590 pmeth->encrypt_init = encrypt_init;
591 pmeth->encrypt = encryptfn;
594 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
595 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
596 int (*decrypt) (EVP_PKEY_CTX *ctx,
599 const unsigned char *in,
602 pmeth->decrypt_init = decrypt_init;
603 pmeth->decrypt = decrypt;
606 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
607 int (*derive_init) (EVP_PKEY_CTX *ctx),
608 int (*derive) (EVP_PKEY_CTX *ctx,
612 pmeth->derive_init = derive_init;
613 pmeth->derive = derive;
616 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
617 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
619 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
624 pmeth->ctrl_str = ctrl_str;
627 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
628 int (*check) (EVP_PKEY *pkey))
630 pmeth->check = check;
633 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
634 int (**pinit) (EVP_PKEY_CTX *ctx))
636 *pinit = pmeth->init;
639 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
640 int (**pcopy) (EVP_PKEY_CTX *dst,
643 *pcopy = pmeth->copy;
646 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
647 void (**pcleanup) (EVP_PKEY_CTX *ctx))
649 *pcleanup = pmeth->cleanup;
652 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
653 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
654 int (**pparamgen) (EVP_PKEY_CTX *ctx,
658 *pparamgen_init = pmeth->paramgen_init;
660 *pparamgen = pmeth->paramgen;
663 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
664 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
665 int (**pkeygen) (EVP_PKEY_CTX *ctx,
669 *pkeygen_init = pmeth->keygen_init;
671 *pkeygen = pmeth->keygen;
674 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
675 int (**psign_init) (EVP_PKEY_CTX *ctx),
676 int (**psign) (EVP_PKEY_CTX *ctx,
677 unsigned char *sig, size_t *siglen,
678 const unsigned char *tbs,
682 *psign_init = pmeth->sign_init;
684 *psign = pmeth->sign;
687 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
688 int (**pverify_init) (EVP_PKEY_CTX *ctx),
689 int (**pverify) (EVP_PKEY_CTX *ctx,
690 const unsigned char *sig,
692 const unsigned char *tbs,
696 *pverify_init = pmeth->verify_init;
698 *pverify = pmeth->verify;
701 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
702 int (**pverify_recover_init) (EVP_PKEY_CTX
704 int (**pverify_recover) (EVP_PKEY_CTX
713 if (pverify_recover_init)
714 *pverify_recover_init = pmeth->verify_recover_init;
716 *pverify_recover = pmeth->verify_recover;
719 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
720 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
722 int (**psignctx) (EVP_PKEY_CTX *ctx,
728 *psignctx_init = pmeth->signctx_init;
730 *psignctx = pmeth->signctx;
733 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
734 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
736 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
737 const unsigned char *sig,
742 *pverifyctx_init = pmeth->verifyctx_init;
744 *pverifyctx = pmeth->verifyctx;
747 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
748 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
749 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
752 const unsigned char *in,
756 *pencrypt_init = pmeth->encrypt_init;
758 *pencryptfn = pmeth->encrypt;
761 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
762 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
763 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
766 const unsigned char *in,
770 *pdecrypt_init = pmeth->decrypt_init;
772 *pdecrypt = pmeth->decrypt;
775 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
776 int (**pderive_init) (EVP_PKEY_CTX *ctx),
777 int (**pderive) (EVP_PKEY_CTX *ctx,
782 *pderive_init = pmeth->derive_init;
784 *pderive = pmeth->derive;
787 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
788 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
790 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
795 *pctrl = pmeth->ctrl;
797 *pctrl_str = pmeth->ctrl_str;
800 void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
801 int (**pcheck) (EVP_PKEY *pkey))
804 *pcheck = pmeth->check;