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