1 /* $OpenBSD: auth2-gss.c,v 1.29 2018/07/31 03:10:27 djm Exp $ */
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/types.h>
48 #include "monitor_wrap.h"
50 extern ServerOptions options;
52 static int input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh);
53 static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
54 static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
55 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
58 * We only support those mechanisms that we know about (ie ones that we know
59 * how to check local user kuserok and the like)
62 userauth_gssapi(struct ssh *ssh)
64 Authctxt *authctxt = ssh->authctxt;
65 gss_OID_desc goid = {0, NULL};
73 if ((r = sshpkt_get_u32(ssh, &mechs)) != 0)
74 fatal("%s: %s", __func__, ssh_err(r));
77 debug("Mechanism negotiation is not supported");
87 if ((r = sshpkt_get_string(ssh, &doid, &len)) != 0)
88 fatal("%s: %s", __func__, ssh_err(r));
90 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
92 goid.elements = doid + 2;
93 goid.length = len - 2;
94 ssh_gssapi_test_oid_supported(&ms, &goid, &present);
96 logit("Badly formed OID received");
98 } while (mechs > 0 && !present);
102 authctxt->server_caused_failure = 1;
106 if (!authctxt->valid || authctxt->user == NULL) {
107 debug2("%s: disabled because of invalid user", __func__);
112 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
114 ssh_gssapi_delete_ctx(&ctxt);
116 authctxt->server_caused_failure = 1;
120 authctxt->methoddata = (void *)ctxt;
122 /* Return the OID that we received */
123 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE)) != 0 ||
124 (r = sshpkt_put_string(ssh, doid, len)) != 0 ||
125 (r = sshpkt_send(ssh)) != 0)
126 fatal("%s: %s", __func__, ssh_err(r));
130 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
131 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
132 authctxt->postponed = 1;
138 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
140 Authctxt *authctxt = ssh->authctxt;
142 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
143 gss_buffer_desc recv_tok;
144 OM_uint32 maj_status, min_status, flags;
149 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
150 fatal("No authentication or GSSAPI context");
152 gssctxt = authctxt->methoddata;
153 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
154 (r = sshpkt_get_end(ssh)) != 0)
155 fatal("%s: %s", __func__, ssh_err(r));
158 recv_tok.length = len;
159 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
164 if (GSS_ERROR(maj_status)) {
165 if (send_tok.length != 0) {
166 if ((r = sshpkt_start(ssh,
167 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK)) != 0 ||
168 (r = sshpkt_put_string(ssh, send_tok.value,
169 send_tok.length)) != 0 ||
170 (r = sshpkt_send(ssh)) != 0)
171 fatal("%s: %s", __func__, ssh_err(r));
173 authctxt->postponed = 0;
174 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
175 userauth_finish(ssh, 0, "gssapi-with-mic", NULL);
177 if (send_tok.length != 0) {
178 if ((r = sshpkt_start(ssh,
179 SSH2_MSG_USERAUTH_GSSAPI_TOKEN)) != 0 ||
180 (r = sshpkt_put_string(ssh, send_tok.value,
181 send_tok.length)) != 0 ||
182 (r = sshpkt_send(ssh)) != 0)
183 fatal("%s: %s", __func__, ssh_err(r));
185 if (maj_status == GSS_S_COMPLETE) {
186 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
187 if (flags & GSS_C_INTEG_FLAG)
188 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC,
191 ssh_dispatch_set(ssh,
192 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
193 &input_gssapi_exchange_complete);
197 gss_release_buffer(&min_status, &send_tok);
202 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
204 Authctxt *authctxt = ssh->authctxt;
206 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
207 gss_buffer_desc recv_tok;
208 OM_uint32 maj_status;
213 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
214 fatal("No authentication or GSSAPI context");
216 gssctxt = authctxt->methoddata;
217 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
218 (r = sshpkt_get_end(ssh)) != 0)
219 fatal("%s: %s", __func__, ssh_err(r));
221 recv_tok.length = len;
223 /* Push the error token into GSSAPI to see what it says */
224 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
227 free(recv_tok.value);
229 /* We can't return anything to the client, even if we wanted to */
230 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
231 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
233 /* The client will have already moved on to the next auth */
235 gss_release_buffer(&maj_status, &send_tok);
240 * This is called when the client thinks we've completed authentication.
241 * It should only be enabled in the dispatch handler by the function above,
242 * which only enables it once the GSSAPI exchange is complete.
246 input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
248 Authctxt *authctxt = ssh->authctxt;
249 int r, authenticated;
250 const char *displayname;
252 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
253 fatal("No authentication or GSSAPI context");
256 * We don't need to check the status, because we're only enabled in
257 * the dispatcher once the exchange is complete
260 if ((r = sshpkt_get_end(ssh)) != 0)
261 fatal("%s: %s", __func__, ssh_err(r));
263 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
265 if ((!use_privsep || mm_is_monitor()) &&
266 (displayname = ssh_gssapi_displayname()) != NULL)
267 auth2_record_info(authctxt, "%s", displayname);
269 authctxt->postponed = 0;
270 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
271 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
272 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
273 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
274 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
279 input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
281 Authctxt *authctxt = ssh->authctxt;
283 int r, authenticated = 0;
285 gss_buffer_desc mic, gssbuf;
286 const char *displayname;
290 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
291 fatal("No authentication or GSSAPI context");
293 gssctxt = authctxt->methoddata;
295 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0)
296 fatal("%s: %s", __func__, ssh_err(r));
297 if ((b = sshbuf_new()) == NULL)
298 fatal("%s: sshbuf_new failed", __func__);
301 ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
304 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
305 fatal("%s: sshbuf_mutable_ptr failed", __func__);
306 gssbuf.length = sshbuf_len(b);
308 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
309 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
311 logit("GSSAPI MIC check failed");
316 if ((!use_privsep || mm_is_monitor()) &&
317 (displayname = ssh_gssapi_displayname()) != NULL)
318 auth2_record_info(authctxt, "%s", displayname);
320 authctxt->postponed = 0;
321 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
322 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
323 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
324 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
325 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
329 Authmethod method_gssapi = {
332 &options.gss_authentication