]>
Commit | Line | Data |
---|---|---|
5598e598 | 1 | /* |
905081a4 | 2 | * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved. |
5598e598 | 3 | * |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
26 | ||
27 | #ifdef GSSAPI | |
28 | ||
29 | #include <openssl/crypto.h> | |
30 | #include <openssl/bn.h> | |
31 | ||
32 | #include "xmalloc.h" | |
33 | #include "buffer.h" | |
34 | #include "bufaux.h" | |
35 | #include "kex.h" | |
36 | #include "log.h" | |
37 | #include "packet.h" | |
38 | #include "dh.h" | |
39 | #include "ssh2.h" | |
40 | #include "ssh-gss.h" | |
41 | ||
42 | /* This is now the same as the DH hash ... */ | |
43 | ||
44 | u_char * | |
45 | kex_gssapi_hash( | |
46 | char *client_version_string, | |
47 | char *server_version_string, | |
48 | char *ckexinit, int ckexinitlen, | |
49 | char *skexinit, int skexinitlen, | |
50 | u_char *serverhostkeyblob, int sbloblen, | |
51 | BIGNUM *client_dh_pub, | |
52 | BIGNUM *server_dh_pub, | |
53 | BIGNUM *shared_secret) | |
54 | { | |
55 | Buffer b; | |
56 | static u_char digest[EVP_MAX_MD_SIZE]; | |
57 | EVP_MD *evp_md = EVP_sha1(); | |
58 | EVP_MD_CTX md; | |
59 | ||
60 | buffer_init(&b); | |
61 | buffer_put_string(&b, client_version_string, strlen(client_version_string)); | |
62 | buffer_put_string(&b, server_version_string, strlen(server_version_string)); | |
63 | ||
64 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ | |
65 | buffer_put_int(&b, ckexinitlen+1); | |
66 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | |
67 | buffer_append(&b, ckexinit, ckexinitlen); | |
68 | buffer_put_int(&b, skexinitlen+1); | |
69 | buffer_put_char(&b, SSH2_MSG_KEXINIT); | |
70 | buffer_append(&b, skexinit, skexinitlen); | |
71 | ||
72 | buffer_put_string(&b, serverhostkeyblob, sbloblen); | |
73 | buffer_put_bignum2(&b, client_dh_pub); | |
74 | buffer_put_bignum2(&b, server_dh_pub); | |
75 | buffer_put_bignum2(&b, shared_secret); | |
76 | ||
77 | #ifdef DEBUG_KEX | |
78 | buffer_dump(&b); | |
79 | #endif | |
80 | EVP_DigestInit(&md, evp_md); | |
81 | EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); | |
82 | EVP_DigestFinal(&md, digest, NULL); | |
83 | ||
84 | buffer_free(&b); | |
85 | ||
86 | #ifdef DEBUG_KEX | |
87 | dump_digest("hash", digest, evp_md->md_size); | |
88 | #endif | |
89 | return digest; | |
90 | } | |
91 | ||
92 | void | |
93 | kexgss_client(Kex *kex) | |
94 | { | |
95 | gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr; | |
96 | Gssctxt ctxt; | |
97 | OM_uint32 maj_status, min_status, ret_flags; | |
5598e598 | 98 | unsigned int klen, kout; |
99 | DH *dh; | |
100 | BIGNUM *dh_server_pub = 0; | |
101 | BIGNUM *shared_secret = 0; | |
102 | unsigned char *kbuf; | |
103 | unsigned char *hash; | |
104 | unsigned char *serverhostkey; | |
105 | int type = 0; | |
106 | int first = 1; | |
107 | int slen = 0; | |
108 | ||
109 | /* Initialise our GSSAPI world */ | |
110 | ssh_gssapi_build_ctx(&ctxt); | |
111 | if (ssh_gssapi_id_kex(&ctxt,kex->name)) { | |
112 | fatal("Couldn't identify host exchange"); | |
113 | } | |
114 | if (ssh_gssapi_import_name(&ctxt,kex->host)) { | |
115 | fatal("Couldn't import hostname "); | |
116 | } | |
117 | ||
118 | /* This code should match that in ssh_dh1_client */ | |
119 | ||
120 | /* Step 1 - e is dh->pub_key */ | |
121 | dh = dh_new_group1(); | |
122 | dh_gen_key(dh, kex->we_need * 8); | |
123 | ||
124 | /* This is f, we initialise it now to make life easier */ | |
125 | dh_server_pub = BN_new(); | |
126 | if (dh_server_pub == NULL) { | |
127 | fatal("dh_server_pub == NULL"); | |
128 | } | |
129 | ||
130 | token_ptr = GSS_C_NO_BUFFER; | |
131 | ||
132 | do { | |
133 | debug("Calling gss_init_sec_context"); | |
134 | ||
135 | maj_status=ssh_gssapi_init_ctx(&ctxt, | |
136 | kex->options.gss_deleg_creds, | |
137 | token_ptr,&send_tok, | |
138 | &ret_flags); | |
139 | ||
140 | if (GSS_ERROR(maj_status)) { | |
141 | fatal("gss_init_context failed"); | |
142 | } | |
143 | ||
144 | /* If we've got an old receive buffer get rid of it */ | |
145 | if (token_ptr != GSS_C_NO_BUFFER) | |
146 | (void) gss_release_buffer(&min_status, &recv_tok); | |
147 | ||
148 | ||
149 | if (maj_status == GSS_S_COMPLETE) { | |
150 | /* If mutual state flag is not true, kex fails */ | |
151 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) { | |
152 | fatal("Mutual authentication failed"); | |
153 | } | |
154 | /* If integ avail flag is not true kex fails */ | |
155 | if (!(ret_flags & GSS_C_INTEG_FLAG)) { | |
156 | fatal("Integrity check failed"); | |
157 | } | |
158 | } | |
159 | ||
160 | /* If we have data to send, then the last message that we | |
161 | * received cannot have been a 'complete'. */ | |
162 | if (send_tok.length !=0) { | |
163 | if (first) { | |
164 | packet_start(SSH2_MSG_KEXGSS_INIT); | |
165 | packet_put_string(send_tok.value, | |
166 | send_tok.length); | |
167 | packet_put_bignum2(dh->pub_key); | |
168 | first=0; | |
169 | } else { | |
170 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
171 | packet_put_string(send_tok.value, | |
172 | send_tok.length); | |
173 | } | |
174 | packet_send(); | |
175 | packet_write_wait(); | |
176 | ||
177 | ||
178 | /* If we've sent them data, they'd better be polite | |
179 | * and reply. */ | |
180 | ||
6d25b646 | 181 | type = packet_read(); |
5598e598 | 182 | switch (type) { |
183 | case SSH2_MSG_KEXGSS_HOSTKEY: | |
184 | debug("Received KEXGSS_HOSTKEY"); | |
185 | serverhostkey=packet_get_string(&slen); | |
186 | break; | |
187 | case SSH2_MSG_KEXGSS_CONTINUE: | |
188 | debug("Received GSSAPI_CONTINUE"); | |
189 | if (maj_status == GSS_S_COMPLETE) | |
190 | fatal("GSSAPI Continue received from server when complete"); | |
191 | recv_tok.value=packet_get_string(&recv_tok.length); | |
192 | break; | |
193 | case SSH2_MSG_KEXGSS_COMPLETE: | |
194 | debug("Received GSSAPI_COMPLETE"); | |
6d25b646 | 195 | packet_get_bignum2(dh_server_pub); |
5598e598 | 196 | msg_tok.value= |
197 | packet_get_string(&msg_tok.length); | |
198 | ||
199 | /* Is there a token included? */ | |
200 | if (packet_get_char()) { | |
201 | recv_tok.value= | |
202 | packet_get_string(&recv_tok.length); | |
203 | /* If we're already complete - protocol error */ | |
204 | if (maj_status == GSS_S_COMPLETE) | |
205 | packet_disconnect("Protocol error: received token when complete"); | |
206 | } else { | |
207 | /* No token included */ | |
208 | if (maj_status != GSS_S_COMPLETE) | |
209 | packet_disconnect("Protocol error: did not receive final token"); | |
210 | } | |
211 | break; | |
212 | default: | |
213 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
214 | type); | |
215 | } | |
216 | token_ptr=&recv_tok; | |
217 | } | |
218 | ||
219 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
220 | ||
221 | /* We _must_ have received a COMPLETE message in reply from the | |
222 | * server, which will have set dh_server_pub and msg_tok */ | |
223 | ||
224 | if (type!=SSH2_MSG_KEXGSS_COMPLETE) | |
225 | fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it"); | |
226 | ||
227 | /* Check f in range [1, p-1] */ | |
228 | if (!dh_pub_is_valid(dh, dh_server_pub)) | |
229 | packet_disconnect("bad server public DH value"); | |
230 | ||
231 | /* compute K=f^x mod p */ | |
232 | klen = DH_size(dh); | |
233 | kbuf = xmalloc(klen); | |
234 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
235 | ||
236 | shared_secret = BN_new(); | |
237 | BN_bin2bn(kbuf,kout, shared_secret); | |
238 | memset(kbuf, 0, klen); | |
239 | xfree(kbuf); | |
240 | ||
241 | hash = kex_gssapi_hash( | |
242 | kex->client_version_string, | |
243 | kex->server_version_string, | |
244 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
245 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
246 | serverhostkey, slen, /* server host key */ | |
247 | dh->pub_key, /* e */ | |
248 | dh_server_pub, /* f */ | |
249 | shared_secret /* K */ | |
250 | ); | |
251 | ||
252 | gssbuf.value=hash; | |
253 | gssbuf.length=20; | |
254 | ||
255 | /* Verify that H matches the token we just got. */ | |
256 | if ((maj_status = gss_verify_mic(&min_status, | |
257 | ctxt.context, | |
258 | &gssbuf, | |
259 | &msg_tok, | |
260 | NULL))) { | |
261 | ||
262 | packet_disconnect("Hash's MIC didn't verify"); | |
263 | } | |
264 | ||
265 | DH_free(dh); | |
266 | ssh_gssapi_delete_ctx(&ctxt); | |
267 | /* save session id */ | |
268 | if (kex->session_id == NULL) { | |
269 | kex->session_id_len = 20; | |
270 | kex->session_id = xmalloc(kex->session_id_len); | |
271 | memcpy(kex->session_id, hash, kex->session_id_len); | |
272 | } | |
273 | ||
274 | kex_derive_keys(kex, hash, shared_secret); | |
275 | BN_clear_free(shared_secret); | |
276 | kex_finish(kex); | |
277 | } | |
278 | ||
279 | ||
280 | ||
281 | ||
282 | void | |
283 | kexgss_server(Kex *kex) | |
284 | { | |
285 | ||
286 | OM_uint32 maj_status, min_status; | |
287 | ||
288 | /* Some GSSAPI implementations use the input value of ret_flags (an | |
289 | * output variable) as a means of triggering mechanism specific | |
290 | * features. Initializing it to zero avoids inadvertently | |
291 | * activating this non-standard behaviour.*/ | |
292 | ||
293 | OM_uint32 ret_flags = 0; | |
294 | gss_buffer_desc gssbuf,send_tok,recv_tok,msg_tok; | |
295 | Gssctxt ctxt; | |
5598e598 | 296 | unsigned int klen, kout; |
297 | unsigned char *kbuf; | |
298 | unsigned char *hash; | |
299 | DH *dh; | |
905081a4 | 300 | BIGNUM *shared_secret = NULL; |
301 | BIGNUM *dh_client_pub = NULL; | |
5598e598 | 302 | int type =0; |
303 | ||
304 | /* Initialise GSSAPI */ | |
305 | ||
306 | ssh_gssapi_build_ctx(&ctxt); | |
307 | if (ssh_gssapi_id_kex(&ctxt,kex->name)) | |
308 | fatal("Unknown gssapi mechanism"); | |
309 | if (ssh_gssapi_acquire_cred(&ctxt)) | |
310 | fatal("Unable to acquire credentials for the server"); | |
311 | ||
5598e598 | 312 | do { |
313 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | |
6d25b646 | 314 | type = packet_read(); |
5598e598 | 315 | switch(type) { |
316 | case SSH2_MSG_KEXGSS_INIT: | |
905081a4 | 317 | if (dh_client_pub!=NULL) |
5598e598 | 318 | fatal("Received KEXGSS_INIT after initialising"); |
319 | recv_tok.value=packet_get_string(&recv_tok.length); | |
905081a4 | 320 | |
321 | dh_client_pub = BN_new(); | |
322 | ||
323 | if (dh_client_pub == NULL) | |
324 | fatal("dh_client_pub == NULL"); | |
6d25b646 | 325 | packet_get_bignum2(dh_client_pub); |
905081a4 | 326 | |
5598e598 | 327 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ |
328 | break; | |
329 | case SSH2_MSG_KEXGSS_CONTINUE: | |
905081a4 | 330 | if (dh_client_pub == NULL) |
5598e598 | 331 | fatal("Received KEXGSS_CONTINUE without initialising"); |
332 | recv_tok.value=packet_get_string(&recv_tok.length); | |
333 | break; | |
334 | default: | |
335 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
336 | type); | |
337 | } | |
338 | maj_status=ssh_gssapi_accept_ctx(&ctxt,&recv_tok, &send_tok, | |
339 | &ret_flags); | |
340 | ||
341 | gss_release_buffer(&min_status,&recv_tok); | |
342 | ||
343 | if (maj_status & GSS_S_CONTINUE_NEEDED) { | |
344 | debug("Sending GSSAPI_CONTINUE"); | |
345 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
346 | packet_put_string(send_tok.value,send_tok.length); | |
347 | packet_send(); | |
348 | packet_write_wait(); | |
349 | gss_release_buffer(&min_status, &send_tok); | |
350 | } | |
351 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
352 | ||
353 | if (GSS_ERROR(maj_status)) | |
354 | fatal("gss_accept_context died"); | |
355 | ||
356 | debug("gss_complete"); | |
357 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) | |
358 | fatal("mutual authentication flag wasn't set"); | |
359 | ||
360 | if (!(ret_flags & GSS_C_INTEG_FLAG)) | |
361 | fatal("Integrity flag wasn't set"); | |
362 | ||
363 | ||
364 | dh = dh_new_group1(); | |
365 | dh_gen_key(dh, kex->we_need * 8); | |
366 | ||
367 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
368 | packet_disconnect("bad client public DH value"); | |
369 | ||
370 | klen = DH_size(dh); | |
371 | kbuf = xmalloc(klen); | |
372 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
373 | ||
374 | shared_secret = BN_new(); | |
375 | BN_bin2bn(kbuf, kout, shared_secret); | |
376 | memset(kbuf, 0, klen); | |
377 | xfree(kbuf); | |
378 | ||
379 | hash = kex_gssapi_hash( | |
380 | kex->client_version_string, | |
381 | kex->server_version_string, | |
382 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
383 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
384 | NULL, 0, /* Change this if we start sending host keys */ | |
385 | dh_client_pub, | |
386 | dh->pub_key, | |
387 | shared_secret | |
388 | ); | |
389 | BN_free(dh_client_pub); | |
390 | ||
391 | if (kex->session_id == NULL) { | |
392 | kex->session_id_len = 20; | |
393 | kex->session_id = xmalloc(kex->session_id_len); | |
394 | memcpy(kex->session_id, hash, kex->session_id_len); | |
395 | } | |
396 | ||
397 | gssbuf.value = hash; | |
398 | gssbuf.length = 20; /* Hashlen appears to always be 20 */ | |
399 | ||
400 | if ((maj_status=gss_get_mic(&min_status, | |
401 | ctxt.context, | |
402 | GSS_C_QOP_DEFAULT, | |
403 | &gssbuf, | |
404 | &msg_tok))) { | |
405 | ssh_gssapi_error(maj_status,min_status); | |
406 | fatal("Couldn't get MIC"); | |
407 | } | |
408 | ||
409 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); | |
410 | packet_put_bignum2(dh->pub_key); | |
411 | packet_put_string((char *)msg_tok.value,msg_tok.length); | |
412 | ||
413 | if (send_tok.length!=0) { | |
414 | packet_put_char(1); /* true */ | |
415 | packet_put_string((char *)send_tok.value,send_tok.length); | |
416 | } else { | |
417 | packet_put_char(0); /* false */ | |
418 | } | |
419 | packet_send(); | |
420 | packet_write_wait(); | |
421 | ||
422 | /* Store the client name, and the delegated credentials for later | |
423 | * use */ | |
424 | if (ssh_gssapi_getclient(&ctxt,&gssapi_client_type, | |
425 | &gssapi_client_name, | |
426 | &gssapi_client_creds)) { | |
427 | fatal("Couldn't convert client name"); | |
428 | } | |
429 | ||
430 | gss_release_buffer(&min_status, &send_tok); | |
431 | ssh_gssapi_delete_ctx(&ctxt); | |
432 | DH_free(dh); | |
433 | ||
434 | kex_derive_keys(kex, hash, shared_secret); | |
435 | BN_clear_free(shared_secret); | |
436 | kex_finish(kex); | |
437 | } | |
438 | ||
439 | void | |
440 | kexgss(Kex *kex) | |
441 | { | |
442 | if (kex->server) | |
443 | kexgss_server(kex); | |
444 | else | |
445 | kexgss_client(kex); | |
446 | } | |
447 | ||
448 | #endif /* GSSAPI */ |