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