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