1*1c7715ddSchristos /* $NetBSD: auth2-gss.c,v 1.18 2024/07/08 22:33:43 christos Exp $ */
2*1c7715ddSchristos /* $OpenBSD: auth2-gss.c,v 1.36 2024/05/17 04:42:13 djm Exp $ */
3ca32bd8dSchristos
4ca32bd8dSchristos /*
5ca32bd8dSchristos * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6ca32bd8dSchristos *
7ca32bd8dSchristos * Redistribution and use in source and binary forms, with or without
8ca32bd8dSchristos * modification, are permitted provided that the following conditions
9ca32bd8dSchristos * are met:
10ca32bd8dSchristos * 1. Redistributions of source code must retain the above copyright
11ca32bd8dSchristos * notice, this list of conditions and the following disclaimer.
12ca32bd8dSchristos * 2. Redistributions in binary form must reproduce the above copyright
13ca32bd8dSchristos * notice, this list of conditions and the following disclaimer in the
14ca32bd8dSchristos * documentation and/or other materials provided with the distribution.
15ca32bd8dSchristos *
16ca32bd8dSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
17ca32bd8dSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca32bd8dSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca32bd8dSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca32bd8dSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca32bd8dSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca32bd8dSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca32bd8dSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca32bd8dSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca32bd8dSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca32bd8dSchristos */
27ca32bd8dSchristos
28313c6c94Schristos #include "includes.h"
29*1c7715ddSchristos __RCSID("$NetBSD: auth2-gss.c,v 1.18 2024/07/08 22:33:43 christos Exp $");
30313c6c94Schristos
31ca32bd8dSchristos #ifdef GSSAPI
32ca32bd8dSchristos
33ca32bd8dSchristos #include <sys/types.h>
34ed75d7a8Schristos #include <stdarg.h>
35ca32bd8dSchristos
36ca32bd8dSchristos #include "xmalloc.h"
3755a4608bSchristos #include "sshkey.h"
38ca32bd8dSchristos #include "hostfile.h"
39ca32bd8dSchristos #include "auth.h"
40ca32bd8dSchristos #include "ssh2.h"
41ca32bd8dSchristos #include "log.h"
428a4530f9Schristos #include "misc.h"
4355a4608bSchristos #include "dispatch.h"
4455a4608bSchristos #include "sshbuf.h"
4555a4608bSchristos #include "ssherr.h"
46ca32bd8dSchristos #include "servconf.h"
47ca32bd8dSchristos #include "packet.h"
4817418e98Schristos #include "kex.h"
49ca32bd8dSchristos #include "ssh-gss.h"
50ca32bd8dSchristos #include "monitor_wrap.h"
51ca32bd8dSchristos
52a629fefcSchristos #define SSH_GSSAPI_MAX_MECHS 2048
53a629fefcSchristos
54ca32bd8dSchristos extern ServerOptions options;
55*1c7715ddSchristos extern struct authmethod_cfg methodcfg_gssapi;
56ca32bd8dSchristos
577a183406Schristos static int input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh);
587a183406Schristos static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
597a183406Schristos static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
607a183406Schristos static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
61ca32bd8dSchristos
62ca32bd8dSchristos /*
63ca32bd8dSchristos * We only support those mechanisms that we know about (ie ones that we know
64ca32bd8dSchristos * how to check local user kuserok and the like)
65ca32bd8dSchristos */
66ca32bd8dSchristos static int
userauth_gssapi(struct ssh * ssh,const char * method)67a03ec00cSchristos userauth_gssapi(struct ssh *ssh, const char *method)
68ca32bd8dSchristos {
697a183406Schristos Authctxt *authctxt = ssh->authctxt;
70ca32bd8dSchristos gss_OID_desc goid = {0, NULL};
71ca32bd8dSchristos Gssctxt *ctxt = NULL;
7255a4608bSchristos int r, present;
7355a4608bSchristos u_int mechs;
74ca32bd8dSchristos OM_uint32 ms;
7555a4608bSchristos size_t len;
76ca32bd8dSchristos u_char *doid = NULL;
77ca32bd8dSchristos
7855a4608bSchristos if ((r = sshpkt_get_u32(ssh, &mechs)) != 0)
7917418e98Schristos fatal_fr(r, "parse packet");
80ca32bd8dSchristos
81ca32bd8dSchristos if (mechs == 0) {
82a629fefcSchristos logit_f("mechanism negotiation is not supported");
83a629fefcSchristos return (0);
84a629fefcSchristos } else if (mechs > SSH_GSSAPI_MAX_MECHS) {
85a629fefcSchristos logit_f("too many mechanisms requested %u > %u", mechs,
86a629fefcSchristos SSH_GSSAPI_MAX_MECHS);
87ca32bd8dSchristos return (0);
88ca32bd8dSchristos }
89ca32bd8dSchristos
90ca32bd8dSchristos do {
91ca32bd8dSchristos mechs--;
92ca32bd8dSchristos
9300a838c4Schristos free(doid);
94ca32bd8dSchristos
95ca32bd8dSchristos present = 0;
9655a4608bSchristos if ((r = sshpkt_get_string(ssh, &doid, &len)) != 0)
9717418e98Schristos fatal_fr(r, "parse oid");
98ca32bd8dSchristos
99ca32bd8dSchristos if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
100ca32bd8dSchristos doid[1] == len - 2) {
101ca32bd8dSchristos goid.elements = doid + 2;
102ca32bd8dSchristos goid.length = len - 2;
1038a4530f9Schristos ssh_gssapi_test_oid_supported(&ms, &goid, &present);
104ca32bd8dSchristos } else {
105a629fefcSchristos logit_f("badly formed OID received");
106ca32bd8dSchristos }
107ca32bd8dSchristos } while (mechs > 0 && !present);
108ca32bd8dSchristos
109ca32bd8dSchristos if (!present) {
11000a838c4Schristos free(doid);
1116f47b660Schristos authctxt->server_caused_failure = 1;
112ca32bd8dSchristos return (0);
113ca32bd8dSchristos }
114ca32bd8dSchristos
11555a4608bSchristos if (!authctxt->valid || authctxt->user == NULL) {
11617418e98Schristos debug2_f("disabled because of invalid user");
11755a4608bSchristos free(doid);
11855a4608bSchristos return (0);
11955a4608bSchristos }
12055a4608bSchristos
121*1c7715ddSchristos if (GSS_ERROR(mm_ssh_gssapi_server_ctx(&ctxt, &goid))) {
122ca32bd8dSchristos if (ctxt != NULL)
123ca32bd8dSchristos ssh_gssapi_delete_ctx(&ctxt);
12400a838c4Schristos free(doid);
1256f47b660Schristos authctxt->server_caused_failure = 1;
126ca32bd8dSchristos return (0);
127ca32bd8dSchristos }
128ca32bd8dSchristos
129ca32bd8dSchristos authctxt->methoddata = (void *)ctxt;
130ca32bd8dSchristos
131ca32bd8dSchristos /* Return the OID that we received */
13255a4608bSchristos if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE)) != 0 ||
13355a4608bSchristos (r = sshpkt_put_string(ssh, doid, len)) != 0 ||
13455a4608bSchristos (r = sshpkt_send(ssh)) != 0)
13517418e98Schristos fatal_fr(r, "send packet");
136ca32bd8dSchristos
13700a838c4Schristos free(doid);
138ca32bd8dSchristos
1397a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
1407a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
141ca32bd8dSchristos authctxt->postponed = 1;
142ca32bd8dSchristos
143ca32bd8dSchristos return (0);
144ca32bd8dSchristos }
145ca32bd8dSchristos
146e4d43b82Schristos static int
input_gssapi_token(int type,u_int32_t plen,struct ssh * ssh)1477a183406Schristos input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
148ca32bd8dSchristos {
1497a183406Schristos Authctxt *authctxt = ssh->authctxt;
150ca32bd8dSchristos Gssctxt *gssctxt;
151ca32bd8dSchristos gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
152ca32bd8dSchristos gss_buffer_desc recv_tok;
153ca32bd8dSchristos OM_uint32 maj_status, min_status, flags;
15455a4608bSchristos u_char *p;
15555a4608bSchristos size_t len;
15655a4608bSchristos int r;
157ca32bd8dSchristos
158*1c7715ddSchristos if (authctxt == NULL)
159ca32bd8dSchristos fatal("No authentication or GSSAPI context");
160ca32bd8dSchristos
161ca32bd8dSchristos gssctxt = authctxt->methoddata;
16255a4608bSchristos if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
16355a4608bSchristos (r = sshpkt_get_end(ssh)) != 0)
16417418e98Schristos fatal_fr(r, "parse packet");
165ca32bd8dSchristos
16655a4608bSchristos recv_tok.value = p;
16755a4608bSchristos recv_tok.length = len;
168*1c7715ddSchristos maj_status = mm_ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
169*1c7715ddSchristos &send_tok, &flags);
170ca32bd8dSchristos
17155a4608bSchristos free(p);
172ca32bd8dSchristos
173ca32bd8dSchristos if (GSS_ERROR(maj_status)) {
174ca32bd8dSchristos if (send_tok.length != 0) {
17555a4608bSchristos if ((r = sshpkt_start(ssh,
17655a4608bSchristos SSH2_MSG_USERAUTH_GSSAPI_ERRTOK)) != 0 ||
17755a4608bSchristos (r = sshpkt_put_string(ssh, send_tok.value,
17855a4608bSchristos send_tok.length)) != 0 ||
17955a4608bSchristos (r = sshpkt_send(ssh)) != 0)
18017418e98Schristos fatal_fr(r, "send ERRTOK packet");
181ca32bd8dSchristos }
182ca32bd8dSchristos authctxt->postponed = 0;
1837a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
1847a183406Schristos userauth_finish(ssh, 0, "gssapi-with-mic", NULL);
185ca32bd8dSchristos } else {
186ca32bd8dSchristos if (send_tok.length != 0) {
18755a4608bSchristos if ((r = sshpkt_start(ssh,
18855a4608bSchristos SSH2_MSG_USERAUTH_GSSAPI_TOKEN)) != 0 ||
18955a4608bSchristos (r = sshpkt_put_string(ssh, send_tok.value,
19055a4608bSchristos send_tok.length)) != 0 ||
19155a4608bSchristos (r = sshpkt_send(ssh)) != 0)
19217418e98Schristos fatal_fr(r, "send TOKEN packet");
193ca32bd8dSchristos }
194ca32bd8dSchristos if (maj_status == GSS_S_COMPLETE) {
1957a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
196ca32bd8dSchristos if (flags & GSS_C_INTEG_FLAG)
1977a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC,
198ca32bd8dSchristos &input_gssapi_mic);
199ca32bd8dSchristos else
2007a183406Schristos ssh_dispatch_set(ssh,
201ca32bd8dSchristos SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
202ca32bd8dSchristos &input_gssapi_exchange_complete);
203ca32bd8dSchristos }
204ca32bd8dSchristos }
205ca32bd8dSchristos
206ca32bd8dSchristos gss_release_buffer(&min_status, &send_tok);
207e4d43b82Schristos return 0;
208ca32bd8dSchristos }
209ca32bd8dSchristos
210e4d43b82Schristos static int
input_gssapi_errtok(int type,u_int32_t plen,struct ssh * ssh)2117a183406Schristos input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
212ca32bd8dSchristos {
2137a183406Schristos Authctxt *authctxt = ssh->authctxt;
214ca32bd8dSchristos Gssctxt *gssctxt;
215ca32bd8dSchristos gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
216ca32bd8dSchristos gss_buffer_desc recv_tok;
217ca32bd8dSchristos OM_uint32 maj_status;
21855a4608bSchristos int r;
21955a4608bSchristos u_char *p;
22055a4608bSchristos size_t len;
221ca32bd8dSchristos
222*1c7715ddSchristos if (authctxt == NULL)
223ca32bd8dSchristos fatal("No authentication or GSSAPI context");
224ca32bd8dSchristos
225ca32bd8dSchristos gssctxt = authctxt->methoddata;
22655a4608bSchristos if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
22755a4608bSchristos (r = sshpkt_get_end(ssh)) != 0)
22817418e98Schristos fatal_fr(r, "parse packet");
22955a4608bSchristos recv_tok.value = p;
230ca32bd8dSchristos recv_tok.length = len;
231ca32bd8dSchristos
232ca32bd8dSchristos /* Push the error token into GSSAPI to see what it says */
233*1c7715ddSchristos maj_status = mm_ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
234*1c7715ddSchristos &send_tok, NULL);
235ca32bd8dSchristos
23600a838c4Schristos free(recv_tok.value);
237ca32bd8dSchristos
238ca32bd8dSchristos /* We can't return anything to the client, even if we wanted to */
2397a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
2407a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
241ca32bd8dSchristos
242ca32bd8dSchristos /* The client will have already moved on to the next auth */
243ca32bd8dSchristos
244ca32bd8dSchristos gss_release_buffer(&maj_status, &send_tok);
245e4d43b82Schristos return 0;
246ca32bd8dSchristos }
247ca32bd8dSchristos
248ca32bd8dSchristos /*
249ca32bd8dSchristos * This is called when the client thinks we've completed authentication.
250ca32bd8dSchristos * It should only be enabled in the dispatch handler by the function above,
251ca32bd8dSchristos * which only enables it once the GSSAPI exchange is complete.
252ca32bd8dSchristos */
253ca32bd8dSchristos
254e4d43b82Schristos static int
input_gssapi_exchange_complete(int type,u_int32_t plen,struct ssh * ssh)2557a183406Schristos input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
256ca32bd8dSchristos {
2577a183406Schristos Authctxt *authctxt = ssh->authctxt;
25855a4608bSchristos int r, authenticated;
259ca32bd8dSchristos
260*1c7715ddSchristos if (authctxt == NULL)
261ca32bd8dSchristos fatal("No authentication or GSSAPI context");
262ca32bd8dSchristos
263ca32bd8dSchristos /*
264ca32bd8dSchristos * We don't need to check the status, because we're only enabled in
265ca32bd8dSchristos * the dispatcher once the exchange is complete
266ca32bd8dSchristos */
267ca32bd8dSchristos
26855a4608bSchristos if ((r = sshpkt_get_end(ssh)) != 0)
26917418e98Schristos fatal_fr(r, "parse packet");
270ca32bd8dSchristos
271*1c7715ddSchristos authenticated = mm_ssh_gssapi_userok(authctxt->user);
2727a183406Schristos
273ca32bd8dSchristos authctxt->postponed = 0;
2747a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
2757a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
2767a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
2777a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
2787a183406Schristos userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
279e4d43b82Schristos return 0;
280ca32bd8dSchristos }
281ca32bd8dSchristos
282e4d43b82Schristos static int
input_gssapi_mic(int type,u_int32_t plen,struct ssh * ssh)2837a183406Schristos input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
284ca32bd8dSchristos {
2857a183406Schristos Authctxt *authctxt = ssh->authctxt;
286ca32bd8dSchristos Gssctxt *gssctxt;
28755a4608bSchristos int r, authenticated = 0;
28855a4608bSchristos struct sshbuf *b;
289ca32bd8dSchristos gss_buffer_desc mic, gssbuf;
29055a4608bSchristos u_char *p;
29155a4608bSchristos size_t len;
292ca32bd8dSchristos
293*1c7715ddSchristos if (authctxt == NULL)
294ca32bd8dSchristos fatal("No authentication or GSSAPI context");
295ca32bd8dSchristos
296ca32bd8dSchristos gssctxt = authctxt->methoddata;
297ca32bd8dSchristos
29855a4608bSchristos if ((r = sshpkt_get_string(ssh, &p, &len)) != 0)
29917418e98Schristos fatal_fr(r, "parse packet");
30055a4608bSchristos if ((b = sshbuf_new()) == NULL)
30117418e98Schristos fatal_f("sshbuf_new failed");
30255a4608bSchristos mic.value = p;
303ca32bd8dSchristos mic.length = len;
30455a4608bSchristos ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
30517418e98Schristos "gssapi-with-mic", ssh->kex->session_id);
306ca32bd8dSchristos
30755a4608bSchristos if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
30817418e98Schristos fatal_f("sshbuf_mutable_ptr failed");
30955a4608bSchristos gssbuf.length = sshbuf_len(b);
310ca32bd8dSchristos
311*1c7715ddSchristos if (!GSS_ERROR(mm_ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic)))
312*1c7715ddSchristos authenticated = mm_ssh_gssapi_userok(authctxt->user);
313ca32bd8dSchristos else
314ca32bd8dSchristos logit("GSSAPI MIC check failed");
315ca32bd8dSchristos
31655a4608bSchristos sshbuf_free(b);
31700a838c4Schristos free(mic.value);
318ca32bd8dSchristos
319ca32bd8dSchristos authctxt->postponed = 0;
3207a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
3217a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
3227a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
3237a183406Schristos ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
3247a183406Schristos userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
325e4d43b82Schristos return 0;
326ca32bd8dSchristos }
327ca32bd8dSchristos
328ca32bd8dSchristos Authmethod method_gssapi = {
329*1c7715ddSchristos &methodcfg_gssapi,
330ca32bd8dSchristos userauth_gssapi,
331ca32bd8dSchristos };
332ca32bd8dSchristos #endif
333