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