1 /*
2 * Copyright (c) 2001-2003 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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 #pragma ident "%Z%%M% %I% %E% SMI"
30
31 #include "includes.h"
32
33 #ifdef GSSAPI
34
35 #include <openssl/opensslconf.h>
36 #include <openssl/crypto.h>
37 #include <openssl/bn.h>
38
39 #include "xmalloc.h"
40 #include "buffer.h"
41 #include "bufaux.h"
42 #include "compat.h"
43 #include "kex.h"
44 #include "log.h"
45 #include "packet.h"
46 #include "dh.h"
47 #include "ssh2.h"
48 #include "ssh-gss.h"
49 #include "auth.h"
50
51 Gssctxt *xxx_gssctxt;
52 extern Authctxt *x_authctxt;
53
54 static void kex_gss_send_error(Gssctxt *ctxt);
55
56 void
57 kexgss_server(Kex *kex)
58 {
59 OM_uint32 maj_status, min_status;
60 gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok;
61 Gssctxt *ctxt = NULL;
62 unsigned int klen, kout;
63 unsigned int sbloblen = 0;
64 unsigned char *kbuf, *hash;
65 unsigned char *server_host_key_blob = NULL;
66 DH *dh;
67 Key *server_host_key = NULL;
68 BIGNUM *shared_secret = NULL;
69 BIGNUM *dh_client_pub = NULL;
70 int type = 0;
71 uint_t slen;
72 gss_OID oid;
73
74 /*
75 * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet
76 * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's
77 * the GSS-API that provides for server host authentication.
78 */
79 if (kex->load_host_key != NULL &&
80 !(datafellows & SSH_BUG_GSSKEX_HOSTKEY))
81 server_host_key = kex->load_host_key(kex->hostkey_type);
82 if (server_host_key != NULL)
83 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
84
85
86 /* Initialise GSSAPI */
87
88 ssh_gssapi_oid_of_kexname(kex->name, &oid);
89 if (oid == GSS_C_NULL_OID) {
90 fatal("Couldn't match the negotiated GSS key exchange");
91 }
92
93 ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid);
94
95 ctxt = xxx_gssctxt;
96
97 do {
98 debug("Wait SSH2_MSG_GSSAPI_INIT");
99 type = packet_read();
100 switch (type) {
101 case SSH2_MSG_KEXGSS_INIT:
102 if (dh_client_pub != NULL)
103 fatal("Received KEXGSS_INIT after "
104 "initialising");
105 recv_tok.value = packet_get_string(&slen);
106 recv_tok.length = slen; /* int vs. size_t */
107
108 dh_client_pub = BN_new();
109
110 if (dh_client_pub == NULL)
111 fatal("dh_client_pub == NULL");
112 packet_get_bignum2(dh_client_pub);
113
114 /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
115 if (sbloblen) {
116 packet_start(SSH2_MSG_KEXGSS_HOSTKEY);
117 packet_put_string(server_host_key_blob,
118 sbloblen);
119 packet_send();
120 packet_write_wait();
121 }
122 break;
123 case SSH2_MSG_KEXGSS_CONTINUE:
124 recv_tok.value = packet_get_string(&slen);
125 recv_tok.length = slen; /* int vs. size_t */
126 break;
127 default:
128 packet_disconnect("Protocol error: didn't expect "
129 "packet type %d", type);
130 }
131
132 maj_status = ssh_gssapi_accept_ctx(ctxt, &recv_tok, &send_tok);
133
134 xfree(recv_tok.value); /* We allocated this, not gss */
135
136 if (dh_client_pub == NULL)
137 fatal("No client public key");
138
139 if (maj_status == GSS_S_CONTINUE_NEEDED) {
140 debug("Sending GSSAPI_CONTINUE");
141 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
142 packet_put_string(send_tok.value, send_tok.length);
143 packet_send();
144 packet_write_wait();
145 (void) gss_release_buffer(&min_status, &send_tok);
146 }
147 } while (maj_status == GSS_S_CONTINUE_NEEDED);
148
149 if (GSS_ERROR(maj_status)) {
150 kex_gss_send_error(ctxt);
151 if (send_tok.length > 0) {
152 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
153 packet_put_string(send_tok.value, send_tok.length);
154 packet_send();
155 packet_write_wait();
156 (void) gss_release_buffer(&min_status, &send_tok);
157 }
158 fatal("accept_ctx died");
159 }
160
161 debug("gss_complete");
162 if (!(ctxt->flags & GSS_C_MUTUAL_FLAG))
163 fatal("Mutual authentication flag wasn't set");
164
165 if (!(ctxt->flags & GSS_C_INTEG_FLAG))
166 fatal("Integrity flag wasn't set");
167
168 dh = dh_new_group1();
169 dh_gen_key(dh, kex->we_need * 8);
170
171 if (!dh_pub_is_valid(dh, dh_client_pub))
172 packet_disconnect("bad client public DH value");
173
174 klen = DH_size(dh);
175 kbuf = xmalloc(klen);
176 kout = DH_compute_key(kbuf, dh_client_pub, dh);
177
178 shared_secret = BN_new();
179 BN_bin2bn(kbuf, kout, shared_secret);
180 (void) memset(kbuf, 0, klen);
181 xfree(kbuf);
182
183 /* The GSSAPI hash is identical to the Diffie Helman one */
184 hash = kex_dh_hash(
185 kex->client_version_string,
186 kex->server_version_string,
187 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
188 buffer_ptr(&kex->my), buffer_len(&kex->my),
189 server_host_key_blob, sbloblen,
190 dh_client_pub,
191 dh->pub_key,
192 shared_secret);
193
194 BN_free(dh_client_pub);
195
196 if (kex->session_id == NULL) {
197 kex->session_id_len = 20;
198 kex->session_id = xmalloc(kex->session_id_len);
199 (void) memcpy(kex->session_id, hash, kex->session_id_len);
200 } else if (x_authctxt != NULL && x_authctxt->success) {
201 ssh_gssapi_storecreds(ctxt, x_authctxt);
202 }
203
204 /* Should fix kex_dh_hash to output hash length */
205 gssbuf.length = 20; /* yes, it's always 20 (SHA-1) */
206 gssbuf.value = hash; /* and it's static constant storage */
207
208 if (GSS_ERROR(ssh_gssapi_get_mic(ctxt, &gssbuf, &msg_tok))) {
209 kex_gss_send_error(ctxt);
210 fatal("Couldn't get MIC");
211 }
212
213 packet_start(SSH2_MSG_KEXGSS_COMPLETE);
214 packet_put_bignum2(dh->pub_key);
215 packet_put_string((char *)msg_tok.value, msg_tok.length);
216 (void) gss_release_buffer(&min_status, &msg_tok);
217
218 if (send_tok.length != 0) {
219 packet_put_char(1); /* true */
220 packet_put_string((char *)send_tok.value, send_tok.length);
221 (void) gss_release_buffer(&min_status, &send_tok);
222 } else {
223 packet_put_char(0); /* false */
224 }
225 packet_send();
226 packet_write_wait();
227
228 DH_free(dh);
229
230 kex_derive_keys(kex, hash, shared_secret);
231 BN_clear_free(shared_secret);
232 kex_finish(kex);
233 }
234
235 static void
236 kex_gss_send_error(Gssctxt *ctxt) {
237 char *errstr;
238 OM_uint32 maj, min;
239
240 errstr = ssh_gssapi_last_error(ctxt, &maj, &min);
241 if (errstr) {
242 packet_start(SSH2_MSG_KEXGSS_ERROR);
243 packet_put_int(maj);
244 packet_put_int(min);
245 packet_put_cstring(errstr);
246 packet_put_cstring("");
247 packet_send();
248 packet_write_wait();
249 /* XXX - We should probably log the error locally here */
250 xfree(errstr);
251 }
252 }
253 #endif /* GSSAPI */