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 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
297 if (ctx->pmeth && ctx->pmeth->cleanup)
298 ctx->pmeth->cleanup(ctx);
299 EVP_PKEY_free(ctx->pkey);
300 EVP_PKEY_free(ctx->peerkey);
301 #ifndef OPENSSL_NO_ENGINE
302 ENGINE_finish(ctx->engine);
307 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
308 int cmd, int p1, void *p2)
311 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
312 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
315 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
318 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
319 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
323 if ((optype != -1) && !(ctx->operation & optype)) {
324 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
328 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
331 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
337 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
338 const char *name, const char *value)
340 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
341 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
344 if (strcmp(name, "digest") == 0)
345 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
347 return ctx->pmeth->ctrl_str(ctx, name, value);
350 /* Utility functions to send a string of hex string to a ctrl */
352 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
359 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
362 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
368 bin = OPENSSL_hexstr2buf(hex, &binlen);
371 if (binlen <= INT_MAX)
372 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
377 /* Pass a message digest to a ctrl */
378 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
382 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
383 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
386 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
389 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
391 return ctx->operation;
394 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
396 ctx->keygen_info = dat;
397 ctx->keygen_info_count = datlen;
400 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
405 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
410 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
415 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
420 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
422 ctx->app_data = data;
425 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
427 return ctx->app_data;
430 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
431 int (*init) (EVP_PKEY_CTX *ctx))
436 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
437 int (*copy) (EVP_PKEY_CTX *dst,
443 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
444 void (*cleanup) (EVP_PKEY_CTX *ctx))
446 pmeth->cleanup = cleanup;
449 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
450 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
451 int (*paramgen) (EVP_PKEY_CTX *ctx,
454 pmeth->paramgen_init = paramgen_init;
455 pmeth->paramgen = paramgen;
458 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
459 int (*keygen_init) (EVP_PKEY_CTX *ctx),
460 int (*keygen) (EVP_PKEY_CTX *ctx,
463 pmeth->keygen_init = keygen_init;
464 pmeth->keygen = keygen;
467 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
468 int (*sign_init) (EVP_PKEY_CTX *ctx),
469 int (*sign) (EVP_PKEY_CTX *ctx,
470 unsigned char *sig, size_t *siglen,
471 const unsigned char *tbs,
474 pmeth->sign_init = sign_init;
478 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
479 int (*verify_init) (EVP_PKEY_CTX *ctx),
480 int (*verify) (EVP_PKEY_CTX *ctx,
481 const unsigned char *sig,
483 const unsigned char *tbs,
486 pmeth->verify_init = verify_init;
487 pmeth->verify = verify;
490 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
491 int (*verify_recover_init) (EVP_PKEY_CTX
493 int (*verify_recover) (EVP_PKEY_CTX
502 pmeth->verify_recover_init = verify_recover_init;
503 pmeth->verify_recover = verify_recover;
506 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
507 int (*signctx_init) (EVP_PKEY_CTX *ctx,
509 int (*signctx) (EVP_PKEY_CTX *ctx,
514 pmeth->signctx_init = signctx_init;
515 pmeth->signctx = signctx;
518 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
519 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
521 int (*verifyctx) (EVP_PKEY_CTX *ctx,
522 const unsigned char *sig,
526 pmeth->verifyctx_init = verifyctx_init;
527 pmeth->verifyctx = verifyctx;
530 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
531 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
532 int (*encryptfn) (EVP_PKEY_CTX *ctx,
535 const unsigned char *in,
538 pmeth->encrypt_init = encrypt_init;
539 pmeth->encrypt = encryptfn;
542 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
543 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
544 int (*decrypt) (EVP_PKEY_CTX *ctx,
547 const unsigned char *in,
550 pmeth->decrypt_init = decrypt_init;
551 pmeth->decrypt = decrypt;
554 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
555 int (*derive_init) (EVP_PKEY_CTX *ctx),
556 int (*derive) (EVP_PKEY_CTX *ctx,
560 pmeth->derive_init = derive_init;
561 pmeth->derive = derive;
564 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
565 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
567 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
572 pmeth->ctrl_str = ctrl_str;
575 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
576 int (**pinit) (EVP_PKEY_CTX *ctx))
578 *pinit = pmeth->init;
581 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
582 int (**pcopy) (EVP_PKEY_CTX *dst,
585 *pcopy = pmeth->copy;
588 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
589 void (**pcleanup) (EVP_PKEY_CTX *ctx))
591 *pcleanup = pmeth->cleanup;
594 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
595 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
596 int (**pparamgen) (EVP_PKEY_CTX *ctx,
600 *pparamgen_init = pmeth->paramgen_init;
602 *pparamgen = pmeth->paramgen;
605 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
606 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
607 int (**pkeygen) (EVP_PKEY_CTX *ctx,
611 *pkeygen_init = pmeth->keygen_init;
613 *pkeygen = pmeth->keygen;
616 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
617 int (**psign_init) (EVP_PKEY_CTX *ctx),
618 int (**psign) (EVP_PKEY_CTX *ctx,
619 unsigned char *sig, size_t *siglen,
620 const unsigned char *tbs,
624 *psign_init = pmeth->sign_init;
626 *psign = pmeth->sign;
629 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
630 int (**pverify_init) (EVP_PKEY_CTX *ctx),
631 int (**pverify) (EVP_PKEY_CTX *ctx,
632 const unsigned char *sig,
634 const unsigned char *tbs,
638 *pverify_init = pmeth->verify_init;
640 *pverify = pmeth->verify;
643 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
644 int (**pverify_recover_init) (EVP_PKEY_CTX
646 int (**pverify_recover) (EVP_PKEY_CTX
655 if (pverify_recover_init)
656 *pverify_recover_init = pmeth->verify_recover_init;
658 *pverify_recover = pmeth->verify_recover;
661 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
662 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
664 int (**psignctx) (EVP_PKEY_CTX *ctx,
670 *psignctx_init = pmeth->signctx_init;
672 *psignctx = pmeth->signctx;
675 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
676 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
678 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
679 const unsigned char *sig,
684 *pverifyctx_init = pmeth->verifyctx_init;
686 *pverifyctx = pmeth->verifyctx;
689 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
690 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
691 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
694 const unsigned char *in,
698 *pencrypt_init = pmeth->encrypt_init;
700 *pencryptfn = pmeth->encrypt;
703 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
704 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
705 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
708 const unsigned char *in,
712 *pdecrypt_init = pmeth->decrypt_init;
714 *pdecrypt = pmeth->decrypt;
717 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
718 int (**pderive_init) (EVP_PKEY_CTX *ctx),
719 int (**pderive) (EVP_PKEY_CTX *ctx,
724 *pderive_init = pmeth->derive_init;
726 *pderive = pmeth->derive;
729 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
730 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
732 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
737 *pctrl = pmeth->ctrl;
739 *pctrl_str = pmeth->ctrl_str;