2 * Copyright 2006-2016 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
51 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
54 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
55 const EVP_PKEY_METHOD *const *b)
57 return ((*a)->pkey_id - (*b)->pkey_id);
60 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
63 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
66 const EVP_PKEY_METHOD *t = &tmp, **ret;
68 if (app_pkey_methods) {
70 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
72 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
74 ret = OBJ_bsearch_pmeth(&t, standard_methods,
75 sizeof(standard_methods) /
76 sizeof(EVP_PKEY_METHOD *));
82 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
85 const EVP_PKEY_METHOD *pmeth;
87 if (!pkey || !pkey->ameth)
89 id = pkey->ameth->pkey_id;
91 #ifndef OPENSSL_NO_ENGINE
92 if (pkey && pkey->engine)
94 /* Try to find an ENGINE which implements this method */
96 if (!ENGINE_init(e)) {
97 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
101 e = ENGINE_get_pkey_meth_engine(id);
104 * If an ENGINE handled this method look it up. Otherwise use internal
109 pmeth = ENGINE_get_pkey_meth(e, id);
112 pmeth = EVP_PKEY_meth_find(id);
115 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
119 ret = OPENSSL_zalloc(sizeof(*ret));
121 #ifndef OPENSSL_NO_ENGINE
124 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
129 ret->operation = EVP_PKEY_OP_UNDEFINED;
132 EVP_PKEY_up_ref(pkey);
135 if (pmeth->init(ret) <= 0) {
136 EVP_PKEY_CTX_free(ret);
144 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
146 EVP_PKEY_METHOD *pmeth;
148 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
153 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
157 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
158 const EVP_PKEY_METHOD *meth)
161 *ppkey_id = meth->pkey_id;
163 *pflags = meth->flags;
166 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
169 dst->init = src->init;
170 dst->copy = src->copy;
171 dst->cleanup = src->cleanup;
173 dst->paramgen_init = src->paramgen_init;
174 dst->paramgen = src->paramgen;
176 dst->keygen_init = src->keygen_init;
177 dst->keygen = src->keygen;
179 dst->sign_init = src->sign_init;
180 dst->sign = src->sign;
182 dst->verify_init = src->verify_init;
183 dst->verify = src->verify;
185 dst->verify_recover_init = src->verify_recover_init;
186 dst->verify_recover = src->verify_recover;
188 dst->signctx_init = src->signctx_init;
189 dst->signctx = src->signctx;
191 dst->verifyctx_init = src->verifyctx_init;
192 dst->verifyctx = src->verifyctx;
194 dst->encrypt_init = src->encrypt_init;
195 dst->encrypt = src->encrypt;
197 dst->decrypt_init = src->decrypt_init;
198 dst->decrypt = src->decrypt;
200 dst->derive_init = src->derive_init;
201 dst->derive = src->derive;
203 dst->ctrl = src->ctrl;
204 dst->ctrl_str = src->ctrl_str;
207 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
209 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
213 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
215 return int_ctx_new(pkey, e, -1);
218 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
220 return int_ctx_new(NULL, e, id);
223 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
226 if (!pctx->pmeth || !pctx->pmeth->copy)
228 #ifndef OPENSSL_NO_ENGINE
229 /* Make sure it's safe to copy a pkey context using an ENGINE */
230 if (pctx->engine && !ENGINE_init(pctx->engine)) {
231 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
235 rctx = OPENSSL_malloc(sizeof(*rctx));
239 rctx->pmeth = pctx->pmeth;
240 #ifndef OPENSSL_NO_ENGINE
241 rctx->engine = pctx->engine;
245 EVP_PKEY_up_ref(pctx->pkey);
247 rctx->pkey = pctx->pkey;
250 EVP_PKEY_up_ref(pctx->peerkey);
252 rctx->peerkey = pctx->peerkey;
255 rctx->app_data = NULL;
256 rctx->operation = pctx->operation;
258 if (pctx->pmeth->copy(rctx, pctx) > 0)
261 EVP_PKEY_CTX_free(rctx);
266 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
268 if (app_pkey_methods == NULL) {
269 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
270 if (app_pkey_methods == NULL)
273 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
275 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
279 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
283 if (ctx->pmeth && ctx->pmeth->cleanup)
284 ctx->pmeth->cleanup(ctx);
285 EVP_PKEY_free(ctx->pkey);
286 EVP_PKEY_free(ctx->peerkey);
287 #ifndef OPENSSL_NO_ENGINE
288 ENGINE_finish(ctx->engine);
293 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
294 int cmd, int p1, void *p2)
297 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
298 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
301 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
304 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
305 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
309 if ((optype != -1) && !(ctx->operation & optype)) {
310 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
314 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
317 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
323 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
324 const char *name, const char *value)
326 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
327 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
330 if (strcmp(name, "digest") == 0) {
332 if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) {
333 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
336 return EVP_PKEY_CTX_set_signature_md(ctx, md);
338 return ctx->pmeth->ctrl_str(ctx, name, value);
341 /* Utility functions to send a string of hex string to a ctrl */
343 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
350 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
353 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
359 bin = OPENSSL_hexstr2buf(hex, &binlen);
362 if (binlen <= INT_MAX)
363 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
368 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
370 return ctx->operation;
373 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
375 ctx->keygen_info = dat;
376 ctx->keygen_info_count = datlen;
379 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
384 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
389 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
394 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
399 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
401 ctx->app_data = data;
404 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
406 return ctx->app_data;
409 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
410 int (*init) (EVP_PKEY_CTX *ctx))
415 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
416 int (*copy) (EVP_PKEY_CTX *dst,
422 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
423 void (*cleanup) (EVP_PKEY_CTX *ctx))
425 pmeth->cleanup = cleanup;
428 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
429 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
430 int (*paramgen) (EVP_PKEY_CTX *ctx,
433 pmeth->paramgen_init = paramgen_init;
434 pmeth->paramgen = paramgen;
437 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
438 int (*keygen_init) (EVP_PKEY_CTX *ctx),
439 int (*keygen) (EVP_PKEY_CTX *ctx,
442 pmeth->keygen_init = keygen_init;
443 pmeth->keygen = keygen;
446 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
447 int (*sign_init) (EVP_PKEY_CTX *ctx),
448 int (*sign) (EVP_PKEY_CTX *ctx,
449 unsigned char *sig, size_t *siglen,
450 const unsigned char *tbs,
453 pmeth->sign_init = sign_init;
457 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
458 int (*verify_init) (EVP_PKEY_CTX *ctx),
459 int (*verify) (EVP_PKEY_CTX *ctx,
460 const unsigned char *sig,
462 const unsigned char *tbs,
465 pmeth->verify_init = verify_init;
466 pmeth->verify = verify;
469 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
470 int (*verify_recover_init) (EVP_PKEY_CTX
472 int (*verify_recover) (EVP_PKEY_CTX
481 pmeth->verify_recover_init = verify_recover_init;
482 pmeth->verify_recover = verify_recover;
485 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
486 int (*signctx_init) (EVP_PKEY_CTX *ctx,
488 int (*signctx) (EVP_PKEY_CTX *ctx,
493 pmeth->signctx_init = signctx_init;
494 pmeth->signctx = signctx;
497 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
498 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
500 int (*verifyctx) (EVP_PKEY_CTX *ctx,
501 const unsigned char *sig,
505 pmeth->verifyctx_init = verifyctx_init;
506 pmeth->verifyctx = verifyctx;
509 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
510 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
511 int (*encryptfn) (EVP_PKEY_CTX *ctx,
514 const unsigned char *in,
517 pmeth->encrypt_init = encrypt_init;
518 pmeth->encrypt = encryptfn;
521 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
522 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
523 int (*decrypt) (EVP_PKEY_CTX *ctx,
526 const unsigned char *in,
529 pmeth->decrypt_init = decrypt_init;
530 pmeth->decrypt = decrypt;
533 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
534 int (*derive_init) (EVP_PKEY_CTX *ctx),
535 int (*derive) (EVP_PKEY_CTX *ctx,
539 pmeth->derive_init = derive_init;
540 pmeth->derive = derive;
543 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
544 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
546 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
551 pmeth->ctrl_str = ctrl_str;
554 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
555 int (**pinit) (EVP_PKEY_CTX *ctx))
557 *pinit = pmeth->init;
560 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
561 int (**pcopy) (EVP_PKEY_CTX *dst,
564 *pcopy = pmeth->copy;
567 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
568 void (**pcleanup) (EVP_PKEY_CTX *ctx))
570 *pcleanup = pmeth->cleanup;
573 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
574 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
575 int (**pparamgen) (EVP_PKEY_CTX *ctx,
579 *pparamgen_init = pmeth->paramgen_init;
581 *pparamgen = pmeth->paramgen;
584 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
585 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
586 int (**pkeygen) (EVP_PKEY_CTX *ctx,
590 *pkeygen_init = pmeth->keygen_init;
592 *pkeygen = pmeth->keygen;
595 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
596 int (**psign_init) (EVP_PKEY_CTX *ctx),
597 int (**psign) (EVP_PKEY_CTX *ctx,
598 unsigned char *sig, size_t *siglen,
599 const unsigned char *tbs,
603 *psign_init = pmeth->sign_init;
605 *psign = pmeth->sign;
608 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
609 int (**pverify_init) (EVP_PKEY_CTX *ctx),
610 int (**pverify) (EVP_PKEY_CTX *ctx,
611 const unsigned char *sig,
613 const unsigned char *tbs,
617 *pverify_init = pmeth->verify_init;
619 *pverify = pmeth->verify;
622 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
623 int (**pverify_recover_init) (EVP_PKEY_CTX
625 int (**pverify_recover) (EVP_PKEY_CTX
634 if (pverify_recover_init)
635 *pverify_recover_init = pmeth->verify_recover_init;
637 *pverify_recover = pmeth->verify_recover;
640 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
641 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
643 int (**psignctx) (EVP_PKEY_CTX *ctx,
649 *psignctx_init = pmeth->signctx_init;
651 *psignctx = pmeth->signctx;
654 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
655 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
657 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
658 const unsigned char *sig,
663 *pverifyctx_init = pmeth->verifyctx_init;
665 *pverifyctx = pmeth->verifyctx;
668 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
669 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
670 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
673 const unsigned char *in,
677 *pencrypt_init = pmeth->encrypt_init;
679 *pencryptfn = pmeth->encrypt;
682 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
683 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
684 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
687 const unsigned char *in,
691 *pdecrypt_init = pmeth->decrypt_init;
693 *pdecrypt = pmeth->decrypt;
696 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
697 int (**pderive_init) (EVP_PKEY_CTX *ctx),
698 int (**pderive) (EVP_PKEY_CTX *ctx,
703 *pderive_init = pmeth->derive_init;
705 *pderive = pmeth->derive;
708 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
709 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
711 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
716 *pctrl = pmeth->ctrl;
718 *pctrl_str = pmeth->ctrl_str;