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