1 /* $OpenBSD: auth2-gss.c,v 1.26 2017/06/24 06:34:38 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>
47 #include "monitor_wrap.h"
49 extern ServerOptions options
;
51 static int input_gssapi_token(int type
, u_int32_t plen
, struct ssh
*ssh
);
52 static int input_gssapi_mic(int type
, u_int32_t plen
, struct ssh
*ssh
);
53 static int input_gssapi_exchange_complete(int type
, u_int32_t plen
, struct ssh
*ssh
);
54 static int input_gssapi_errtok(int, u_int32_t
, struct ssh
*);
57 * We only support those mechanisms that we know about (ie ones that we know
58 * how to check local user kuserok and the like)
61 userauth_gssapi(struct ssh
*ssh
)
63 Authctxt
*authctxt
= ssh
->authctxt
;
64 gss_OID_desc goid
= {0, NULL
};
72 if (!authctxt
->valid
|| authctxt
->user
== NULL
)
75 mechs
= packet_get_int();
77 debug("Mechanism negotiation is not supported");
87 doid
= packet_get_string(&len
);
89 if (len
> 2 && doid
[0] == SSH_GSS_OIDTYPE
&&
91 goid
.elements
= doid
+ 2;
92 goid
.length
= len
- 2;
93 ssh_gssapi_test_oid_supported(&ms
, &goid
, &present
);
95 logit("Badly formed OID received");
97 } while (mechs
> 0 && !present
);
101 authctxt
->server_caused_failure
= 1;
105 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt
, &goid
)))) {
107 ssh_gssapi_delete_ctx(&ctxt
);
109 authctxt
->server_caused_failure
= 1;
113 authctxt
->methoddata
= (void *)ctxt
;
115 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
);
117 /* Return the OID that we received */
118 packet_put_string(doid
, len
);
123 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
124 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
125 authctxt
->postponed
= 1;
131 input_gssapi_token(int type
, u_int32_t plen
, struct ssh
*ssh
)
133 Authctxt
*authctxt
= ssh
->authctxt
;
135 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
136 gss_buffer_desc recv_tok
;
137 OM_uint32 maj_status
, min_status
, flags
;
140 if (authctxt
== NULL
|| (authctxt
->methoddata
== NULL
&& !use_privsep
))
141 fatal("No authentication or GSSAPI context");
143 gssctxt
= authctxt
->methoddata
;
144 recv_tok
.value
= packet_get_string(&len
);
145 recv_tok
.length
= len
; /* u_int vs. size_t */
149 maj_status
= PRIVSEP(ssh_gssapi_accept_ctx(gssctxt
, &recv_tok
,
152 free(recv_tok
.value
);
154 if (GSS_ERROR(maj_status
)) {
155 if (send_tok
.length
!= 0) {
156 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
157 packet_put_string(send_tok
.value
, send_tok
.length
);
160 authctxt
->postponed
= 0;
161 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
162 userauth_finish(ssh
, 0, "gssapi-with-mic", NULL
);
164 if (send_tok
.length
!= 0) {
165 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
166 packet_put_string(send_tok
.value
, send_tok
.length
);
169 if (maj_status
== GSS_S_COMPLETE
) {
170 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
171 if (flags
& GSS_C_INTEG_FLAG
)
172 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_MIC
,
175 ssh_dispatch_set(ssh
,
176 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
,
177 &input_gssapi_exchange_complete
);
181 gss_release_buffer(&min_status
, &send_tok
);
186 input_gssapi_errtok(int type
, u_int32_t plen
, struct ssh
*ssh
)
188 Authctxt
*authctxt
= ssh
->authctxt
;
190 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
191 gss_buffer_desc recv_tok
;
192 OM_uint32 maj_status
;
195 if (authctxt
== NULL
|| (authctxt
->methoddata
== NULL
&& !use_privsep
))
196 fatal("No authentication or GSSAPI context");
198 gssctxt
= authctxt
->methoddata
;
199 recv_tok
.value
= packet_get_string(&len
);
200 recv_tok
.length
= len
;
204 /* Push the error token into GSSAPI to see what it says */
205 maj_status
= PRIVSEP(ssh_gssapi_accept_ctx(gssctxt
, &recv_tok
,
208 free(recv_tok
.value
);
210 /* We can't return anything to the client, even if we wanted to */
211 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
212 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, NULL
);
214 /* The client will have already moved on to the next auth */
216 gss_release_buffer(&maj_status
, &send_tok
);
221 * This is called when the client thinks we've completed authentication.
222 * It should only be enabled in the dispatch handler by the function above,
223 * which only enables it once the GSSAPI exchange is complete.
227 input_gssapi_exchange_complete(int type
, u_int32_t plen
, struct ssh
*ssh
)
229 Authctxt
*authctxt
= ssh
->authctxt
;
231 const char *displayname
;
233 if (authctxt
== NULL
|| (authctxt
->methoddata
== NULL
&& !use_privsep
))
234 fatal("No authentication or GSSAPI context");
237 * We don't need to check the status, because we're only enabled in
238 * the dispatcher once the exchange is complete
243 authenticated
= PRIVSEP(ssh_gssapi_userok(authctxt
->user
));
245 if ((!use_privsep
|| mm_is_monitor()) &&
246 (displayname
= ssh_gssapi_displayname()) != NULL
)
247 auth2_record_info(authctxt
, "%s", displayname
);
249 authctxt
->postponed
= 0;
250 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
251 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, NULL
);
252 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_MIC
, NULL
);
253 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
, NULL
);
254 userauth_finish(ssh
, authenticated
, "gssapi-with-mic", NULL
);
259 input_gssapi_mic(int type
, u_int32_t plen
, struct ssh
*ssh
)
261 Authctxt
*authctxt
= ssh
->authctxt
;
263 int authenticated
= 0;
265 gss_buffer_desc mic
, gssbuf
;
267 const char *displayname
;
269 if (authctxt
== NULL
|| (authctxt
->methoddata
== NULL
&& !use_privsep
))
270 fatal("No authentication or GSSAPI context");
272 gssctxt
= authctxt
->methoddata
;
274 mic
.value
= packet_get_string(&len
);
277 ssh_gssapi_buildmic(&b
, authctxt
->user
, authctxt
->service
,
280 gssbuf
.value
= buffer_ptr(&b
);
281 gssbuf
.length
= buffer_len(&b
);
283 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt
, &gssbuf
, &mic
))))
284 authenticated
= PRIVSEP(ssh_gssapi_userok(authctxt
->user
));
286 logit("GSSAPI MIC check failed");
291 if ((!use_privsep
|| mm_is_monitor()) &&
292 (displayname
= ssh_gssapi_displayname()) != NULL
)
293 auth2_record_info(authctxt
, "%s", displayname
);
295 authctxt
->postponed
= 0;
296 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, NULL
);
297 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, NULL
);
298 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_MIC
, NULL
);
299 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
, NULL
);
300 userauth_finish(ssh
, authenticated
, "gssapi-with-mic", NULL
);
304 Authmethod method_gssapi
= {
307 &options
.gss_authentication