]> andersk Git - gssapi-openssh.git/blame - openssh/kexgsss.c
Protocol 2 only by default
[gssapi-openssh.git] / openssh / kexgsss.c
CommitLineData
1c14df9e 1/*
34fee935 2 * Copyright (c) 2001-2005 Simon Wilkinson. All rights reserved.
1c14df9e 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"
88928908 42
43static void kex_gss_send_error(Gssctxt *ctxt);
1c14df9e 44
45void
46kexgss_server(Kex *kex)
47{
1c14df9e 48 OM_uint32 maj_status, min_status;
49
34fee935 50 /*
51 * Some GSSAPI implementations use the input value of ret_flags (an
1c14df9e 52 * output variable) as a means of triggering mechanism specific
53 * features. Initializing it to zero avoids inadvertently
34fee935 54 * activating this non-standard behaviour.
55 */
1c14df9e 56
57 OM_uint32 ret_flags = 0;
34fee935 58 gss_buffer_desc gssbuf, recv_tok, msg_tok;
59 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1c14df9e 60 Gssctxt *ctxt = NULL;
e00da40d 61 u_int slen, klen, kout, hashlen;
62 u_char *kbuf, *hash;
34fee935 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;
88928908 69 gss_OID oid;
1c14df9e 70
71 /* Initialise GSSAPI */
72
34fee935 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)))) {
88928908 88 kex_gss_send_error(ctxt);
34fee935 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
1c14df9e 120 do {
121 debug("Wait SSH2_MSG_GSSAPI_INIT");
122 type = packet_read();
123 switch(type) {
124 case SSH2_MSG_KEXGSS_INIT:
34fee935 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 */
1c14df9e 136 break;
137 case SSH2_MSG_KEXGSS_CONTINUE:
34fee935 138 recv_tok.value = packet_get_string(&slen);
139 recv_tok.length = slen;
1c14df9e 140 break;
141 default:
34fee935 142 packet_disconnect(
143 "Protocol error: didn't expect packet type %d",
144 type);
1c14df9e 145 }
1c14df9e 146
34fee935 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)
88928908 153 fatal("Zero length token output when incomplete");
88928908 154
155 if (dh_client_pub == NULL)
156 fatal("No client public key");
1c14df9e 157
158 if (maj_status & GSS_S_CONTINUE_NEEDED) {
159 debug("Sending GSSAPI_CONTINUE");
160 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
34fee935 161 packet_put_string(send_tok.value, send_tok.length);
1c14df9e 162 packet_send();
1c14df9e 163 gss_release_buffer(&min_status, &send_tok);
164 }
165 } while (maj_status & GSS_S_CONTINUE_NEEDED);
166
167 if (GSS_ERROR(maj_status)) {
88928908 168 kex_gss_send_error(ctxt);
34fee935 169 if (send_tok.length > 0) {
88928908 170 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
34fee935 171 packet_put_string(send_tok.value, send_tok.length);
88928908 172 packet_send();
34fee935 173 }
174 packet_disconnect("GSSAPI Key Exchange handshake failed");
1c14df9e 175 }
34fee935 176
88928908 177 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
34fee935 178 fatal("Mutual Authentication flag wasn't set");
179
88928908 180 if (!(ret_flags & GSS_C_INTEG_FLAG))
34fee935 181 fatal("Integrity flag wasn't set");
1c14df9e 182
34fee935 183 if (!dh_pub_is_valid(dh, dh_client_pub))
184 packet_disconnect("bad client public DH value");
1c14df9e 185
34fee935 186 klen = DH_size(dh);
187 kbuf = xmalloc(klen);
188 kout = DH_compute_key(kbuf, dh_client_pub, dh);
1c14df9e 189
190 shared_secret = BN_new();
191 BN_bin2bn(kbuf, kout, shared_secret);
192 memset(kbuf, 0, klen);
193 xfree(kbuf);
34fee935 194
195 if (gex) {
e00da40d 196 kexgex_hash(
197 kex->evp_md,
34fee935 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,
e00da40d 206 shared_secret,
207 &hash, &hashlen
34fee935 208 );
209 }
210 else {
211 /* The GSSAPI hash is identical to the Diffie Helman one */
e00da40d 212 kex_dh_hash(
34fee935 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 */
e00da40d 217 dh_client_pub, dh->pub_key, shared_secret,
218 &hash, &hashlen
34fee935 219 );
220 }
1c14df9e 221 BN_free(dh_client_pub);
34fee935 222
1c14df9e 223 if (kex->session_id == NULL) {
e00da40d 224 kex->session_id_len = hashlen;
1c14df9e 225 kex->session_id = xmalloc(kex->session_id_len);
226 memcpy(kex->session_id, hash, kex->session_id_len);
227 }
34fee935 228
1c14df9e 229 gssbuf.value = hash;
e00da40d 230 gssbuf.length = hashlen;
34fee935 231
232 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
88928908 233 fatal("Couldn't get MIC");
34fee935 234
1c14df9e 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
34fee935 239 if (send_tok.length != 0) {
1c14df9e 240 packet_put_char(1); /* true */
34fee935 241 packet_put_string((char *)send_tok.value, send_tok.length);
1c14df9e 242 } else {
243 packet_put_char(0); /* false */
244 }
34fee935 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
1c14df9e 255 DH_free(dh);
256
e00da40d 257 kex_derive_keys(kex, hash, hashlen, shared_secret);
1c14df9e 258 BN_clear_free(shared_secret);
259 kex_finish(kex);
260}
261
88928908 262static void
263kex_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}
1c14df9e 280#endif /* GSSAPI */
This page took 0.095314 seconds and 5 git commands to generate.