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