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) {
335 if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) {
336 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
339 return EVP_PKEY_CTX_set_signature_md(ctx, md);
341 return ctx->pmeth->ctrl_str(ctx, name, value);
344 /* Utility functions to send a string of hex string to a ctrl */
346 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
353 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
356 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
362 bin = OPENSSL_hexstr2buf(hex, &binlen);
365 if (binlen <= INT_MAX)
366 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
371 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
373 return ctx->operation;
376 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
378 ctx->keygen_info = dat;
379 ctx->keygen_info_count = datlen;
382 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
387 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
392 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
397 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
402 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
404 ctx->app_data = data;
407 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
409 return ctx->app_data;
412 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
413 int (*init) (EVP_PKEY_CTX *ctx))
418 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
419 int (*copy) (EVP_PKEY_CTX *dst,
425 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
426 void (*cleanup) (EVP_PKEY_CTX *ctx))
428 pmeth->cleanup = cleanup;
431 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
432 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
433 int (*paramgen) (EVP_PKEY_CTX *ctx,
436 pmeth->paramgen_init = paramgen_init;
437 pmeth->paramgen = paramgen;
440 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
441 int (*keygen_init) (EVP_PKEY_CTX *ctx),
442 int (*keygen) (EVP_PKEY_CTX *ctx,
445 pmeth->keygen_init = keygen_init;
446 pmeth->keygen = keygen;
449 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
450 int (*sign_init) (EVP_PKEY_CTX *ctx),
451 int (*sign) (EVP_PKEY_CTX *ctx,
452 unsigned char *sig, size_t *siglen,
453 const unsigned char *tbs,
456 pmeth->sign_init = sign_init;
460 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
461 int (*verify_init) (EVP_PKEY_CTX *ctx),
462 int (*verify) (EVP_PKEY_CTX *ctx,
463 const unsigned char *sig,
465 const unsigned char *tbs,
468 pmeth->verify_init = verify_init;
469 pmeth->verify = verify;
472 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
473 int (*verify_recover_init) (EVP_PKEY_CTX
475 int (*verify_recover) (EVP_PKEY_CTX
484 pmeth->verify_recover_init = verify_recover_init;
485 pmeth->verify_recover = verify_recover;
488 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
489 int (*signctx_init) (EVP_PKEY_CTX *ctx,
491 int (*signctx) (EVP_PKEY_CTX *ctx,
496 pmeth->signctx_init = signctx_init;
497 pmeth->signctx = signctx;
500 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
501 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
503 int (*verifyctx) (EVP_PKEY_CTX *ctx,
504 const unsigned char *sig,
508 pmeth->verifyctx_init = verifyctx_init;
509 pmeth->verifyctx = verifyctx;
512 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
513 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
514 int (*encryptfn) (EVP_PKEY_CTX *ctx,
517 const unsigned char *in,
520 pmeth->encrypt_init = encrypt_init;
521 pmeth->encrypt = encryptfn;
524 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
525 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
526 int (*decrypt) (EVP_PKEY_CTX *ctx,
529 const unsigned char *in,
532 pmeth->decrypt_init = decrypt_init;
533 pmeth->decrypt = decrypt;
536 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
537 int (*derive_init) (EVP_PKEY_CTX *ctx),
538 int (*derive) (EVP_PKEY_CTX *ctx,
542 pmeth->derive_init = derive_init;
543 pmeth->derive = derive;
546 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
547 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
549 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
554 pmeth->ctrl_str = ctrl_str;
557 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
558 int (**pinit) (EVP_PKEY_CTX *ctx))
560 *pinit = pmeth->init;
563 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
564 int (**pcopy) (EVP_PKEY_CTX *dst,
567 *pcopy = pmeth->copy;
570 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
571 void (**pcleanup) (EVP_PKEY_CTX *ctx))
573 *pcleanup = pmeth->cleanup;
576 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
577 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
578 int (**pparamgen) (EVP_PKEY_CTX *ctx,
582 *pparamgen_init = pmeth->paramgen_init;
584 *pparamgen = pmeth->paramgen;
587 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
588 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
589 int (**pkeygen) (EVP_PKEY_CTX *ctx,
593 *pkeygen_init = pmeth->keygen_init;
595 *pkeygen = pmeth->keygen;
598 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
599 int (**psign_init) (EVP_PKEY_CTX *ctx),
600 int (**psign) (EVP_PKEY_CTX *ctx,
601 unsigned char *sig, size_t *siglen,
602 const unsigned char *tbs,
606 *psign_init = pmeth->sign_init;
608 *psign = pmeth->sign;
611 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
612 int (**pverify_init) (EVP_PKEY_CTX *ctx),
613 int (**pverify) (EVP_PKEY_CTX *ctx,
614 const unsigned char *sig,
616 const unsigned char *tbs,
620 *pverify_init = pmeth->verify_init;
622 *pverify = pmeth->verify;
625 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
626 int (**pverify_recover_init) (EVP_PKEY_CTX
628 int (**pverify_recover) (EVP_PKEY_CTX
637 if (pverify_recover_init)
638 *pverify_recover_init = pmeth->verify_recover_init;
640 *pverify_recover = pmeth->verify_recover;
643 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
644 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
646 int (**psignctx) (EVP_PKEY_CTX *ctx,
652 *psignctx_init = pmeth->signctx_init;
654 *psignctx = pmeth->signctx;
657 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
658 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
660 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
661 const unsigned char *sig,
666 *pverifyctx_init = pmeth->verifyctx_init;
668 *pverifyctx = pmeth->verifyctx;
671 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
672 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
673 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
676 const unsigned char *in,
680 *pencrypt_init = pmeth->encrypt_init;
682 *pencryptfn = pmeth->encrypt;
685 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
686 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
687 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
690 const unsigned char *in,
694 *pdecrypt_init = pmeth->decrypt_init;
696 *pdecrypt = pmeth->decrypt;
699 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
700 int (**pderive_init) (EVP_PKEY_CTX *ctx),
701 int (**pderive) (EVP_PKEY_CTX *ctx,
706 *pderive_init = pmeth->derive_init;
708 *pderive = pmeth->derive;
711 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
712 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
714 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
719 *pctrl = pmeth->ctrl;
721 *pctrl_str = pmeth->ctrl_str;