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