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