2 * Copyright (c) 2000 Niels Provos. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
36 #include "monitor_wrap.h"
39 kexgex_server(Kex *kex)
41 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
44 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
45 u_int sbloblen, klen, kout, slen, hashlen;
46 int min = -1, max = -1, nbits = -1, type;
48 if (kex->load_host_key == NULL)
49 fatal("Cannot load hostkey");
50 server_host_key = kex->load_host_key(kex->hostkey_type);
51 if (server_host_key == NULL)
52 fatal("Unsupported hostkey type %d", kex->hostkey_type);
56 case SSH2_MSG_KEX_DH_GEX_REQUEST:
57 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
58 min = packet_get_int();
59 nbits = packet_get_int();
60 max = packet_get_int();
61 min = MAX(DH_GRP_MIN, min);
62 max = MIN(DH_GRP_MAX, max);
64 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
65 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
66 nbits = packet_get_int();
69 /* unused for old GEX */
72 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
76 if (max < min || nbits < min || max < nbits)
77 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
80 /* Contact privileged parent */
81 dh = PRIVSEP(choose_dh(min, nbits, max));
83 packet_disconnect("Protocol error: no matching DH grp found");
85 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
86 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
87 packet_put_bignum2(dh->p);
88 packet_put_bignum2(dh->g);
94 /* Compute our exchange value in parallel with the client */
95 dh_gen_key(dh, kex->we_need * 8);
97 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
98 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
101 if ((dh_client_pub = BN_new()) == NULL)
102 fatal("dh_client_pub == NULL");
103 packet_get_bignum2(dh_client_pub);
107 fprintf(stderr, "dh_client_pub= ");
108 BN_print_fp(stderr, dh_client_pub);
109 fprintf(stderr, "\n");
110 debug("bits %d", BN_num_bits(dh_client_pub));
114 DHparams_print_fp(stderr, dh);
115 fprintf(stderr, "pub= ");
116 BN_print_fp(stderr, dh->pub_key);
117 fprintf(stderr, "\n");
119 if (!dh_pub_is_valid(dh, dh_client_pub))
120 packet_disconnect("bad client public DH value");
123 kbuf = xmalloc(klen);
124 kout = DH_compute_key(kbuf, dh_client_pub, dh);
126 dump_digest("shared secret", kbuf, kout);
128 if ((shared_secret = BN_new()) == NULL)
129 fatal("kexgex_server: BN_new failed");
130 BN_bin2bn(kbuf, kout, shared_secret);
131 memset(kbuf, 0, klen);
134 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
136 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
142 kex->client_version_string,
143 kex->server_version_string,
144 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
145 buffer_ptr(&kex->my), buffer_len(&kex->my),
146 server_host_key_blob, sbloblen,
154 BN_clear_free(dh_client_pub);
156 /* save session id := H */
157 if (kex->session_id == NULL) {
158 kex->session_id_len = hashlen;
159 kex->session_id = xmalloc(kex->session_id_len);
160 memcpy(kex->session_id, hash, kex->session_id_len);
164 PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
166 /* destroy_sensitive_data(); */
168 /* send server hostkey, DH pubkey 'f' and singed H */
169 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
170 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
171 packet_put_string(server_host_key_blob, sbloblen);
172 packet_put_bignum2(dh->pub_key); /* f */
173 packet_put_string(signature, slen);
177 xfree(server_host_key_blob);
178 /* have keys, free DH */
181 kex_derive_keys(kex, hash, hashlen, shared_secret);
182 BN_clear_free(shared_secret);