xref: /openbsd-src/lib/libcrypto/evp/pmeth_lib.c (revision 451a46108e37dd69f14bcfaf09625106486ae26a)
1 /* $OpenBSD: pmeth_lib.c,v 1.28 2023/06/20 14:05:46 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 *ret;
157 	const EVP_PKEY_METHOD *pmeth;
158 
159 	if (id == -1) {
160 		if (!pkey || !pkey->ameth)
161 			return NULL;
162 		id = pkey->ameth->pkey_id;
163 	}
164 #ifndef OPENSSL_NO_ENGINE
165 	if (pkey && pkey->engine)
166 		e = pkey->engine;
167 	/* Try to find an ENGINE which implements this method */
168 	if (e) {
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 	/* If an ENGINE handled this method look it up. Otherwise
177 	 * use internal tables.
178 	 */
179 
180 	if (e)
181 		pmeth = ENGINE_get_pkey_meth(e, id);
182 	else
183 #endif
184 		pmeth = EVP_PKEY_meth_find(id);
185 
186 	if (pmeth == NULL) {
187 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
188 		return NULL;
189 	}
190 
191 	ret = malloc(sizeof(EVP_PKEY_CTX));
192 	if (ret == NULL) {
193 #ifndef OPENSSL_NO_ENGINE
194 		ENGINE_finish(e);
195 #endif
196 		EVPerror(ERR_R_MALLOC_FAILURE);
197 		return NULL;
198 	}
199 	ret->engine = e;
200 	ret->pmeth = pmeth;
201 	ret->operation = EVP_PKEY_OP_UNDEFINED;
202 	ret->pkey = pkey;
203 	ret->peerkey = NULL;
204 	ret->pkey_gencb = 0;
205 	if (pkey)
206 		CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
207 	ret->data = NULL;
208 
209 	if (pmeth->init) {
210 		if (pmeth->init(ret) <= 0) {
211 			EVP_PKEY_CTX_free(ret);
212 			return NULL;
213 		}
214 	}
215 
216 	return ret;
217 }
218 
219 EVP_PKEY_METHOD*
220 EVP_PKEY_meth_new(int id, int flags)
221 {
222 	EVP_PKEY_METHOD *pmeth;
223 
224 	if ((pmeth = calloc(1, sizeof(EVP_PKEY_METHOD))) == NULL)
225 		return NULL;
226 
227 	pmeth->pkey_id = id;
228 	pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
229 
230 	return pmeth;
231 }
232 
233 void
234 EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, const EVP_PKEY_METHOD *meth)
235 {
236 	if (ppkey_id)
237 		*ppkey_id = meth->pkey_id;
238 	if (pflags)
239 		*pflags = meth->flags;
240 }
241 
242 void
243 EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
244 {
245 	EVP_PKEY_METHOD preserve;
246 
247 	preserve.pkey_id = dst->pkey_id;
248 	preserve.flags = dst->flags;
249 
250 	*dst = *src;
251 
252 	dst->pkey_id = preserve.pkey_id;
253 	dst->flags = preserve.flags;
254 }
255 
256 void
257 EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
258 {
259 	if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
260 		free(pmeth);
261 }
262 
263 EVP_PKEY_CTX *
264 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
265 {
266 	return int_ctx_new(pkey, e, -1);
267 }
268 
269 EVP_PKEY_CTX *
270 EVP_PKEY_CTX_new_id(int id, ENGINE *e)
271 {
272 	return int_ctx_new(NULL, e, id);
273 }
274 
275 EVP_PKEY_CTX *
276 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
277 {
278 	EVP_PKEY_CTX *rctx = NULL;
279 
280 	if (pctx->pmeth == NULL || pctx->pmeth->copy == NULL)
281 		goto err;
282 #ifndef OPENSSL_NO_ENGINE
283 	/* Make sure it's safe to copy a pkey context using an ENGINE */
284 	if (pctx->engine != NULL && !ENGINE_init(pctx->engine)) {
285 		EVPerror(ERR_R_ENGINE_LIB);
286 		goto err;
287 	}
288 #endif
289 	if ((rctx = calloc(1, sizeof(*rctx))) == NULL) {
290 		EVPerror(ERR_R_MALLOC_FAILURE);
291 		goto err;
292 	}
293 
294 	rctx->pmeth = pctx->pmeth;
295 #ifndef OPENSSL_NO_ENGINE
296 	rctx->engine = pctx->engine;
297 #endif
298 
299 	if ((rctx->pkey = pctx->pkey) != NULL)
300 		EVP_PKEY_up_ref(rctx->pkey);
301 	if ((rctx->peerkey = pctx->peerkey) != NULL)
302 		EVP_PKEY_up_ref(rctx->peerkey);
303 
304 	rctx->operation = pctx->operation;
305 
306 	if (pctx->pmeth->copy(rctx, pctx) <= 0)
307 		goto err;
308 
309 	return rctx;
310 
311  err:
312 	EVP_PKEY_CTX_free(rctx);
313 	return NULL;
314 }
315 
316 int
317 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
318 {
319 	if (pkey_app_methods == NULL) {
320 		pkey_app_methods = sk_EVP_PKEY_METHOD_new(NULL);
321 		if (pkey_app_methods == NULL)
322 			return 0;
323 	}
324 
325 	if (!sk_EVP_PKEY_METHOD_push(pkey_app_methods, pmeth))
326 		return 0;
327 
328 	return 1;
329 }
330 
331 void
332 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
333 {
334 	if (ctx == NULL)
335 		return;
336 	if (ctx->pmeth && ctx->pmeth->cleanup)
337 		ctx->pmeth->cleanup(ctx);
338 	EVP_PKEY_free(ctx->pkey);
339 	EVP_PKEY_free(ctx->peerkey);
340 #ifndef OPENSSL_NO_ENGINE
341 	ENGINE_finish(ctx->engine);
342 #endif
343 	free(ctx);
344 }
345 
346 int
347 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
348     int p1, void *p2)
349 {
350 	int ret;
351 
352 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
353 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
354 		return -2;
355 	}
356 	if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
357 		return -1;
358 
359 	if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
360 		EVPerror(EVP_R_NO_OPERATION_SET);
361 		return -1;
362 	}
363 
364 	if ((optype != -1) && !(ctx->operation & optype)) {
365 		EVPerror(EVP_R_INVALID_OPERATION);
366 		return -1;
367 	}
368 
369 	ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
370 
371 	if (ret == -2)
372 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
373 
374 	return ret;
375 
376 }
377 
378 int
379 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
380 {
381 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
382 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
383 		return -2;
384 	}
385 	if (!strcmp(name, "digest")) {
386 		return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG,
387 		    EVP_PKEY_CTRL_MD, value);
388 	}
389 	return ctx->pmeth->ctrl_str(ctx, name, value);
390 }
391 
392 int
393 EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
394 {
395 	size_t len;
396 
397 	if ((len = strlen(str)) > INT_MAX)
398 		return -1;
399 
400 	return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
401 }
402 
403 int
404 EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hexstr)
405 {
406 	unsigned char *hex = NULL;
407 	long length;
408 	int ret = 0;
409 
410 	if ((hex = string_to_hex(hexstr, &length)) == NULL)
411 		goto err;
412 	if (length < 0 || length > INT_MAX) {
413 		ret = -1;
414 		goto err;
415 	}
416 
417 	ret = ctx->pmeth->ctrl(ctx, cmd, length, hex);
418 
419  err:
420 	free(hex);
421 	return ret;
422 }
423 
424 int
425 EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name)
426 {
427 	const EVP_MD *md;
428 
429 	if ((md = EVP_get_digestbyname(md_name)) == NULL) {
430 		EVPerror(EVP_R_INVALID_DIGEST);
431 		return 0;
432 	}
433 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)md);
434 }
435 
436 int
437 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
438 {
439 	return ctx->operation;
440 }
441 
442 void
443 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
444 {
445 	ctx->keygen_info = dat;
446 	ctx->keygen_info_count = datlen;
447 }
448 
449 void
450 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
451 {
452 	ctx->data = data;
453 }
454 
455 void *
456 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
457 {
458 	return ctx->data;
459 }
460 
461 EVP_PKEY *
462 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
463 {
464 	return ctx->pkey;
465 }
466 
467 EVP_PKEY *
468 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
469 {
470 	return ctx->peerkey;
471 }
472 
473 void
474 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
475 {
476 	ctx->app_data = data;
477 }
478 
479 void *
480 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
481 {
482 	return ctx->app_data;
483 }
484 
485 void
486 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
487     int (*init)(EVP_PKEY_CTX *ctx))
488 {
489 	pmeth->init = init;
490 }
491 
492 void
493 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
494     int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
495 {
496 	pmeth->copy = copy;
497 }
498 
499 void
500 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
501     void (*cleanup)(EVP_PKEY_CTX *ctx))
502 {
503 	pmeth->cleanup = cleanup;
504 }
505 
506 void
507 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
508     int (*paramgen_init)(EVP_PKEY_CTX *ctx),
509     int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
510 {
511 	pmeth->paramgen_init = paramgen_init;
512 	pmeth->paramgen = paramgen;
513 }
514 
515 void
516 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
517     int (*keygen_init)(EVP_PKEY_CTX *ctx),
518     int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
519 {
520 	pmeth->keygen_init = keygen_init;
521 	pmeth->keygen = keygen;
522 }
523 
524 void
525 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
526     int (*sign_init)(EVP_PKEY_CTX *ctx),
527     int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
528     const unsigned char *tbs, size_t tbslen))
529 {
530 	pmeth->sign_init = sign_init;
531 	pmeth->sign = sign;
532 }
533 
534 void
535 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
536     int (*verify_init)(EVP_PKEY_CTX *ctx),
537     int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
538     const unsigned char *tbs, size_t tbslen))
539 {
540 	pmeth->verify_init = verify_init;
541 	pmeth->verify = verify;
542 }
543 
544 void
545 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
546     int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
547     int (*verify_recover)(EVP_PKEY_CTX *ctx,
548     unsigned char *sig, size_t *siglen,
549     const unsigned char *tbs, size_t tbslen))
550 {
551 	pmeth->verify_recover_init = verify_recover_init;
552 	pmeth->verify_recover = verify_recover;
553 }
554 
555 void
556 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
557     int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
558     int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
559     EVP_MD_CTX *mctx))
560 {
561 	pmeth->signctx_init = signctx_init;
562 	pmeth->signctx = signctx;
563 }
564 
565 void
566 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
567     int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
568     int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
569     EVP_MD_CTX *mctx))
570 {
571 	pmeth->verifyctx_init = verifyctx_init;
572 	pmeth->verifyctx = verifyctx;
573 }
574 
575 void
576 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
577     int (*encrypt_init)(EVP_PKEY_CTX *ctx),
578     int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
579     const unsigned char *in, size_t inlen))
580 {
581 	pmeth->encrypt_init = encrypt_init;
582 	pmeth->encrypt = encryptfn;
583 }
584 
585 void
586 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
587     int (*decrypt_init)(EVP_PKEY_CTX *ctx),
588     int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
589     const unsigned char *in, size_t inlen))
590 {
591 	pmeth->decrypt_init = decrypt_init;
592 	pmeth->decrypt = decrypt;
593 }
594 
595 void
596 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
597     int (*derive_init)(EVP_PKEY_CTX *ctx),
598     int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
599 {
600 	pmeth->derive_init = derive_init;
601 	pmeth->derive = derive;
602 }
603 
604 void
605 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
606     int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
607     int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
608 {
609 	pmeth->ctrl = ctrl;
610 	pmeth->ctrl_str = ctrl_str;
611 }
612 
613 void
614 EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, int (*check)(EVP_PKEY *pkey))
615 {
616 	pmeth->check = check;
617 }
618 
619 void
620 EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
621     int (*public_check)(EVP_PKEY *pkey))
622 {
623 	pmeth->public_check = public_check;
624 }
625 
626 void
627 EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
628     int (*param_check)(EVP_PKEY *pkey))
629 {
630 	pmeth->param_check = param_check;
631 }
632