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