xref: /openbsd/lib/libcrypto/dh/dh_pmeth.c (revision 1e10d066)
1 /* $OpenBSD: dh_pmeth.c,v 1.17 2024/08/26 22:00:47 op 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/asn1t.h>
65 #include <openssl/bn.h>
66 #include <openssl/dh.h>
67 #include <openssl/err.h>
68 #include <openssl/evp.h>
69 #include <openssl/x509.h>
70 
71 #include "bn_local.h"
72 #include "dh_local.h"
73 #include "evp_local.h"
74 
75 /* DH pkey context structure */
76 
77 typedef struct {
78 	/* Parameter gen parameters */
79 	int prime_len;
80 	int generator;
81 	int use_dsa;
82 	/* Keygen callback info */
83 	int gentmp[2];
84 	/* message digest */
85 } DH_PKEY_CTX;
86 
87 static int
pkey_dh_init(EVP_PKEY_CTX * ctx)88 pkey_dh_init(EVP_PKEY_CTX *ctx)
89 {
90 	DH_PKEY_CTX *dctx;
91 
92 	dctx = malloc(sizeof(DH_PKEY_CTX));
93 	if (!dctx)
94 		return 0;
95 	dctx->prime_len = 1024;
96 	dctx->generator = 2;
97 	dctx->use_dsa = 0;
98 
99 	ctx->data = dctx;
100 	ctx->keygen_info = dctx->gentmp;
101 	ctx->keygen_info_count = 2;
102 
103 	return 1;
104 }
105 
106 static int
pkey_dh_copy(EVP_PKEY_CTX * dst,EVP_PKEY_CTX * src)107 pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
108 {
109 	DH_PKEY_CTX *dctx, *sctx;
110 
111 	if (!pkey_dh_init(dst))
112 		return 0;
113 	sctx = src->data;
114 	dctx = dst->data;
115 	dctx->prime_len = sctx->prime_len;
116 	dctx->generator = sctx->generator;
117 	dctx->use_dsa = sctx->use_dsa;
118 	return 1;
119 }
120 
121 static void
pkey_dh_cleanup(EVP_PKEY_CTX * ctx)122 pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
123 {
124 	DH_PKEY_CTX *dctx = ctx->data;
125 
126 	free(dctx);
127 }
128 
129 static int
pkey_dh_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)130 pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
131 {
132 	DH_PKEY_CTX *dctx = ctx->data;
133 
134 	switch (type) {
135 	case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
136 		if (p1 < 256)
137 			return -2;
138 		dctx->prime_len = p1;
139 		return 1;
140 
141 	case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
142 		dctx->generator = p1;
143 		return 1;
144 
145 	case EVP_PKEY_CTRL_PEER_KEY:
146 		/* Default behaviour is OK */
147 		return 1;
148 
149 	default:
150 		return -2;
151 	}
152 }
153 
154 static int
pkey_dh_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)155 pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
156 {
157 	const char *errstr;
158 	int len;
159 
160 	if (!strcmp(type, "dh_paramgen_prime_len")) {
161 		len = strtonum(value, INT_MIN, INT_MAX, &errstr);
162 		if (errstr != NULL)
163 			return -2;
164 		return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
165 	} else if (!strcmp(type, "dh_paramgen_generator")) {
166 		len = strtonum(value, INT_MIN, INT_MAX, &errstr);
167 		if (errstr != NULL)
168 			return -2;
169 		return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
170 	}
171 
172 	return -2;
173 }
174 
175 static int
pkey_dh_paramgen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)176 pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
177 {
178 	DH *dh;
179 	DH_PKEY_CTX *dctx = ctx->data;
180 	BN_GENCB *pcb = NULL;
181 	BN_GENCB cb = {0};
182 	int ret = 0;
183 
184 	if ((dh = DH_new()) == NULL)
185 		goto err;
186 	if (ctx->pkey_gencb != NULL) {
187 		pcb = &cb;
188 		evp_pkey_set_cb_translate(pcb, ctx);
189 	}
190 	if (!DH_generate_parameters_ex(dh, dctx->prime_len, dctx->generator, pcb))
191 		goto err;
192 	if (!EVP_PKEY_assign_DH(pkey, dh))
193 		goto err;
194 	dh = NULL;
195 
196 	ret = 1;
197  err:
198 	DH_free(dh);
199 
200 	return ret;
201 }
202 
203 static int
pkey_dh_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)204 pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
205 {
206 	DH *dh = NULL;
207 	int ret = 0;
208 
209 	if (ctx->pkey == NULL) {
210 		DHerror(DH_R_NO_PARAMETERS_SET);
211 		goto err;
212 	}
213 
214 	if ((dh = DH_new()) == NULL)
215 		goto err;
216 	if (!EVP_PKEY_set1_DH(pkey, dh))
217 		goto err;
218 
219 	if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
220 		goto err;
221 	if (!DH_generate_key(dh))
222 		goto err;
223 
224 	ret = 1;
225 
226  err:
227 	DH_free(dh);
228 
229 	return ret;
230 }
231 
232 static int
pkey_dh_derive(EVP_PKEY_CTX * ctx,unsigned char * key,size_t * keylen)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