]> andersk Git - gssapi-openssh.git/blob - openssh/kexgssc.c
openssh-3.6.1p1-gssapi-20030416.diff from Simon
[gssapi-openssh.git] / openssh / kexgssc.c
1 /*
2  * Copyright (c) 2001-2003 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
42 void
43 kexgss_client(Kex *kex)
44 {
45         gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr;
46         Gssctxt *ctxt;
47         OM_uint32 maj_status, min_status, ret_flags;
48         unsigned int klen, kout;
49         DH *dh; 
50         BIGNUM *dh_server_pub = 0;
51         BIGNUM *shared_secret = 0;      
52         unsigned char *kbuf;
53         unsigned char *hash;
54         unsigned char *serverhostkey;
55         char *msg;
56         char *lang;
57         int type = 0;
58         int first = 1;
59         int slen = 0;
60         
61         /* Initialise our GSSAPI world */
62         ssh_gssapi_build_ctx(&ctxt);
63         if (ssh_gssapi_client_id_kex(ctxt,kex->name)==NULL) {
64                 fatal("Couldn't identify host exchange");
65         }
66
67         if (ssh_gssapi_import_name(ctxt,kex->host)) {
68                 fatal("Couldn't import hostname ");
69         }
70         
71         /* This code should match that in ssh_dh1_client */
72                 
73         /* Step 1 - e is dh->pub_key */
74         dh = dh_new_group1();
75         dh_gen_key(dh, kex->we_need * 8);
76
77         /* This is f, we initialise it now to make life easier */
78         dh_server_pub = BN_new();
79         if (dh_server_pub == NULL) {
80                 fatal("dh_server_pub == NULL");
81         }
82                 
83         token_ptr = GSS_C_NO_BUFFER;
84                          
85         do {
86                 debug("Calling gss_init_sec_context");
87                 
88                 maj_status=ssh_gssapi_init_ctx(ctxt,
89                                                kex->options.gss_deleg_creds,
90                                                token_ptr,&send_tok,
91                                                &ret_flags);
92
93                 if (GSS_ERROR(maj_status)) {
94                         if (send_tok.length!=0) {
95                                 /* Hmmm - not sure about this */
96                                 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
97                                 packet_put_string(send_tok.value,
98                                                   send_tok.length);
99                         }                         
100                         fatal("gss_init_context failed");
101                 }
102
103                 /* If we've got an old receive buffer get rid of it */
104                 if (token_ptr != GSS_C_NO_BUFFER)
105                         (void) gss_release_buffer(&min_status, &recv_tok);
106         
107                 
108                 if (maj_status == GSS_S_COMPLETE) {
109                         /* If mutual state flag is not true, kex fails */
110                         if (!(ret_flags & GSS_C_MUTUAL_FLAG)) {
111                                 fatal("Mutual authentication failed");
112                         }
113                         /* If integ avail flag is not true kex fails */
114                         if (!(ret_flags & GSS_C_INTEG_FLAG)) {
115                                 fatal("Integrity check failed");
116                         }
117                 }
118                 
119                 /* If we have data to send, then the last message that we
120                  * received cannot have been a 'complete'. */
121                 if (send_tok.length !=0) {
122                         if (first) {
123                                 packet_start(SSH2_MSG_KEXGSS_INIT);
124                                 packet_put_string(send_tok.value,
125                                                   send_tok.length);
126                                 packet_put_bignum2(dh->pub_key);
127                                 first=0;
128                         } else {
129                                 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
130                                 packet_put_string(send_tok.value,
131                                                   send_tok.length);
132                         }
133                         packet_send();
134                         packet_write_wait();
135
136                         
137                         /* If we've sent them data, they'd better be polite
138                          * and reply. */
139                 
140                         type = packet_read();
141                         switch (type) {
142                         case SSH2_MSG_KEXGSS_HOSTKEY:
143                                 debug("Received KEXGSS_HOSTKEY");
144                                 serverhostkey=packet_get_string(&slen);
145                                 break;
146                         case SSH2_MSG_KEXGSS_CONTINUE:
147                                 debug("Received GSSAPI_CONTINUE");
148                                 if (maj_status == GSS_S_COMPLETE) 
149                                         fatal("GSSAPI Continue received from server when complete");
150                                 recv_tok.value=packet_get_string(&recv_tok.length);
151                                 break;
152                         case SSH2_MSG_KEXGSS_COMPLETE:
153                                 debug("Received GSSAPI_COMPLETE");
154                                 packet_get_bignum2(dh_server_pub);
155                                 msg_tok.value=
156                                     packet_get_string(&msg_tok.length);
157
158                                 /* Is there a token included? */
159                                 if (packet_get_char()) {
160                                         recv_tok.value=
161                                             packet_get_string(&recv_tok.length);
162                                         /* If we're already complete - protocol error */
163                                         if (maj_status == GSS_S_COMPLETE)
164                                                 packet_disconnect("Protocol error: received token when complete");
165                                 } else {
166                                         /* No token included */
167                                         if (maj_status != GSS_S_COMPLETE)
168                                                 packet_disconnect("Protocol error: did not receive final token");
169                                 }
170                                 break;
171                         case SSH2_MSG_KEXGSS_ERROR:
172                                 debug("Received Error");
173                                 maj_status=packet_get_int();
174                                 min_status=packet_get_int();
175                                 msg=packet_get_string(NULL);
176                                 lang=packet_get_string(NULL);
177                                 fprintf(stderr,"GSSAPI Error: \n%s",msg);
178                         default:
179                                 packet_disconnect("Protocol error: didn't expect packet type %d",
180                                 type);
181                         }
182                         token_ptr=&recv_tok;
183                 } else {
184                         /* No data, and not complete */
185                         if (maj_status!=GSS_S_COMPLETE) {
186                                 fatal("Not complete, and no token output");
187                         }
188                 }
189         } while (maj_status & GSS_S_CONTINUE_NEEDED);
190         
191         /* We _must_ have received a COMPLETE message in reply from the 
192          * server, which will have set dh_server_pub and msg_tok */
193          
194         if (type!=SSH2_MSG_KEXGSS_COMPLETE)
195            fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
196                         
197         /* Check f in range [1, p-1] */
198         if (!dh_pub_is_valid(dh, dh_server_pub))
199                         packet_disconnect("bad server public DH value");
200                         
201         /* compute K=f^x mod p */
202         klen = DH_size(dh);
203         kbuf = xmalloc(klen);
204         kout = DH_compute_key(kbuf, dh_server_pub, dh);
205         
206         shared_secret = BN_new();
207         BN_bin2bn(kbuf,kout, shared_secret);
208         memset(kbuf, 0, klen);
209         xfree(kbuf);
210         
211         /* The GSS hash is identical to the DH one */
212         hash = kex_dh_hash(
213             kex->client_version_string,
214             kex->server_version_string,
215             buffer_ptr(&kex->my), buffer_len(&kex->my),
216             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
217             serverhostkey, slen, /* server host key */
218             dh->pub_key,        /* e */
219             dh_server_pub,      /* f */
220             shared_secret       /* K */
221         );
222         
223         gssbuf.value=hash;
224         gssbuf.length=20;
225         
226         /* Verify that H matches the token we just got. */
227                 if ((maj_status = gss_verify_mic(&min_status,
228                                          ctxt->context,
229                                          &gssbuf,
230                                          &msg_tok,
231                                          NULL))) {
232
233                 packet_disconnect("Hash's MIC didn't verify");
234         }       
235         
236         DH_free(dh);
237         ssh_gssapi_delete_ctx(&ctxt);
238         /* save session id */
239         if (kex->session_id == NULL) {
240                 kex->session_id_len = 20;
241                 kex->session_id = xmalloc(kex->session_id_len);
242                 memcpy(kex->session_id, hash, kex->session_id_len);
243         }
244         
245         kex_derive_keys(kex, hash, shared_secret);
246         BN_clear_free(shared_secret);
247         kex_finish(kex);
248 }
249
250 #endif /* GSSAPI */
This page took 0.055033 seconds and 5 git commands to generate.