diff options
Diffstat (limited to 'kexgsss.c')
-rw-r--r-- | kexgsss.c | 462 |
1 files changed, 318 insertions, 144 deletions
@@ -24,7 +24,7 @@ | |||
24 | 24 | ||
25 | #include "includes.h" | 25 | #include "includes.h" |
26 | 26 | ||
27 | #ifdef GSSAPI | 27 | #if defined(GSSAPI) && defined(WITH_OPENSSL) |
28 | 28 | ||
29 | #include <string.h> | 29 | #include <string.h> |
30 | 30 | ||
@@ -42,40 +42,41 @@ | |||
42 | #include "dh.h" | 42 | #include "dh.h" |
43 | #include "ssh-gss.h" | 43 | #include "ssh-gss.h" |
44 | #include "monitor_wrap.h" | 44 | #include "monitor_wrap.h" |
45 | #include "misc.h" | 45 | #include "misc.h" /* servconf.h needs misc.h for struct ForwardOptions */ |
46 | #include "servconf.h" | 46 | #include "servconf.h" |
47 | #include "ssh-gss.h" | ||
47 | #include "digest.h" | 48 | #include "digest.h" |
49 | #include "ssherr.h" | ||
48 | 50 | ||
49 | extern ServerOptions options; | 51 | extern ServerOptions options; |
50 | 52 | ||
51 | int | 53 | int |
52 | kexgss_server(struct ssh *ssh) | 54 | kexgss_server(struct ssh *ssh) |
53 | { | 55 | { |
56 | struct kex *kex = ssh->kex; | ||
54 | OM_uint32 maj_status, min_status; | 57 | OM_uint32 maj_status, min_status; |
55 | 58 | ||
56 | /* | 59 | /* |
57 | * Some GSSAPI implementations use the input value of ret_flags (an | 60 | * Some GSSAPI implementations use the input value of ret_flags (an |
58 | * output variable) as a means of triggering mechanism specific | 61 | * output variable) as a means of triggering mechanism specific |
59 | * features. Initializing it to zero avoids inadvertently | 62 | * features. Initializing it to zero avoids inadvertently |
60 | * activating this non-standard behaviour. | 63 | * activating this non-standard behaviour. |
61 | */ | 64 | */ |
62 | 65 | ||
63 | OM_uint32 ret_flags = 0; | 66 | OM_uint32 ret_flags = 0; |
64 | gss_buffer_desc gssbuf, recv_tok, msg_tok; | 67 | gss_buffer_desc gssbuf, recv_tok, msg_tok; |
65 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | 68 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
66 | Gssctxt *ctxt = NULL; | 69 | Gssctxt *ctxt = NULL; |
67 | u_int slen, klen, kout; | 70 | struct sshbuf *shared_secret = NULL; |
68 | u_char *kbuf; | 71 | struct sshbuf *client_pubkey = NULL; |
69 | DH *dh; | 72 | struct sshbuf *server_pubkey = NULL; |
70 | int min = -1, max = -1, nbits = -1; | 73 | struct sshbuf *empty = sshbuf_new(); |
71 | const BIGNUM *pub_key, *dh_p, *dh_g; | ||
72 | BIGNUM *shared_secret = NULL; | ||
73 | BIGNUM *dh_client_pub = NULL; | ||
74 | int type = 0; | 74 | int type = 0; |
75 | gss_OID oid; | 75 | gss_OID oid; |
76 | char *mechs; | 76 | char *mechs; |
77 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | 77 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
78 | size_t hashlen; | 78 | size_t hashlen; |
79 | int r; | ||
79 | 80 | ||
80 | /* Initialise GSSAPI */ | 81 | /* Initialise GSSAPI */ |
81 | 82 | ||
@@ -88,8 +89,8 @@ kexgss_server(struct ssh *ssh) | |||
88 | free(mechs); | 89 | free(mechs); |
89 | } | 90 | } |
90 | 91 | ||
91 | debug2("%s: Identifying %s", __func__, ssh->kex->name); | 92 | debug2("%s: Identifying %s", __func__, kex->name); |
92 | oid = ssh_gssapi_id_kex(NULL, ssh->kex->name, ssh->kex->kex_type); | 93 | oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type); |
93 | if (oid == GSS_C_NO_OID) | 94 | if (oid == GSS_C_NO_OID) |
94 | fatal("Unknown gssapi mechanism"); | 95 | fatal("Unknown gssapi mechanism"); |
95 | 96 | ||
@@ -98,94 +99,82 @@ kexgss_server(struct ssh *ssh) | |||
98 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) | 99 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) |
99 | fatal("Unable to acquire credentials for the server"); | 100 | fatal("Unable to acquire credentials for the server"); |
100 | 101 | ||
101 | switch (ssh->kex->kex_type) { | ||
102 | case KEX_GSS_GRP1_SHA1: | ||
103 | dh = dh_new_group1(); | ||
104 | break; | ||
105 | case KEX_GSS_GRP14_SHA1: | ||
106 | dh = dh_new_group14(); | ||
107 | break; | ||
108 | case KEX_GSS_GEX_SHA1: | ||
109 | debug("Doing group exchange"); | ||
110 | packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ); | ||
111 | min = packet_get_int(); | ||
112 | nbits = packet_get_int(); | ||
113 | max = packet_get_int(); | ||
114 | packet_check_eom(); | ||
115 | if (max < min || nbits < min || max < nbits) | ||
116 | fatal("GSS_GEX, bad parameters: %d !< %d !< %d", | ||
117 | min, nbits, max); | ||
118 | dh = PRIVSEP(choose_dh(MAX(DH_GRP_MIN, min), | ||
119 | nbits, MIN(DH_GRP_MAX, max))); | ||
120 | if (dh == NULL) | ||
121 | packet_disconnect("Protocol error: no matching group found"); | ||
122 | DH_get0_pqg(dh, &dh_p, NULL, &dh_g); | ||
123 | |||
124 | packet_start(SSH2_MSG_KEXGSS_GROUP); | ||
125 | packet_put_bignum2(dh_p); | ||
126 | packet_put_bignum2(dh_g); | ||
127 | packet_send(); | ||
128 | |||
129 | packet_write_wait(); | ||
130 | break; | ||
131 | default: | ||
132 | fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type); | ||
133 | } | ||
134 | |||
135 | dh_gen_key(dh, ssh->kex->we_need * 8); | ||
136 | |||
137 | do { | 102 | do { |
138 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | 103 | debug("Wait SSH2_MSG_KEXGSS_INIT"); |
139 | type = packet_read(); | 104 | type = ssh_packet_read(ssh); |
140 | switch(type) { | 105 | switch(type) { |
141 | case SSH2_MSG_KEXGSS_INIT: | 106 | case SSH2_MSG_KEXGSS_INIT: |
142 | if (dh_client_pub != NULL) | 107 | if (client_pubkey != NULL) |
143 | fatal("Received KEXGSS_INIT after initialising"); | 108 | fatal("Received KEXGSS_INIT after initialising"); |
144 | recv_tok.value = packet_get_string(&slen); | 109 | if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh, |
145 | recv_tok.length = slen; | 110 | &recv_tok)) != 0 || |
146 | 111 | (r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 || | |
147 | if ((dh_client_pub = BN_new()) == NULL) | 112 | (r = sshpkt_get_end(ssh)) != 0) |
148 | fatal("dh_client_pub == NULL"); | 113 | fatal("sshpkt failed: %s", ssh_err(r)); |
149 | 114 | ||
150 | packet_get_bignum2(dh_client_pub); | 115 | switch (kex->kex_type) { |
116 | case KEX_GSS_GRP1_SHA1: | ||
117 | case KEX_GSS_GRP14_SHA1: | ||
118 | case KEX_GSS_GRP14_SHA256: | ||
119 | case KEX_GSS_GRP16_SHA512: | ||
120 | r = kex_dh_enc(kex, client_pubkey, &server_pubkey, | ||
121 | &shared_secret); | ||
122 | break; | ||
123 | case KEX_GSS_NISTP256_SHA256: | ||
124 | r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey, | ||
125 | &shared_secret); | ||
126 | break; | ||
127 | case KEX_GSS_C25519_SHA256: | ||
128 | r = kex_c25519_enc(kex, client_pubkey, &server_pubkey, | ||
129 | &shared_secret); | ||
130 | break; | ||
131 | default: | ||
132 | fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); | ||
133 | } | ||
134 | if (r != 0) | ||
135 | goto out; | ||
151 | 136 | ||
152 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ | 137 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ |
153 | break; | 138 | break; |
154 | case SSH2_MSG_KEXGSS_CONTINUE: | 139 | case SSH2_MSG_KEXGSS_CONTINUE: |
155 | recv_tok.value = packet_get_string(&slen); | 140 | if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh, |
156 | recv_tok.length = slen; | 141 | &recv_tok)) != 0 || |
142 | (r = sshpkt_get_end(ssh)) != 0) | ||
143 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
157 | break; | 144 | break; |
158 | default: | 145 | default: |
159 | packet_disconnect( | 146 | sshpkt_disconnect(ssh, |
160 | "Protocol error: didn't expect packet type %d", | 147 | "Protocol error: didn't expect packet type %d", |
161 | type); | 148 | type); |
162 | } | 149 | } |
163 | 150 | ||
164 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, | 151 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, |
165 | &send_tok, &ret_flags)); | 152 | &send_tok, &ret_flags)); |
166 | 153 | ||
167 | free(recv_tok.value); | 154 | gss_release_buffer(&min_status, &recv_tok); |
168 | 155 | ||
169 | if (maj_status != GSS_S_COMPLETE && send_tok.length == 0) | 156 | if (maj_status != GSS_S_COMPLETE && send_tok.length == 0) |
170 | fatal("Zero length token output when incomplete"); | 157 | fatal("Zero length token output when incomplete"); |
171 | 158 | ||
172 | if (dh_client_pub == NULL) | 159 | if (client_pubkey == NULL) |
173 | fatal("No client public key"); | 160 | fatal("No client public key"); |
174 | 161 | ||
175 | if (maj_status & GSS_S_CONTINUE_NEEDED) { | 162 | if (maj_status & GSS_S_CONTINUE_NEEDED) { |
176 | debug("Sending GSSAPI_CONTINUE"); | 163 | debug("Sending GSSAPI_CONTINUE"); |
177 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | 164 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 || |
178 | packet_put_string(send_tok.value, send_tok.length); | 165 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 || |
179 | packet_send(); | 166 | (r = sshpkt_send(ssh)) != 0) |
167 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
180 | gss_release_buffer(&min_status, &send_tok); | 168 | gss_release_buffer(&min_status, &send_tok); |
181 | } | 169 | } |
182 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | 170 | } while (maj_status & GSS_S_CONTINUE_NEEDED); |
183 | 171 | ||
184 | if (GSS_ERROR(maj_status)) { | 172 | if (GSS_ERROR(maj_status)) { |
185 | if (send_tok.length > 0) { | 173 | if (send_tok.length > 0) { |
186 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | 174 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 || |
187 | packet_put_string(send_tok.value, send_tok.length); | 175 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 || |
188 | packet_send(); | 176 | (r = sshpkt_send(ssh)) != 0) |
177 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
189 | } | 178 | } |
190 | fatal("accept_ctx died"); | 179 | fatal("accept_ctx died"); |
191 | } | 180 | } |
@@ -195,106 +184,291 @@ kexgss_server(struct ssh *ssh) | |||
195 | 184 | ||
196 | if (!(ret_flags & GSS_C_INTEG_FLAG)) | 185 | if (!(ret_flags & GSS_C_INTEG_FLAG)) |
197 | fatal("Integrity flag wasn't set"); | 186 | fatal("Integrity flag wasn't set"); |
198 | |||
199 | if (!dh_pub_is_valid(dh, dh_client_pub)) | ||
200 | packet_disconnect("bad client public DH value"); | ||
201 | 187 | ||
202 | klen = DH_size(dh); | 188 | hashlen = sizeof(hash); |
203 | kbuf = xmalloc(klen); | 189 | if ((r = kex_gen_hash( |
204 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | 190 | kex->hash_alg, |
205 | if (kout < 0) | 191 | kex->client_version, |
206 | fatal("DH_compute_key: failed"); | 192 | kex->server_version, |
193 | kex->peer, | ||
194 | kex->my, | ||
195 | empty, | ||
196 | client_pubkey, | ||
197 | server_pubkey, | ||
198 | shared_secret, | ||
199 | hash, &hashlen)) != 0) | ||
200 | goto out; | ||
201 | |||
202 | gssbuf.value = hash; | ||
203 | gssbuf.length = hashlen; | ||
207 | 204 | ||
208 | shared_secret = BN_new(); | 205 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok)))) |
209 | if (shared_secret == NULL) | 206 | fatal("Couldn't get MIC"); |
210 | fatal("kexgss_server: BN_new failed"); | ||
211 | 207 | ||
212 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) | 208 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 || |
213 | fatal("kexgss_server: BN_bin2bn failed"); | 209 | (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 || |
210 | (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0) | ||
211 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
214 | 212 | ||
215 | memset(kbuf, 0, klen); | 213 | if (send_tok.length != 0) { |
216 | free(kbuf); | 214 | if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */ |
215 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0) | ||
216 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
217 | } else { | ||
218 | if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */ | ||
219 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
220 | } | ||
221 | if ((r = sshpkt_send(ssh)) != 0) | ||
222 | fatal("sshpkt_send failed: %s", ssh_err(r)); | ||
217 | 223 | ||
218 | DH_get0_key(dh, &pub_key, NULL); | 224 | gss_release_buffer(&min_status, &send_tok); |
219 | DH_get0_pqg(dh, &dh_p, NULL, &dh_g); | 225 | gss_release_buffer(&min_status, &msg_tok); |
220 | 226 | ||
221 | hashlen = sizeof(hash); | 227 | if (gss_kex_context == NULL) |
222 | switch (ssh->kex->kex_type) { | 228 | gss_kex_context = ctxt; |
223 | case KEX_GSS_GRP1_SHA1: | 229 | else |
224 | case KEX_GSS_GRP14_SHA1: | 230 | ssh_gssapi_delete_ctx(&ctxt); |
225 | kex_dh_hash( | 231 | |
226 | ssh->kex->hash_alg, | 232 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) |
227 | ssh->kex->client_version_string, ssh->kex->server_version_string, | 233 | r = kex_send_newkeys(ssh); |
228 | sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer), | 234 | |
229 | sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my), | 235 | /* If this was a rekey, then save out any delegated credentials we |
230 | NULL, 0, /* Change this if we start sending host keys */ | 236 | * just exchanged. */ |
231 | dh_client_pub, pub_key, shared_secret, | 237 | if (options.gss_store_rekey) |
232 | hash, &hashlen | 238 | ssh_gssapi_rekey_creds(); |
233 | ); | 239 | out: |
234 | break; | 240 | sshbuf_free(empty); |
235 | case KEX_GSS_GEX_SHA1: | 241 | explicit_bzero(hash, sizeof(hash)); |
236 | kexgex_hash( | 242 | sshbuf_free(shared_secret); |
237 | ssh->kex->hash_alg, | 243 | sshbuf_free(client_pubkey); |
238 | ssh->kex->client_version_string, ssh->kex->server_version_string, | 244 | sshbuf_free(server_pubkey); |
239 | sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer), | 245 | return r; |
240 | sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my), | 246 | } |
241 | NULL, 0, | 247 | |
242 | min, nbits, max, | 248 | int |
243 | dh_p, dh_g, | 249 | kexgssgex_server(struct ssh *ssh) |
244 | dh_client_pub, | 250 | { |
245 | pub_key, | 251 | struct kex *kex = ssh->kex; |
246 | shared_secret, | 252 | OM_uint32 maj_status, min_status; |
247 | hash, &hashlen | 253 | |
248 | ); | 254 | /* |
249 | break; | 255 | * Some GSSAPI implementations use the input value of ret_flags (an |
250 | default: | 256 | * output variable) as a means of triggering mechanism specific |
251 | fatal("%s: Unexpected KEX type %d", __func__, ssh->kex->kex_type); | 257 | * features. Initializing it to zero avoids inadvertently |
258 | * activating this non-standard behaviour. | ||
259 | */ | ||
260 | |||
261 | OM_uint32 ret_flags = 0; | ||
262 | gss_buffer_desc gssbuf, recv_tok, msg_tok; | ||
263 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | ||
264 | Gssctxt *ctxt = NULL; | ||
265 | struct sshbuf *shared_secret = NULL; | ||
266 | int type = 0; | ||
267 | gss_OID oid; | ||
268 | char *mechs; | ||
269 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | ||
270 | size_t hashlen; | ||
271 | BIGNUM *dh_client_pub = NULL; | ||
272 | const BIGNUM *pub_key, *dh_p, *dh_g; | ||
273 | int min = -1, max = -1, nbits = -1; | ||
274 | int cmin = -1, cmax = -1; /* client proposal */ | ||
275 | struct sshbuf *empty = sshbuf_new(); | ||
276 | int r; | ||
277 | |||
278 | /* Initialise GSSAPI */ | ||
279 | |||
280 | /* If we're rekeying, privsep means that some of the private structures | ||
281 | * in the GSSAPI code are no longer available. This kludges them back | ||
282 | * into life | ||
283 | */ | ||
284 | if (!ssh_gssapi_oid_table_ok()) | ||
285 | if ((mechs = ssh_gssapi_server_mechanisms())) | ||
286 | free(mechs); | ||
287 | |||
288 | debug2("%s: Identifying %s", __func__, kex->name); | ||
289 | oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type); | ||
290 | if (oid == GSS_C_NO_OID) | ||
291 | fatal("Unknown gssapi mechanism"); | ||
292 | |||
293 | debug2("%s: Acquiring credentials", __func__); | ||
294 | |||
295 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) | ||
296 | fatal("Unable to acquire credentials for the server"); | ||
297 | |||
298 | /* 5. S generates an ephemeral key pair (do the allocations early) */ | ||
299 | debug("Doing group exchange"); | ||
300 | ssh_packet_read_expect(ssh, SSH2_MSG_KEXGSS_GROUPREQ); | ||
301 | /* store client proposal to provide valid signature */ | ||
302 | if ((r = sshpkt_get_u32(ssh, &cmin)) != 0 || | ||
303 | (r = sshpkt_get_u32(ssh, &nbits)) != 0 || | ||
304 | (r = sshpkt_get_u32(ssh, &cmax)) != 0 || | ||
305 | (r = sshpkt_get_end(ssh)) != 0) | ||
306 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
307 | kex->nbits = nbits; | ||
308 | kex->min = cmin; | ||
309 | kex->max = cmax; | ||
310 | min = MAX(DH_GRP_MIN, cmin); | ||
311 | max = MIN(DH_GRP_MAX, cmax); | ||
312 | nbits = MAXIMUM(DH_GRP_MIN, nbits); | ||
313 | nbits = MINIMUM(DH_GRP_MAX, nbits); | ||
314 | if (max < min || nbits < min || max < nbits) | ||
315 | fatal("GSS_GEX, bad parameters: %d !< %d !< %d", | ||
316 | min, nbits, max); | ||
317 | kex->dh = PRIVSEP(choose_dh(min, nbits, max)); | ||
318 | if (kex->dh == NULL) { | ||
319 | sshpkt_disconnect(ssh, "Protocol error: no matching group found"); | ||
320 | fatal("Protocol error: no matching group found"); | ||
252 | } | 321 | } |
253 | 322 | ||
254 | BN_clear_free(dh_client_pub); | 323 | DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); |
324 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_GROUP)) != 0 || | ||
325 | (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 || | ||
326 | (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 || | ||
327 | (r = sshpkt_send(ssh)) != 0) | ||
328 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
329 | |||
330 | if ((r = ssh_packet_write_wait(ssh)) != 0) | ||
331 | fatal("ssh_packet_write_wait: %s", ssh_err(r)); | ||
332 | |||
333 | /* Compute our exchange value in parallel with the client */ | ||
334 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) | ||
335 | goto out; | ||
336 | |||
337 | do { | ||
338 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | ||
339 | type = ssh_packet_read(ssh); | ||
340 | switch(type) { | ||
341 | case SSH2_MSG_KEXGSS_INIT: | ||
342 | if (dh_client_pub != NULL) | ||
343 | fatal("Received KEXGSS_INIT after initialising"); | ||
344 | if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh, | ||
345 | &recv_tok)) != 0 || | ||
346 | (r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 || | ||
347 | (r = sshpkt_get_end(ssh)) != 0) | ||
348 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
349 | |||
350 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ | ||
351 | break; | ||
352 | case SSH2_MSG_KEXGSS_CONTINUE: | ||
353 | if ((r = ssh_gssapi_sshpkt_get_buffer_desc(ssh, | ||
354 | &recv_tok)) != 0 || | ||
355 | (r = sshpkt_get_end(ssh)) != 0) | ||
356 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
357 | break; | ||
358 | default: | ||
359 | sshpkt_disconnect(ssh, | ||
360 | "Protocol error: didn't expect packet type %d", | ||
361 | type); | ||
362 | } | ||
363 | |||
364 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, | ||
365 | &send_tok, &ret_flags)); | ||
255 | 366 | ||
256 | if (ssh->kex->session_id == NULL) { | 367 | gss_release_buffer(&min_status, &recv_tok); |
257 | ssh->kex->session_id_len = hashlen; | 368 | |
258 | ssh->kex->session_id = xmalloc(ssh->kex->session_id_len); | 369 | if (maj_status != GSS_S_COMPLETE && send_tok.length == 0) |
259 | memcpy(ssh->kex->session_id, hash, ssh->kex->session_id_len); | 370 | fatal("Zero length token output when incomplete"); |
371 | |||
372 | if (dh_client_pub == NULL) | ||
373 | fatal("No client public key"); | ||
374 | |||
375 | if (maj_status & GSS_S_CONTINUE_NEEDED) { | ||
376 | debug("Sending GSSAPI_CONTINUE"); | ||
377 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 || | ||
378 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 || | ||
379 | (r = sshpkt_send(ssh)) != 0) | ||
380 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
381 | gss_release_buffer(&min_status, &send_tok); | ||
382 | } | ||
383 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | ||
384 | |||
385 | if (GSS_ERROR(maj_status)) { | ||
386 | if (send_tok.length > 0) { | ||
387 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_CONTINUE)) != 0 || | ||
388 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0 || | ||
389 | (r = sshpkt_send(ssh)) != 0) | ||
390 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
391 | } | ||
392 | fatal("accept_ctx died"); | ||
393 | } | ||
394 | |||
395 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) | ||
396 | fatal("Mutual Authentication flag wasn't set"); | ||
397 | |||
398 | if (!(ret_flags & GSS_C_INTEG_FLAG)) | ||
399 | fatal("Integrity flag wasn't set"); | ||
400 | |||
401 | /* calculate shared secret */ | ||
402 | if ((shared_secret = sshbuf_new()) == NULL) { | ||
403 | r = SSH_ERR_ALLOC_FAIL; | ||
404 | goto out; | ||
260 | } | 405 | } |
406 | if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0) | ||
407 | goto out; | ||
408 | |||
409 | DH_get0_key(kex->dh, &pub_key, NULL); | ||
410 | DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g); | ||
411 | hashlen = sizeof(hash); | ||
412 | if ((r = kexgex_hash( | ||
413 | kex->hash_alg, | ||
414 | kex->client_version, | ||
415 | kex->server_version, | ||
416 | kex->peer, | ||
417 | kex->my, | ||
418 | empty, | ||
419 | cmin, nbits, cmax, | ||
420 | dh_p, dh_g, | ||
421 | dh_client_pub, | ||
422 | pub_key, | ||
423 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), | ||
424 | hash, &hashlen)) != 0) | ||
425 | fatal("kexgex_hash failed: %s", ssh_err(r)); | ||
261 | 426 | ||
262 | gssbuf.value = hash; | 427 | gssbuf.value = hash; |
263 | gssbuf.length = hashlen; | 428 | gssbuf.length = hashlen; |
264 | 429 | ||
265 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) | 430 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt, &gssbuf, &msg_tok)))) |
266 | fatal("Couldn't get MIC"); | 431 | fatal("Couldn't get MIC"); |
267 | 432 | ||
268 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); | 433 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXGSS_COMPLETE)) != 0 || |
269 | packet_put_bignum2(pub_key); | 434 | (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || |
270 | packet_put_string(msg_tok.value,msg_tok.length); | 435 | (r = sshpkt_put_string(ssh, msg_tok.value, msg_tok.length)) != 0) |
436 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
271 | 437 | ||
272 | if (send_tok.length != 0) { | 438 | if (send_tok.length != 0) { |
273 | packet_put_char(1); /* true */ | 439 | if ((r = sshpkt_put_u8(ssh, 1)) != 0 || /* true */ |
274 | packet_put_string(send_tok.value, send_tok.length); | 440 | (r = sshpkt_put_string(ssh, send_tok.value, send_tok.length)) != 0) |
441 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
275 | } else { | 442 | } else { |
276 | packet_put_char(0); /* false */ | 443 | if ((r = sshpkt_put_u8(ssh, 0)) != 0) /* false */ |
444 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
277 | } | 445 | } |
278 | packet_send(); | 446 | if ((r = sshpkt_send(ssh)) != 0) |
447 | fatal("sshpkt failed: %s", ssh_err(r)); | ||
279 | 448 | ||
280 | gss_release_buffer(&min_status, &send_tok); | 449 | gss_release_buffer(&min_status, &send_tok); |
281 | gss_release_buffer(&min_status, &msg_tok); | 450 | gss_release_buffer(&min_status, &msg_tok); |
282 | 451 | ||
283 | if (gss_kex_context == NULL) | 452 | if (gss_kex_context == NULL) |
284 | gss_kex_context = ctxt; | 453 | gss_kex_context = ctxt; |
285 | else | 454 | else |
286 | ssh_gssapi_delete_ctx(&ctxt); | 455 | ssh_gssapi_delete_ctx(&ctxt); |
287 | 456 | ||
288 | DH_free(dh); | 457 | /* Finally derive the keys and send them */ |
289 | 458 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) | |
290 | kex_derive_keys_bn(ssh, hash, hashlen, shared_secret); | 459 | r = kex_send_newkeys(ssh); |
291 | BN_clear_free(shared_secret); | ||
292 | kex_send_newkeys(ssh); | ||
293 | 460 | ||
294 | /* If this was a rekey, then save out any delegated credentials we | 461 | /* If this was a rekey, then save out any delegated credentials we |
295 | * just exchanged. */ | 462 | * just exchanged. */ |
296 | if (options.gss_store_rekey) | 463 | if (options.gss_store_rekey) |
297 | ssh_gssapi_rekey_creds(); | 464 | ssh_gssapi_rekey_creds(); |
298 | return 0; | 465 | out: |
466 | sshbuf_free(empty); | ||
467 | explicit_bzero(hash, sizeof(hash)); | ||
468 | DH_free(kex->dh); | ||
469 | kex->dh = NULL; | ||
470 | BN_clear_free(dh_client_pub); | ||
471 | sshbuf_free(shared_secret); | ||
472 | return r; | ||
299 | } | 473 | } |
300 | #endif /* GSSAPI */ | 474 | #endif /* defined(GSSAPI) && defined(WITH_OPENSSL) */ |