]> andersk Git - gssapi-openssh.git/blob - openssh/kexgsss.c
The man2html from jbasney on pkilab2 works whereas the standard one doesn't.
[gssapi-openssh.git] / openssh / kexgsss.c
1 /*
2  * Copyright (c) 2001-2009 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 <string.h>
30
31 #include <openssl/crypto.h>
32 #include <openssl/bn.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 #include "ssh-gss.h"
44 #include "monitor_wrap.h"
45 #include "servconf.h"
46
47 static void kex_gss_send_error(Gssctxt *ctxt);
48 extern ServerOptions options;
49
50 void
51 kexgss_server(Kex *kex)
52 {
53         OM_uint32 maj_status, min_status;
54         
55         /* 
56          * Some GSSAPI implementations use the input value of ret_flags (an
57          * output variable) as a means of triggering mechanism specific 
58          * features. Initializing it to zero avoids inadvertently 
59          * activating this non-standard behaviour.
60          */
61
62         OM_uint32 ret_flags = 0;
63         gss_buffer_desc gssbuf, recv_tok, msg_tok;
64         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
65         Gssctxt *ctxt = NULL;
66         u_int slen, klen, kout, hashlen;
67         u_char *kbuf, *hash;
68         DH *dh;
69         int min = -1, max = -1, nbits = -1;
70         BIGNUM *shared_secret = NULL;
71         BIGNUM *dh_client_pub = NULL;
72         int type = 0;
73         gss_OID oid;
74         char *mechs;
75
76         /* Initialise GSSAPI */
77
78         /* If we're rekeying, privsep means that some of the private structures
79          * in the GSSAPI code are no longer available. This kludges them back
80          * into life
81          */
82         if (!ssh_gssapi_oid_table_ok()) 
83                 if ((mechs = ssh_gssapi_server_mechanisms()))
84                         xfree(mechs);
85
86         debug2("%s: Identifying %s", __func__, kex->name);
87         oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
88         if (oid == GSS_C_NO_OID)
89            fatal("Unknown gssapi mechanism");
90
91         debug2("%s: Acquiring credentials", __func__);
92
93         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) {
94                 kex_gss_send_error(ctxt);
95                 fatal("Unable to acquire credentials for the server");
96     }
97
98         switch (kex->kex_type) {
99         case KEX_GSS_GRP1_SHA1:
100                 dh = dh_new_group1();
101                 break;
102         case KEX_GSS_GRP14_SHA1:
103                 dh = dh_new_group14();
104                 break;
105         case KEX_GSS_GEX_SHA1:
106                 debug("Doing group exchange");
107                 packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
108                 min = packet_get_int();
109                 nbits = packet_get_int();
110                 max = packet_get_int();
111                 min = MAX(DH_GRP_MIN, min);
112                 max = MIN(DH_GRP_MAX, max);
113                 packet_check_eom();
114                 if (max < min || nbits < min || max < nbits)
115                         fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
116                             min, nbits, max);
117                 dh = PRIVSEP(choose_dh(min, nbits, max));
118                 if (dh == NULL)
119                         packet_disconnect("Protocol error: no matching group found");
120
121                 packet_start(SSH2_MSG_KEXGSS_GROUP);
122                 packet_put_bignum2(dh->p);
123                 packet_put_bignum2(dh->g);
124                 packet_send();
125
126                 packet_write_wait();
127                 break;
128         default:
129                 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
130         }
131
132         dh_gen_key(dh, kex->we_need * 8);
133
134         do {
135                 debug("Wait SSH2_MSG_GSSAPI_INIT");
136                 type = packet_read();
137                 switch(type) {
138                 case SSH2_MSG_KEXGSS_INIT:
139                         if (dh_client_pub != NULL) 
140                                 fatal("Received KEXGSS_INIT after initialising");
141                         recv_tok.value = packet_get_string(&slen);
142                         recv_tok.length = slen; 
143
144                         if ((dh_client_pub = BN_new()) == NULL)
145                                 fatal("dh_client_pub == NULL");
146
147                         packet_get_bignum2(dh_client_pub);
148
149                         /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
150                         break;
151                 case SSH2_MSG_KEXGSS_CONTINUE:
152                         recv_tok.value = packet_get_string(&slen);
153                         recv_tok.length = slen; 
154                         break;
155                 default:
156                         packet_disconnect(
157                             "Protocol error: didn't expect packet type %d",
158                             type);
159                 }
160
161                 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, 
162                     &send_tok, &ret_flags));
163
164                 xfree(recv_tok.value);
165
166                 if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
167                         fatal("Zero length token output when incomplete");
168
169                 if (dh_client_pub == NULL)
170                         fatal("No client public key");
171                 
172                 if (maj_status & GSS_S_CONTINUE_NEEDED) {
173                         debug("Sending GSSAPI_CONTINUE");
174                         packet_start(SSH2_MSG_KEXGSS_CONTINUE);
175                         packet_put_string((char *)send_tok.value, send_tok.length);
176                         packet_send();
177                         gss_release_buffer(&min_status, &send_tok);
178                 }
179         } while (maj_status & GSS_S_CONTINUE_NEEDED);
180
181         if (GSS_ERROR(maj_status)) {
182                 kex_gss_send_error(ctxt);
183                 if (send_tok.length > 0) {
184                         packet_start(SSH2_MSG_KEXGSS_CONTINUE);
185                         packet_put_string((char *)send_tok.value, send_tok.length);
186                         packet_send();
187                 }
188                 packet_disconnect("GSSAPI Key Exchange handshake failed");
189         }
190
191         if (!(ret_flags & GSS_C_MUTUAL_FLAG))
192                 fatal("Mutual Authentication flag wasn't set");
193
194         if (!(ret_flags & GSS_C_INTEG_FLAG))
195                 fatal("Integrity flag wasn't set");
196         
197         if (!dh_pub_is_valid(dh, dh_client_pub))
198                 packet_disconnect("bad client public DH value");
199
200         klen = DH_size(dh);
201         kbuf = xmalloc(klen); 
202         kout = DH_compute_key(kbuf, dh_client_pub, dh);
203         if (kout < 0)
204                 fatal("DH_compute_key: failed");
205
206         shared_secret = BN_new();
207         if (shared_secret == NULL)
208                 fatal("kexgss_server: BN_new failed");
209
210         if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
211                 fatal("kexgss_server: BN_bin2bn failed");
212
213         memset(kbuf, 0, klen);
214         xfree(kbuf);
215
216         switch (kex->kex_type) {
217         case KEX_GSS_GRP1_SHA1:
218         case KEX_GSS_GRP14_SHA1:
219                 kex_dh_hash(
220                     kex->client_version_string, kex->server_version_string,
221                     buffer_ptr(&kex->peer), buffer_len(&kex->peer),
222                     buffer_ptr(&kex->my), buffer_len(&kex->my),
223                     NULL, 0, /* Change this if we start sending host keys */
224                     dh_client_pub, dh->pub_key, shared_secret,
225                     &hash, &hashlen
226                 );
227                 break;
228         case KEX_GSS_GEX_SHA1:
229                 kexgex_hash(
230                     kex->evp_md,
231                     kex->client_version_string, kex->server_version_string,
232                     buffer_ptr(&kex->peer), buffer_len(&kex->peer),
233                     buffer_ptr(&kex->my), buffer_len(&kex->my),
234                     NULL, 0,
235                     min, nbits, max,
236                     dh->p, dh->g,
237                     dh_client_pub,
238                     dh->pub_key,
239                     shared_secret,
240                     &hash, &hashlen
241                 );
242                 break;
243         default:
244                 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
245         }
246
247         BN_clear_free(dh_client_pub);
248
249         if (kex->session_id == NULL) {
250                 kex->session_id_len = hashlen;
251                 kex->session_id = xmalloc(kex->session_id_len);
252                 memcpy(kex->session_id, hash, kex->session_id_len);
253         }
254
255         gssbuf.value = hash;
256         gssbuf.length = hashlen;
257
258         if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
259                 fatal("Couldn't get MIC");
260
261         packet_start(SSH2_MSG_KEXGSS_COMPLETE);
262         packet_put_bignum2(dh->pub_key);
263         packet_put_string((char *)msg_tok.value,msg_tok.length);
264
265         if (send_tok.length != 0) {
266                 packet_put_char(1); /* true */
267                 packet_put_string((char *)send_tok.value, send_tok.length);
268         } else {
269                 packet_put_char(0); /* false */
270         }
271         packet_send();
272
273         gss_release_buffer(&min_status, &send_tok);
274         gss_release_buffer(&min_status, &msg_tok);
275
276         if (gss_kex_context == NULL)
277                 gss_kex_context = ctxt;
278         else 
279                 ssh_gssapi_delete_ctx(&ctxt);
280
281         DH_free(dh);
282
283         kex_derive_keys(kex, hash, hashlen, shared_secret);
284         BN_clear_free(shared_secret);
285         kex_finish(kex);
286
287         /* If this was a rekey, then save out any delegated credentials we
288          * just exchanged.  */
289         if (options.gss_store_rekey)
290                 ssh_gssapi_rekey_creds();
291 }
292
293 static void 
294 kex_gss_send_error(Gssctxt *ctxt) {
295         char *errstr;
296         OM_uint32 maj,min;
297                 
298         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
299         if (errstr) {
300                 packet_start(SSH2_MSG_KEXGSS_ERROR);
301                 packet_put_int(maj);
302                 packet_put_int(min);
303                 packet_put_cstring(errstr);
304                 packet_put_cstring("");
305                 packet_send();
306                 packet_write_wait();
307                 /* XXX - We should probably log the error locally here */
308                 xfree(errstr);
309         }
310 }
311 #endif /* GSSAPI */
This page took 0.132591 seconds and 5 git commands to generate.