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