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