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