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