xref: /openbsd/lib/libcrypto/dh/dh_ameth.c (revision 5a38ef86)
1 /* $OpenBSD: dh_ameth.c,v 1.19 2021/12/12 21:30:13 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 <stdio.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/bn.h>
63 #include <openssl/dh.h>
64 #include <openssl/err.h>
65 #include <openssl/x509.h>
66 
67 #include "asn1_locl.h"
68 #include "evp_locl.h"
69 
70 static void
71 int_dh_free(EVP_PKEY *pkey)
72 {
73 	DH_free(pkey->pkey.dh);
74 }
75 
76 static int
77 dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
78 {
79 	const unsigned char *p, *pm;
80 	int pklen, pmlen;
81 	int ptype;
82 	const void *pval;
83 	const ASN1_STRING *pstr;
84 	X509_ALGOR *palg;
85 	ASN1_INTEGER *public_key = NULL;
86 	DH *dh = NULL;
87 
88 	if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
89 		return 0;
90 	X509_ALGOR_get0(NULL, &ptype, &pval, palg);
91 
92 	if (ptype != V_ASN1_SEQUENCE) {
93 		DHerror(DH_R_PARAMETER_ENCODING_ERROR);
94 		goto err;
95 	}
96 
97 	pstr = pval;
98 	pm = pstr->data;
99 	pmlen = pstr->length;
100 
101 	if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) {
102 		DHerror(DH_R_DECODE_ERROR);
103 		goto err;
104 	}
105 
106 	if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen))) {
107 		DHerror(DH_R_DECODE_ERROR);
108 		goto err;
109 	}
110 
111 	/* We have parameters now set public key */
112 	if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
113 		DHerror(DH_R_BN_DECODE_ERROR);
114 		goto err;
115 	}
116 
117 	ASN1_INTEGER_free(public_key);
118 	EVP_PKEY_assign_DH(pkey, dh);
119 	return 1;
120 
121 err:
122 	if (public_key)
123 		ASN1_INTEGER_free(public_key);
124 	DH_free(dh);
125 	return 0;
126 }
127 
128 static int
129 dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
130 {
131 	DH *dh;
132 	int ptype;
133 	unsigned char *penc = NULL;
134 	int penclen;
135 	ASN1_STRING *str;
136 	ASN1_INTEGER *pub_key = NULL;
137 
138 	dh=pkey->pkey.dh;
139 
140 	str = ASN1_STRING_new();
141 	if (str == NULL) {
142 		DHerror(ERR_R_MALLOC_FAILURE);
143 		goto err;
144 	}
145 
146 	str->length = i2d_DHparams(dh, &str->data);
147 	if (str->length <= 0) {
148 		DHerror(ERR_R_MALLOC_FAILURE);
149 		goto err;
150 	}
151 	ptype = V_ASN1_SEQUENCE;
152 
153 	pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
154 	if (!pub_key)
155 		goto err;
156 
157 	penclen = i2d_ASN1_INTEGER(pub_key, &penc);
158 
159 	ASN1_INTEGER_free(pub_key);
160 
161 	if (penclen <= 0) {
162 		DHerror(ERR_R_MALLOC_FAILURE);
163 		goto err;
164 		}
165 
166 	if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH), ptype,
167 	    (void *)str, penc, penclen))
168 		return 1;
169 
170 err:
171 	free(penc);
172 	ASN1_STRING_free(str);
173 
174 	return 0;
175 }
176 
177 /*
178  * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in
179  * that the AlgorithmIdentifier contains the paramaters, the private key
180  * is explcitly included and the pubkey must be recalculated.
181  */
182 
183 static int
184 dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
185 {
186 	const unsigned char *p, *pm;
187 	int pklen, pmlen;
188 	int ptype;
189 	const void *pval;
190 	const ASN1_STRING *pstr;
191 	const X509_ALGOR *palg;
192 	ASN1_INTEGER *privkey = NULL;
193 	DH *dh = NULL;
194 
195 	if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
196 		return 0;
197 
198 	X509_ALGOR_get0(NULL, &ptype, &pval, palg);
199 
200 	if (ptype != V_ASN1_SEQUENCE)
201 		goto decerr;
202 
203 	if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
204 		goto decerr;
205 
206 	pstr = pval;
207 	pm = pstr->data;
208 	pmlen = pstr->length;
209 	if (!(dh = d2i_DHparams(NULL, &pm, pmlen)))
210 		goto decerr;
211 	/* We have parameters now set private key */
212 	if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
213 		DHerror(DH_R_BN_ERROR);
214 		goto dherr;
215 	}
216 	/* Calculate public key */
217 	if (!DH_generate_key(dh))
218 		goto dherr;
219 
220 	EVP_PKEY_assign_DH(pkey, dh);
221 
222 	ASN1_INTEGER_free(privkey);
223 
224 	return 1;
225 
226 decerr:
227 	DHerror(EVP_R_DECODE_ERROR);
228 dherr:
229 	ASN1_INTEGER_free(privkey);
230 	DH_free(dh);
231 	return 0;
232 }
233 
234 static int
235 dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
236 {
237 	ASN1_STRING *params = NULL;
238 	ASN1_INTEGER *prkey = NULL;
239 	unsigned char *dp = NULL;
240 	int dplen;
241 
242 	params = ASN1_STRING_new();
243 
244 	if (!params) {
245 		DHerror(ERR_R_MALLOC_FAILURE);
246 		goto err;
247 	}
248 
249 	params->length = i2d_DHparams(pkey->pkey.dh, &params->data);
250 	if (params->length <= 0) {
251 		DHerror(ERR_R_MALLOC_FAILURE);
252 		goto err;
253 	}
254 	params->type = V_ASN1_SEQUENCE;
255 
256 	/* Get private key into integer */
257 	prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
258 
259 	if (!prkey) {
260 		DHerror(DH_R_BN_ERROR);
261 		goto err;
262 	}
263 
264 	dplen = i2d_ASN1_INTEGER(prkey, &dp);
265 
266 	ASN1_INTEGER_free(prkey);
267 	prkey = NULL;
268 
269 	if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dhKeyAgreement), 0,
270 	    V_ASN1_SEQUENCE, params, dp, dplen))
271 		goto err;
272 
273 	return 1;
274 
275 err:
276 	free(dp);
277 	ASN1_STRING_free(params);
278 	ASN1_INTEGER_free(prkey);
279 	return 0;
280 }
281 
282 static void
283 update_buflen(const BIGNUM *b, size_t *pbuflen)
284 {
285 	size_t i;
286 
287 	if (!b)
288 		return;
289 	if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
290 		*pbuflen = i;
291 }
292 
293 static int
294 dh_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
295 {
296 	DH *dh;
297 
298 	if (!(dh = d2i_DHparams(NULL, pder, derlen))) {
299 		DHerror(ERR_R_DH_LIB);
300 		return 0;
301 	}
302 	EVP_PKEY_assign_DH(pkey, dh);
303 	return 1;
304 }
305 
306 static int
307 dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
308 {
309 	return i2d_DHparams(pkey->pkey.dh, pder);
310 }
311 
312 static int
313 do_dh_print(BIO *bp, const DH *x, int indent, ASN1_PCTX *ctx, int ptype)
314 {
315 	unsigned char *m = NULL;
316 	int reason = ERR_R_BUF_LIB, ret = 0;
317 	size_t buf_len = 0;
318 	const char *ktype = NULL;
319 	BIGNUM *priv_key, *pub_key;
320 
321 	if (ptype == 2)
322 		priv_key = x->priv_key;
323 	else
324 		priv_key = NULL;
325 
326 	if (ptype > 0)
327 		pub_key = x->pub_key;
328 	else
329 		pub_key = NULL;
330 
331 	update_buflen(x->p, &buf_len);
332 
333 	if (buf_len == 0) {
334 		reason = ERR_R_PASSED_NULL_PARAMETER;
335 		goto err;
336 	}
337 
338 	update_buflen(x->g, &buf_len);
339 	update_buflen(pub_key, &buf_len);
340 	update_buflen(priv_key, &buf_len);
341 
342 	if (ptype == 2)
343 		ktype = "PKCS#3 DH Private-Key";
344 	else if (ptype == 1)
345 		ktype = "PKCS#3 DH Public-Key";
346 	else
347 		ktype = "PKCS#3 DH Parameters";
348 
349 	m= malloc(buf_len + 10);
350 	if (m == NULL) {
351 		reason = ERR_R_MALLOC_FAILURE;
352 		goto err;
353 	}
354 
355 	BIO_indent(bp, indent, 128);
356 	if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0)
357 		goto err;
358 	indent += 4;
359 
360 	if (!ASN1_bn_print(bp, "private-key:", priv_key, m, indent))
361 		goto err;
362 	if (!ASN1_bn_print(bp, "public-key:", pub_key, m, indent))
363 		goto err;
364 
365 	if (!ASN1_bn_print(bp, "prime:", x->p, m, indent))
366 		goto err;
367 	if (!ASN1_bn_print(bp, "generator:", x->g, m, indent))
368 		goto err;
369 	if (x->length != 0) {
370 		BIO_indent(bp, indent, 128);
371 		if (BIO_printf(bp, "recommended-private-length: %d bits\n",
372 		    (int)x->length) <= 0)
373 			goto err;
374 	}
375 
376 	ret = 1;
377 	if (0) {
378 err:
379 		DHerror(reason);
380 	}
381 	free(m);
382 	return(ret);
383 }
384 
385 static int
386 int_dh_size(const EVP_PKEY *pkey)
387 {
388 	return DH_size(pkey->pkey.dh);
389 }
390 
391 static int
392 dh_bits(const EVP_PKEY *pkey)
393 {
394 	return BN_num_bits(pkey->pkey.dh->p);
395 }
396 
397 static int
398 dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
399 {
400 	if (BN_cmp(a->pkey.dh->p, b->pkey.dh->p) ||
401 	    BN_cmp(a->pkey.dh->g, b->pkey.dh->g))
402 		return 0;
403 	else
404 		return 1;
405 }
406 
407 static int
408 dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
409 {
410 	BIGNUM *a;
411 
412 	if ((a = BN_dup(from->pkey.dh->p)) == NULL)
413 		return 0;
414 	BN_free(to->pkey.dh->p);
415 	to->pkey.dh->p = a;
416 
417 	if ((a = BN_dup(from->pkey.dh->g)) == NULL)
418 		return 0;
419 	BN_free(to->pkey.dh->g);
420 	to->pkey.dh->g = a;
421 
422 	return 1;
423 }
424 
425 static int
426 dh_missing_parameters(const EVP_PKEY *a)
427 {
428 	if (!a->pkey.dh->p || !a->pkey.dh->g)
429 		return 1;
430 	return 0;
431 }
432 
433 static int
434 dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
435 {
436 	if (dh_cmp_parameters(a, b) == 0)
437 		return 0;
438 	if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0)
439 		return 0;
440 	else
441 		return 1;
442 }
443 
444 static int
445 dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
446 {
447 	return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 0);
448 }
449 
450 static int
451 dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
452 {
453 	return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 1);
454 }
455 
456 static int
457 dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
458 {
459 	return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 2);
460 }
461 
462 int
463 DHparams_print(BIO *bp, const DH *x)
464 {
465 	return do_dh_print(bp, x, 4, NULL, 0);
466 }
467 
468 const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
469 	.pkey_id = EVP_PKEY_DH,
470 	.pkey_base_id = EVP_PKEY_DH,
471 
472 	.pem_str = "DH",
473 	.info = "OpenSSL PKCS#3 DH method",
474 
475 	.pub_decode = dh_pub_decode,
476 	.pub_encode = dh_pub_encode,
477 	.pub_cmp = dh_pub_cmp,
478 	.pub_print = dh_public_print,
479 
480 	.priv_decode = dh_priv_decode,
481 	.priv_encode = dh_priv_encode,
482 	.priv_print = dh_private_print,
483 
484 	.pkey_size = int_dh_size,
485 	.pkey_bits = dh_bits,
486 
487 	.param_decode = dh_param_decode,
488 	.param_encode = dh_param_encode,
489 	.param_missing = dh_missing_parameters,
490 	.param_copy = dh_copy_parameters,
491 	.param_cmp = dh_cmp_parameters,
492 	.param_print = dh_param_print,
493 
494 	.pkey_free = int_dh_free,
495 };
496