1 /* $OpenBSD: auth2-gss.c,v 1.17 2011/03/10 02:52:57 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>
46 #include "monitor_wrap.h"
48 extern ServerOptions options;
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 *);
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)
60 userauth_gssapi(Authctxt *authctxt)
62 gss_OID_desc goid = {0, NULL};
65 gss_OID_set supported;
71 if (!authctxt->valid || authctxt->user == NULL)
74 mechs = packet_get_int();
76 debug("Mechanism negotiation is not supported");
80 ssh_gssapi_supported_oids(&supported);
88 doid = packet_get_string(&len);
90 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
92 goid.elements = doid + 2;
93 goid.length = len - 2;
94 gss_test_oid_set_member(&ms, &goid, supported,
97 logit("Badly formed OID received");
99 } while (mechs > 0 && !present);
101 gss_release_oid_set(&ms, &supported);
105 authctxt->server_caused_failure = 1;
109 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
111 ssh_gssapi_delete_ctx(&ctxt);
113 authctxt->server_caused_failure = 1;
117 authctxt->methoddata = (void *)ctxt;
119 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
121 /* Return the OID that we received */
122 packet_put_string(doid, len);
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;
135 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
137 Authctxt *authctxt = ctxt;
139 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
140 gss_buffer_desc recv_tok;
141 OM_uint32 maj_status, min_status, flags;
144 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
145 fatal("No authentication or GSSAPI context");
147 gssctxt = authctxt->methoddata;
148 recv_tok.value = packet_get_string(&len);
149 recv_tok.length = len; /* u_int vs. size_t */
153 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
156 xfree(recv_tok.value);
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);
164 authctxt->postponed = 0;
165 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
166 userauth_finish(authctxt, 0, "gssapi-with-mic");
168 if (send_tok.length != 0) {
169 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
170 packet_put_string(send_tok.value, send_tok.length);
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,
180 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
181 &input_gssapi_exchange_complete);
185 gss_release_buffer(&min_status, &send_tok);
189 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
191 Authctxt *authctxt = ctxt;
193 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
194 gss_buffer_desc recv_tok;
195 OM_uint32 maj_status;
198 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
199 fatal("No authentication or GSSAPI context");
201 gssctxt = authctxt->methoddata;
202 recv_tok.value = packet_get_string(&len);
203 recv_tok.length = len;
207 /* Push the error token into GSSAPI to see what it says */
208 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
211 xfree(recv_tok.value);
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);
217 /* The client will have already moved on to the next auth */
219 gss_release_buffer(&maj_status, &send_tok);
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.
229 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
231 Authctxt *authctxt = ctxt;
235 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
236 fatal("No authentication or GSSAPI context");
238 gssctxt = authctxt->methoddata;
241 * We don't need to check the status, because we're only enabled in
242 * the dispatcher once the exchange is complete
247 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
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");
258 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
260 Authctxt *authctxt = ctxt;
262 int authenticated = 0;
264 gss_buffer_desc mic, gssbuf;
267 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
268 fatal("No authentication or GSSAPI context");
270 gssctxt = authctxt->methoddata;
272 mic.value = packet_get_string(&len);
275 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
278 gssbuf.value = buffer_ptr(&b);
279 gssbuf.length = buffer_len(&b);
281 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
282 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
284 logit("GSSAPI MIC check failed");
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");
297 Authmethod method_gssapi = {
300 &options.gss_authentication