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