]>
Commit | Line | Data |
---|---|---|
b46fa825 | 1 | /* |
fe4ad273 | 2 | * Copyright (c) 2001-2005 Simon Wilkinson. All rights reserved. |
b46fa825 | 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" | |
30460aeb | 35 | #include "cipher.h" |
36 | #include "key.h" | |
b46fa825 | 37 | #include "kex.h" |
38 | #include "log.h" | |
39 | #include "packet.h" | |
40 | #include "dh.h" | |
d7590ddf | 41 | #include "canohost.h" |
b46fa825 | 42 | #include "ssh2.h" |
43 | #include "ssh-gss.h" | |
b46fa825 | 44 | |
b46fa825 | 45 | void |
fe4ad273 | 46 | kexgss_client(Kex *kex) { |
47 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
48 | gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr; | |
b46fa825 | 49 | Gssctxt *ctxt; |
50 | OM_uint32 maj_status, min_status, ret_flags; | |
f472818d | 51 | u_int klen, kout, slen = 0, hashlen, strlen; |
b46fa825 | 52 | DH *dh; |
fe4ad273 | 53 | BIGNUM *dh_server_pub = NULL; |
54 | BIGNUM *shared_secret = NULL; | |
55 | BIGNUM *p = NULL; | |
56 | BIGNUM *g = NULL; | |
f472818d | 57 | u_char *kbuf, *hash; |
58 | u_char *serverhostkey = NULL; | |
23987cb8 | 59 | char *msg; |
60 | char *lang; | |
b46fa825 | 61 | int type = 0; |
62 | int first = 1; | |
fe4ad273 | 63 | int gex = 0; |
f472818d | 64 | int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX; |
fe4ad273 | 65 | |
66 | /* Initialise our GSSAPI world */ | |
b46fa825 | 67 | ssh_gssapi_build_ctx(&ctxt); |
fe4ad273 | 68 | if (ssh_gssapi_id_kex(ctxt, kex->name, &gex) == NULL) |
b46fa825 | 69 | fatal("Couldn't identify host exchange"); |
d7590ddf | 70 | |
fe4ad273 | 71 | if (ssh_gssapi_import_name(ctxt, kex->gss_host)) |
72 | fatal("Couldn't import hostname"); | |
73 | ||
74 | if (gex) { | |
75 | debug("Doing group exchange\n"); | |
76 | nbits = dh_estimate(kex->we_need * 8); | |
fe4ad273 | 77 | packet_start(SSH2_MSG_KEXGSS_GROUPREQ); |
78 | packet_put_int(min); | |
79 | packet_put_int(nbits); | |
80 | packet_put_int(max); | |
81 | ||
82 | packet_send(); | |
83 | ||
84 | packet_read_expect(SSH2_MSG_KEXGSS_GROUP); | |
85 | ||
86 | if ((p = BN_new()) == NULL) | |
87 | fatal("BN_new() failed"); | |
88 | packet_get_bignum2(p); | |
89 | if ((g = BN_new()) == NULL) | |
90 | fatal("BN_new() failed"); | |
91 | packet_get_bignum2(g); | |
92 | packet_check_eom(); | |
93 | ||
94 | if (BN_num_bits(p) < min || BN_num_bits(p) > max) | |
95 | fatal("GSSGRP_GEX group out of range: %d !< %d !< %d", | |
96 | min, BN_num_bits(p), max); | |
97 | ||
98 | dh = dh_new_group(g, p); | |
99 | } else { | |
100 | dh = dh_new_group1(); | |
b46fa825 | 101 | } |
102 | ||
b46fa825 | 103 | /* Step 1 - e is dh->pub_key */ |
b46fa825 | 104 | dh_gen_key(dh, kex->we_need * 8); |
105 | ||
106 | /* This is f, we initialise it now to make life easier */ | |
fe4ad273 | 107 | dh_server_pub = BN_new(); |
108 | if (dh_server_pub == NULL) | |
109 | fatal("dh_server_pub == NULL"); | |
110 | ||
b46fa825 | 111 | token_ptr = GSS_C_NO_BUFFER; |
e23e524c | 112 | |
b46fa825 | 113 | do { |
114 | debug("Calling gss_init_sec_context"); | |
115 | ||
fe4ad273 | 116 | maj_status = ssh_gssapi_init_ctx(ctxt, |
117 | kex->gss_deleg_creds, token_ptr, &send_tok, | |
118 | &ret_flags); | |
b46fa825 | 119 | |
120 | if (GSS_ERROR(maj_status)) { | |
fe4ad273 | 121 | if (send_tok.length != 0) { |
e23e524c | 122 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); |
123 | packet_put_string(send_tok.value, | |
fe4ad273 | 124 | send_tok.length); |
125 | } | |
b46fa825 | 126 | fatal("gss_init_context failed"); |
e23e524c | 127 | } |
b46fa825 | 128 | |
129 | /* If we've got an old receive buffer get rid of it */ | |
130 | if (token_ptr != GSS_C_NO_BUFFER) | |
fe4ad273 | 131 | xfree(recv_tok.value); |
132 | ||
b46fa825 | 133 | if (maj_status == GSS_S_COMPLETE) { |
134 | /* If mutual state flag is not true, kex fails */ | |
fe4ad273 | 135 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) |
b46fa825 | 136 | fatal("Mutual authentication failed"); |
fe4ad273 | 137 | |
b46fa825 | 138 | /* If integ avail flag is not true kex fails */ |
fe4ad273 | 139 | if (!(ret_flags & GSS_C_INTEG_FLAG)) |
b46fa825 | 140 | fatal("Integrity check failed"); |
b46fa825 | 141 | } |
fe4ad273 | 142 | |
143 | /* | |
144 | * If we have data to send, then the last message that we | |
145 | * received cannot have been a 'complete'. | |
146 | */ | |
147 | if (send_tok.length != 0) { | |
b46fa825 | 148 | if (first) { |
149 | packet_start(SSH2_MSG_KEXGSS_INIT); | |
150 | packet_put_string(send_tok.value, | |
fe4ad273 | 151 | send_tok.length); |
b46fa825 | 152 | packet_put_bignum2(dh->pub_key); |
fe4ad273 | 153 | first = 0; |
b46fa825 | 154 | } else { |
155 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
156 | packet_put_string(send_tok.value, | |
fe4ad273 | 157 | send_tok.length); |
b46fa825 | 158 | } |
159 | packet_send(); | |
fe4ad273 | 160 | gss_release_buffer(&min_status, &send_tok); |
161 | ||
162 | /* If we've sent them data, they should reply */ | |
163 | do { | |
164 | type = packet_read(); | |
165 | if (type == SSH2_MSG_KEXGSS_HOSTKEY) { | |
166 | debug("Received KEXGSS_HOSTKEY"); | |
167 | if (serverhostkey) | |
168 | fatal("Server host key received more than once"); | |
169 | serverhostkey = | |
170 | packet_get_string(&slen); | |
171 | } | |
172 | } while (type == SSH2_MSG_KEXGSS_HOSTKEY); | |
b46fa825 | 173 | |
b46fa825 | 174 | switch (type) { |
b46fa825 | 175 | case SSH2_MSG_KEXGSS_CONTINUE: |
176 | debug("Received GSSAPI_CONTINUE"); | |
177 | if (maj_status == GSS_S_COMPLETE) | |
178 | fatal("GSSAPI Continue received from server when complete"); | |
fe4ad273 | 179 | recv_tok.value = packet_get_string(&strlen); |
180 | recv_tok.length = strlen; | |
b46fa825 | 181 | break; |
182 | case SSH2_MSG_KEXGSS_COMPLETE: | |
183 | debug("Received GSSAPI_COMPLETE"); | |
fe4ad273 | 184 | packet_get_bignum2(dh_server_pub); |
185 | msg_tok.value = packet_get_string(&strlen); | |
186 | msg_tok.length = strlen; | |
b46fa825 | 187 | |
188 | /* Is there a token included? */ | |
189 | if (packet_get_char()) { | |
190 | recv_tok.value= | |
23987cb8 | 191 | packet_get_string(&strlen); |
fe4ad273 | 192 | recv_tok.length = strlen; |
b46fa825 | 193 | /* If we're already complete - protocol error */ |
194 | if (maj_status == GSS_S_COMPLETE) | |
195 | packet_disconnect("Protocol error: received token when complete"); | |
fe4ad273 | 196 | } else { |
197 | /* No token included */ | |
198 | if (maj_status != GSS_S_COMPLETE) | |
199 | packet_disconnect("Protocol error: did not receive final token"); | |
b46fa825 | 200 | } |
201 | break; | |
23987cb8 | 202 | case SSH2_MSG_KEXGSS_ERROR: |
203 | debug("Received Error"); | |
fe4ad273 | 204 | maj_status = packet_get_int(); |
205 | min_status = packet_get_int(); | |
206 | msg = packet_get_string(NULL); | |
207 | lang = packet_get_string(NULL); | |
0b2e7773 | 208 | fatal("GSSAPI Key Exchange Error: \n%s",msg); |
b46fa825 | 209 | default: |
210 | packet_disconnect("Protocol error: didn't expect packet type %d", | |
211 | type); | |
212 | } | |
fe4ad273 | 213 | token_ptr = &recv_tok; |
e23e524c | 214 | } else { |
215 | /* No data, and not complete */ | |
fe4ad273 | 216 | if (maj_status != GSS_S_COMPLETE) |
e23e524c | 217 | fatal("Not complete, and no token output"); |
b46fa825 | 218 | } |
fe4ad273 | 219 | } while (maj_status & GSS_S_CONTINUE_NEEDED); |
220 | ||
221 | /* | |
222 | * We _must_ have received a COMPLETE message in reply from the | |
223 | * server, which will have set dh_server_pub and msg_tok | |
224 | */ | |
225 | ||
226 | if (type != SSH2_MSG_KEXGSS_COMPLETE) | |
227 | fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it"); | |
228 | ||
b46fa825 | 229 | /* Check f in range [1, p-1] */ |
fe4ad273 | 230 | if (!dh_pub_is_valid(dh, dh_server_pub)) |
231 | packet_disconnect("bad server public DH value"); | |
b46fa825 | 232 | |
fe4ad273 | 233 | /* compute K=f^x mod p */ |
234 | klen = DH_size(dh); | |
235 | kbuf = xmalloc(klen); | |
236 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
237 | ||
238 | shared_secret = BN_new(); | |
239 | BN_bin2bn(kbuf,kout, shared_secret); | |
240 | memset(kbuf, 0, klen); | |
241 | xfree(kbuf); | |
242 | ||
243 | if (gex) { | |
08822d99 | 244 | kexgex_hash( |
245 | kex->evp_md, | |
246 | kex->client_version_string, | |
fe4ad273 | 247 | kex->server_version_string, |
248 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
249 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
250 | serverhostkey, slen, | |
251 | min, nbits, max, | |
252 | dh->p, dh->g, | |
253 | dh->pub_key, | |
254 | dh_server_pub, | |
08822d99 | 255 | shared_secret, |
256 | &hash, &hashlen | |
fe4ad273 | 257 | ); |
258 | } else { | |
259 | /* The GSS hash is identical to the DH one */ | |
08822d99 | 260 | kex_dh_hash( kex->client_version_string, |
fe4ad273 | 261 | kex->server_version_string, |
262 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
263 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
264 | serverhostkey, slen, /* server host key */ | |
265 | dh->pub_key, /* e */ | |
266 | dh_server_pub, /* f */ | |
08822d99 | 267 | shared_secret, /* K */ |
268 | &hash, &hashlen | |
fe4ad273 | 269 | ); |
b46fa825 | 270 | } |
fe4ad273 | 271 | |
272 | gssbuf.value = hash; | |
f472818d | 273 | gssbuf.length = hashlen; |
fe4ad273 | 274 | |
275 | /* Verify that the hash matches the MIC we just got. */ | |
276 | if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok))) | |
277 | packet_disconnect("Hash's MIC didn't verify"); | |
278 | ||
279 | xfree(msg_tok.value); | |
280 | ||
281 | DH_free(dh); | |
282 | if (serverhostkey) | |
283 | xfree(serverhostkey); | |
284 | BN_clear_free(dh_server_pub); | |
285 | ||
286 | /* save session id */ | |
287 | if (kex->session_id == NULL) { | |
f472818d | 288 | kex->session_id_len = hashlen; |
fe4ad273 | 289 | kex->session_id = xmalloc(kex->session_id_len); |
290 | memcpy(kex->session_id, hash, kex->session_id_len); | |
291 | } | |
292 | ||
293 | if (gss_kex_context == NULL) | |
294 | gss_kex_context = ctxt; | |
295 | else | |
296 | ssh_gssapi_delete_ctx(&ctxt); | |
297 | ||
08822d99 | 298 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
b46fa825 | 299 | BN_clear_free(shared_secret); |
fe4ad273 | 300 | kex_finish(kex); |
b46fa825 | 301 | } |
302 | ||
303 | #endif /* GSSAPI */ |