blob: ab90a9da12780dc30d2c7721668a715d0b034918 [file] [log] [blame]
Damien Millera5103f42014-02-04 11:20:14 +11001/* $OpenBSD: kexgexs.c,v 1.19 2014/02/02 03:44:31 djm Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
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"
Damien Miller8e7fb332003-02-24 12:03:03 +110028
Damien Miller72ef7c12015-01-15 02:21:31 +110029#ifdef WITH_OPENSSL
30
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
32
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100036#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037
Damien Miller4499f4c2010-11-20 15:15:49 +110038#include <openssl/dh.h>
39
Damien Miller8e7fb332003-02-24 12:03:03 +110040#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110042#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "cipher.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110044#include "kex.h"
45#include "log.h"
46#include "packet.h"
47#include "dh.h"
48#include "ssh2.h"
49#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#ifdef GSSAPI
51#include "ssh-gss.h"
52#endif
Damien Miller8e7fb332003-02-24 12:03:03 +110053#include "monitor_wrap.h"
54
55void
56kexgex_server(Kex *kex)
57{
58 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +110059 Key *server_host_public, *server_host_private;
Damien Miller8e7fb332003-02-24 12:03:03 +110060 DH *dh;
61 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
Damien Miller570c2ab2006-11-05 05:32:02 +110062 u_int sbloblen, klen, slen, hashlen;
Damien Millerccf7e222009-01-28 16:23:06 +110063 int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
64 int type, kout;
Damien Miller8e7fb332003-02-24 12:03:03 +110065
Damien Miller0a80ca12010-02-27 07:55:05 +110066 if (kex->load_host_public_key == NULL ||
67 kex->load_host_private_key == NULL)
Damien Miller8e7fb332003-02-24 12:03:03 +110068 fatal("Cannot load hostkey");
Damien Miller0a80ca12010-02-27 07:55:05 +110069 server_host_public = kex->load_host_public_key(kex->hostkey_type);
70 if (server_host_public == NULL)
Damien Miller8e7fb332003-02-24 12:03:03 +110071 fatal("Unsupported hostkey type %d", kex->hostkey_type);
Damien Miller0a80ca12010-02-27 07:55:05 +110072 server_host_private = kex->load_host_private_key(kex->hostkey_type);
Damien Miller8e7fb332003-02-24 12:03:03 +110073
74 type = packet_read();
75 switch (type) {
76 case SSH2_MSG_KEX_DH_GEX_REQUEST:
77 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
Damien Millerccf7e222009-01-28 16:23:06 +110078 omin = min = packet_get_int();
79 onbits = nbits = packet_get_int();
80 omax = max = packet_get_int();
Damien Miller8e7fb332003-02-24 12:03:03 +110081 min = MAX(DH_GRP_MIN, min);
82 max = MIN(DH_GRP_MAX, max);
Damien Millerccf7e222009-01-28 16:23:06 +110083 nbits = MAX(DH_GRP_MIN, nbits);
84 nbits = MIN(DH_GRP_MAX, nbits);
Damien Miller8e7fb332003-02-24 12:03:03 +110085 break;
86 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
87 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
Damien Millerccf7e222009-01-28 16:23:06 +110088 onbits = nbits = packet_get_int();
Damien Miller8e7fb332003-02-24 12:03:03 +110089 /* unused for old GEX */
Damien Millerccf7e222009-01-28 16:23:06 +110090 omin = min = DH_GRP_MIN;
91 omax = max = DH_GRP_MAX;
Damien Miller8e7fb332003-02-24 12:03:03 +110092 break;
93 default:
94 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
95 }
96 packet_check_eom();
97
Damien Millerccf7e222009-01-28 16:23:06 +110098 if (omax < omin || onbits < omin || omax < onbits)
Damien Miller8e7fb332003-02-24 12:03:03 +110099 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
Damien Millerccf7e222009-01-28 16:23:06 +1100100 omin, onbits, omax);
Damien Miller8e7fb332003-02-24 12:03:03 +1100101
102 /* Contact privileged parent */
103 dh = PRIVSEP(choose_dh(min, nbits, max));
104 if (dh == NULL)
105 packet_disconnect("Protocol error: no matching DH grp found");
106
107 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
108 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
109 packet_put_bignum2(dh->p);
110 packet_put_bignum2(dh->g);
111 packet_send();
112
113 /* flush */
114 packet_write_wait();
115
116 /* Compute our exchange value in parallel with the client */
117 dh_gen_key(dh, kex->we_need * 8);
118
119 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
120 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
121
122 /* key, cert */
123 if ((dh_client_pub = BN_new()) == NULL)
124 fatal("dh_client_pub == NULL");
125 packet_get_bignum2(dh_client_pub);
126 packet_check_eom();
127
128#ifdef DEBUG_KEXDH
129 fprintf(stderr, "dh_client_pub= ");
130 BN_print_fp(stderr, dh_client_pub);
131 fprintf(stderr, "\n");
132 debug("bits %d", BN_num_bits(dh_client_pub));
133#endif
134
135#ifdef DEBUG_KEXDH
136 DHparams_print_fp(stderr, dh);
137 fprintf(stderr, "pub= ");
138 BN_print_fp(stderr, dh->pub_key);
139 fprintf(stderr, "\n");
140#endif
141 if (!dh_pub_is_valid(dh, dh_client_pub))
142 packet_disconnect("bad client public DH value");
143
144 klen = DH_size(dh);
145 kbuf = xmalloc(klen);
Damien Miller570c2ab2006-11-05 05:32:02 +1100146 if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
147 fatal("DH_compute_key: failed");
Damien Miller8e7fb332003-02-24 12:03:03 +1100148#ifdef DEBUG_KEXDH
149 dump_digest("shared secret", kbuf, kout);
150#endif
151 if ((shared_secret = BN_new()) == NULL)
152 fatal("kexgex_server: BN_new failed");
Darren Tucker0bc85572006-11-07 23:14:41 +1100153 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
154 fatal("kexgex_server: BN_bin2bn failed");
Damien Millera5103f42014-02-04 11:20:14 +1100155 explicit_bzero(kbuf, klen);
Darren Tuckera627d422013-06-02 07:31:17 +1000156 free(kbuf);
Damien Miller8e7fb332003-02-24 12:03:03 +1100157
Damien Miller0a80ca12010-02-27 07:55:05 +1100158 key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
Damien Miller8e7fb332003-02-24 12:03:03 +1100159
160 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
Damien Millerccf7e222009-01-28 16:23:06 +1100161 omin = min = omax = max = -1;
Damien Miller8e7fb332003-02-24 12:03:03 +1100162
Damien Miller19bb3a52005-11-05 15:19:35 +1100163 /* calc H */
164 kexgex_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100165 kex->hash_alg,
Damien Miller8e7fb332003-02-24 12:03:03 +1100166 kex->client_version_string,
167 kex->server_version_string,
168 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
169 buffer_ptr(&kex->my), buffer_len(&kex->my),
170 server_host_key_blob, sbloblen,
Damien Millerccf7e222009-01-28 16:23:06 +1100171 omin, onbits, omax,
Damien Miller8e7fb332003-02-24 12:03:03 +1100172 dh->p, dh->g,
173 dh_client_pub,
174 dh->pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100175 shared_secret,
176 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100177 );
178 BN_clear_free(dh_client_pub);
179
180 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100181 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100182 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100183 kex->session_id = xmalloc(kex->session_id_len);
184 memcpy(kex->session_id, hash, kex->session_id_len);
185 }
186
187 /* sign H */
Damien Miller85b45e02013-07-20 13:21:52 +1000188 kex->sign(server_host_private, server_host_public, &signature, &slen,
189 hash, hashlen);
Damien Miller8e7fb332003-02-24 12:03:03 +1100190
191 /* destroy_sensitive_data(); */
192
193 /* send server hostkey, DH pubkey 'f' and singed H */
194 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
195 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
196 packet_put_string(server_host_key_blob, sbloblen);
197 packet_put_bignum2(dh->pub_key); /* f */
198 packet_put_string(signature, slen);
199 packet_send();
200
Darren Tuckera627d422013-06-02 07:31:17 +1000201 free(signature);
202 free(server_host_key_blob);
Damien Miller8e7fb332003-02-24 12:03:03 +1100203 /* have keys, free DH */
204 DH_free(dh);
205
Damien Miller91b580e2014-01-12 19:21:22 +1100206 kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 BN_clear_free(shared_secret);
208
209 kex_finish(kex);
210}
Damien Miller72ef7c12015-01-15 02:21:31 +1100211#endif /* WITH_OPENSSL */