• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $OpenBSD: auth2-gss.c,v 1.17 2011/03/10 02:52:57 djm Exp $ */
2 
3 /*
4  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 
29 #ifdef GSSAPI
30 
31 #include <sys/types.h>
32 
33 #include <stdarg.h>
34 
35 #include "xmalloc.h"
36 #include "key.h"
37 #include "hostfile.h"
38 #include "auth.h"
39 #include "ssh2.h"
40 #include "log.h"
41 #include "dispatch.h"
42 #include "buffer.h"
43 #include "servconf.h"
44 #include "packet.h"
45 #include "ssh-gss.h"
46 #include "monitor_wrap.h"
47 
48 extern ServerOptions options;
49 
50 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
51 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_errtok(int, u_int32_t, void *);
54 
55 /*
56  * We only support those mechanisms that we know about (ie ones that we know
57  * how to check local user kuserok and the like)
58  */
59 static int
userauth_gssapi(Authctxt * authctxt)60 userauth_gssapi(Authctxt *authctxt)
61 {
62 	gss_OID_desc goid = {0, NULL};
63 	Gssctxt *ctxt = NULL;
64 	int mechs;
65 	gss_OID_set supported;
66 	int present;
67 	OM_uint32 ms;
68 	u_int len;
69 	u_char *doid = NULL;
70 
71 	if (!authctxt->valid || authctxt->user == NULL)
72 		return (0);
73 
74 	mechs = packet_get_int();
75 	if (mechs == 0) {
76 		debug("Mechanism negotiation is not supported");
77 		return (0);
78 	}
79 
80 	ssh_gssapi_supported_oids(&supported);
81 	do {
82 		mechs--;
83 
84 		if (doid)
85 			xfree(doid);
86 
87 		present = 0;
88 		doid = packet_get_string(&len);
89 
90 		if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
91 		    doid[1] == len - 2) {
92 			goid.elements = doid + 2;
93 			goid.length   = len - 2;
94 			gss_test_oid_set_member(&ms, &goid, supported,
95 			    &present);
96 		} else {
97 			logit("Badly formed OID received");
98 		}
99 	} while (mechs > 0 && !present);
100 
101 	gss_release_oid_set(&ms, &supported);
102 
103 	if (!present) {
104 		xfree(doid);
105 		authctxt->server_caused_failure = 1;
106 		return (0);
107 	}
108 
109 	if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
110 		if (ctxt != NULL)
111 			ssh_gssapi_delete_ctx(&ctxt);
112 		xfree(doid);
113 		authctxt->server_caused_failure = 1;
114 		return (0);
115 	}
116 
117 	authctxt->methoddata = (void *)ctxt;
118 
119 	packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
120 
121 	/* Return the OID that we received */
122 	packet_put_string(doid, len);
123 
124 	packet_send();
125 	xfree(doid);
126 
127 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
128 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
129 	authctxt->postponed = 1;
130 
131 	return (0);
132 }
133 
134 static void
input_gssapi_token(int type,u_int32_t plen,void * ctxt)135 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
136 {
137 	Authctxt *authctxt = ctxt;
138 	Gssctxt *gssctxt;
139 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
140 	gss_buffer_desc recv_tok;
141 	OM_uint32 maj_status, min_status, flags;
142 	u_int len;
143 
144 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
145 		fatal("No authentication or GSSAPI context");
146 
147 	gssctxt = authctxt->methoddata;
148 	recv_tok.value = packet_get_string(&len);
149 	recv_tok.length = len; /* u_int vs. size_t */
150 
151 	packet_check_eom();
152 
153 	maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
154 	    &send_tok, &flags));
155 
156 	xfree(recv_tok.value);
157 
158 	if (GSS_ERROR(maj_status)) {
159 		if (send_tok.length != 0) {
160 			packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
161 			packet_put_string(send_tok.value, send_tok.length);
162 			packet_send();
163 		}
164 		authctxt->postponed = 0;
165 		dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
166 		userauth_finish(authctxt, 0, "gssapi-with-mic");
167 	} else {
168 		if (send_tok.length != 0) {
169 			packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
170 			packet_put_string(send_tok.value, send_tok.length);
171 			packet_send();
172 		}
173 		if (maj_status == GSS_S_COMPLETE) {
174 			dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
175 			if (flags & GSS_C_INTEG_FLAG)
176 				dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
177 				    &input_gssapi_mic);
178 			else
179 				dispatch_set(
180 				    SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
181 				    &input_gssapi_exchange_complete);
182 		}
183 	}
184 
185 	gss_release_buffer(&min_status, &send_tok);
186 }
187 
188 static void
input_gssapi_errtok(int type,u_int32_t plen,void * ctxt)189 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
190 {
191 	Authctxt *authctxt = ctxt;
192 	Gssctxt *gssctxt;
193 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
194 	gss_buffer_desc recv_tok;
195 	OM_uint32 maj_status;
196 	u_int len;
197 
198 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
199 		fatal("No authentication or GSSAPI context");
200 
201 	gssctxt = authctxt->methoddata;
202 	recv_tok.value = packet_get_string(&len);
203 	recv_tok.length = len;
204 
205 	packet_check_eom();
206 
207 	/* Push the error token into GSSAPI to see what it says */
208 	maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
209 	    &send_tok, NULL));
210 
211 	xfree(recv_tok.value);
212 
213 	/* We can't return anything to the client, even if we wanted to */
214 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
215 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
216 
217 	/* The client will have already moved on to the next auth */
218 
219 	gss_release_buffer(&maj_status, &send_tok);
220 }
221 
222 /*
223  * This is called when the client thinks we've completed authentication.
224  * It should only be enabled in the dispatch handler by the function above,
225  * which only enables it once the GSSAPI exchange is complete.
226  */
227 
228 static void
input_gssapi_exchange_complete(int type,u_int32_t plen,void * ctxt)229 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
230 {
231 	Authctxt *authctxt = ctxt;
232 	Gssctxt *gssctxt;
233 	int authenticated;
234 
235 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
236 		fatal("No authentication or GSSAPI context");
237 
238 	gssctxt = authctxt->methoddata;
239 
240 	/*
241 	 * We don't need to check the status, because we're only enabled in
242 	 * the dispatcher once the exchange is complete
243 	 */
244 
245 	packet_check_eom();
246 
247 	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
248 
249 	authctxt->postponed = 0;
250 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
251 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
252 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
253 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
254 	userauth_finish(authctxt, authenticated, "gssapi-with-mic");
255 }
256 
257 static void
input_gssapi_mic(int type,u_int32_t plen,void * ctxt)258 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
259 {
260 	Authctxt *authctxt = ctxt;
261 	Gssctxt *gssctxt;
262 	int authenticated = 0;
263 	Buffer b;
264 	gss_buffer_desc mic, gssbuf;
265 	u_int len;
266 
267 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
268 		fatal("No authentication or GSSAPI context");
269 
270 	gssctxt = authctxt->methoddata;
271 
272 	mic.value = packet_get_string(&len);
273 	mic.length = len;
274 
275 	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
276 	    "gssapi-with-mic");
277 
278 	gssbuf.value = buffer_ptr(&b);
279 	gssbuf.length = buffer_len(&b);
280 
281 	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
282 		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
283 	else
284 		logit("GSSAPI MIC check failed");
285 
286 	buffer_free(&b);
287 	xfree(mic.value);
288 
289 	authctxt->postponed = 0;
290 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
291 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
292 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
293 	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
294 	userauth_finish(authctxt, authenticated, "gssapi-with-mic");
295 }
296 
297 Authmethod method_gssapi = {
298 	"gssapi-with-mic",
299 	userauth_gssapi,
300 	&options.gss_authentication
301 };
302 
303 #endif /* GSSAPI */
304