xref: /openbsd/lib/libcrypto/dh/dh_pmeth.c (revision 4bdff4be)
1 /* $OpenBSD: dh_pmeth.c,v 1.16 2024/01/01 16:01:48 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 <string.h>
62 
63 #include <openssl/asn1t.h>
64 #include <openssl/bn.h>
65 #include <openssl/dh.h>
66 #include <openssl/err.h>
67 #include <openssl/evp.h>
68 #include <openssl/x509.h>
69 
70 #include "bn_local.h"
71 #include "dh_local.h"
72 #include "evp_local.h"
73 
74 /* DH pkey context structure */
75 
76 typedef struct {
77 	/* Parameter gen parameters */
78 	int prime_len;
79 	int generator;
80 	int use_dsa;
81 	/* Keygen callback info */
82 	int gentmp[2];
83 	/* message digest */
84 } DH_PKEY_CTX;
85 
86 static int
87 pkey_dh_init(EVP_PKEY_CTX *ctx)
88 {
89 	DH_PKEY_CTX *dctx;
90 
91 	dctx = malloc(sizeof(DH_PKEY_CTX));
92 	if (!dctx)
93 		return 0;
94 	dctx->prime_len = 1024;
95 	dctx->generator = 2;
96 	dctx->use_dsa = 0;
97 
98 	ctx->data = dctx;
99 	ctx->keygen_info = dctx->gentmp;
100 	ctx->keygen_info_count = 2;
101 
102 	return 1;
103 }
104 
105 static int
106 pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
107 {
108 	DH_PKEY_CTX *dctx, *sctx;
109 
110 	if (!pkey_dh_init(dst))
111 		return 0;
112 	sctx = src->data;
113 	dctx = dst->data;
114 	dctx->prime_len = sctx->prime_len;
115 	dctx->generator = sctx->generator;
116 	dctx->use_dsa = sctx->use_dsa;
117 	return 1;
118 }
119 
120 static void
121 pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
122 {
123 	DH_PKEY_CTX *dctx = ctx->data;
124 
125 	free(dctx);
126 }
127 
128 static int
129 pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
130 {
131 	DH_PKEY_CTX *dctx = ctx->data;
132 
133 	switch (type) {
134 	case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
135 		if (p1 < 256)
136 			return -2;
137 		dctx->prime_len = p1;
138 		return 1;
139 
140 	case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
141 		dctx->generator = p1;
142 		return 1;
143 
144 	case EVP_PKEY_CTRL_PEER_KEY:
145 		/* Default behaviour is OK */
146 		return 1;
147 
148 	default:
149 		return -2;
150 	}
151 }
152 
153 static int
154 pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
155 {
156 	long lval;
157 	char *ep;
158 	int len;
159 
160 	if (!strcmp(type, "dh_paramgen_prime_len")) {
161 		errno = 0;
162 		lval = strtol(value, &ep, 10);
163 		if (value[0] == '\0' || *ep != '\0')
164 			goto not_a_number;
165 		if ((errno == ERANGE &&
166 		    (lval == LONG_MAX || lval == LONG_MIN)) ||
167 		    (lval > INT_MAX || lval < INT_MIN))
168 			goto out_of_range;
169 		len = lval;
170 		return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
171 	} else if (!strcmp(type, "dh_paramgen_generator")) {
172 		errno = 0;
173 		lval = strtol(value, &ep, 10);
174 		if (value[0] == '\0' || *ep != '\0')
175 			goto not_a_number;
176 		if ((errno == ERANGE &&
177 		    (lval == LONG_MAX || lval == LONG_MIN)) ||
178 		    (lval > INT_MAX || lval < INT_MIN))
179 			goto out_of_range;
180 		len = lval;
181 		return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
182 	}
183 
184 not_a_number:
185 out_of_range:
186 	return -2;
187 }
188 
189 static int
190 pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
191 {
192 	DH *dh;
193 	DH_PKEY_CTX *dctx = ctx->data;
194 	BN_GENCB *pcb = NULL;
195 	BN_GENCB cb = {0};
196 	int ret = 0;
197 
198 	if ((dh = DH_new()) == NULL)
199 		goto err;
200 	if (ctx->pkey_gencb != NULL) {
201 		pcb = &cb;
202 		evp_pkey_set_cb_translate(pcb, ctx);
203 	}
204 	if (!DH_generate_parameters_ex(dh, dctx->prime_len, dctx->generator, pcb))
205 		goto err;
206 	if (!EVP_PKEY_assign_DH(pkey, dh))
207 		goto err;
208 	dh = NULL;
209 
210 	ret = 1;
211  err:
212 	DH_free(dh);
213 
214 	return ret;
215 }
216 
217 static int
218 pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
219 {
220 	DH *dh = NULL;
221 	int ret = 0;
222 
223 	if (ctx->pkey == NULL) {
224 		DHerror(DH_R_NO_PARAMETERS_SET);
225 		goto err;
226 	}
227 
228 	if ((dh = DH_new()) == NULL)
229 		goto err;
230 	if (!EVP_PKEY_set1_DH(pkey, dh))
231 		goto err;
232 
233 	if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
234 		goto err;
235 	if (!DH_generate_key(dh))
236 		goto err;
237 
238 	ret = 1;
239 
240  err:
241 	DH_free(dh);
242 
243 	return ret;
244 }
245 
246 static int
247 pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
248 {
249 	int ret;
250 
251 	if (!ctx->pkey || !ctx->peerkey) {
252 		DHerror(DH_R_KEYS_NOT_SET);
253 		return 0;
254 	}
255 	ret = DH_compute_key(key, ctx->peerkey->pkey.dh->pub_key,
256 	    ctx->pkey->pkey.dh);
257 	if (ret < 0)
258 		return ret;
259 	*keylen = ret;
260 	return 1;
261 }
262 
263 const EVP_PKEY_METHOD dh_pkey_meth = {
264 	.pkey_id = EVP_PKEY_DH,
265 	.flags = EVP_PKEY_FLAG_AUTOARGLEN,
266 
267 	.init = pkey_dh_init,
268 	.copy = pkey_dh_copy,
269 	.cleanup = pkey_dh_cleanup,
270 
271 	.paramgen = pkey_dh_paramgen,
272 
273 	.keygen = pkey_dh_keygen,
274 
275 	.derive = pkey_dh_derive,
276 
277 	.ctrl = pkey_dh_ctrl,
278 	.ctrl_str = pkey_dh_ctrl_str
279 };
280