]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - kexgexc.c
Vendor import of OpenSSH 7.7p1.
[FreeBSD/FreeBSD.git] / kexgexc.c
1 /* $OpenBSD: kexgexc.c,v 1.27 2018/02/07 02:06:51 jsing Exp $ */
2 /*
3  * Copyright (c) 2000 Niels Provos.  All rights reserved.
4  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #ifdef WITH_OPENSSL
30
31 #include <sys/types.h>
32
33 #include <openssl/dh.h>
34
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <signal.h>
39
40 #include "sshkey.h"
41 #include "cipher.h"
42 #include "digest.h"
43 #include "kex.h"
44 #include "log.h"
45 #include "packet.h"
46 #include "dh.h"
47 #include "ssh2.h"
48 #include "compat.h"
49 #include "dispatch.h"
50 #include "ssherr.h"
51 #include "sshbuf.h"
52 #include "misc.h"
53
54 static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
55 static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
56
57 int
58 kexgex_client(struct ssh *ssh)
59 {
60         struct kex *kex = ssh->kex;
61         int r;
62         u_int nbits;
63
64         nbits = dh_estimate(kex->dh_need * 8);
65
66         kex->min = DH_GRP_MIN;
67         kex->max = DH_GRP_MAX;
68         kex->nbits = nbits;
69         if (datafellows & SSH_BUG_DHGEX_LARGE)
70                 kex->nbits = MINIMUM(kex->nbits, 4096);
71         /* New GEX request */
72         if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
73             (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
74             (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
75             (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
76             (r = sshpkt_send(ssh)) != 0)
77                 goto out;
78         debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
79             kex->min, kex->nbits, kex->max);
80 #ifdef DEBUG_KEXDH
81         fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
82             kex->min, kex->nbits, kex->max);
83 #endif
84         ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
85             &input_kex_dh_gex_group);
86         r = 0;
87  out:
88         return r;
89 }
90
91 static int
92 input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
93 {
94         struct kex *kex = ssh->kex;
95         BIGNUM *p = NULL, *g = NULL;
96         int r, bits;
97
98         debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
99
100         if ((p = BN_new()) == NULL ||
101             (g = BN_new()) == NULL) {
102                 r = SSH_ERR_ALLOC_FAIL;
103                 goto out;
104         }
105         if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
106             (r = sshpkt_get_bignum2(ssh, g)) != 0 ||
107             (r = sshpkt_get_end(ssh)) != 0)
108                 goto out;
109         if ((bits = BN_num_bits(p)) < 0 ||
110             (u_int)bits < kex->min || (u_int)bits > kex->max) {
111                 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
112                 goto out;
113         }
114         if ((kex->dh = dh_new_group(g, p)) == NULL) {
115                 r = SSH_ERR_ALLOC_FAIL;
116                 goto out;
117         }
118         p = g = NULL; /* belong to kex->dh now */
119
120         /* generate and send 'e', client DH public key */
121         if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
122             (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
123             (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
124             (r = sshpkt_send(ssh)) != 0)
125                 goto out;
126         debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
127 #ifdef DEBUG_KEXDH
128         DHparams_print_fp(stderr, kex->dh);
129         fprintf(stderr, "pub= ");
130         BN_print_fp(stderr, kex->dh->pub_key);
131         fprintf(stderr, "\n");
132 #endif
133         ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
134         ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
135         r = 0;
136 out:
137         BN_clear_free(p);
138         BN_clear_free(g);
139         return r;
140 }
141
142 static int
143 input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
144 {
145         struct kex *kex = ssh->kex;
146         BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
147         struct sshkey *server_host_key = NULL;
148         u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
149         u_char hash[SSH_DIGEST_MAX_LENGTH];
150         size_t klen = 0, slen, sbloblen, hashlen;
151         int kout, r;
152
153         debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
154         if (kex->verify_host_key == NULL) {
155                 r = SSH_ERR_INVALID_ARGUMENT;
156                 goto out;
157         }
158         /* key, cert */
159         if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
160             &sbloblen)) != 0 ||
161             (r = sshkey_from_blob(server_host_key_blob, sbloblen,
162             &server_host_key)) != 0)
163                 goto out;
164         if (server_host_key->type != kex->hostkey_type ||
165             (kex->hostkey_type == KEY_ECDSA &&
166             server_host_key->ecdsa_nid != kex->hostkey_nid)) {
167                 r = SSH_ERR_KEY_TYPE_MISMATCH;
168                 goto out;
169         }
170         if (kex->verify_host_key(server_host_key, ssh) == -1) {
171                 r = SSH_ERR_SIGNATURE_INVALID;
172                 goto out;
173         }
174         /* DH parameter f, server public DH key */
175         if ((dh_server_pub = BN_new()) == NULL) {
176                 r = SSH_ERR_ALLOC_FAIL;
177                 goto out;
178         }
179         /* signed H */
180         if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
181             (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
182             (r = sshpkt_get_end(ssh)) != 0)
183                 goto out;
184 #ifdef DEBUG_KEXDH
185         fprintf(stderr, "dh_server_pub= ");
186         BN_print_fp(stderr, dh_server_pub);
187         fprintf(stderr, "\n");
188         debug("bits %d", BN_num_bits(dh_server_pub));
189 #endif
190         if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
191                 sshpkt_disconnect(ssh, "bad server public DH value");
192                 r = SSH_ERR_MESSAGE_INCOMPLETE;
193                 goto out;
194         }
195
196         klen = DH_size(kex->dh);
197         if ((kbuf = malloc(klen)) == NULL ||
198             (shared_secret = BN_new()) == NULL) {
199                 r = SSH_ERR_ALLOC_FAIL;
200                 goto out;
201         }
202         if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
203             BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
204                 r = SSH_ERR_LIBCRYPTO_ERROR;
205                 goto out;
206         }
207 #ifdef DEBUG_KEXDH
208         dump_digest("shared secret", kbuf, kout);
209 #endif
210         if (ssh->compat & SSH_OLD_DHGEX)
211                 kex->min = kex->max = -1;
212
213         /* calc and verify H */
214         hashlen = sizeof(hash);
215         if ((r = kexgex_hash(
216             kex->hash_alg,
217             kex->client_version_string,
218             kex->server_version_string,
219             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
220             sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
221             server_host_key_blob, sbloblen,
222             kex->min, kex->nbits, kex->max,
223             kex->dh->p, kex->dh->g,
224             kex->dh->pub_key,
225             dh_server_pub,
226             shared_secret,
227             hash, &hashlen)) != 0)
228                 goto out;
229
230         if ((r = sshkey_verify(server_host_key, signature, slen, hash,
231             hashlen, kex->hostkey_alg, ssh->compat)) != 0)
232                 goto out;
233
234         /* save session id */
235         if (kex->session_id == NULL) {
236                 kex->session_id_len = hashlen;
237                 kex->session_id = malloc(kex->session_id_len);
238                 if (kex->session_id == NULL) {
239                         r = SSH_ERR_ALLOC_FAIL;
240                         goto out;
241                 }
242                 memcpy(kex->session_id, hash, kex->session_id_len);
243         }
244
245         if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
246                 r = kex_send_newkeys(ssh);
247  out:
248         explicit_bzero(hash, sizeof(hash));
249         DH_free(kex->dh);
250         kex->dh = NULL;
251         BN_clear_free(dh_server_pub);
252         if (kbuf) {
253                 explicit_bzero(kbuf, klen);
254                 free(kbuf);
255         }
256         BN_clear_free(shared_secret);
257         sshkey_free(server_host_key);
258         free(server_host_key_blob);
259         free(signature);
260         return r;
261 }
262 #endif /* WITH_OPENSSL */