xref: /openbsd-src/lib/libcrypto/evp/pmeth_lib.c (revision d506f56649e5b312a51951da77de5ce255d519e5)
1 /* $OpenBSD: pmeth_lib.c,v 1.29 2023/06/20 14:10:05 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58 
59 #include <limits.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #include <openssl/opensslconf.h>
65 
66 #include <openssl/err.h>
67 #include <openssl/evp.h>
68 #include <openssl/objects.h>
69 #include <openssl/x509v3.h>
70 
71 #ifndef OPENSSL_NO_ENGINE
72 #include <openssl/engine.h>
73 #endif
74 
75 #include "asn1_local.h"
76 #include "evp_local.h"
77 
78 DECLARE_STACK_OF(EVP_PKEY_METHOD)
79 STACK_OF(EVP_PKEY_METHOD) *pkey_app_methods = NULL;
80 
81 extern const EVP_PKEY_METHOD cmac_pkey_meth;
82 extern const EVP_PKEY_METHOD dh_pkey_meth;
83 extern const EVP_PKEY_METHOD dsa_pkey_meth;
84 extern const EVP_PKEY_METHOD ec_pkey_meth;
85 extern const EVP_PKEY_METHOD ed25519_pkey_meth;
86 extern const EVP_PKEY_METHOD gostimit_pkey_meth;
87 extern const EVP_PKEY_METHOD gostr01_pkey_meth;
88 extern const EVP_PKEY_METHOD hkdf_pkey_meth;
89 extern const EVP_PKEY_METHOD hmac_pkey_meth;
90 extern const EVP_PKEY_METHOD rsa_pkey_meth;
91 extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
92 extern const EVP_PKEY_METHOD x25519_pkey_meth;
93 
94 static const EVP_PKEY_METHOD *pkey_methods[] = {
95 	&cmac_pkey_meth,
96 	&dh_pkey_meth,
97 	&dsa_pkey_meth,
98 	&ec_pkey_meth,
99 	&ed25519_pkey_meth,
100 	&gostimit_pkey_meth,
101 	&gostr01_pkey_meth,
102 	&hkdf_pkey_meth,
103 	&hmac_pkey_meth,
104 	&rsa_pkey_meth,
105 	&rsa_pss_pkey_meth,
106 	&x25519_pkey_meth,
107 };
108 
109 static const size_t pkey_methods_count =
110     sizeof(pkey_methods) / sizeof(pkey_methods[0]);
111 
112 int
113 evp_pkey_meth_get_count(void)
114 {
115 	int num = pkey_methods_count;
116 
117 	if (pkey_app_methods != NULL)
118 		num += sk_EVP_PKEY_METHOD_num(pkey_app_methods);
119 
120 	return num;
121 }
122 
123 const EVP_PKEY_METHOD *
124 evp_pkey_meth_get0(int idx)
125 {
126 	int num = pkey_methods_count;
127 
128 	if (idx < 0)
129 		return NULL;
130 	if (idx < num)
131 		return pkey_methods[idx];
132 
133 	idx -= num;
134 
135 	return sk_EVP_PKEY_METHOD_value(pkey_app_methods, idx);
136 }
137 
138 const EVP_PKEY_METHOD *
139 EVP_PKEY_meth_find(int type)
140 {
141 	const EVP_PKEY_METHOD *pmeth;
142 	int i;
143 
144 	for (i = evp_pkey_meth_get_count() - 1; i >= 0; i--) {
145 		pmeth = evp_pkey_meth_get0(i);
146 		if (pmeth->pkey_id == type)
147 			return pmeth;
148 	}
149 
150 	return NULL;
151 }
152 
153 static EVP_PKEY_CTX *
154 int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
155 {
156 	EVP_PKEY_CTX *pkey_ctx = NULL;
157 	const EVP_PKEY_METHOD *pmeth;
158 
159 	if (id == -1) {
160 		if (pkey == NULL || pkey->ameth == NULL)
161 			return NULL;
162 		id = pkey->ameth->pkey_id;
163 	}
164 #ifndef OPENSSL_NO_ENGINE
165 	if (pkey != NULL && pkey->engine != NULL)
166 		e = pkey->engine;
167 	/* Try to find an ENGINE which implements this method. */
168 	if (e != NULL) {
169 		if (!ENGINE_init(e)) {
170 			EVPerror(ERR_R_ENGINE_LIB);
171 			return NULL;
172 		}
173 	} else
174 		e = ENGINE_get_pkey_meth_engine(id);
175 
176 	/* Look up method handler in ENGINE or use internal tables. */
177 	if (e != NULL)
178 		pmeth = ENGINE_get_pkey_meth(e, id);
179 	else
180 #endif
181 		pmeth = EVP_PKEY_meth_find(id);
182 
183 	if (pmeth == NULL) {
184 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
185 		goto err;
186 	}
187 
188 	if ((pkey_ctx = calloc(1, sizeof(*pkey_ctx))) == NULL) {
189 		EVPerror(ERR_R_MALLOC_FAILURE);
190 		goto err;
191 	}
192 	pkey_ctx->engine = e;
193 	e = NULL;
194 	pkey_ctx->pmeth = pmeth;
195 	pkey_ctx->operation = EVP_PKEY_OP_UNDEFINED;
196 	if ((pkey_ctx->pkey = pkey) != NULL)
197 		EVP_PKEY_up_ref(pkey_ctx->pkey);
198 
199 	if (pmeth->init != NULL) {
200 		if (pmeth->init(pkey_ctx) <= 0)
201 			goto err;
202 	}
203 
204 	return pkey_ctx;
205 
206  err:
207 	EVP_PKEY_CTX_free(pkey_ctx);
208 #ifndef OPENSSL_NO_ENGINE
209 	ENGINE_finish(e);
210 #endif
211 
212 	return NULL;
213 }
214 
215 EVP_PKEY_METHOD*
216 EVP_PKEY_meth_new(int id, int flags)
217 {
218 	EVP_PKEY_METHOD *pmeth;
219 
220 	if ((pmeth = calloc(1, sizeof(EVP_PKEY_METHOD))) == NULL)
221 		return NULL;
222 
223 	pmeth->pkey_id = id;
224 	pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
225 
226 	return pmeth;
227 }
228 
229 void
230 EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, const EVP_PKEY_METHOD *meth)
231 {
232 	if (ppkey_id)
233 		*ppkey_id = meth->pkey_id;
234 	if (pflags)
235 		*pflags = meth->flags;
236 }
237 
238 void
239 EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
240 {
241 	EVP_PKEY_METHOD preserve;
242 
243 	preserve.pkey_id = dst->pkey_id;
244 	preserve.flags = dst->flags;
245 
246 	*dst = *src;
247 
248 	dst->pkey_id = preserve.pkey_id;
249 	dst->flags = preserve.flags;
250 }
251 
252 void
253 EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
254 {
255 	if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
256 		free(pmeth);
257 }
258 
259 EVP_PKEY_CTX *
260 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
261 {
262 	return int_ctx_new(pkey, e, -1);
263 }
264 
265 EVP_PKEY_CTX *
266 EVP_PKEY_CTX_new_id(int id, ENGINE *e)
267 {
268 	return int_ctx_new(NULL, e, id);
269 }
270 
271 EVP_PKEY_CTX *
272 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
273 {
274 	EVP_PKEY_CTX *rctx = NULL;
275 
276 	if (pctx->pmeth == NULL || pctx->pmeth->copy == NULL)
277 		goto err;
278 #ifndef OPENSSL_NO_ENGINE
279 	/* Make sure it's safe to copy a pkey context using an ENGINE */
280 	if (pctx->engine != NULL && !ENGINE_init(pctx->engine)) {
281 		EVPerror(ERR_R_ENGINE_LIB);
282 		goto err;
283 	}
284 #endif
285 	if ((rctx = calloc(1, sizeof(*rctx))) == NULL) {
286 		EVPerror(ERR_R_MALLOC_FAILURE);
287 		goto err;
288 	}
289 
290 	rctx->pmeth = pctx->pmeth;
291 #ifndef OPENSSL_NO_ENGINE
292 	rctx->engine = pctx->engine;
293 #endif
294 
295 	if ((rctx->pkey = pctx->pkey) != NULL)
296 		EVP_PKEY_up_ref(rctx->pkey);
297 	if ((rctx->peerkey = pctx->peerkey) != NULL)
298 		EVP_PKEY_up_ref(rctx->peerkey);
299 
300 	rctx->operation = pctx->operation;
301 
302 	if (pctx->pmeth->copy(rctx, pctx) <= 0)
303 		goto err;
304 
305 	return rctx;
306 
307  err:
308 	EVP_PKEY_CTX_free(rctx);
309 	return NULL;
310 }
311 
312 int
313 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
314 {
315 	if (pkey_app_methods == NULL) {
316 		pkey_app_methods = sk_EVP_PKEY_METHOD_new(NULL);
317 		if (pkey_app_methods == NULL)
318 			return 0;
319 	}
320 
321 	if (!sk_EVP_PKEY_METHOD_push(pkey_app_methods, pmeth))
322 		return 0;
323 
324 	return 1;
325 }
326 
327 void
328 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
329 {
330 	if (ctx == NULL)
331 		return;
332 	if (ctx->pmeth && ctx->pmeth->cleanup)
333 		ctx->pmeth->cleanup(ctx);
334 	EVP_PKEY_free(ctx->pkey);
335 	EVP_PKEY_free(ctx->peerkey);
336 #ifndef OPENSSL_NO_ENGINE
337 	ENGINE_finish(ctx->engine);
338 #endif
339 	free(ctx);
340 }
341 
342 int
343 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
344     int p1, void *p2)
345 {
346 	int ret;
347 
348 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
349 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
350 		return -2;
351 	}
352 	if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
353 		return -1;
354 
355 	if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
356 		EVPerror(EVP_R_NO_OPERATION_SET);
357 		return -1;
358 	}
359 
360 	if ((optype != -1) && !(ctx->operation & optype)) {
361 		EVPerror(EVP_R_INVALID_OPERATION);
362 		return -1;
363 	}
364 
365 	ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
366 
367 	if (ret == -2)
368 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
369 
370 	return ret;
371 
372 }
373 
374 int
375 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
376 {
377 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
378 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
379 		return -2;
380 	}
381 	if (!strcmp(name, "digest")) {
382 		return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG,
383 		    EVP_PKEY_CTRL_MD, value);
384 	}
385 	return ctx->pmeth->ctrl_str(ctx, name, value);
386 }
387 
388 int
389 EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
390 {
391 	size_t len;
392 
393 	if ((len = strlen(str)) > INT_MAX)
394 		return -1;
395 
396 	return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
397 }
398 
399 int
400 EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hexstr)
401 {
402 	unsigned char *hex = NULL;
403 	long length;
404 	int ret = 0;
405 
406 	if ((hex = string_to_hex(hexstr, &length)) == NULL)
407 		goto err;
408 	if (length < 0 || length > INT_MAX) {
409 		ret = -1;
410 		goto err;
411 	}
412 
413 	ret = ctx->pmeth->ctrl(ctx, cmd, length, hex);
414 
415  err:
416 	free(hex);
417 	return ret;
418 }
419 
420 int
421 EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name)
422 {
423 	const EVP_MD *md;
424 
425 	if ((md = EVP_get_digestbyname(md_name)) == NULL) {
426 		EVPerror(EVP_R_INVALID_DIGEST);
427 		return 0;
428 	}
429 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)md);
430 }
431 
432 int
433 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
434 {
435 	return ctx->operation;
436 }
437 
438 void
439 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
440 {
441 	ctx->keygen_info = dat;
442 	ctx->keygen_info_count = datlen;
443 }
444 
445 void
446 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
447 {
448 	ctx->data = data;
449 }
450 
451 void *
452 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
453 {
454 	return ctx->data;
455 }
456 
457 EVP_PKEY *
458 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
459 {
460 	return ctx->pkey;
461 }
462 
463 EVP_PKEY *
464 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
465 {
466 	return ctx->peerkey;
467 }
468 
469 void
470 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
471 {
472 	ctx->app_data = data;
473 }
474 
475 void *
476 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
477 {
478 	return ctx->app_data;
479 }
480 
481 void
482 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
483     int (*init)(EVP_PKEY_CTX *ctx))
484 {
485 	pmeth->init = init;
486 }
487 
488 void
489 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
490     int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
491 {
492 	pmeth->copy = copy;
493 }
494 
495 void
496 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
497     void (*cleanup)(EVP_PKEY_CTX *ctx))
498 {
499 	pmeth->cleanup = cleanup;
500 }
501 
502 void
503 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
504     int (*paramgen_init)(EVP_PKEY_CTX *ctx),
505     int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
506 {
507 	pmeth->paramgen_init = paramgen_init;
508 	pmeth->paramgen = paramgen;
509 }
510 
511 void
512 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
513     int (*keygen_init)(EVP_PKEY_CTX *ctx),
514     int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
515 {
516 	pmeth->keygen_init = keygen_init;
517 	pmeth->keygen = keygen;
518 }
519 
520 void
521 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
522     int (*sign_init)(EVP_PKEY_CTX *ctx),
523     int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
524     const unsigned char *tbs, size_t tbslen))
525 {
526 	pmeth->sign_init = sign_init;
527 	pmeth->sign = sign;
528 }
529 
530 void
531 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
532     int (*verify_init)(EVP_PKEY_CTX *ctx),
533     int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
534     const unsigned char *tbs, size_t tbslen))
535 {
536 	pmeth->verify_init = verify_init;
537 	pmeth->verify = verify;
538 }
539 
540 void
541 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
542     int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
543     int (*verify_recover)(EVP_PKEY_CTX *ctx,
544     unsigned char *sig, size_t *siglen,
545     const unsigned char *tbs, size_t tbslen))
546 {
547 	pmeth->verify_recover_init = verify_recover_init;
548 	pmeth->verify_recover = verify_recover;
549 }
550 
551 void
552 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
553     int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
554     int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
555     EVP_MD_CTX *mctx))
556 {
557 	pmeth->signctx_init = signctx_init;
558 	pmeth->signctx = signctx;
559 }
560 
561 void
562 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
563     int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
564     int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
565     EVP_MD_CTX *mctx))
566 {
567 	pmeth->verifyctx_init = verifyctx_init;
568 	pmeth->verifyctx = verifyctx;
569 }
570 
571 void
572 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
573     int (*encrypt_init)(EVP_PKEY_CTX *ctx),
574     int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
575     const unsigned char *in, size_t inlen))
576 {
577 	pmeth->encrypt_init = encrypt_init;
578 	pmeth->encrypt = encryptfn;
579 }
580 
581 void
582 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
583     int (*decrypt_init)(EVP_PKEY_CTX *ctx),
584     int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
585     const unsigned char *in, size_t inlen))
586 {
587 	pmeth->decrypt_init = decrypt_init;
588 	pmeth->decrypt = decrypt;
589 }
590 
591 void
592 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
593     int (*derive_init)(EVP_PKEY_CTX *ctx),
594     int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
595 {
596 	pmeth->derive_init = derive_init;
597 	pmeth->derive = derive;
598 }
599 
600 void
601 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
602     int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
603     int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
604 {
605 	pmeth->ctrl = ctrl;
606 	pmeth->ctrl_str = ctrl_str;
607 }
608 
609 void
610 EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, int (*check)(EVP_PKEY *pkey))
611 {
612 	pmeth->check = check;
613 }
614 
615 void
616 EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
617     int (*public_check)(EVP_PKEY *pkey))
618 {
619 	pmeth->public_check = public_check;
620 }
621 
622 void
623 EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
624     int (*param_check)(EVP_PKEY *pkey))
625 {
626 	pmeth->param_check = param_check;
627 }
628