xref: /openbsd-src/lib/libcrypto/evp/pmeth_lib.c (revision c9675a23de50ec5aa20be3956f170f2eccffb293)
1 /* $OpenBSD: pmeth_lib.c,v 1.26 2022/11/26 16:08:53 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. Othewise
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;
279 
280 	if (!pctx->pmeth || !pctx->pmeth->copy)
281 		return NULL;
282 #ifndef OPENSSL_NO_ENGINE
283 	/* Make sure it's safe to copy a pkey context using an ENGINE */
284 	if (pctx->engine && !ENGINE_init(pctx->engine)) {
285 		EVPerror(ERR_R_ENGINE_LIB);
286 		return 0;
287 	}
288 #endif
289 	rctx = malloc(sizeof(EVP_PKEY_CTX));
290 	if (!rctx)
291 		return NULL;
292 
293 	rctx->pmeth = pctx->pmeth;
294 #ifndef OPENSSL_NO_ENGINE
295 	rctx->engine = pctx->engine;
296 #endif
297 
298 	if (pctx->pkey)
299 		CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
300 
301 	rctx->pkey = pctx->pkey;
302 
303 	if (pctx->peerkey)
304 		CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
305 
306 	rctx->peerkey = pctx->peerkey;
307 
308 	rctx->data = NULL;
309 	rctx->app_data = NULL;
310 	rctx->operation = pctx->operation;
311 
312 	if (pctx->pmeth->copy(rctx, pctx) > 0)
313 		return rctx;
314 
315 	EVP_PKEY_CTX_free(rctx);
316 	return NULL;
317 }
318 
319 int
320 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
321 {
322 	if (pkey_app_methods == NULL) {
323 		pkey_app_methods = sk_EVP_PKEY_METHOD_new(NULL);
324 		if (pkey_app_methods == NULL)
325 			return 0;
326 	}
327 
328 	if (!sk_EVP_PKEY_METHOD_push(pkey_app_methods, pmeth))
329 		return 0;
330 
331 	return 1;
332 }
333 
334 void
335 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
336 {
337 	if (ctx == NULL)
338 		return;
339 	if (ctx->pmeth && ctx->pmeth->cleanup)
340 		ctx->pmeth->cleanup(ctx);
341 	EVP_PKEY_free(ctx->pkey);
342 	EVP_PKEY_free(ctx->peerkey);
343 #ifndef OPENSSL_NO_ENGINE
344 	ENGINE_finish(ctx->engine);
345 #endif
346 	free(ctx);
347 }
348 
349 int
350 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
351     int p1, void *p2)
352 {
353 	int ret;
354 
355 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
356 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
357 		return -2;
358 	}
359 	if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
360 		return -1;
361 
362 	if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
363 		EVPerror(EVP_R_NO_OPERATION_SET);
364 		return -1;
365 	}
366 
367 	if ((optype != -1) && !(ctx->operation & optype)) {
368 		EVPerror(EVP_R_INVALID_OPERATION);
369 		return -1;
370 	}
371 
372 	ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
373 
374 	if (ret == -2)
375 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
376 
377 	return ret;
378 
379 }
380 
381 int
382 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
383 {
384 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
385 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
386 		return -2;
387 	}
388 	if (!strcmp(name, "digest")) {
389 		return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG,
390 		    EVP_PKEY_CTRL_MD, value);
391 	}
392 	return ctx->pmeth->ctrl_str(ctx, name, value);
393 }
394 
395 int
396 EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
397 {
398 	size_t len;
399 
400 	if ((len = strlen(str)) > INT_MAX)
401 		return -1;
402 
403 	return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
404 }
405 
406 int
407 EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hexstr)
408 {
409 	unsigned char *hex = NULL;
410 	long length;
411 	int ret = 0;
412 
413 	if ((hex = string_to_hex(hexstr, &length)) == NULL)
414 		goto err;
415 	if (length < 0 || length > INT_MAX) {
416 		ret = -1;
417 		goto err;
418 	}
419 
420 	ret = ctx->pmeth->ctrl(ctx, cmd, length, hex);
421 
422  err:
423 	free(hex);
424 	return ret;
425 }
426 
427 int
428 EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name)
429 {
430 	const EVP_MD *md;
431 
432 	if ((md = EVP_get_digestbyname(md_name)) == NULL) {
433 		EVPerror(EVP_R_INVALID_DIGEST);
434 		return 0;
435 	}
436 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)md);
437 }
438 
439 int
440 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
441 {
442 	return ctx->operation;
443 }
444 
445 void
446 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
447 {
448 	ctx->keygen_info = dat;
449 	ctx->keygen_info_count = datlen;
450 }
451 
452 void
453 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
454 {
455 	ctx->data = data;
456 }
457 
458 void *
459 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
460 {
461 	return ctx->data;
462 }
463 
464 EVP_PKEY *
465 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
466 {
467 	return ctx->pkey;
468 }
469 
470 EVP_PKEY *
471 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
472 {
473 	return ctx->peerkey;
474 }
475 
476 void
477 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
478 {
479 	ctx->app_data = data;
480 }
481 
482 void *
483 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
484 {
485 	return ctx->app_data;
486 }
487 
488 void
489 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
490     int (*init)(EVP_PKEY_CTX *ctx))
491 {
492 	pmeth->init = init;
493 }
494 
495 void
496 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
497     int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
498 {
499 	pmeth->copy = copy;
500 }
501 
502 void
503 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
504     void (*cleanup)(EVP_PKEY_CTX *ctx))
505 {
506 	pmeth->cleanup = cleanup;
507 }
508 
509 void
510 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
511     int (*paramgen_init)(EVP_PKEY_CTX *ctx),
512     int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
513 {
514 	pmeth->paramgen_init = paramgen_init;
515 	pmeth->paramgen = paramgen;
516 }
517 
518 void
519 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
520     int (*keygen_init)(EVP_PKEY_CTX *ctx),
521     int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
522 {
523 	pmeth->keygen_init = keygen_init;
524 	pmeth->keygen = keygen;
525 }
526 
527 void
528 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
529     int (*sign_init)(EVP_PKEY_CTX *ctx),
530     int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
531     const unsigned char *tbs, size_t tbslen))
532 {
533 	pmeth->sign_init = sign_init;
534 	pmeth->sign = sign;
535 }
536 
537 void
538 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
539     int (*verify_init)(EVP_PKEY_CTX *ctx),
540     int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
541     const unsigned char *tbs, size_t tbslen))
542 {
543 	pmeth->verify_init = verify_init;
544 	pmeth->verify = verify;
545 }
546 
547 void
548 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
549     int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
550     int (*verify_recover)(EVP_PKEY_CTX *ctx,
551     unsigned char *sig, size_t *siglen,
552     const unsigned char *tbs, size_t tbslen))
553 {
554 	pmeth->verify_recover_init = verify_recover_init;
555 	pmeth->verify_recover = verify_recover;
556 }
557 
558 void
559 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
560     int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
561     int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
562     EVP_MD_CTX *mctx))
563 {
564 	pmeth->signctx_init = signctx_init;
565 	pmeth->signctx = signctx;
566 }
567 
568 void
569 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
570     int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
571     int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
572     EVP_MD_CTX *mctx))
573 {
574 	pmeth->verifyctx_init = verifyctx_init;
575 	pmeth->verifyctx = verifyctx;
576 }
577 
578 void
579 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
580     int (*encrypt_init)(EVP_PKEY_CTX *ctx),
581     int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
582     const unsigned char *in, size_t inlen))
583 {
584 	pmeth->encrypt_init = encrypt_init;
585 	pmeth->encrypt = encryptfn;
586 }
587 
588 void
589 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
590     int (*decrypt_init)(EVP_PKEY_CTX *ctx),
591     int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
592     const unsigned char *in, size_t inlen))
593 {
594 	pmeth->decrypt_init = decrypt_init;
595 	pmeth->decrypt = decrypt;
596 }
597 
598 void
599 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
600     int (*derive_init)(EVP_PKEY_CTX *ctx),
601     int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
602 {
603 	pmeth->derive_init = derive_init;
604 	pmeth->derive = derive;
605 }
606 
607 void
608 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
609     int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
610     int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
611 {
612 	pmeth->ctrl = ctrl;
613 	pmeth->ctrl_str = ctrl_str;
614 }
615 
616 void
617 EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, int (*check)(EVP_PKEY *pkey))
618 {
619 	pmeth->check = check;
620 }
621 
622 void
623 EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
624     int (*public_check)(EVP_PKEY *pkey))
625 {
626 	pmeth->public_check = public_check;
627 }
628 
629 void
630 EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
631     int (*param_check)(EVP_PKEY *pkey))
632 {
633 	pmeth->param_check = param_check;
634 }
635