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