xref: /openbsd-src/lib/libcrypto/evp/pmeth_lib.c (revision 9c1150c2d47b6bc9ca96d03146d30a4fd6718824)
1 /* $OpenBSD: pmeth_lib.c,v 1.39 2024/03/02 11:17:27 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 hkdf_pkey_meth;
80 extern const EVP_PKEY_METHOD hmac_pkey_meth;
81 extern const EVP_PKEY_METHOD rsa_pkey_meth;
82 extern const EVP_PKEY_METHOD rsa_pss_pkey_meth;
83 extern const EVP_PKEY_METHOD x25519_pkey_meth;
84 
85 static const EVP_PKEY_METHOD *pkey_methods[] = {
86 	&cmac_pkey_meth,
87 	&dh_pkey_meth,
88 	&dsa_pkey_meth,
89 	&ec_pkey_meth,
90 	&ed25519_pkey_meth,
91 	&hkdf_pkey_meth,
92 	&hmac_pkey_meth,
93 	&rsa_pkey_meth,
94 	&rsa_pss_pkey_meth,
95 	&x25519_pkey_meth,
96 };
97 
98 #define N_PKEY_METHODS (sizeof(pkey_methods) / sizeof(pkey_methods[0]))
99 
100 static const EVP_PKEY_METHOD *
101 evp_pkey_method_find(int nid)
102 {
103 	size_t i;
104 
105 	for (i = 0; i < N_PKEY_METHODS; i++) {
106 		const EVP_PKEY_METHOD *pmeth = pkey_methods[i];
107 		if (pmeth->pkey_id == nid)
108 			return pmeth;
109 	}
110 
111 	return NULL;
112 }
113 
114 static EVP_PKEY_CTX *
115 evp_pkey_ctx_new(EVP_PKEY *pkey, int nid)
116 {
117 	EVP_PKEY_CTX *pkey_ctx = NULL;
118 	const EVP_PKEY_METHOD *pmeth;
119 
120 	if (nid == -1) {
121 		if (pkey == NULL || pkey->ameth == NULL)
122 			return NULL;
123 		nid = pkey->ameth->pkey_id;
124 	}
125 
126 	if ((pmeth = evp_pkey_method_find(nid)) == NULL) {
127 		EVPerror(EVP_R_UNSUPPORTED_ALGORITHM);
128 		goto err;
129 	}
130 
131 	if ((pkey_ctx = calloc(1, sizeof(*pkey_ctx))) == NULL) {
132 		EVPerror(ERR_R_MALLOC_FAILURE);
133 		goto err;
134 	}
135 	pkey_ctx->pmeth = pmeth;
136 	pkey_ctx->operation = EVP_PKEY_OP_UNDEFINED;
137 	if ((pkey_ctx->pkey = pkey) != NULL)
138 		EVP_PKEY_up_ref(pkey_ctx->pkey);
139 
140 	if (pmeth->init != NULL) {
141 		if (pmeth->init(pkey_ctx) <= 0)
142 			goto err;
143 	}
144 
145 	return pkey_ctx;
146 
147  err:
148 	EVP_PKEY_CTX_free(pkey_ctx);
149 
150 	return NULL;
151 }
152 
153 EVP_PKEY_CTX *
154 EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *engine)
155 {
156 	return evp_pkey_ctx_new(pkey, -1);
157 }
158 
159 EVP_PKEY_CTX *
160 EVP_PKEY_CTX_new_id(int nid, ENGINE *engine)
161 {
162 	return evp_pkey_ctx_new(NULL, nid);
163 }
164 
165 EVP_PKEY_CTX *
166 EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
167 {
168 	EVP_PKEY_CTX *rctx = NULL;
169 
170 	if (pctx->pmeth == NULL || pctx->pmeth->copy == NULL)
171 		goto err;
172 	if ((rctx = calloc(1, sizeof(*rctx))) == NULL) {
173 		EVPerror(ERR_R_MALLOC_FAILURE);
174 		goto err;
175 	}
176 
177 	rctx->pmeth = pctx->pmeth;
178 
179 	if ((rctx->pkey = pctx->pkey) != NULL)
180 		EVP_PKEY_up_ref(rctx->pkey);
181 	if ((rctx->peerkey = pctx->peerkey) != NULL)
182 		EVP_PKEY_up_ref(rctx->peerkey);
183 
184 	rctx->operation = pctx->operation;
185 
186 	if (pctx->pmeth->copy(rctx, pctx) <= 0)
187 		goto err;
188 
189 	return rctx;
190 
191  err:
192 	EVP_PKEY_CTX_free(rctx);
193 	return NULL;
194 }
195 
196 void
197 EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
198 {
199 	if (ctx == NULL)
200 		return;
201 	if (ctx->pmeth && ctx->pmeth->cleanup)
202 		ctx->pmeth->cleanup(ctx);
203 	EVP_PKEY_free(ctx->pkey);
204 	EVP_PKEY_free(ctx->peerkey);
205 	free(ctx);
206 }
207 
208 int
209 EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
210     int p1, void *p2)
211 {
212 	int ret;
213 
214 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
215 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
216 		return -2;
217 	}
218 	if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
219 		return -1;
220 
221 	if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
222 		EVPerror(EVP_R_NO_OPERATION_SET);
223 		return -1;
224 	}
225 
226 	if ((optype != -1) && !(ctx->operation & optype)) {
227 		EVPerror(EVP_R_INVALID_OPERATION);
228 		return -1;
229 	}
230 
231 	ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
232 
233 	if (ret == -2)
234 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
235 
236 	return ret;
237 
238 }
239 
240 int
241 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *name, const char *value)
242 {
243 	if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
244 		EVPerror(EVP_R_COMMAND_NOT_SUPPORTED);
245 		return -2;
246 	}
247 	if (!strcmp(name, "digest")) {
248 		return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG,
249 		    EVP_PKEY_CTRL_MD, value);
250 	}
251 	return ctx->pmeth->ctrl_str(ctx, name, value);
252 }
253 
254 int
255 EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
256 {
257 	size_t len;
258 
259 	if ((len = strlen(str)) > INT_MAX)
260 		return -1;
261 
262 	return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
263 }
264 
265 int
266 EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hexstr)
267 {
268 	unsigned char *hex = NULL;
269 	long length;
270 	int ret = 0;
271 
272 	if ((hex = string_to_hex(hexstr, &length)) == NULL)
273 		goto err;
274 	if (length < 0 || length > INT_MAX) {
275 		ret = -1;
276 		goto err;
277 	}
278 
279 	ret = ctx->pmeth->ctrl(ctx, cmd, length, hex);
280 
281  err:
282 	free(hex);
283 	return ret;
284 }
285 
286 int
287 EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name)
288 {
289 	const EVP_MD *md;
290 
291 	if ((md = EVP_get_digestbyname(md_name)) == NULL) {
292 		EVPerror(EVP_R_INVALID_DIGEST);
293 		return 0;
294 	}
295 	return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)md);
296 }
297 
298 int
299 EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
300 {
301 	return ctx->operation;
302 }
303 
304 void
305 EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
306 {
307 	ctx->keygen_info = dat;
308 	ctx->keygen_info_count = datlen;
309 }
310 
311 void
312 EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
313 {
314 	ctx->data = data;
315 }
316 
317 void *
318 EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
319 {
320 	return ctx->data;
321 }
322 
323 EVP_PKEY *
324 EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
325 {
326 	return ctx->pkey;
327 }
328 
329 EVP_PKEY *
330 EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
331 {
332 	return ctx->peerkey;
333 }
334 
335 void
336 EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
337 {
338 	ctx->app_data = data;
339 }
340 
341 void *
342 EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
343 {
344 	return ctx->app_data;
345 }
346