xref: /openbsd-src/lib/libcrypto/evp/pmeth_lib.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /* $OpenBSD: pmeth_lib.c,v 1.37 2024/01/13 12:46:59 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 #include "asn1_local.h"
72 #include "evp_local.h"
73 
74 extern const EVP_PKEY_METHOD cmac_pkey_meth;
75 extern const EVP_PKEY_METHOD dh_pkey_meth;
76 extern const EVP_PKEY_METHOD dsa_pkey_meth;
77 extern const EVP_PKEY_METHOD ec_pkey_meth;
78 extern const EVP_PKEY_METHOD ed25519_pkey_meth;
79 extern const EVP_PKEY_METHOD gostimit_pkey_meth;
80 extern const EVP_PKEY_METHOD gostr01_pkey_meth;
81 extern const EVP_PKEY_METHOD hkdf_pkey_meth;
82 extern const EVP_PKEY_METHOD hmac_pkey_meth;
83 extern const EVP_PKEY_METHOD rsa_pkey_meth;
84 extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
85 extern const EVP_PKEY_METHOD x25519_pkey_meth;
86 
87 static const EVP_PKEY_METHOD *pkey_methods[] = {
88 	&cmac_pkey_meth,
89 	&dh_pkey_meth,
90 	&dsa_pkey_meth,
91 	&ec_pkey_meth,
92 	&ed25519_pkey_meth,
93 	&gostimit_pkey_meth,
94 	&gostr01_pkey_meth,
95 	&hkdf_pkey_meth,
96 	&hmac_pkey_meth,
97 	&rsa_pkey_meth,
98 	&rsa_pss_pkey_meth,
99 	&x25519_pkey_meth,
100 };
101 
102 #define N_PKEY_METHODS (sizeof(pkey_methods) / sizeof(pkey_methods[0]))
103 
104 static const EVP_PKEY_METHOD *
105 evp_pkey_method_find(int nid)
106 {
107 	size_t i;
108 
109 	for (i = 0; i < N_PKEY_METHODS; i++) {
110 		const EVP_PKEY_METHOD *pmeth = pkey_methods[i];
111 		if (pmeth->pkey_id == nid)
112 			return pmeth;
113 	}
114 
115 	return NULL;
116 }
117 
118 static EVP_PKEY_CTX *
119 evp_pkey_ctx_new(EVP_PKEY *pkey, int nid)
120 {
121 	EVP_PKEY_CTX *pkey_ctx = NULL;
122 	const EVP_PKEY_METHOD *pmeth;
123 
124 	if (nid == -1) {
125 		if (pkey == NULL || pkey->ameth == NULL)
126 			return NULL;
127 		nid = pkey->ameth->pkey_id;
128 	}
129 
130 	if ((pmeth = evp_pkey_method_find(nid)) == NULL) {
131 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
132 		goto err;
133 	}
134 
135 	if ((pkey_ctx = calloc(1, sizeof(*pkey_ctx))) == NULL) {
136 		EVPerror(ERR_R_MALLOC_FAILURE);
137 		goto err;
138 	}
139 	pkey_ctx->pmeth = pmeth;
140 	pkey_ctx->operation = EVP_PKEY_OP_UNDEFINED;
141 	if ((pkey_ctx->pkey = pkey) != NULL)
142 		EVP_PKEY_up_ref(pkey_ctx->pkey);
143 
144 	if (pmeth->init != NULL) {
145 		if (pmeth->init(pkey_ctx) <= 0)
146 			goto err;
147 	}
148 
149 	return pkey_ctx;
150 
151  err:
152 	EVP_PKEY_CTX_free(pkey_ctx);
153 
154 	return NULL;
155 }
156 
157 EVP_PKEY_CTX *
158 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *engine)
159 {
160 	return evp_pkey_ctx_new(pkey, -1);
161 }
162 
163 EVP_PKEY_CTX *
164 EVP_PKEY_CTX_new_id(int nid, ENGINE *engine)
165 {
166 	return evp_pkey_ctx_new(NULL, nid);
167 }
168 
169 EVP_PKEY_CTX *
170 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
171 {
172 	EVP_PKEY_CTX *rctx = NULL;
173 
174 	if (pctx->pmeth == NULL || pctx->pmeth->copy == NULL)
175 		goto err;
176 	if ((rctx = calloc(1, sizeof(*rctx))) == NULL) {
177 		EVPerror(ERR_R_MALLOC_FAILURE);
178 		goto err;
179 	}
180 
181 	rctx->pmeth = pctx->pmeth;
182 
183 	if ((rctx->pkey = pctx->pkey) != NULL)
184 		EVP_PKEY_up_ref(rctx->pkey);
185 	if ((rctx->peerkey = pctx->peerkey) != NULL)
186 		EVP_PKEY_up_ref(rctx->peerkey);
187 
188 	rctx->operation = pctx->operation;
189 
190 	if (pctx->pmeth->copy(rctx, pctx) <= 0)
191 		goto err;
192 
193 	return rctx;
194 
195  err:
196 	EVP_PKEY_CTX_free(rctx);
197 	return NULL;
198 }
199 
200 void
201 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
202 {
203 	if (ctx == NULL)
204 		return;
205 	if (ctx->pmeth && ctx->pmeth->cleanup)
206 		ctx->pmeth->cleanup(ctx);
207 	EVP_PKEY_free(ctx->pkey);
208 	EVP_PKEY_free(ctx->peerkey);
209 	free(ctx);
210 }
211 
212 int
213 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
214     int p1, void *p2)
215 {
216 	int ret;
217 
218 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
219 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
220 		return -2;
221 	}
222 	if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
223 		return -1;
224 
225 	if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
226 		EVPerror(EVP_R_NO_OPERATION_SET);
227 		return -1;
228 	}
229 
230 	if ((optype != -1) && !(ctx->operation & optype)) {
231 		EVPerror(EVP_R_INVALID_OPERATION);
232 		return -1;
233 	}
234 
235 	ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
236 
237 	if (ret == -2)
238 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
239 
240 	return ret;
241 
242 }
243 
244 int
245 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
246 {
247 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
248 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
249 		return -2;
250 	}
251 	if (!strcmp(name, "digest")) {
252 		return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG,
253 		    EVP_PKEY_CTRL_MD, value);
254 	}
255 	return ctx->pmeth->ctrl_str(ctx, name, value);
256 }
257 
258 int
259 EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
260 {
261 	size_t len;
262 
263 	if ((len = strlen(str)) > INT_MAX)
264 		return -1;
265 
266 	return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
267 }
268 
269 int
270 EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hexstr)
271 {
272 	unsigned char *hex = NULL;
273 	long length;
274 	int ret = 0;
275 
276 	if ((hex = string_to_hex(hexstr, &length)) == NULL)
277 		goto err;
278 	if (length < 0 || length > INT_MAX) {
279 		ret = -1;
280 		goto err;
281 	}
282 
283 	ret = ctx->pmeth->ctrl(ctx, cmd, length, hex);
284 
285  err:
286 	free(hex);
287 	return ret;
288 }
289 
290 int
291 EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name)
292 {
293 	const EVP_MD *md;
294 
295 	if ((md = EVP_get_digestbyname(md_name)) == NULL) {
296 		EVPerror(EVP_R_INVALID_DIGEST);
297 		return 0;
298 	}
299 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)md);
300 }
301 
302 int
303 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
304 {
305 	return ctx->operation;
306 }
307 
308 void
309 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
310 {
311 	ctx->keygen_info = dat;
312 	ctx->keygen_info_count = datlen;
313 }
314 
315 void
316 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
317 {
318 	ctx->data = data;
319 }
320 
321 void *
322 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
323 {
324 	return ctx->data;
325 }
326 
327 EVP_PKEY *
328 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
329 {
330 	return ctx->pkey;
331 }
332 
333 EVP_PKEY *
334 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
335 {
336 	return ctx->peerkey;
337 }
338 
339 void
340 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
341 {
342 	ctx->app_data = data;
343 }
344 
345 void *
346 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
347 {
348 	return ctx->app_data;
349 }
350 
351 /*
352  * Remove all the functions below in the next major bump
353  */
354 
355 const EVP_PKEY_METHOD *
356 EVP_PKEY_meth_find(int type)
357 {
358 	EVPerror(ERR_R_DISABLED);
359 	return NULL;
360 }
361 
362 EVP_PKEY_METHOD*
363 EVP_PKEY_meth_new(int id, int flags)
364 {
365 	EVPerror(ERR_R_DISABLED);
366 	return NULL;
367 }
368 
369 void
370 EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, const EVP_PKEY_METHOD *meth)
371 {
372 }
373 
374 void
375 EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
376 {
377 }
378 
379 void
380 EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
381 {
382 }
383 
384 int
385 EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
386 {
387 	EVPerror(ERR_R_DISABLED);
388 	return 0;
389 }
390 
391 void
392 EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
393     int (*init)(EVP_PKEY_CTX *ctx))
394 {
395 }
396 
397 void
398 EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
399     int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
400 {
401 }
402 
403 void
404 EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
405     void (*cleanup)(EVP_PKEY_CTX *ctx))
406 {
407 }
408 
409 void
410 EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
411     int (*paramgen_init)(EVP_PKEY_CTX *ctx),
412     int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
413 {
414 }
415 
416 void
417 EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
418     int (*keygen_init)(EVP_PKEY_CTX *ctx),
419     int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
420 {
421 }
422 
423 void
424 EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
425     int (*sign_init)(EVP_PKEY_CTX *ctx),
426     int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
427     const unsigned char *tbs, size_t tbslen))
428 {
429 }
430 
431 void
432 EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
433     int (*verify_init)(EVP_PKEY_CTX *ctx),
434     int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
435     const unsigned char *tbs, size_t tbslen))
436 {
437 }
438 
439 void
440 EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
441     int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
442     int (*verify_recover)(EVP_PKEY_CTX *ctx,
443     unsigned char *sig, size_t *siglen,
444     const unsigned char *tbs, size_t tbslen))
445 {
446 }
447 
448 void
449 EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
450     int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
451     int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
452     EVP_MD_CTX *mctx))
453 {
454 }
455 
456 void
457 EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
458     int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
459     int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
460     EVP_MD_CTX *mctx))
461 {
462 }
463 
464 void
465 EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
466     int (*encrypt_init)(EVP_PKEY_CTX *ctx),
467     int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
468     const unsigned char *in, size_t inlen))
469 {
470 }
471 
472 void
473 EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
474     int (*decrypt_init)(EVP_PKEY_CTX *ctx),
475     int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
476     const unsigned char *in, size_t inlen))
477 {
478 }
479 
480 void
481 EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
482     int (*derive_init)(EVP_PKEY_CTX *ctx),
483     int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
484 {
485 }
486 
487 void
488 EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
489     int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
490     int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
491 {
492 }
493 
494 void
495 EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, int (*check)(EVP_PKEY *pkey))
496 {
497 }
498 
499 void
500 EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
501     int (*public_check)(EVP_PKEY *pkey))
502 {
503 }
504 
505 void
506 EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
507     int (*param_check)(EVP_PKEY *pkey))
508 {
509 }
510