xref: /titanic_44/usr/src/cmd/ssh/libssh/common/kexgsss.c (revision 7c1a057666319a524c052ff0e99e9e26c2695e77)
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 2006 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/crypto.h>
36 #include <openssl/bn.h>
37 
38 #include "xmalloc.h"
39 #include "buffer.h"
40 #include "bufaux.h"
41 #include "compat.h"
42 #include "kex.h"
43 #include "log.h"
44 #include "packet.h"
45 #include "dh.h"
46 #include "ssh2.h"
47 #include "ssh-gss.h"
48 #include "monitor_wrap.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 	u_int 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 initialising");
104 			recv_tok.value=packet_get_string(&slen);
105 			recv_tok.length=slen; /* int vs. size_t */
106 
107 		        dh_client_pub = BN_new();
108 
109 		        if (dh_client_pub == NULL)
110         			fatal("dh_client_pub == NULL");
111 		  	packet_get_bignum2(dh_client_pub);
112 
113 		  	/* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
114 			if (sbloblen) {
115 				packet_start(SSH2_MSG_KEXGSS_HOSTKEY);
116 				packet_put_string(server_host_key_blob, sbloblen);
117 				packet_send();
118 				packet_write_wait();
119 			}
120 			break;
121 		case SSH2_MSG_KEXGSS_CONTINUE:
122 			recv_tok.value=packet_get_string(&slen);
123 			recv_tok.length=slen; /* int vs. size_t */
124 			break;
125 		default:
126 			packet_disconnect("Protocol error: didn't expect packet type %d",
127 					   type);
128 		}
129 
130 		maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok,
131 							 &send_tok));
132 
133 		xfree(recv_tok.value); /* We allocated this, not gss */
134 
135 		if (dh_client_pub == NULL)
136 			fatal("No client public key");
137 
138 		if (maj_status == GSS_S_CONTINUE_NEEDED) {
139 			debug("Sending GSSAPI_CONTINUE");
140 			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
141 			packet_put_string(send_tok.value,send_tok.length);
142 			packet_send();
143 			packet_write_wait();
144 			(void) gss_release_buffer(&min_status, &send_tok);
145 		}
146 	} while (maj_status == GSS_S_CONTINUE_NEEDED);
147 
148 	if (GSS_ERROR(maj_status)) {
149 		kex_gss_send_error(ctxt);
150 		if (send_tok.length>0) {
151 			packet_start(SSH2_MSG_KEXGSS_CONTINUE);
152 			packet_put_string(send_tok.value,send_tok.length);
153 			packet_send();
154 			packet_write_wait();
155 			(void) gss_release_buffer(&min_status, &send_tok);
156 		}
157 		fatal("accept_ctx died");
158 	}
159 
160 	debug("gss_complete");
161 	if (!(ctxt->flags & GSS_C_MUTUAL_FLAG))
162 		fatal("Mutual authentication flag wasn't set");
163 
164 	if (!(ctxt->flags & GSS_C_INTEG_FLAG))
165 		fatal("Integrity flag wasn't set");
166 
167 	dh = dh_new_group1();
168 	dh_gen_key(dh, kex->we_need * 8);
169 
170         if (!dh_pub_is_valid(dh, dh_client_pub))
171                 packet_disconnect("bad client public DH value");
172 
173         klen = DH_size(dh);
174         kbuf = xmalloc(klen);
175         kout = DH_compute_key(kbuf, dh_client_pub, dh);
176 
177 	shared_secret = BN_new();
178 	BN_bin2bn(kbuf, kout, shared_secret);
179 	(void) memset(kbuf, 0, klen);
180 	xfree(kbuf);
181 
182 	/* The GSSAPI hash is identical to the Diffie Helman one */
183         hash = kex_dh_hash(
184             kex->client_version_string,
185             kex->server_version_string,
186             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
187             buffer_ptr(&kex->my), buffer_len(&kex->my),
188             server_host_key_blob, sbloblen,
189             dh_client_pub,
190             dh->pub_key,
191             shared_secret
192 	);
193 	BN_free(dh_client_pub);
194 
195 	if (kex->session_id == NULL) {
196 		kex->session_id_len = 20;
197 		kex->session_id = xmalloc(kex->session_id_len);
198 		(void) memcpy(kex->session_id, hash, kex->session_id_len);
199 	} else if (x_authctxt != NULL && x_authctxt->success) {
200 		ssh_gssapi_storecreds(ctxt, x_authctxt);
201 	}
202 
203 	/* Should fix kex_dh_hash to output hash length */
204 	gssbuf.length = 20;	/* yes, it's always 20 (SHA-1) */
205 	gssbuf.value = hash;	/* and it's static constant storage */
206 
207 	if (GSS_ERROR(ssh_gssapi_get_mic(ctxt,&gssbuf,&msg_tok))) {
208 		kex_gss_send_error(ctxt);
209 		fatal("Couldn't get MIC");
210 	}
211 
212 	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
213 	packet_put_bignum2(dh->pub_key);
214 	packet_put_string((char *)msg_tok.value,msg_tok.length);
215 	(void) gss_release_buffer(&min_status, &msg_tok);
216 
217 	if (send_tok.length != 0) {
218 		packet_put_char(1); /* true */
219 		packet_put_string((char *)send_tok.value,send_tok.length);
220 		(void) gss_release_buffer(&min_status, &send_tok);
221 	} else {
222 		packet_put_char(0); /* false */
223 	}
224  	packet_send();
225 	packet_write_wait();
226 
227 	DH_free(dh);
228 
229 	kex_derive_keys(kex, hash, shared_secret);
230 	BN_clear_free(shared_secret);
231 	kex_finish(kex);
232 }
233 
234 static void
235 kex_gss_send_error(Gssctxt *ctxt) {
236 	char *errstr;
237 	OM_uint32 maj,min;
238 
239 	errstr = ssh_gssapi_last_error(ctxt,&maj,&min);
240 	if (errstr) {
241 		packet_start(SSH2_MSG_KEXGSS_ERROR);
242 		packet_put_int(maj);
243 		packet_put_int(min);
244 		packet_put_cstring(errstr);
245 		packet_put_cstring("");
246 		packet_send();
247 		packet_write_wait();
248 		/* XXX - We should probably log the error locally here */
249 		xfree(errstr);
250 	}
251 }
252 #endif /* GSSAPI */
253