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
41 #ifndef OPENSSL_NO_RSA
54 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
57 static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
58 const EVP_PKEY_METHOD *const *b)
60 return ((*a)->pkey_id - (*b)->pkey_id);
63 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
66 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
69 const EVP_PKEY_METHOD *t = &tmp, **ret;
71 if (app_pkey_methods) {
73 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
75 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
77 ret = OBJ_bsearch_pmeth(&t, standard_methods,
78 sizeof(standard_methods) /
79 sizeof(EVP_PKEY_METHOD *));
85 static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
88 const EVP_PKEY_METHOD *pmeth;
90 if (!pkey || !pkey->ameth)
92 id = pkey->ameth->pkey_id;
94 #ifndef OPENSSL_NO_ENGINE
95 if (pkey && pkey->engine)
97 /* Try to find an ENGINE which implements this method */
99 if (!ENGINE_init(e)) {
100 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
104 e = ENGINE_get_pkey_meth_engine(id);
107 * If an ENGINE handled this method look it up. Otherwise use internal
112 pmeth = ENGINE_get_pkey_meth(e, id);
115 pmeth = EVP_PKEY_meth_find(id);
118 EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
122 ret = OPENSSL_zalloc(sizeof(*ret));
124 #ifndef OPENSSL_NO_ENGINE
127 EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
132 ret->operation = EVP_PKEY_OP_UNDEFINED;
135 EVP_PKEY_up_ref(pkey);
138 if (pmeth->init(ret) <= 0) {
139 EVP_PKEY_CTX_free(ret);
147 EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
149 EVP_PKEY_METHOD *pmeth;
151 pmeth = OPENSSL_zalloc(sizeof(*pmeth));
156 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
160 void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
161 const EVP_PKEY_METHOD *meth)
164 *ppkey_id = meth->pkey_id;
166 *pflags = meth->flags;
169 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
172 dst->init = src->init;
173 dst->copy = src->copy;
174 dst->cleanup = src->cleanup;
176 dst->paramgen_init = src->paramgen_init;
177 dst->paramgen = src->paramgen;
179 dst->keygen_init = src->keygen_init;
180 dst->keygen = src->keygen;
182 dst->sign_init = src->sign_init;
183 dst->sign = src->sign;
185 dst->verify_init = src->verify_init;
186 dst->verify = src->verify;
188 dst->verify_recover_init = src->verify_recover_init;
189 dst->verify_recover = src->verify_recover;
191 dst->signctx_init = src->signctx_init;
192 dst->signctx = src->signctx;
194 dst->verifyctx_init = src->verifyctx_init;
195 dst->verifyctx = src->verifyctx;
197 dst->encrypt_init = src->encrypt_init;
198 dst->encrypt = src->encrypt;
200 dst->decrypt_init = src->decrypt_init;
201 dst->decrypt = src->decrypt;
203 dst->derive_init = src->derive_init;
204 dst->derive = src->derive;
206 dst->ctrl = src->ctrl;
207 dst->ctrl_str = src->ctrl_str;
210 void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
212 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
216 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
218 return int_ctx_new(pkey, e, -1);
221 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
223 return int_ctx_new(NULL, e, id);
226 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
229 if (!pctx->pmeth || !pctx->pmeth->copy)
231 #ifndef OPENSSL_NO_ENGINE
232 /* Make sure it's safe to copy a pkey context using an ENGINE */
233 if (pctx->engine && !ENGINE_init(pctx->engine)) {
234 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
238 rctx = OPENSSL_malloc(sizeof(*rctx));
242 rctx->pmeth = pctx->pmeth;
243 #ifndef OPENSSL_NO_ENGINE
244 rctx->engine = pctx->engine;
248 EVP_PKEY_up_ref(pctx->pkey);
250 rctx->pkey = pctx->pkey;
253 EVP_PKEY_up_ref(pctx->peerkey);
255 rctx->peerkey = pctx->peerkey;
258 rctx->app_data = NULL;
259 rctx->operation = pctx->operation;
261 if (pctx->pmeth->copy(rctx, pctx) > 0)
264 EVP_PKEY_CTX_free(rctx);
269 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
271 if (app_pkey_methods == NULL) {
272 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
273 if (app_pkey_methods == NULL)
276 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
278 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
282 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
286 if (ctx->pmeth && ctx->pmeth->cleanup)
287 ctx->pmeth->cleanup(ctx);
288 EVP_PKEY_free(ctx->pkey);
289 EVP_PKEY_free(ctx->peerkey);
290 #ifndef OPENSSL_NO_ENGINE
291 ENGINE_finish(ctx->engine);
296 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
297 int cmd, int p1, void *p2)
300 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
301 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
304 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
307 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
308 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
312 if ((optype != -1) && !(ctx->operation & optype)) {
313 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
317 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
320 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
326 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
327 const char *name, const char *value)
329 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
330 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
333 if (strcmp(name, "digest") == 0)
334 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
336 return ctx->pmeth->ctrl_str(ctx, name, value);
339 /* Utility functions to send a string of hex string to a ctrl */
341 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
348 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
351 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
357 bin = OPENSSL_hexstr2buf(hex, &binlen);
360 if (binlen <= INT_MAX)
361 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
365 /* Pass a message digest to a ctrl */
366 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
369 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
370 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
373 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
376 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
378 return ctx->operation;
381 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
383 ctx->keygen_info = dat;
384 ctx->keygen_info_count = datlen;
387 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
392 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
397 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
402 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
407 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
409 ctx->app_data = data;
412 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
414 return ctx->app_data;
417 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
418 int (*init) (EVP_PKEY_CTX *ctx))
423 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
424 int (*copy) (EVP_PKEY_CTX *dst,
430 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
431 void (*cleanup) (EVP_PKEY_CTX *ctx))
433 pmeth->cleanup = cleanup;
436 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
437 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
438 int (*paramgen) (EVP_PKEY_CTX *ctx,
441 pmeth->paramgen_init = paramgen_init;
442 pmeth->paramgen = paramgen;
445 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
446 int (*keygen_init) (EVP_PKEY_CTX *ctx),
447 int (*keygen) (EVP_PKEY_CTX *ctx,
450 pmeth->keygen_init = keygen_init;
451 pmeth->keygen = keygen;
454 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
455 int (*sign_init) (EVP_PKEY_CTX *ctx),
456 int (*sign) (EVP_PKEY_CTX *ctx,
457 unsigned char *sig, size_t *siglen,
458 const unsigned char *tbs,
461 pmeth->sign_init = sign_init;
465 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
466 int (*verify_init) (EVP_PKEY_CTX *ctx),
467 int (*verify) (EVP_PKEY_CTX *ctx,
468 const unsigned char *sig,
470 const unsigned char *tbs,
473 pmeth->verify_init = verify_init;
474 pmeth->verify = verify;
477 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
478 int (*verify_recover_init) (EVP_PKEY_CTX
480 int (*verify_recover) (EVP_PKEY_CTX
489 pmeth->verify_recover_init = verify_recover_init;
490 pmeth->verify_recover = verify_recover;
493 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
494 int (*signctx_init) (EVP_PKEY_CTX *ctx,
496 int (*signctx) (EVP_PKEY_CTX *ctx,
501 pmeth->signctx_init = signctx_init;
502 pmeth->signctx = signctx;
505 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
506 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
508 int (*verifyctx) (EVP_PKEY_CTX *ctx,
509 const unsigned char *sig,
513 pmeth->verifyctx_init = verifyctx_init;
514 pmeth->verifyctx = verifyctx;
517 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
518 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
519 int (*encryptfn) (EVP_PKEY_CTX *ctx,
522 const unsigned char *in,
525 pmeth->encrypt_init = encrypt_init;
526 pmeth->encrypt = encryptfn;
529 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
530 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
531 int (*decrypt) (EVP_PKEY_CTX *ctx,
534 const unsigned char *in,
537 pmeth->decrypt_init = decrypt_init;
538 pmeth->decrypt = decrypt;
541 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
542 int (*derive_init) (EVP_PKEY_CTX *ctx),
543 int (*derive) (EVP_PKEY_CTX *ctx,
547 pmeth->derive_init = derive_init;
548 pmeth->derive = derive;
551 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
552 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
554 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
559 pmeth->ctrl_str = ctrl_str;
562 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
563 int (**pinit) (EVP_PKEY_CTX *ctx))
565 *pinit = pmeth->init;
568 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
569 int (**pcopy) (EVP_PKEY_CTX *dst,
572 *pcopy = pmeth->copy;
575 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
576 void (**pcleanup) (EVP_PKEY_CTX *ctx))
578 *pcleanup = pmeth->cleanup;
581 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
582 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
583 int (**pparamgen) (EVP_PKEY_CTX *ctx,
587 *pparamgen_init = pmeth->paramgen_init;
589 *pparamgen = pmeth->paramgen;
592 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
593 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
594 int (**pkeygen) (EVP_PKEY_CTX *ctx,
598 *pkeygen_init = pmeth->keygen_init;
600 *pkeygen = pmeth->keygen;
603 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
604 int (**psign_init) (EVP_PKEY_CTX *ctx),
605 int (**psign) (EVP_PKEY_CTX *ctx,
606 unsigned char *sig, size_t *siglen,
607 const unsigned char *tbs,
611 *psign_init = pmeth->sign_init;
613 *psign = pmeth->sign;
616 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
617 int (**pverify_init) (EVP_PKEY_CTX *ctx),
618 int (**pverify) (EVP_PKEY_CTX *ctx,
619 const unsigned char *sig,
621 const unsigned char *tbs,
625 *pverify_init = pmeth->verify_init;
627 *pverify = pmeth->verify;
630 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
631 int (**pverify_recover_init) (EVP_PKEY_CTX
633 int (**pverify_recover) (EVP_PKEY_CTX
642 if (pverify_recover_init)
643 *pverify_recover_init = pmeth->verify_recover_init;
645 *pverify_recover = pmeth->verify_recover;
648 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
649 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
651 int (**psignctx) (EVP_PKEY_CTX *ctx,
657 *psignctx_init = pmeth->signctx_init;
659 *psignctx = pmeth->signctx;
662 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
663 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
665 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
666 const unsigned char *sig,
671 *pverifyctx_init = pmeth->verifyctx_init;
673 *pverifyctx = pmeth->verifyctx;
676 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
677 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
678 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
681 const unsigned char *in,
685 *pencrypt_init = pmeth->encrypt_init;
687 *pencryptfn = pmeth->encrypt;
690 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
691 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
692 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
695 const unsigned char *in,
699 *pdecrypt_init = pmeth->decrypt_init;
701 *pdecrypt = pmeth->decrypt;
704 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
705 int (**pderive_init) (EVP_PKEY_CTX *ctx),
706 int (**pderive) (EVP_PKEY_CTX *ctx,
711 *pderive_init = pmeth->derive_init;
713 *pderive = pmeth->derive;
716 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
717 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
719 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
724 *pctrl = pmeth->ctrl;
726 *pctrl_str = pmeth->ctrl_str;