]> andersk Git - gssapi-openssh.git/blob - openssh/kexgssc.c
http://www.sxw.org.uk/computing/patches/openssh-4.6p1-gsskex-20070312.patch committed...
[gssapi-openssh.git] / openssh / kexgssc.c
1 /*
2  * Copyright (c) 2001-2006 Simon Wilkinson. All rights reserved.
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 "includes.h"
30
31 #include <openssl/crypto.h>
32 #include <openssl/bn.h>
33
34 #include <string.h>
35
36 #include "xmalloc.h"
37 #include "buffer.h"
38 #include "ssh2.h"
39 #include "key.h"
40 #include "cipher.h"
41 #include "kex.h"
42 #include "log.h"
43 #include "packet.h"
44 #include "dh.h"
45
46 #include "ssh-gss.h"
47
48 void
49 kexgss_client(Kex *kex) {
50         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
51         gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
52         Gssctxt *ctxt;
53         OM_uint32 maj_status, min_status, ret_flags;
54         u_int klen, kout, slen = 0, hashlen, strlen;
55         DH *dh; 
56         BIGNUM *dh_server_pub = NULL;
57         BIGNUM *shared_secret = NULL;
58         BIGNUM *p = NULL;
59         BIGNUM *g = NULL;       
60         u_char *kbuf, *hash;
61         u_char *serverhostkey = NULL;
62         char *msg;
63         char *lang;
64         int type = 0;
65         int first = 1;
66         int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
67
68         /* Initialise our GSSAPI world */       
69         ssh_gssapi_build_ctx(&ctxt);
70         if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type) 
71             == GSS_C_NO_OID)
72                 fatal("Couldn't identify host exchange");
73
74         if (ssh_gssapi_import_name(ctxt, kex->gss_host))
75                 fatal("Couldn't import hostname");
76         
77         switch (kex->kex_type) {
78         case KEX_GSS_GRP1_SHA1:
79                 dh = dh_new_group1();
80                 break;
81         case KEX_GSS_GRP14_SHA1:
82                 dh = dh_new_group14();
83                 break;
84         case KEX_GSS_GEX_SHA1:
85                 debug("Doing group exchange\n");
86                 nbits = dh_estimate(kex->we_need * 8);
87                 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
88                 packet_put_int(min);
89                 packet_put_int(nbits);
90                 packet_put_int(max);
91
92                 packet_send();
93
94                 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
95
96                 if ((p = BN_new()) == NULL)
97                         fatal("BN_new() failed");
98                 packet_get_bignum2(p);
99                 if ((g = BN_new()) == NULL)
100                         fatal("BN_new() failed");
101                 packet_get_bignum2(g);
102                 packet_check_eom();
103
104                 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
105                         fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
106                             min, BN_num_bits(p), max);
107
108                 dh = dh_new_group(g, p);
109                 break;
110         default:
111                 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
112         }
113         
114         /* Step 1 - e is dh->pub_key */
115         dh_gen_key(dh, kex->we_need * 8);
116
117         /* This is f, we initialise it now to make life easier */
118         dh_server_pub = BN_new();
119         if (dh_server_pub == NULL)
120                 fatal("dh_server_pub == NULL");
121
122         token_ptr = GSS_C_NO_BUFFER;
123                          
124         do {
125                 debug("Calling gss_init_sec_context");
126                 
127                 maj_status = ssh_gssapi_init_ctx(ctxt,
128                     kex->gss_deleg_creds, token_ptr, &send_tok,
129                     &ret_flags);
130
131                 if (GSS_ERROR(maj_status)) {
132                         if (send_tok.length != 0) {
133                                 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
134                                 packet_put_string(send_tok.value,
135                                     send_tok.length);
136                         }
137                         fatal("gss_init_context failed");
138                 }
139
140                 /* If we've got an old receive buffer get rid of it */
141                 if (token_ptr != GSS_C_NO_BUFFER)
142                         xfree(recv_tok.value);
143
144                 if (maj_status == GSS_S_COMPLETE) {
145                         /* If mutual state flag is not true, kex fails */
146                         if (!(ret_flags & GSS_C_MUTUAL_FLAG))
147                                 fatal("Mutual authentication failed");
148
149                         /* If integ avail flag is not true kex fails */
150                         if (!(ret_flags & GSS_C_INTEG_FLAG))
151                                 fatal("Integrity check failed");
152                 }
153
154                 /* 
155                  * If we have data to send, then the last message that we
156                  * received cannot have been a 'complete'. 
157                  */
158                 if (send_tok.length != 0) {
159                         if (first) {
160                                 packet_start(SSH2_MSG_KEXGSS_INIT);
161                                 packet_put_string(send_tok.value,
162                                     send_tok.length);
163                                 packet_put_bignum2(dh->pub_key);
164                                 first = 0;
165                         } else {
166                                 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
167                                 packet_put_string(send_tok.value,
168                                     send_tok.length);
169                         }
170                         packet_send();
171                         gss_release_buffer(&min_status, &send_tok);
172
173                         /* If we've sent them data, they should reply */
174                         do {    
175                                 type = packet_read();
176                                 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
177                                         debug("Received KEXGSS_HOSTKEY");
178                                         if (serverhostkey)
179                                                 fatal("Server host key received more than once");
180                                         serverhostkey = 
181                                             packet_get_string(&slen);
182                                 }
183                         } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
184
185                         switch (type) {
186                         case SSH2_MSG_KEXGSS_CONTINUE:
187                                 debug("Received GSSAPI_CONTINUE");
188                                 if (maj_status == GSS_S_COMPLETE) 
189                                         fatal("GSSAPI Continue received from server when complete");
190                                 recv_tok.value = packet_get_string(&strlen);
191                                 recv_tok.length = strlen; 
192                                 break;
193                         case SSH2_MSG_KEXGSS_COMPLETE:
194                                 debug("Received GSSAPI_COMPLETE");
195                                 packet_get_bignum2(dh_server_pub);
196                                 msg_tok.value =  packet_get_string(&strlen);
197                                 msg_tok.length = strlen; 
198
199                                 /* Is there a token included? */
200                                 if (packet_get_char()) {
201                                         recv_tok.value=
202                                             packet_get_string(&strlen);
203                                         recv_tok.length = strlen;
204                                         /* If we're already complete - protocol error */
205                                         if (maj_status == GSS_S_COMPLETE)
206                                                 packet_disconnect("Protocol error: received token when complete");
207                                         } else {
208                                                 /* No token included */
209                                                 if (maj_status != GSS_S_COMPLETE)
210                                                         packet_disconnect("Protocol error: did not receive final token");
211                                 }
212                                 break;
213                         case SSH2_MSG_KEXGSS_ERROR:
214                                 debug("Received Error");
215                                 maj_status = packet_get_int();
216                                 min_status = packet_get_int();
217                                 msg = packet_get_string(NULL);
218                                 lang = packet_get_string(NULL);
219                                 fatal("GSSAPI Error: \n%.400s",msg);
220                         default:
221                                 packet_disconnect("Protocol error: didn't expect packet type %d",
222                                 type);
223                         }
224                         token_ptr = &recv_tok;
225                 } else {
226                         /* No data, and not complete */
227                         if (maj_status != GSS_S_COMPLETE)
228                                 fatal("Not complete, and no token output");
229                 }
230         } while (maj_status & GSS_S_CONTINUE_NEEDED);
231
232         /* 
233          * We _must_ have received a COMPLETE message in reply from the 
234          * server, which will have set dh_server_pub and msg_tok 
235          */
236
237         if (type != SSH2_MSG_KEXGSS_COMPLETE)
238                 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
239
240         /* Check f in range [1, p-1] */
241         if (!dh_pub_is_valid(dh, dh_server_pub))
242                 packet_disconnect("bad server public DH value");
243
244         /* compute K=f^x mod p */
245         klen = DH_size(dh);
246         kbuf = xmalloc(klen);
247         kout = DH_compute_key(kbuf, dh_server_pub, dh);
248
249         shared_secret = BN_new();
250         BN_bin2bn(kbuf,kout, shared_secret);
251         memset(kbuf, 0, klen);
252         xfree(kbuf);
253
254         switch (kex->kex_type) {
255         case KEX_GSS_GRP1_SHA1:
256         case KEX_GSS_GRP14_SHA1:
257                 kex_dh_hash( kex->client_version_string, 
258                     kex->server_version_string,
259                     buffer_ptr(&kex->my), buffer_len(&kex->my),
260                     buffer_ptr(&kex->peer), buffer_len(&kex->peer),
261                     serverhostkey, slen, /* server host key */
262                     dh->pub_key,        /* e */
263                     dh_server_pub,      /* f */
264                     shared_secret,      /* K */
265                     &hash, &hashlen
266                 );
267                 break;
268         case KEX_GSS_GEX_SHA1:
269                 kexgex_hash(
270                     kex->evp_md,
271                     kex->client_version_string,
272                     kex->server_version_string,
273                     buffer_ptr(&kex->my), buffer_len(&kex->my),
274                     buffer_ptr(&kex->peer), buffer_len(&kex->peer),
275                     serverhostkey, slen,
276                     min, nbits, max,
277                     dh->p, dh->g,
278                     dh->pub_key,
279                     dh_server_pub,
280                     shared_secret,
281                     &hash, &hashlen
282                 );
283                 break;
284         default:
285                 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
286         }
287
288         gssbuf.value = hash;
289         gssbuf.length = hashlen;
290
291         /* Verify that the hash matches the MIC we just got. */
292         if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
293                 packet_disconnect("Hash's MIC didn't verify");
294
295         xfree(msg_tok.value);
296
297         DH_free(dh);
298         if (serverhostkey)
299                 xfree(serverhostkey);
300         BN_clear_free(dh_server_pub);
301
302         /* save session id */
303         if (kex->session_id == NULL) {
304                 kex->session_id_len = hashlen;
305                 kex->session_id = xmalloc(kex->session_id_len);
306                 memcpy(kex->session_id, hash, kex->session_id_len);
307         }
308
309         if (gss_kex_context == NULL)
310                 gss_kex_context = ctxt;
311         else
312                 ssh_gssapi_delete_ctx(&ctxt);
313
314         kex_derive_keys(kex, hash, hashlen, shared_secret);
315         BN_clear_free(shared_secret);
316         kex_finish(kex);
317 }
318
319 #endif /* GSSAPI */
This page took 0.118264 seconds and 5 git commands to generate.