xref: /openbsd-src/usr.bin/ssh/auth2-gss.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /* $OpenBSD: auth2-gss.c,v 1.26 2017/06/24 06:34:38 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 #ifdef GSSAPI
28 
29 #include <sys/types.h>
30 
31 #include "xmalloc.h"
32 #include "key.h"
33 #include "hostfile.h"
34 #include "auth.h"
35 #include "ssh2.h"
36 #include "log.h"
37 #include "dispatch.h"
38 #include "buffer.h"
39 #include "servconf.h"
40 #include "packet.h"
41 #include "ssh-gss.h"
42 #include "monitor_wrap.h"
43 
44 extern ServerOptions options;
45 
46 static int input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh);
47 static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
48 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
49 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
50 
51 /*
52  * We only support those mechanisms that we know about (ie ones that we know
53  * how to check local user kuserok and the like)
54  */
55 static int
56 userauth_gssapi(struct ssh *ssh)
57 {
58 	Authctxt *authctxt = ssh->authctxt;
59 	gss_OID_desc goid = {0, NULL};
60 	Gssctxt *ctxt = NULL;
61 	int mechs;
62 	int present;
63 	OM_uint32 ms;
64 	u_int len;
65 	u_char *doid = NULL;
66 
67 	if (!authctxt->valid || authctxt->user == NULL)
68 		return (0);
69 
70 	mechs = packet_get_int();
71 	if (mechs == 0) {
72 		debug("Mechanism negotiation is not supported");
73 		return (0);
74 	}
75 
76 	do {
77 		mechs--;
78 
79 		free(doid);
80 
81 		present = 0;
82 		doid = packet_get_string(&len);
83 
84 		if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
85 		    doid[1] == len - 2) {
86 			goid.elements = doid + 2;
87 			goid.length   = len - 2;
88 			ssh_gssapi_test_oid_supported(&ms, &goid, &present);
89 		} else {
90 			logit("Badly formed OID received");
91 		}
92 	} while (mechs > 0 && !present);
93 
94 	if (!present) {
95 		free(doid);
96 		authctxt->server_caused_failure = 1;
97 		return (0);
98 	}
99 
100 	if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
101 		if (ctxt != NULL)
102 			ssh_gssapi_delete_ctx(&ctxt);
103 		free(doid);
104 		authctxt->server_caused_failure = 1;
105 		return (0);
106 	}
107 
108 	authctxt->methoddata = (void *)ctxt;
109 
110 	packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
111 
112 	/* Return the OID that we received */
113 	packet_put_string(doid, len);
114 
115 	packet_send();
116 	free(doid);
117 
118 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
119 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
120 	authctxt->postponed = 1;
121 
122 	return (0);
123 }
124 
125 static int
126 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
127 {
128 	Authctxt *authctxt = ssh->authctxt;
129 	Gssctxt *gssctxt;
130 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
131 	gss_buffer_desc recv_tok;
132 	OM_uint32 maj_status, min_status, flags;
133 	u_int len;
134 
135 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
136 		fatal("No authentication or GSSAPI context");
137 
138 	gssctxt = authctxt->methoddata;
139 	recv_tok.value = packet_get_string(&len);
140 	recv_tok.length = len; /* u_int vs. size_t */
141 
142 	packet_check_eom();
143 
144 	maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
145 	    &send_tok, &flags));
146 
147 	free(recv_tok.value);
148 
149 	if (GSS_ERROR(maj_status)) {
150 		if (send_tok.length != 0) {
151 			packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
152 			packet_put_string(send_tok.value, send_tok.length);
153 			packet_send();
154 		}
155 		authctxt->postponed = 0;
156 		ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
157 		userauth_finish(ssh, 0, "gssapi-with-mic", NULL);
158 	} else {
159 		if (send_tok.length != 0) {
160 			packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
161 			packet_put_string(send_tok.value, send_tok.length);
162 			packet_send();
163 		}
164 		if (maj_status == GSS_S_COMPLETE) {
165 			ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
166 			if (flags & GSS_C_INTEG_FLAG)
167 				ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC,
168 				    &input_gssapi_mic);
169 			else
170 				ssh_dispatch_set(ssh,
171 				    SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
172 				    &input_gssapi_exchange_complete);
173 		}
174 	}
175 
176 	gss_release_buffer(&min_status, &send_tok);
177 	return 0;
178 }
179 
180 static int
181 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
182 {
183 	Authctxt *authctxt = ssh->authctxt;
184 	Gssctxt *gssctxt;
185 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
186 	gss_buffer_desc recv_tok;
187 	OM_uint32 maj_status;
188 	u_int len;
189 
190 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
191 		fatal("No authentication or GSSAPI context");
192 
193 	gssctxt = authctxt->methoddata;
194 	recv_tok.value = packet_get_string(&len);
195 	recv_tok.length = len;
196 
197 	packet_check_eom();
198 
199 	/* Push the error token into GSSAPI to see what it says */
200 	maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
201 	    &send_tok, NULL));
202 
203 	free(recv_tok.value);
204 
205 	/* We can't return anything to the client, even if we wanted to */
206 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
207 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
208 
209 	/* The client will have already moved on to the next auth */
210 
211 	gss_release_buffer(&maj_status, &send_tok);
212 	return 0;
213 }
214 
215 /*
216  * This is called when the client thinks we've completed authentication.
217  * It should only be enabled in the dispatch handler by the function above,
218  * which only enables it once the GSSAPI exchange is complete.
219  */
220 
221 static int
222 input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
223 {
224 	Authctxt *authctxt = ssh->authctxt;
225 	int authenticated;
226 	const char *displayname;
227 
228 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
229 		fatal("No authentication or GSSAPI context");
230 
231 	/*
232 	 * We don't need to check the status, because we're only enabled in
233 	 * the dispatcher once the exchange is complete
234 	 */
235 
236 	packet_check_eom();
237 
238 	authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
239 
240 	if ((!use_privsep || mm_is_monitor()) &&
241 	    (displayname = ssh_gssapi_displayname()) != NULL)
242 		auth2_record_info(authctxt, "%s", displayname);
243 
244 	authctxt->postponed = 0;
245 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
246 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
247 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
248 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
249 	userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
250 	return 0;
251 }
252 
253 static int
254 input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
255 {
256 	Authctxt *authctxt = ssh->authctxt;
257 	Gssctxt *gssctxt;
258 	int authenticated = 0;
259 	Buffer b;
260 	gss_buffer_desc mic, gssbuf;
261 	u_int len;
262 	const char *displayname;
263 
264 	if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
265 		fatal("No authentication or GSSAPI context");
266 
267 	gssctxt = authctxt->methoddata;
268 
269 	mic.value = packet_get_string(&len);
270 	mic.length = len;
271 
272 	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
273 	    "gssapi-with-mic");
274 
275 	gssbuf.value = buffer_ptr(&b);
276 	gssbuf.length = buffer_len(&b);
277 
278 	if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
279 		authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
280 	else
281 		logit("GSSAPI MIC check failed");
282 
283 	buffer_free(&b);
284 	free(mic.value);
285 
286 	if ((!use_privsep || mm_is_monitor()) &&
287 	    (displayname = ssh_gssapi_displayname()) != NULL)
288 		auth2_record_info(authctxt, "%s", displayname);
289 
290 	authctxt->postponed = 0;
291 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
292 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
293 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
294 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
295 	userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
296 	return 0;
297 }
298 
299 Authmethod method_gssapi = {
300 	"gssapi-with-mic",
301 	userauth_gssapi,
302 	&options.gss_authentication
303 };
304 #endif
305