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