1/* $OpenBSD: kexdh.c,v 1.34 2020/12/04 02:29:25 djm Exp $ */
2/*
3 * Copyright (c) 2019 Markus Friedl.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
27
28#ifdef WITH_OPENSSL
29
30#include <sys/types.h>
31
32#include <stdio.h>
33#include <string.h>
34#include <signal.h>
35
36#include "openbsd-compat/openssl-compat.h"
37#include <openssl/dh.h>
38
39#include "sshkey.h"
40#include "kex.h"
41#include "sshbuf.h"
42#include "digest.h"
43#include "ssherr.h"
44#include "dh.h"
45#include "log.h"
46
47int
48kex_dh_keygen(struct kex *kex)
49{
50	switch (kex->kex_type) {
51	case KEX_DH_GRP1_SHA1:
52		kex->dh = dh_new_group1();
53		break;
54	case KEX_DH_GRP14_SHA1:
55	case KEX_DH_GRP14_SHA256:
56		kex->dh = dh_new_group14();
57		break;
58	case KEX_DH_GRP16_SHA512:
59		kex->dh = dh_new_group16();
60		break;
61	case KEX_DH_GRP18_SHA512:
62		kex->dh = dh_new_group18();
63		break;
64	default:
65		return SSH_ERR_INVALID_ARGUMENT;
66	}
67	if (kex->dh == NULL)
68		return SSH_ERR_ALLOC_FAIL;
69	return (dh_gen_key(kex->dh, kex->we_need * 8));
70}
71
72int
73kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
74{
75	BIGNUM *shared_secret = NULL;
76	u_char *kbuf = NULL;
77	size_t klen = 0;
78	int kout, r;
79
80#ifdef DEBUG_KEXDH
81	fprintf(stderr, "dh_pub= ");
82	BN_print_fp(stderr, dh_pub);
83	fprintf(stderr, "\n");
84	debug("bits %d", BN_num_bits(dh_pub));
85	DHparams_print_fp(stderr, kex->dh);
86	fprintf(stderr, "\n");
87#endif
88
89	if (!dh_pub_is_valid(kex->dh, dh_pub)) {
90		r = SSH_ERR_MESSAGE_INCOMPLETE;
91		goto out;
92	}
93	klen = DH_size(kex->dh);
94	if ((kbuf = malloc(klen)) == NULL ||
95	    (shared_secret = BN_new()) == NULL) {
96		r = SSH_ERR_ALLOC_FAIL;
97		goto out;
98	}
99	if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
100	    BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
101		r = SSH_ERR_LIBCRYPTO_ERROR;
102		goto out;
103	}
104#ifdef DEBUG_KEXDH
105	dump_digest("shared secret", kbuf, kout);
106#endif
107	r = sshbuf_put_bignum2(out, shared_secret);
108 out:
109	freezero(kbuf, klen);
110	BN_clear_free(shared_secret);
111	return r;
112}
113
114int
115kex_dh_keypair(struct kex *kex)
116{
117	const BIGNUM *pub_key;
118	struct sshbuf *buf = NULL;
119	int r;
120
121	if ((r = kex_dh_keygen(kex)) != 0)
122		return r;
123	DH_get0_key(kex->dh, &pub_key, NULL);
124	if ((buf = sshbuf_new()) == NULL)
125		return SSH_ERR_ALLOC_FAIL;
126	if ((r = sshbuf_put_bignum2(buf, pub_key)) != 0 ||
127	    (r = sshbuf_get_u32(buf, NULL)) != 0)
128		goto out;
129#ifdef DEBUG_KEXDH
130	DHparams_print_fp(stderr, kex->dh);
131	fprintf(stderr, "pub= ");
132	BN_print_fp(stderr, pub_key);
133	fprintf(stderr, "\n");
134#endif
135	kex->client_pub = buf;
136	buf = NULL;
137 out:
138	sshbuf_free(buf);
139	return r;
140}
141
142int
143kex_dh_enc(struct kex *kex, const struct sshbuf *client_blob,
144    struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
145{
146	const BIGNUM *pub_key;
147	struct sshbuf *server_blob = NULL;
148	int r;
149
150	*server_blobp = NULL;
151	*shared_secretp = NULL;
152
153	if ((r = kex_dh_keygen(kex)) != 0)
154		goto out;
155	DH_get0_key(kex->dh, &pub_key, NULL);
156	if ((server_blob = sshbuf_new()) == NULL) {
157		r = SSH_ERR_ALLOC_FAIL;
158		goto out;
159	}
160	if ((r = sshbuf_put_bignum2(server_blob, pub_key)) != 0 ||
161	    (r = sshbuf_get_u32(server_blob, NULL)) != 0)
162		goto out;
163	if ((r = kex_dh_dec(kex, client_blob, shared_secretp)) != 0)
164		goto out;
165	*server_blobp = server_blob;
166	server_blob = NULL;
167 out:
168	DH_free(kex->dh);
169	kex->dh = NULL;
170	sshbuf_free(server_blob);
171	return r;
172}
173
174int
175kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
176    struct sshbuf **shared_secretp)
177{
178	struct sshbuf *buf = NULL;
179	BIGNUM *dh_pub = NULL;
180	int r;
181
182	*shared_secretp = NULL;
183
184	if ((buf = sshbuf_new()) == NULL) {
185		r = SSH_ERR_ALLOC_FAIL;
186		goto out;
187	}
188	if ((r = sshbuf_put_stringb(buf, dh_blob)) != 0 ||
189	    (r = sshbuf_get_bignum2(buf, &dh_pub)) != 0)
190		goto out;
191	sshbuf_reset(buf);
192	if ((r = kex_dh_compute_key(kex, dh_pub, buf)) != 0)
193		goto out;
194	*shared_secretp = buf;
195	buf = NULL;
196 out:
197	BN_free(dh_pub);
198	DH_free(kex->dh);
199	kex->dh = NULL;
200	sshbuf_free(buf);
201	return r;
202}
203#endif /* WITH_OPENSSL */
204