xref: /titanic_41/usr/src/cmd/ssh/libssh/common/kexgsss.c (revision 1915be19b7a1f7c113b706bd54d6ca393e8d8107)
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 2007 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 "auth.h"
49 
50 Gssctxt *xxx_gssctxt;
51 extern Authctxt *x_authctxt;
52 
53 static void kex_gss_send_error(Gssctxt *ctxt);
54 
55 void
56 kexgss_server(Kex *kex)
57 {
58 	OM_uint32 maj_status, min_status;
59 	gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok;
60 	Gssctxt *ctxt = NULL;
61         unsigned int klen, kout;
62         unsigned int sbloblen = 0;
63         unsigned char *kbuf, *hash;
64 	unsigned char *server_host_key_blob = NULL;
65         DH *dh;
66 	Key *server_host_key = NULL;
67         BIGNUM *shared_secret = NULL;
68         BIGNUM *dh_client_pub = NULL;
69 	int type =0;
70 	u_int slen;
71 	gss_OID oid;
72 
73 	/*
74 	 * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet
75 	 * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's
76 	 * the GSS-API that provides for server host authentication.
77 	 */
78 	if (kex->load_host_key != NULL &&
79 	    !(datafellows & SSH_BUG_GSSKEX_HOSTKEY))
80 		server_host_key = kex->load_host_key(kex->hostkey_type);
81 	if (server_host_key != NULL)
82 		key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
83 
84 
85 	/* Initialise GSSAPI */
86 
87 	ssh_gssapi_oid_of_kexname(kex->name, &oid);
88 	if (oid == GSS_C_NULL_OID) {
89 		fatal("Couldn't match the negotiated GSS key exchange");
90 	}
91 
92 	ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid);
93 
94 	ctxt = xxx_gssctxt;
95 
96 	do {
97 		debug("Wait SSH2_MSG_GSSAPI_INIT");
98 		type = packet_read();
99 		switch(type) {
100 		case SSH2_MSG_KEXGSS_INIT:
101 			if (dh_client_pub!=NULL)
102 				fatal("Received KEXGSS_INIT after initialising");
103 			recv_tok.value=packet_get_string(&slen);
104 			recv_tok.length=slen; /* int vs. size_t */
105 
106 		        dh_client_pub = BN_new();
107 
108 		        if (dh_client_pub == NULL)
109         			fatal("dh_client_pub == NULL");
110 		  	packet_get_bignum2(dh_client_pub);
111 
112 		  	/* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
113 			if (sbloblen) {
114 				packet_start(SSH2_MSG_KEXGSS_HOSTKEY);
115 				packet_put_string(server_host_key_blob, sbloblen);
116 				packet_send();
117 				packet_write_wait();
118 			}
119 			break;
120 		case SSH2_MSG_KEXGSS_CONTINUE:
121 			recv_tok.value=packet_get_string(&slen);
122 			recv_tok.length=slen; /* int vs. size_t */
123 			break;
124 		default:
125 			packet_disconnect("Protocol error: didn't expect packet type %d",
126 					   type);
127 		}
128 
129 		maj_status = ssh_gssapi_accept_ctx(ctxt,&recv_tok, &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 	} else if (x_authctxt != NULL && x_authctxt->success) {
198 		ssh_gssapi_storecreds(ctxt, x_authctxt);
199 	}
200 
201 	/* Should fix kex_dh_hash to output hash length */
202 	gssbuf.length = 20;	/* yes, it's always 20 (SHA-1) */
203 	gssbuf.value = hash;	/* and it's static constant storage */
204 
205 	if (GSS_ERROR(ssh_gssapi_get_mic(ctxt,&gssbuf,&msg_tok))) {
206 		kex_gss_send_error(ctxt);
207 		fatal("Couldn't get MIC");
208 	}
209 
210 	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
211 	packet_put_bignum2(dh->pub_key);
212 	packet_put_string((char *)msg_tok.value,msg_tok.length);
213 	(void) gss_release_buffer(&min_status, &msg_tok);
214 
215 	if (send_tok.length != 0) {
216 		packet_put_char(1); /* true */
217 		packet_put_string((char *)send_tok.value,send_tok.length);
218 		(void) gss_release_buffer(&min_status, &send_tok);
219 	} else {
220 		packet_put_char(0); /* false */
221 	}
222  	packet_send();
223 	packet_write_wait();
224 
225 	DH_free(dh);
226 
227 	kex_derive_keys(kex, hash, shared_secret);
228 	BN_clear_free(shared_secret);
229 	kex_finish(kex);
230 }
231 
232 static void
233 kex_gss_send_error(Gssctxt *ctxt) {
234 	char *errstr;
235 	OM_uint32 maj,min;
236 
237 	errstr = ssh_gssapi_last_error(ctxt,&maj,&min);
238 	if (errstr) {
239 		packet_start(SSH2_MSG_KEXGSS_ERROR);
240 		packet_put_int(maj);
241 		packet_put_int(min);
242 		packet_put_cstring(errstr);
243 		packet_put_cstring("");
244 		packet_send();
245 		packet_write_wait();
246 		/* XXX - We should probably log the error locally here */
247 		xfree(errstr);
248 	}
249 }
250 #endif /* GSSAPI */
251