2 * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #include <openssl/crypto.h>
32 #include <openssl/bn.h>
44 #include "monitor_wrap.h"
47 static void kex_gss_send_error(Gssctxt *ctxt);
48 extern ServerOptions options;
51 kexgss_server(Kex *kex)
53 OM_uint32 maj_status, min_status;
56 * Some GSSAPI implementations use the input value of ret_flags (an
57 * output variable) as a means of triggering mechanism specific
58 * features. Initializing it to zero avoids inadvertently
59 * activating this non-standard behaviour.
62 OM_uint32 ret_flags = 0;
63 gss_buffer_desc gssbuf, recv_tok, msg_tok;
64 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
66 u_int slen, klen, kout, hashlen;
69 int min = -1, max = -1, nbits = -1;
70 BIGNUM *shared_secret = NULL;
71 BIGNUM *dh_client_pub = NULL;
76 /* Initialise GSSAPI */
78 /* If we're rekeying, privsep means that some of the private structures
79 * in the GSSAPI code are no longer available. This kludges them back
82 if (!ssh_gssapi_oid_table_ok())
83 if ((mechs = ssh_gssapi_server_mechanisms()))
86 debug2("%s: Identifying %s", __func__, kex->name);
87 oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
88 if (oid == GSS_C_NO_OID)
89 fatal("Unknown gssapi mechanism");
91 debug2("%s: Acquiring credentials", __func__);
93 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) {
94 kex_gss_send_error(ctxt);
95 fatal("Unable to acquire credentials for the server");
98 switch (kex->kex_type) {
99 case KEX_GSS_GRP1_SHA1:
100 dh = dh_new_group1();
102 case KEX_GSS_GRP14_SHA1:
103 dh = dh_new_group14();
105 case KEX_GSS_GEX_SHA1:
106 debug("Doing group exchange");
107 packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
108 min = packet_get_int();
109 nbits = packet_get_int();
110 max = packet_get_int();
111 min = MAX(DH_GRP_MIN, min);
112 max = MIN(DH_GRP_MAX, max);
114 if (max < min || nbits < min || max < nbits)
115 fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
117 dh = PRIVSEP(choose_dh(min, nbits, max));
119 packet_disconnect("Protocol error: no matching group found");
121 packet_start(SSH2_MSG_KEXGSS_GROUP);
122 packet_put_bignum2(dh->p);
123 packet_put_bignum2(dh->g);
129 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
132 dh_gen_key(dh, kex->we_need * 8);
135 debug("Wait SSH2_MSG_GSSAPI_INIT");
136 type = packet_read();
138 case SSH2_MSG_KEXGSS_INIT:
139 if (dh_client_pub != NULL)
140 fatal("Received KEXGSS_INIT after initialising");
141 recv_tok.value = packet_get_string(&slen);
142 recv_tok.length = slen;
144 if ((dh_client_pub = BN_new()) == NULL)
145 fatal("dh_client_pub == NULL");
147 packet_get_bignum2(dh_client_pub);
149 /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
151 case SSH2_MSG_KEXGSS_CONTINUE:
152 recv_tok.value = packet_get_string(&slen);
153 recv_tok.length = slen;
157 "Protocol error: didn't expect packet type %d",
161 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
162 &send_tok, &ret_flags));
164 xfree(recv_tok.value);
166 if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
167 fatal("Zero length token output when incomplete");
169 if (dh_client_pub == NULL)
170 fatal("No client public key");
172 if (maj_status & GSS_S_CONTINUE_NEEDED) {
173 debug("Sending GSSAPI_CONTINUE");
174 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
175 packet_put_string((char *)send_tok.value, send_tok.length);
177 gss_release_buffer(&min_status, &send_tok);
179 } while (maj_status & GSS_S_CONTINUE_NEEDED);
181 if (GSS_ERROR(maj_status)) {
182 kex_gss_send_error(ctxt);
183 if (send_tok.length > 0) {
184 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
185 packet_put_string((char *)send_tok.value, send_tok.length);
188 packet_disconnect("GSSAPI Key Exchange handshake failed");
191 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
192 fatal("Mutual Authentication flag wasn't set");
194 if (!(ret_flags & GSS_C_INTEG_FLAG))
195 fatal("Integrity flag wasn't set");
197 if (!dh_pub_is_valid(dh, dh_client_pub))
198 packet_disconnect("bad client public DH value");
201 kbuf = xmalloc(klen);
202 kout = DH_compute_key(kbuf, dh_client_pub, dh);
204 fatal("DH_compute_key: failed");
206 shared_secret = BN_new();
207 if (shared_secret == NULL)
208 fatal("kexgss_server: BN_new failed");
210 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
211 fatal("kexgss_server: BN_bin2bn failed");
213 memset(kbuf, 0, klen);
216 switch (kex->kex_type) {
217 case KEX_GSS_GRP1_SHA1:
218 case KEX_GSS_GRP14_SHA1:
220 kex->client_version_string, kex->server_version_string,
221 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
222 buffer_ptr(&kex->my), buffer_len(&kex->my),
223 NULL, 0, /* Change this if we start sending host keys */
224 dh_client_pub, dh->pub_key, shared_secret,
228 case KEX_GSS_GEX_SHA1:
231 kex->client_version_string, kex->server_version_string,
232 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
233 buffer_ptr(&kex->my), buffer_len(&kex->my),
244 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
247 BN_clear_free(dh_client_pub);
249 if (kex->session_id == NULL) {
250 kex->session_id_len = hashlen;
251 kex->session_id = xmalloc(kex->session_id_len);
252 memcpy(kex->session_id, hash, kex->session_id_len);
256 gssbuf.length = hashlen;
258 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
259 fatal("Couldn't get MIC");
261 packet_start(SSH2_MSG_KEXGSS_COMPLETE);
262 packet_put_bignum2(dh->pub_key);
263 packet_put_string((char *)msg_tok.value,msg_tok.length);
265 if (send_tok.length != 0) {
266 packet_put_char(1); /* true */
267 packet_put_string((char *)send_tok.value, send_tok.length);
269 packet_put_char(0); /* false */
273 gss_release_buffer(&min_status, &send_tok);
274 gss_release_buffer(&min_status, &msg_tok);
276 if (gss_kex_context == NULL)
277 gss_kex_context = ctxt;
279 ssh_gssapi_delete_ctx(&ctxt);
283 kex_derive_keys(kex, hash, hashlen, shared_secret);
284 BN_clear_free(shared_secret);
287 /* If this was a rekey, then save out any delegated credentials we
289 if (options.gss_store_rekey)
290 ssh_gssapi_rekey_creds();
294 kex_gss_send_error(Gssctxt *ctxt) {
298 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
300 packet_start(SSH2_MSG_KEXGSS_ERROR);
303 packet_put_cstring(errstr);
304 packet_put_cstring("");
307 /* XXX - We should probably log the error locally here */