2 * Copyright (c) 2001-2003 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.
29 #include <openssl/crypto.h>
30 #include <openssl/bn.h>
43 kexgss_client(Kex *kex)
45 gss_buffer_desc gssbuf,send_tok,recv_tok, msg_tok, *token_ptr;
47 OM_uint32 maj_status, min_status, ret_flags;
48 unsigned int klen, kout;
50 BIGNUM *dh_server_pub = 0;
51 BIGNUM *shared_secret = 0;
54 unsigned char *serverhostkey;
61 /* Initialise our GSSAPI world */
62 ssh_gssapi_build_ctx(&ctxt);
63 if (ssh_gssapi_client_id_kex(ctxt,kex->name)==NULL) {
64 fatal("Couldn't identify host exchange");
67 if (ssh_gssapi_import_name(ctxt,kex->host)) {
68 fatal("Couldn't import hostname ");
71 /* This code should match that in ssh_dh1_client */
73 /* Step 1 - e is dh->pub_key */
75 dh_gen_key(dh, kex->we_need * 8);
77 /* This is f, we initialise it now to make life easier */
78 dh_server_pub = BN_new();
79 if (dh_server_pub == NULL) {
80 fatal("dh_server_pub == NULL");
83 token_ptr = GSS_C_NO_BUFFER;
86 debug("Calling gss_init_sec_context");
88 maj_status=ssh_gssapi_init_ctx(ctxt,
89 kex->options.gss_deleg_creds,
93 if (GSS_ERROR(maj_status)) {
94 if (send_tok.length!=0) {
95 /* Hmmm - not sure about this */
96 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
97 packet_put_string(send_tok.value,
100 fatal("gss_init_context failed");
103 /* If we've got an old receive buffer get rid of it */
104 if (token_ptr != GSS_C_NO_BUFFER)
105 (void) gss_release_buffer(&min_status, &recv_tok);
108 if (maj_status == GSS_S_COMPLETE) {
109 /* If mutual state flag is not true, kex fails */
110 if (!(ret_flags & GSS_C_MUTUAL_FLAG)) {
111 fatal("Mutual authentication failed");
113 /* If integ avail flag is not true kex fails */
114 if (!(ret_flags & GSS_C_INTEG_FLAG)) {
115 fatal("Integrity check failed");
119 /* If we have data to send, then the last message that we
120 * received cannot have been a 'complete'. */
121 if (send_tok.length !=0) {
123 packet_start(SSH2_MSG_KEXGSS_INIT);
124 packet_put_string(send_tok.value,
126 packet_put_bignum2(dh->pub_key);
129 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
130 packet_put_string(send_tok.value,
137 /* If we've sent them data, they'd better be polite
140 type = packet_read();
142 case SSH2_MSG_KEXGSS_HOSTKEY:
143 debug("Received KEXGSS_HOSTKEY");
144 serverhostkey=packet_get_string(&slen);
146 case SSH2_MSG_KEXGSS_CONTINUE:
147 debug("Received GSSAPI_CONTINUE");
148 if (maj_status == GSS_S_COMPLETE)
149 fatal("GSSAPI Continue received from server when complete");
150 recv_tok.value=packet_get_string(&recv_tok.length);
152 case SSH2_MSG_KEXGSS_COMPLETE:
153 debug("Received GSSAPI_COMPLETE");
154 packet_get_bignum2(dh_server_pub);
156 packet_get_string(&msg_tok.length);
158 /* Is there a token included? */
159 if (packet_get_char()) {
161 packet_get_string(&recv_tok.length);
162 /* If we're already complete - protocol error */
163 if (maj_status == GSS_S_COMPLETE)
164 packet_disconnect("Protocol error: received token when complete");
166 /* No token included */
167 if (maj_status != GSS_S_COMPLETE)
168 packet_disconnect("Protocol error: did not receive final token");
171 case SSH2_MSG_KEXGSS_ERROR:
172 debug("Received Error");
173 maj_status=packet_get_int();
174 min_status=packet_get_int();
175 msg=packet_get_string(NULL);
176 lang=packet_get_string(NULL);
177 fprintf(stderr,"GSSAPI Error: \n%s",msg);
179 packet_disconnect("Protocol error: didn't expect packet type %d",
184 /* No data, and not complete */
185 if (maj_status!=GSS_S_COMPLETE) {
186 fatal("Not complete, and no token output");
189 } while (maj_status & GSS_S_CONTINUE_NEEDED);
191 /* We _must_ have received a COMPLETE message in reply from the
192 * server, which will have set dh_server_pub and msg_tok */
194 if (type!=SSH2_MSG_KEXGSS_COMPLETE)
195 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
197 /* Check f in range [1, p-1] */
198 if (!dh_pub_is_valid(dh, dh_server_pub))
199 packet_disconnect("bad server public DH value");
201 /* compute K=f^x mod p */
203 kbuf = xmalloc(klen);
204 kout = DH_compute_key(kbuf, dh_server_pub, dh);
206 shared_secret = BN_new();
207 BN_bin2bn(kbuf,kout, shared_secret);
208 memset(kbuf, 0, klen);
211 /* The GSS hash is identical to the DH one */
213 kex->client_version_string,
214 kex->server_version_string,
215 buffer_ptr(&kex->my), buffer_len(&kex->my),
216 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
217 serverhostkey, slen, /* server host key */
219 dh_server_pub, /* f */
220 shared_secret /* K */
226 /* Verify that H matches the token we just got. */
227 if ((maj_status = gss_verify_mic(&min_status,
233 packet_disconnect("Hash's MIC didn't verify");
237 ssh_gssapi_delete_ctx(&ctxt);
238 /* save session id */
239 if (kex->session_id == NULL) {
240 kex->session_id_len = 20;
241 kex->session_id = xmalloc(kex->session_id_len);
242 memcpy(kex->session_id, hash, kex->session_id_len);
245 kex_derive_keys(kex, hash, shared_secret);
246 BN_clear_free(shared_secret);