blob: 93d576bfb8daa37cd111bef435a3676dd00ea878 [file] [log] [blame]
Damien Miller15b05cf2012-12-03 09:53:20 +11001/* $OpenBSD: auth2-gss.c,v 1.18 2012/12/02 20:34:09 djm Exp $ */
Darren Tucker0efd1552003-08-26 11:49:55 +10002
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
Darren Tucker8c6feda2006-08-05 15:24:59 +100029#ifdef GSSAPI
30
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/types.h>
Darren Tucker0efd1552003-08-26 11:49:55 +100032
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
34
Damien Millerd7834352006-08-05 12:39:39 +100035#include "xmalloc.h"
36#include "key.h"
37#include "hostfile.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100038#include "auth.h"
39#include "ssh2.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100040#include "log.h"
41#include "dispatch.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100043#include "servconf.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100044#include "packet.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100045#include "ssh-gss.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "monitor_wrap.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100047
48extern ServerOptions options;
49
50static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
Damien Miller0425d402003-11-17 22:18:21 +110051static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +100052static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53static void input_gssapi_errtok(int, u_int32_t, void *);
54
55/*
56 * We only support those mechanisms that we know about (ie ones that we know
Damien Miller6fd6def2005-11-05 15:07:05 +110057 * how to check local user kuserok and the like)
Darren Tucker0efd1552003-08-26 11:49:55 +100058 */
59static int
60userauth_gssapi(Authctxt *authctxt)
61{
Darren Tucker3f9fdc72004-06-22 12:56:01 +100062 gss_OID_desc goid = {0, NULL};
Darren Tucker0efd1552003-08-26 11:49:55 +100063 Gssctxt *ctxt = NULL;
64 int mechs;
65 gss_OID_set supported;
66 int present;
67 OM_uint32 ms;
68 u_int len;
Damien Millereccb9de2005-06-17 12:59:34 +100069 u_char *doid = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +100070
71 if (!authctxt->valid || authctxt->user == NULL)
72 return (0);
73
74 mechs = packet_get_int();
75 if (mechs == 0) {
76 debug("Mechanism negotiation is not supported");
77 return (0);
78 }
79
80 ssh_gssapi_supported_oids(&supported);
81 do {
82 mechs--;
83
84 if (doid)
85 xfree(doid);
86
Darren Tucker655a5e02003-11-03 20:09:03 +110087 present = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +100088 doid = packet_get_string(&len);
89
Damien Miller0dc1bef2005-07-17 17:22:45 +100090 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
91 doid[1] == len - 2) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +100092 goid.elements = doid + 2;
93 goid.length = len - 2;
94 gss_test_oid_set_member(&ms, &goid, supported,
Darren Tucker655a5e02003-11-03 20:09:03 +110095 &present);
Darren Tucker0efd1552003-08-26 11:49:55 +100096 } else {
Darren Tucker655a5e02003-11-03 20:09:03 +110097 logit("Badly formed OID received");
Darren Tucker0efd1552003-08-26 11:49:55 +100098 }
Darren Tucker0efd1552003-08-26 11:49:55 +100099 } while (mechs > 0 && !present);
100
101 gss_release_oid_set(&ms, &supported);
102
103 if (!present) {
104 xfree(doid);
Damien Miller3fcdfd52011-05-05 14:04:11 +1000105 authctxt->server_caused_failure = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000106 return (0);
107 }
108
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000109 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
Damien Millerf23c0962006-03-26 00:04:53 +1100110 if (ctxt != NULL)
111 ssh_gssapi_delete_ctx(&ctxt);
Damien Miller982d3262003-09-02 22:59:01 +1000112 xfree(doid);
Damien Miller3fcdfd52011-05-05 14:04:11 +1000113 authctxt->server_caused_failure = 1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000114 return (0);
Damien Miller982d3262003-09-02 22:59:01 +1000115 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000116
Damien Miller6fd6def2005-11-05 15:07:05 +1100117 authctxt->methoddata = (void *)ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000118
119 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
120
Darren Tucker655a5e02003-11-03 20:09:03 +1100121 /* Return the OID that we received */
Darren Tucker0efd1552003-08-26 11:49:55 +1000122 packet_put_string(doid, len);
123
124 packet_send();
125 xfree(doid);
126
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;
130
131 return (0);
132}
133
134static void
135input_gssapi_token(int type, u_int32_t plen, void *ctxt)
136{
137 Authctxt *authctxt = ctxt;
138 Gssctxt *gssctxt;
139 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
140 gss_buffer_desc recv_tok;
Damien Miller0425d402003-11-17 22:18:21 +1100141 OM_uint32 maj_status, min_status, flags;
Darren Tucker0efd1552003-08-26 11:49:55 +1000142 u_int len;
143
144 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
145 fatal("No authentication or GSSAPI context");
146
147 gssctxt = authctxt->methoddata;
148 recv_tok.value = packet_get_string(&len);
149 recv_tok.length = len; /* u_int vs. size_t */
150
151 packet_check_eom();
152
153 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
Damien Miller0425d402003-11-17 22:18:21 +1100154 &send_tok, &flags));
Darren Tucker0efd1552003-08-26 11:49:55 +1000155
156 xfree(recv_tok.value);
157
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);
162 packet_send();
163 }
164 authctxt->postponed = 0;
165 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
Damien Miller15b05cf2012-12-03 09:53:20 +1100166 userauth_finish(authctxt, 0, "gssapi-with-mic", NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000167 } else {
168 if (send_tok.length != 0) {
169 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
170 packet_put_string(send_tok.value, send_tok.length);
171 packet_send();
172 }
173 if (maj_status == GSS_S_COMPLETE) {
174 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
Damien Miller0425d402003-11-17 22:18:21 +1100175 if (flags & GSS_C_INTEG_FLAG)
176 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
177 &input_gssapi_mic);
178 else
179 dispatch_set(
180 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
181 &input_gssapi_exchange_complete);
Darren Tucker0efd1552003-08-26 11:49:55 +1000182 }
183 }
184
185 gss_release_buffer(&min_status, &send_tok);
186}
187
188static void
189input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
190{
191 Authctxt *authctxt = ctxt;
192 Gssctxt *gssctxt;
193 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
194 gss_buffer_desc recv_tok;
195 OM_uint32 maj_status;
Damien Miller55c47ed2003-09-02 22:14:07 +1000196 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000197
198 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
199 fatal("No authentication or GSSAPI context");
200
201 gssctxt = authctxt->methoddata;
Damien Miller55c47ed2003-09-02 22:14:07 +1000202 recv_tok.value = packet_get_string(&len);
203 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000204
205 packet_check_eom();
206
207 /* Push the error token into GSSAPI to see what it says */
208 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
209 &send_tok, NULL));
210
211 xfree(recv_tok.value);
212
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);
216
217 /* The client will have already moved on to the next auth */
218
219 gss_release_buffer(&maj_status, &send_tok);
220}
221
222/*
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.
226 */
227
228static void
229input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
230{
231 Authctxt *authctxt = ctxt;
232 Gssctxt *gssctxt;
233 int authenticated;
234
235 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
236 fatal("No authentication or GSSAPI context");
237
238 gssctxt = authctxt->methoddata;
239
240 /*
Damien Miller0425d402003-11-17 22:18:21 +1100241 * We don't need to check the status, because we're only enabled in
242 * the dispatcher once the exchange is complete
Darren Tucker0efd1552003-08-26 11:49:55 +1000243 */
244
245 packet_check_eom();
246
247 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
248
249 authctxt->postponed = 0;
250 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
251 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
Damien Miller0425d402003-11-17 22:18:21 +1100252 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000253 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
Damien Miller15b05cf2012-12-03 09:53:20 +1100254 userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL);
Damien Miller0425d402003-11-17 22:18:21 +1100255}
256
257static void
258input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
259{
260 Authctxt *authctxt = ctxt;
261 Gssctxt *gssctxt;
262 int authenticated = 0;
263 Buffer b;
264 gss_buffer_desc mic, gssbuf;
265 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +1100266
Damien Miller0425d402003-11-17 22:18:21 +1100267 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
268 fatal("No authentication or GSSAPI context");
Damien Miller787b2ec2003-11-21 23:56:47 +1100269
Damien Miller0425d402003-11-17 22:18:21 +1100270 gssctxt = authctxt->methoddata;
Damien Miller787b2ec2003-11-21 23:56:47 +1100271
Damien Miller0425d402003-11-17 22:18:21 +1100272 mic.value = packet_get_string(&len);
273 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +1100274
Damien Miller0425d402003-11-17 22:18:21 +1100275 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
276 "gssapi-with-mic");
Damien Miller787b2ec2003-11-21 23:56:47 +1100277
Damien Miller0425d402003-11-17 22:18:21 +1100278 gssbuf.value = buffer_ptr(&b);
279 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100280
Damien Miller0425d402003-11-17 22:18:21 +1100281 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
282 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
283 else
284 logit("GSSAPI MIC check failed");
285
286 buffer_free(&b);
287 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +1100288
Damien Miller0425d402003-11-17 22:18:21 +1100289 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);
Damien Miller15b05cf2012-12-03 09:53:20 +1100294 userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000295}
296
297Authmethod method_gssapi = {
Damien Miller0425d402003-11-17 22:18:21 +1100298 "gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000299 userauth_gssapi,
300 &options.gss_authentication
301};
Darren Tucker8c6feda2006-08-05 15:24:59 +1000302
303#endif /* GSSAPI */