1 /* $OpenBSD: pmeth_lib.c,v 1.14 2018/04/14 07:09:21 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/opensslconf.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/objects.h>
69 #ifndef OPENSSL_NO_ENGINE
70 #include <openssl/engine.h>
73 #include "asn1_locl.h"
76 typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
78 DECLARE_STACK_OF(EVP_PKEY_METHOD)
79 STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
81 extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
82 extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth;
83 extern const EVP_PKEY_METHOD gostimit_pkey_meth, gostr01_pkey_meth;
85 static const EVP_PKEY_METHOD *standard_methods[] = {
86 #ifndef OPENSSL_NO_RSA
92 #ifndef OPENSSL_NO_DSA
98 #ifndef OPENSSL_NO_GOST
106 static int pmeth_cmp_BSEARCH_CMP_FN(const void *, const void *);
107 static int pmeth_cmp(const EVP_PKEY_METHOD * const *, const EVP_PKEY_METHOD * const *);
108 static const EVP_PKEY_METHOD * *OBJ_bsearch_pmeth(const EVP_PKEY_METHOD * *key, const EVP_PKEY_METHOD * const *base, int num);
111 pmeth_cmp(const EVP_PKEY_METHOD * const *a, const EVP_PKEY_METHOD * const *b)
113 return ((*a)->pkey_id - (*b)->pkey_id);
118 pmeth_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
120 const EVP_PKEY_METHOD * const *a = a_;
121 const EVP_PKEY_METHOD * const *b = b_;
122 return pmeth_cmp(a, b);
125 static const EVP_PKEY_METHOD * *
126 OBJ_bsearch_pmeth(const EVP_PKEY_METHOD * *key, const EVP_PKEY_METHOD * const *base, int num)
128 return (const EVP_PKEY_METHOD * *)OBJ_bsearch_(key, base, num, sizeof(const EVP_PKEY_METHOD *),
129 pmeth_cmp_BSEARCH_CMP_FN);
132 const EVP_PKEY_METHOD *
133 EVP_PKEY_meth_find(int type)
136 const EVP_PKEY_METHOD *t = &tmp, **ret;
139 if (app_pkey_methods) {
141 idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
143 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
145 ret = OBJ_bsearch_pmeth(&t, standard_methods,
146 sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *));
152 static EVP_PKEY_CTX *
153 int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
156 const EVP_PKEY_METHOD *pmeth;
159 if (!pkey || !pkey->ameth)
161 id = pkey->ameth->pkey_id;
163 #ifndef OPENSSL_NO_ENGINE
164 if (pkey && pkey->engine)
166 /* Try to find an ENGINE which implements this method */
168 if (!ENGINE_init(e)) {
169 EVPerror(ERR_R_ENGINE_LIB);
173 e = ENGINE_get_pkey_meth_engine(id);
175 /* If an ENGINE handled this method look it up. Othewise
176 * use internal tables.
180 pmeth = ENGINE_get_pkey_meth(e, id);
183 pmeth = EVP_PKEY_meth_find(id);
186 EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
190 ret = malloc(sizeof(EVP_PKEY_CTX));
192 #ifndef OPENSSL_NO_ENGINE
195 EVPerror(ERR_R_MALLOC_FAILURE);
200 ret->operation = EVP_PKEY_OP_UNDEFINED;
205 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
209 if (pmeth->init(ret) <= 0) {
210 EVP_PKEY_CTX_free(ret);
219 EVP_PKEY_meth_new(int id, int flags)
221 EVP_PKEY_METHOD *pmeth;
223 pmeth = calloc(1, sizeof(EVP_PKEY_METHOD));
228 pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
233 pmeth->paramgen_init = 0;
235 pmeth->keygen_init = 0;
237 pmeth->sign_init = 0;
239 pmeth->verify_init = 0;
241 pmeth->verify_recover_init = 0;
242 pmeth->verify_recover = 0;
243 pmeth->signctx_init = 0;
245 pmeth->verifyctx_init = 0;
246 pmeth->verifyctx = 0;
247 pmeth->encrypt_init = 0;
249 pmeth->decrypt_init = 0;
251 pmeth->derive_init = 0;
260 EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, const EVP_PKEY_METHOD *meth)
263 *ppkey_id = meth->pkey_id;
265 *pflags = meth->flags;
269 EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
271 dst->init = src->init;
272 dst->copy = src->copy;
273 dst->cleanup = src->cleanup;
275 dst->paramgen_init = src->paramgen_init;
276 dst->paramgen = src->paramgen;
278 dst->keygen_init = src->keygen_init;
279 dst->keygen = src->keygen;
281 dst->sign_init = src->sign_init;
282 dst->sign = src->sign;
284 dst->verify_init = src->verify_init;
285 dst->verify = src->verify;
287 dst->verify_recover_init = src->verify_recover_init;
288 dst->verify_recover = src->verify_recover;
290 dst->signctx_init = src->signctx_init;
291 dst->signctx = src->signctx;
293 dst->verifyctx_init = src->verifyctx_init;
294 dst->verifyctx = src->verifyctx;
296 dst->encrypt_init = src->encrypt_init;
297 dst->encrypt = src->encrypt;
299 dst->decrypt_init = src->decrypt_init;
300 dst->decrypt = src->decrypt;
302 dst->derive_init = src->derive_init;
303 dst->derive = src->derive;
305 dst->ctrl = src->ctrl;
306 dst->ctrl_str = src->ctrl_str;
310 EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
312 if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
317 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
319 return int_ctx_new(pkey, e, -1);
323 EVP_PKEY_CTX_new_id(int id, ENGINE *e)
325 return int_ctx_new(NULL, e, id);
329 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
333 if (!pctx->pmeth || !pctx->pmeth->copy)
335 #ifndef OPENSSL_NO_ENGINE
336 /* Make sure it's safe to copy a pkey context using an ENGINE */
337 if (pctx->engine && !ENGINE_init(pctx->engine)) {
338 EVPerror(ERR_R_ENGINE_LIB);
342 rctx = malloc(sizeof(EVP_PKEY_CTX));
346 rctx->pmeth = pctx->pmeth;
347 #ifndef OPENSSL_NO_ENGINE
348 rctx->engine = pctx->engine;
352 CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
354 rctx->pkey = pctx->pkey;
357 CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
359 rctx->peerkey = pctx->peerkey;
362 rctx->app_data = NULL;
363 rctx->operation = pctx->operation;
365 if (pctx->pmeth->copy(rctx, pctx) > 0)
368 EVP_PKEY_CTX_free(rctx);
373 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
375 if (app_pkey_methods == NULL) {
376 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
377 if (!app_pkey_methods)
380 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
382 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
387 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
391 if (ctx->pmeth && ctx->pmeth->cleanup)
392 ctx->pmeth->cleanup(ctx);
393 EVP_PKEY_free(ctx->pkey);
394 EVP_PKEY_free(ctx->peerkey);
395 #ifndef OPENSSL_NO_ENGINE
396 ENGINE_finish(ctx->engine);
402 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
407 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
408 EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
411 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
414 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
415 EVPerror(EVP_R_NO_OPERATION_SET);
419 if ((optype != -1) && !(ctx->operation & optype)) {
420 EVPerror(EVP_R_INVALID_OPERATION);
424 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
427 EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
434 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
436 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
437 EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
440 if (!strcmp(name, "digest")) {
442 if (!value || !(md = EVP_get_digestbyname(value))) {
443 EVPerror(EVP_R_INVALID_DIGEST);
446 return EVP_PKEY_CTX_set_signature_md(ctx, md);
448 return ctx->pmeth->ctrl_str(ctx, name, value);
452 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
454 return ctx->operation;
458 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
460 ctx->keygen_info = dat;
461 ctx->keygen_info_count = datlen;
465 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
471 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
477 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
483 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
489 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
491 ctx->app_data = data;
495 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
497 return ctx->app_data;
501 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
502 int (*init)(EVP_PKEY_CTX *ctx))
508 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
509 int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
515 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
516 void (*cleanup)(EVP_PKEY_CTX *ctx))
518 pmeth->cleanup = cleanup;
522 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
523 int (*paramgen_init)(EVP_PKEY_CTX *ctx),
524 int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
526 pmeth->paramgen_init = paramgen_init;
527 pmeth->paramgen = paramgen;
531 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
532 int (*keygen_init)(EVP_PKEY_CTX *ctx),
533 int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
535 pmeth->keygen_init = keygen_init;
536 pmeth->keygen = keygen;
540 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
541 int (*sign_init)(EVP_PKEY_CTX *ctx),
542 int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
543 const unsigned char *tbs, size_t tbslen))
545 pmeth->sign_init = sign_init;
550 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
551 int (*verify_init)(EVP_PKEY_CTX *ctx),
552 int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
553 const unsigned char *tbs, size_t tbslen))
555 pmeth->verify_init = verify_init;
556 pmeth->verify = verify;
560 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
561 int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
562 int (*verify_recover)(EVP_PKEY_CTX *ctx,
563 unsigned char *sig, size_t *siglen,
564 const unsigned char *tbs, size_t tbslen))
566 pmeth->verify_recover_init = verify_recover_init;
567 pmeth->verify_recover = verify_recover;
571 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
572 int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
573 int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
576 pmeth->signctx_init = signctx_init;
577 pmeth->signctx = signctx;
581 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
582 int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
583 int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
586 pmeth->verifyctx_init = verifyctx_init;
587 pmeth->verifyctx = verifyctx;
591 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
592 int (*encrypt_init)(EVP_PKEY_CTX *ctx),
593 int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
594 const unsigned char *in, size_t inlen))
596 pmeth->encrypt_init = encrypt_init;
597 pmeth->encrypt = encryptfn;
601 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
602 int (*decrypt_init)(EVP_PKEY_CTX *ctx),
603 int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
604 const unsigned char *in, size_t inlen))
606 pmeth->decrypt_init = decrypt_init;
607 pmeth->decrypt = decrypt;
611 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
612 int (*derive_init)(EVP_PKEY_CTX *ctx),
613 int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
615 pmeth->derive_init = derive_init;
616 pmeth->derive = derive;
620 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
621 int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
622 int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
625 pmeth->ctrl_str = ctrl_str;