blob: f794e05b779cb3ef3a7a58af8ef4b723b8e6eada [file] [log] [blame]
markus@openbsd.orgb8d92142018-07-09 21:37:55 +00001/* $OpenBSD: gss-genr.c,v 1.25 2018/07/09 21:37:55 markus Exp $ */
Darren Tucker0efd1552003-08-26 11:49:55 +10002
3/*
Darren Tuckerb1e128f2007-06-12 23:44:36 +10004 * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
Darren Tucker0efd1552003-08-26 11:49:55 +10005 *
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
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100032
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000033#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Darren Tuckeraa721962015-05-22 17:49:46 +100036#include <signal.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100037#include <unistd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038
Darren Tucker0efd1552003-08-26 11:49:55 +100039#include "xmalloc.h"
markus@openbsd.orgb8d92142018-07-09 21:37:55 +000040#include "ssherr.h"
41#include "sshbuf.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100042#include "log.h"
Damien Miller0425d402003-11-17 22:18:21 +110043#include "ssh2.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100044
45#include "ssh-gss.h"
46
Damien Miller0425d402003-11-17 22:18:21 +110047extern u_char *session_id2;
48extern u_int session_id2_len;
Darren Tucker0efd1552003-08-26 11:49:55 +100049
50/* Check that the OID in a data stream matches that in the context */
51int
52ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
53{
54 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
55 ctx->oid->length == len &&
56 memcmp(ctx->oid->elements, data, len) == 0);
57}
58
59/* Set the contexts OID from a data stream */
60void
61ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
62{
63 if (ctx->oid != GSS_C_NO_OID) {
Darren Tuckera627d422013-06-02 07:31:17 +100064 free(ctx->oid->elements);
65 free(ctx->oid);
Darren Tucker0efd1552003-08-26 11:49:55 +100066 }
Damien Miller6c81fee2013-11-08 12:19:55 +110067 ctx->oid = xcalloc(1, sizeof(gss_OID_desc));
Darren Tucker0efd1552003-08-26 11:49:55 +100068 ctx->oid->length = len;
69 ctx->oid->elements = xmalloc(len);
70 memcpy(ctx->oid->elements, data, len);
71}
72
73/* Set the contexts OID */
74void
75ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
76{
77 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
78}
79
80/* All this effort to report an error ... */
81void
82ssh_gssapi_error(Gssctxt *ctxt)
83{
Damien Millera66cf682006-03-26 00:05:23 +110084 char *s;
85
86 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
87 debug("%s", s);
Darren Tuckera627d422013-06-02 07:31:17 +100088 free(s);
Darren Tucker0efd1552003-08-26 11:49:55 +100089}
90
91char *
Damien Miller0dc1bef2005-07-17 17:22:45 +100092ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
93 OM_uint32 *minor_status)
Darren Tucker0efd1552003-08-26 11:49:55 +100094{
95 OM_uint32 lmin;
96 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
97 OM_uint32 ctx;
markus@openbsd.orgb8d92142018-07-09 21:37:55 +000098 struct sshbuf *b;
Darren Tucker0efd1552003-08-26 11:49:55 +100099 char *ret;
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000100 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000101
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000102 if ((b = sshbuf_new()) == NULL)
103 fatal("%s: sshbuf_new failed", __func__);
Darren Tucker0efd1552003-08-26 11:49:55 +1000104
105 if (major_status != NULL)
106 *major_status = ctxt->major;
107 if (minor_status != NULL)
108 *minor_status = ctxt->minor;
109
110 ctx = 0;
111 /* The GSSAPI error */
112 do {
113 gss_display_status(&lmin, ctxt->major,
Darren Tuckerb1e128f2007-06-12 23:44:36 +1000114 GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000115
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000116 if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
117 (r = sshbuf_put_u8(b, '\n')) != 0)
118 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000119
120 gss_release_buffer(&lmin, &msg);
121 } while (ctx != 0);
122
123 /* The mechanism specific error */
124 do {
125 gss_display_status(&lmin, ctxt->minor,
Darren Tuckerb1e128f2007-06-12 23:44:36 +1000126 GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000127
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000128 if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
129 (r = sshbuf_put_u8(b, '\n')) != 0)
130 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000131
132 gss_release_buffer(&lmin, &msg);
133 } while (ctx != 0);
134
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000135 if ((r = sshbuf_put_u8(b, '\n')) != 0)
136 fatal("%s: buffer error: %s", __func__, ssh_err(r));
137 ret = xstrdup((const char *)sshbuf_ptr(b));
138 sshbuf_free(b);
Darren Tucker0efd1552003-08-26 11:49:55 +1000139 return (ret);
140}
141
142/*
143 * Initialise our GSSAPI context. We use this opaque structure to contain all
144 * of the data which both the client and server need to persist across
145 * {accept,init}_sec_context calls, so that when we do it from the userauth
146 * stuff life is a little easier
147 */
148void
149ssh_gssapi_build_ctx(Gssctxt **ctx)
150{
Damien Miller07d86be2006-03-26 14:19:21 +1100151 *ctx = xcalloc(1, sizeof (Gssctxt));
Darren Tucker0efd1552003-08-26 11:49:55 +1000152 (*ctx)->context = GSS_C_NO_CONTEXT;
153 (*ctx)->name = GSS_C_NO_NAME;
154 (*ctx)->oid = GSS_C_NO_OID;
155 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
156 (*ctx)->client = GSS_C_NO_NAME;
157 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
158}
159
160/* Delete our context, providing it has been built correctly */
161void
162ssh_gssapi_delete_ctx(Gssctxt **ctx)
163{
164 OM_uint32 ms;
165
166 if ((*ctx) == NULL)
167 return;
168 if ((*ctx)->context != GSS_C_NO_CONTEXT)
169 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
170 if ((*ctx)->name != GSS_C_NO_NAME)
171 gss_release_name(&ms, &(*ctx)->name);
172 if ((*ctx)->oid != GSS_C_NO_OID) {
Darren Tuckera627d422013-06-02 07:31:17 +1000173 free((*ctx)->oid->elements);
174 free((*ctx)->oid);
Darren Tucker0efd1552003-08-26 11:49:55 +1000175 (*ctx)->oid = GSS_C_NO_OID;
176 }
177 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
178 gss_release_cred(&ms, &(*ctx)->creds);
179 if ((*ctx)->client != GSS_C_NO_NAME)
180 gss_release_name(&ms, &(*ctx)->client);
181 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
182 gss_release_cred(&ms, &(*ctx)->client_creds);
183
Darren Tuckera627d422013-06-02 07:31:17 +1000184 free(*ctx);
Darren Tucker0efd1552003-08-26 11:49:55 +1000185 *ctx = NULL;
186}
187
188/*
189 * Wrapper to init_sec_context
190 * Requires that the context contains:
191 * oid
192 * server name (from ssh_gssapi_import_name)
193 */
194OM_uint32
195ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
196 gss_buffer_desc* send_tok, OM_uint32 *flags)
197{
198 int deleg_flag = 0;
199
200 if (deleg_creds) {
201 deleg_flag = GSS_C_DELEG_FLAG;
202 debug("Delegating credentials");
203 }
204
205 ctx->major = gss_init_sec_context(&ctx->minor,
206 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
207 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
208 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
209
210 if (GSS_ERROR(ctx->major))
211 ssh_gssapi_error(ctx);
212
213 return (ctx->major);
214}
215
216/* Create a service name for the given host */
217OM_uint32
218ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
219{
220 gss_buffer_desc gssbuf;
Damien Miller63e437f2006-04-23 12:05:46 +1000221 char *val;
Darren Tucker0efd1552003-08-26 11:49:55 +1000222
Damien Miller63e437f2006-04-23 12:05:46 +1000223 xasprintf(&val, "host@%s", host);
224 gssbuf.value = val;
225 gssbuf.length = strlen(gssbuf.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000226
227 if ((ctx->major = gss_import_name(&ctx->minor,
228 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
229 ssh_gssapi_error(ctx);
230
Darren Tuckera627d422013-06-02 07:31:17 +1000231 free(gssbuf.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000232 return (ctx->major);
233}
234
Darren Tucker0efd1552003-08-26 11:49:55 +1000235OM_uint32
Damien Miller0425d402003-11-17 22:18:21 +1100236ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
237{
238 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
239 GSS_C_QOP_DEFAULT, buffer, hash)))
240 ssh_gssapi_error(ctx);
Damien Miller787b2ec2003-11-21 23:56:47 +1100241
Damien Miller0425d402003-11-17 22:18:21 +1100242 return (ctx->major);
243}
244
245void
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000246ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
Damien Millera8e06ce2003-11-21 23:48:55 +1100247 const char *context)
Damien Miller787b2ec2003-11-21 23:56:47 +1100248{
markus@openbsd.orgb8d92142018-07-09 21:37:55 +0000249 int r;
250
251 sshbuf_reset(b);
252 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
253 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
254 (r = sshbuf_put_cstring(b, user)) != 0 ||
255 (r = sshbuf_put_cstring(b, service)) != 0 ||
256 (r = sshbuf_put_cstring(b, context)) != 0)
257 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100258}
259
Damien Millera1cb9f32006-08-19 00:33:34 +1000260int
Damien Miller3d2d6e92006-08-19 00:46:43 +1000261ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
Damien Millera1cb9f32006-08-19 00:33:34 +1000262{
263 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
264 OM_uint32 major, minor;
265 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
266
267 /* RFC 4462 says we MUST NOT do SPNEGO */
268 if (oid->length == spnego_oid.length &&
269 (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
Damien Millerdeccaa72006-08-19 08:50:57 +1000270 return 0; /* false */
Damien Millera1cb9f32006-08-19 00:33:34 +1000271
272 ssh_gssapi_build_ctx(ctx);
273 ssh_gssapi_set_oid(*ctx, oid);
274 major = ssh_gssapi_import_name(*ctx, host);
275 if (!GSS_ERROR(major)) {
276 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
277 NULL);
278 gss_release_buffer(&minor, &token);
Damien Miller76758b62006-08-30 11:08:04 +1000279 if ((*ctx)->context != GSS_C_NO_CONTEXT)
280 gss_delete_sec_context(&minor, &(*ctx)->context,
281 GSS_C_NO_BUFFER);
Damien Millera1cb9f32006-08-19 00:33:34 +1000282 }
283
284 if (GSS_ERROR(major))
285 ssh_gssapi_delete_ctx(ctx);
286
287 return (!GSS_ERROR(major));
288}
289
Darren Tucker0efd1552003-08-26 11:49:55 +1000290#endif /* GSSAPI */