]>
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" | |
41 | #include "ssh2.h" | |
42 | #include "ssh-gss.h" | |
43 | #include "monitor_wrap.h" | |
23987cb8 | 44 | |
45 | static void kex_gss_send_error(Gssctxt *ctxt); | |
b46fa825 | 46 | |
47 | void | |
48 | kexgss_server(Kex *kex) | |
49 | { | |
b46fa825 | 50 | OM_uint32 maj_status, min_status; |
51 | ||
fe4ad273 | 52 | /* |
53 | * Some GSSAPI implementations use the input value of ret_flags (an | |
e23e524c | 54 | * output variable) as a means of triggering mechanism specific |
55 | * features. Initializing it to zero avoids inadvertently | |
fe4ad273 | 56 | * activating this non-standard behaviour. |
57 | */ | |
b46fa825 | 58 | |
59 | OM_uint32 ret_flags = 0; | |
fe4ad273 | 60 | gss_buffer_desc gssbuf, recv_tok, msg_tok; |
61 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
b46fa825 | 62 | Gssctxt *ctxt = NULL; |
f472818d | 63 | u_int slen, klen, kout, hashlen; |
64 | u_char *kbuf, *hash; | |
fe4ad273 | 65 | DH *dh; |
66 | int min = -1, max = -1, nbits = -1; | |
67 | BIGNUM *shared_secret = NULL; | |
68 | BIGNUM *dh_client_pub = NULL; | |
69 | int type = 0; | |
70 | int gex; | |
23987cb8 | 71 | gss_OID oid; |
b46fa825 | 72 | |
73 | /* Initialise GSSAPI */ | |
74 | ||
fe4ad273 | 75 | /* If we're rekeying, privsep means that some of the private structures |
76 | * in the GSSAPI code are no longer available. This kludges them back | |
77 | * into life | |
78 | */ | |
79 | if (!ssh_gssapi_oid_table_ok()) | |
80 | ssh_gssapi_server_mechanisms(); | |
81 | ||
82 | debug2("%s: Identifying %s", __func__, kex->name); | |
83 | oid = ssh_gssapi_id_kex(NULL, kex->name, &gex); | |
84 | if (oid == NULL) | |
85 | fatal("Unknown gssapi mechanism"); | |
86 | ||
87 | debug2("%s: Acquiring credentials", __func__); | |
88 | ||
89 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) { | |
23987cb8 | 90 | kex_gss_send_error(ctxt); |
fe4ad273 | 91 | fatal("Unable to acquire credentials for the server"); |
92 | } | |
93 | ||
94 | if (gex) { | |
95 | debug("Doing group exchange"); | |
96 | packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ); | |
97 | min = packet_get_int(); | |
98 | nbits = packet_get_int(); | |
99 | max = packet_get_int(); | |
100 | min = MAX(DH_GRP_MIN, min); | |
101 | max = MIN(DH_GRP_MAX, max); | |
102 | packet_check_eom(); | |
103 | if (max < min || nbits < min || max < nbits) | |
104 | fatal("GSS_GEX, bad parameters: %d !< %d !< %d", | |
105 | min, nbits, max); | |
106 | dh = PRIVSEP(choose_dh(min, nbits, max)); | |
107 | if (dh == NULL) | |
108 | packet_disconnect("Protocol error: no matching group found"); | |
109 | ||
110 | packet_start(SSH2_MSG_KEXGSS_GROUP); | |
111 | packet_put_bignum2(dh->p); | |
112 | packet_put_bignum2(dh->g); | |
113 | packet_send(); | |
114 | ||
115 | packet_write_wait(); | |
116 | ||
117 | } else { | |
118 | dh = dh_new_group1(); | |
119 | } | |
120 | dh_gen_key(dh, kex->we_need * 8); | |
121 | ||
b46fa825 | 122 | do { |
123 | debug("Wait SSH2_MSG_GSSAPI_INIT"); | |
124 | type = packet_read(); | |
125 | switch(type) { | |
126 | case SSH2_MSG_KEXGSS_INIT: | |
fe4ad273 | 127 | if (dh_client_pub != NULL) |
128 | fatal("Received KEXGSS_INIT after initialising"); | |
129 | recv_tok.value = packet_get_string(&slen); | |
130 | recv_tok.length = slen; | |
131 | ||
132 | if ((dh_client_pub = BN_new()) == NULL) | |
133 | fatal("dh_client_pub == NULL"); | |
134 | ||
135 | packet_get_bignum2(dh_client_pub); | |
136 | ||
137 | /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ | |
b46fa825 | 138 | break; |
139 | case SSH2_MSG_KEXGSS_CONTINUE: | |
fe4ad273 | 140 | recv_tok.value = packet_get_string(&slen); |
141 | recv_tok.length = slen; | |
b46fa825 | 142 | break; |
143 | default: | |
fe4ad273 | 144 | packet_disconnect( |
145 | "Protocol error: didn't expect packet type %d", | |
146 | type); | |
b46fa825 | 147 | } |
b46fa825 | 148 | |
fe4ad273 | 149 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok, |
150 | &send_tok, &ret_flags)); | |
151 | ||
152 | xfree(recv_tok.value); | |
153 | ||
154 | if (maj_status != GSS_S_COMPLETE && send_tok.length == 0) | |
155 | fatal("Zero length token output when incomplete"); | |
156 | ||
e23e524c | 157 | if (dh_client_pub == NULL) |
158 | fatal("No client public key"); | |
159 | ||
c045675e | 160 | if (maj_status & GSS_S_CONTINUE_NEEDED) { |
b46fa825 | 161 | debug("Sending GSSAPI_CONTINUE"); |
162 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); | |
fe4ad273 | 163 | packet_put_string(send_tok.value, send_tok.length); |
b46fa825 | 164 | packet_send(); |
b46fa825 | 165 | gss_release_buffer(&min_status, &send_tok); |
166 | } | |
167 | } while (maj_status & GSS_S_CONTINUE_NEEDED); | |
168 | ||
169 | if (GSS_ERROR(maj_status)) { | |
23987cb8 | 170 | kex_gss_send_error(ctxt); |
fe4ad273 | 171 | if (send_tok.length > 0) { |
e23e524c | 172 | packet_start(SSH2_MSG_KEXGSS_CONTINUE); |
fe4ad273 | 173 | packet_put_string(send_tok.value, send_tok.length); |
e23e524c | 174 | packet_send(); |
fe4ad273 | 175 | } |
176 | packet_disconnect("GSSAPI Key Exchange handshake failed"); | |
b46fa825 | 177 | } |
fe4ad273 | 178 | |
23987cb8 | 179 | if (!(ret_flags & GSS_C_MUTUAL_FLAG)) |
fe4ad273 | 180 | fatal("Mutual Authentication flag wasn't set"); |
181 | ||
23987cb8 | 182 | if (!(ret_flags & GSS_C_INTEG_FLAG)) |
fe4ad273 | 183 | fatal("Integrity flag wasn't set"); |
b46fa825 | 184 | |
fe4ad273 | 185 | if (!dh_pub_is_valid(dh, dh_client_pub)) |
186 | packet_disconnect("bad client public DH value"); | |
b46fa825 | 187 | |
fe4ad273 | 188 | klen = DH_size(dh); |
189 | kbuf = xmalloc(klen); | |
190 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
b46fa825 | 191 | |
192 | shared_secret = BN_new(); | |
193 | BN_bin2bn(kbuf, kout, shared_secret); | |
194 | memset(kbuf, 0, klen); | |
195 | xfree(kbuf); | |
fe4ad273 | 196 | |
197 | if (gex) { | |
08822d99 | 198 | kexgex_hash( |
199 | kex->evp_md, | |
fe4ad273 | 200 | kex->client_version_string, kex->server_version_string, |
201 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
202 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
203 | NULL, 0, | |
204 | min, nbits, max, | |
205 | dh->p, dh->g, | |
206 | dh_client_pub, | |
207 | dh->pub_key, | |
08822d99 | 208 | shared_secret, |
209 | &hash, &hashlen | |
fe4ad273 | 210 | ); |
211 | } | |
212 | else { | |
213 | /* The GSSAPI hash is identical to the Diffie Helman one */ | |
08822d99 | 214 | kex_dh_hash( |
fe4ad273 | 215 | kex->client_version_string, kex->server_version_string, |
216 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
217 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
218 | NULL, 0, /* Change this if we start sending host keys */ | |
08822d99 | 219 | dh_client_pub, dh->pub_key, shared_secret, |
220 | &hash, &hashlen | |
fe4ad273 | 221 | ); |
222 | } | |
b46fa825 | 223 | BN_free(dh_client_pub); |
fe4ad273 | 224 | |
b46fa825 | 225 | if (kex->session_id == NULL) { |
d33fd7cd | 226 | kex->session_id_len = hashlen; |
b46fa825 | 227 | kex->session_id = xmalloc(kex->session_id_len); |
228 | memcpy(kex->session_id, hash, kex->session_id_len); | |
229 | } | |
fe4ad273 | 230 | |
b46fa825 | 231 | gssbuf.value = hash; |
d33fd7cd | 232 | gssbuf.length = hashlen; |
fe4ad273 | 233 | |
234 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok)))) | |
23987cb8 | 235 | fatal("Couldn't get MIC"); |
fe4ad273 | 236 | |
b46fa825 | 237 | packet_start(SSH2_MSG_KEXGSS_COMPLETE); |
238 | packet_put_bignum2(dh->pub_key); | |
239 | packet_put_string((char *)msg_tok.value,msg_tok.length); | |
240 | ||
fe4ad273 | 241 | if (send_tok.length != 0) { |
b46fa825 | 242 | packet_put_char(1); /* true */ |
fe4ad273 | 243 | packet_put_string((char *)send_tok.value, send_tok.length); |
b46fa825 | 244 | } else { |
245 | packet_put_char(0); /* false */ | |
246 | } | |
fe4ad273 | 247 | packet_send(); |
248 | ||
249 | gss_release_buffer(&min_status, &send_tok); | |
250 | gss_release_buffer(&min_status, &msg_tok); | |
251 | ||
252 | if (gss_kex_context == NULL) | |
253 | gss_kex_context = ctxt; | |
254 | else | |
255 | ssh_gssapi_delete_ctx(&ctxt); | |
256 | ||
b46fa825 | 257 | DH_free(dh); |
258 | ||
08822d99 | 259 | kex_derive_keys(kex, hash, hashlen, shared_secret); |
b46fa825 | 260 | BN_clear_free(shared_secret); |
261 | kex_finish(kex); | |
262 | } | |
263 | ||
23987cb8 | 264 | static void |
265 | kex_gss_send_error(Gssctxt *ctxt) { | |
266 | char *errstr; | |
267 | OM_uint32 maj,min; | |
268 | ||
269 | errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min)); | |
270 | if (errstr) { | |
271 | packet_start(SSH2_MSG_KEXGSS_ERROR); | |
272 | packet_put_int(maj); | |
273 | packet_put_int(min); | |
274 | packet_put_cstring(errstr); | |
275 | packet_put_cstring(""); | |
276 | packet_send(); | |
277 | packet_write_wait(); | |
278 | /* XXX - We should probably log the error locally here */ | |
279 | xfree(errstr); | |
280 | } | |
281 | } | |
b46fa825 | 282 | #endif /* GSSAPI */ |