]>
Commit | Line | Data |
---|---|---|
cf851879 | 1 | /* $OpenBSD: kexgexs.c,v 1.7 2006/08/01 23:22:47 stevesk Exp $ */ |
98a58eda | 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" | |
98a58eda | 28 | |
536c14e8 | 29 | #include <sys/param.h> |
30 | ||
cf851879 | 31 | #include <stdio.h> |
00146caa | 32 | #include <string.h> |
33 | ||
98a58eda | 34 | #include "xmalloc.h" |
35 | #include "key.h" | |
36 | #include "kex.h" | |
37 | #include "log.h" | |
38 | #include "packet.h" | |
39 | #include "dh.h" | |
40 | #include "ssh2.h" | |
41 | #include "compat.h" | |
42 | #include "monitor_wrap.h" | |
43 | ||
44 | void | |
45 | kexgex_server(Kex *kex) | |
46 | { | |
47 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | |
48 | Key *server_host_key; | |
49 | DH *dh; | |
50 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | |
47e5dc72 | 51 | u_int sbloblen, klen, kout, slen, hashlen; |
98a58eda | 52 | int min = -1, max = -1, nbits = -1, type; |
53 | ||
54 | if (kex->load_host_key == NULL) | |
55 | fatal("Cannot load hostkey"); | |
56 | server_host_key = kex->load_host_key(kex->hostkey_type); | |
57 | if (server_host_key == NULL) | |
58 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | |
59 | ||
60 | type = packet_read(); | |
61 | switch (type) { | |
62 | case SSH2_MSG_KEX_DH_GEX_REQUEST: | |
63 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); | |
64 | min = packet_get_int(); | |
65 | nbits = packet_get_int(); | |
66 | max = packet_get_int(); | |
67 | min = MAX(DH_GRP_MIN, min); | |
68 | max = MIN(DH_GRP_MAX, max); | |
69 | break; | |
70 | case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD: | |
71 | debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received"); | |
72 | nbits = packet_get_int(); | |
73 | min = DH_GRP_MIN; | |
74 | max = DH_GRP_MAX; | |
75 | /* unused for old GEX */ | |
76 | break; | |
77 | default: | |
78 | fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type); | |
79 | } | |
80 | packet_check_eom(); | |
81 | ||
82 | if (max < min || nbits < min || max < nbits) | |
83 | fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d", | |
84 | min, nbits, max); | |
85 | ||
86 | /* Contact privileged parent */ | |
87 | dh = PRIVSEP(choose_dh(min, nbits, max)); | |
88 | if (dh == NULL) | |
89 | packet_disconnect("Protocol error: no matching DH grp found"); | |
90 | ||
91 | debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); | |
92 | packet_start(SSH2_MSG_KEX_DH_GEX_GROUP); | |
93 | packet_put_bignum2(dh->p); | |
94 | packet_put_bignum2(dh->g); | |
95 | packet_send(); | |
96 | ||
97 | /* flush */ | |
98 | packet_write_wait(); | |
99 | ||
100 | /* Compute our exchange value in parallel with the client */ | |
101 | dh_gen_key(dh, kex->we_need * 8); | |
102 | ||
103 | debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); | |
104 | packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT); | |
105 | ||
106 | /* key, cert */ | |
107 | if ((dh_client_pub = BN_new()) == NULL) | |
108 | fatal("dh_client_pub == NULL"); | |
109 | packet_get_bignum2(dh_client_pub); | |
110 | packet_check_eom(); | |
111 | ||
112 | #ifdef DEBUG_KEXDH | |
113 | fprintf(stderr, "dh_client_pub= "); | |
114 | BN_print_fp(stderr, dh_client_pub); | |
115 | fprintf(stderr, "\n"); | |
116 | debug("bits %d", BN_num_bits(dh_client_pub)); | |
117 | #endif | |
118 | ||
119 | #ifdef DEBUG_KEXDH | |
120 | DHparams_print_fp(stderr, dh); | |
121 | fprintf(stderr, "pub= "); | |
122 | BN_print_fp(stderr, dh->pub_key); | |
123 | fprintf(stderr, "\n"); | |
124 | #endif | |
125 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
126 | packet_disconnect("bad client public DH value"); | |
127 | ||
128 | klen = DH_size(dh); | |
129 | kbuf = xmalloc(klen); | |
130 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
131 | #ifdef DEBUG_KEXDH | |
132 | dump_digest("shared secret", kbuf, kout); | |
133 | #endif | |
134 | if ((shared_secret = BN_new()) == NULL) | |
135 | fatal("kexgex_server: BN_new failed"); | |
136 | BN_bin2bn(kbuf, kout, shared_secret); | |
137 | memset(kbuf, 0, klen); | |
138 | xfree(kbuf); | |
139 | ||
140 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); | |
141 | ||
142 | if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) | |
143 | min = max = -1; | |
144 | ||
47e5dc72 | 145 | /* calc H */ |
146 | kexgex_hash( | |
147 | kex->evp_md, | |
98a58eda | 148 | kex->client_version_string, |
149 | kex->server_version_string, | |
150 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
151 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
152 | server_host_key_blob, sbloblen, | |
153 | min, nbits, max, | |
154 | dh->p, dh->g, | |
155 | dh_client_pub, | |
156 | dh->pub_key, | |
47e5dc72 | 157 | shared_secret, |
158 | &hash, &hashlen | |
98a58eda | 159 | ); |
160 | BN_clear_free(dh_client_pub); | |
161 | ||
162 | /* save session id := H */ | |
98a58eda | 163 | if (kex->session_id == NULL) { |
47e5dc72 | 164 | kex->session_id_len = hashlen; |
98a58eda | 165 | kex->session_id = xmalloc(kex->session_id_len); |
166 | memcpy(kex->session_id, hash, kex->session_id_len); | |
167 | } | |
168 | ||
169 | /* sign H */ | |
47e5dc72 | 170 | PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen)); |
98a58eda | 171 | |
172 | /* destroy_sensitive_data(); */ | |
173 | ||
174 | /* send server hostkey, DH pubkey 'f' and singed H */ | |
175 | debug("SSH2_MSG_KEX_DH_GEX_REPLY sent"); | |
176 | packet_start(SSH2_MSG_KEX_DH_GEX_REPLY); | |
177 | packet_put_string(server_host_key_blob, sbloblen); | |
178 | packet_put_bignum2(dh->pub_key); /* f */ | |
179 | packet_put_string(signature, slen); | |
180 | packet_send(); | |
181 | ||
182 | xfree(signature); | |
183 | xfree(server_host_key_blob); | |
184 | /* have keys, free DH */ | |
185 | DH_free(dh); | |
186 | ||
47e5dc72 | 187 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
98a58eda | 188 | BN_clear_free(shared_secret); |
189 | ||
190 | kex_finish(kex); | |
191 | } |