]>
Commit | Line | Data |
---|---|---|
1c14df9e | 1 | /* |
88928908 | 2 | * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. |
1c14df9e | 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" | |
1c14df9e | 41 | #include "canohost.h" |
42 | ||
1c14df9e | 43 | void |
44 | kexgss_client(Kex *kex) | |
45 | { | |
46 | gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr; | |
47 | Gssctxt *ctxt; | |
48 | OM_uint32 maj_status, min_status, ret_flags; | |
49 | unsigned int klen, kout; | |
50 | DH *dh; | |
51 | BIGNUM *dh_server_pub = 0; | |
52 | BIGNUM *shared_secret = 0; | |
53 | unsigned char *kbuf; | |
54 | unsigned char *hash; | |
55 | unsigned char *serverhostkey; | |
88928908 | 56 | char *msg; |
57 | char *lang; | |
1c14df9e | 58 | int type = 0; |
59 | int first = 1; | |
88928908 | 60 | int slen = 0, strlen; |
1c14df9e | 61 | |
62 | /* Initialise our GSSAPI world */ | |
63 | ssh_gssapi_build_ctx(&ctxt); | |
88928908 | 64 | if (ssh_gssapi_client_id_kex(ctxt,kex->name)==NULL) { |
1c14df9e | 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)) { | |
88928908 | 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 | } | |
1c14df9e | 100 | fatal("gss_init_context failed"); |
88928908 | 101 | } |
1c14df9e | 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"); | |
88928908 | 150 | recv_tok.value=packet_get_string(&strlen); |
151 | recv_tok.length=strlen; /* int vs. size_t */ | |
1c14df9e | 152 | break; |
153 | case SSH2_MSG_KEXGSS_COMPLETE: | |
154 | debug("Received GSSAPI_COMPLETE"); | |
155 | packet_get_bignum2(dh_server_pub); | |
88928908 | 156 | msg_tok.value=packet_get_string(&strlen); |
157 | msg_tok.length=strlen; /* int vs. size_t */ | |
1c14df9e | 158 | |
159 | /* Is there a token included? */ | |
160 | if (packet_get_char()) { | |
161 | recv_tok.value= | |
88928908 | 162 | packet_get_string(&strlen); |
163 | recv_tok.length=strlen; /*int/size_t*/ | |
1c14df9e | 164 | /* If we're already complete - protocol error */ |
165 | if (maj_status == GSS_S_COMPLETE) | |
166 | packet_disconnect("Protocol error: received token when complete"); | |
167 | } else { | |
168 | /* No token included */ | |
169 | if (maj_status != GSS_S_COMPLETE) | |
170 | packet_disconnect("Protocol error: did not receive final token"); | |
171 | } | |
172 | break; | |
88928908 | 173 | case SSH2_MSG_KEXGSS_ERROR: |
174 | debug("Received Error"); | |
175 | maj_status=packet_get_int(); | |
176 | min_status=packet_get_int(); | |
177 | msg=packet_get_string(NULL); | |
178 | lang=packet_get_string(NULL); | |
179 | fprintf(stderr,"GSSAPI Error: \n%s",msg); | |
1c14df9e | 180 | default: |
181 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
182 | type); | |
183 | } | |
184 | token_ptr=&recv_tok; | |
88928908 | 185 | } else { |
186 | /* No data, and not complete */ | |
187 | if (maj_status!=GSS_S_COMPLETE) { | |
188 | fatal("Not complete, and no token output"); | |
189 | } | |
1c14df9e | 190 | } |
1c14df9e | 191 | } while (maj_status & GSS_S_CONTINUE_NEEDED); |
192 | ||
193 | /* We _must_ have received a COMPLETE message in reply from the | |
194 | * server, which will have set dh_server_pub and msg_tok */ | |
195 | ||
196 | if (type!=SSH2_MSG_KEXGSS_COMPLETE) | |
197 | fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it"); | |
198 | ||
199 | /* Check f in range [1, p-1] */ | |
200 | if (!dh_pub_is_valid(dh, dh_server_pub)) | |
201 | packet_disconnect("bad server public DH value"); | |
202 | ||
203 | /* compute K=f^x mod p */ | |
204 | klen = DH_size(dh); | |
205 | kbuf = xmalloc(klen); | |
206 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
207 | ||
208 | shared_secret = BN_new(); | |
209 | BN_bin2bn(kbuf,kout, shared_secret); | |
210 | memset(kbuf, 0, klen); | |
211 | xfree(kbuf); | |
212 | ||
88928908 | 213 | /* The GSS hash is identical to the DH one */ |
214 | hash = kex_dh_hash( | |
1c14df9e | 215 | kex->client_version_string, |
216 | kex->server_version_string, | |
217 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
218 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
219 | serverhostkey, slen, /* server host key */ | |
220 | dh->pub_key, /* e */ | |
221 | dh_server_pub, /* f */ | |
222 | shared_secret /* K */ | |
223 | ); | |
224 | ||
225 | gssbuf.value=hash; | |
226 | gssbuf.length=20; | |
227 | ||
228 | /* Verify that H matches the token we just got. */ | |
229 | if ((maj_status = gss_verify_mic(&min_status, | |
230 | ctxt->context, | |
231 | &gssbuf, | |
232 | &msg_tok, | |
233 | NULL))) { | |
234 | ||
235 | packet_disconnect("Hash's MIC didn't verify"); | |
236 | } | |
237 | ||
238 | DH_free(dh); | |
239 | ssh_gssapi_delete_ctx(&ctxt); | |
240 | /* save session id */ | |
241 | if (kex->session_id == NULL) { | |
242 | kex->session_id_len = 20; | |
243 | kex->session_id = xmalloc(kex->session_id_len); | |
244 | memcpy(kex->session_id, hash, kex->session_id_len); | |
245 | } | |
246 | ||
247 | kex_derive_keys(kex, hash, shared_secret); | |
248 | BN_clear_free(shared_secret); | |
249 | kex_finish(kex); | |
250 | } | |
251 | ||
252 | #endif /* GSSAPI */ |