1 /* $OpenBSD: gss-genr.c,v 1.28 2021/01/27 10:05:28 djm Exp $ */
4 * Copyright (c) 2001-2007 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>
48 /* sshbuf_get for gss_buffer_desc */
50 ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
56 if ((r = sshbuf_get_string(b, &p, &len)) != 0)
63 /* Check that the OID in a data stream matches that in the context */
65 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
67 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
68 ctx->oid->length == len &&
69 memcmp(ctx->oid->elements, data, len) == 0);
72 /* Set the contexts OID from a data stream */
74 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
76 if (ctx->oid != GSS_C_NO_OID) {
77 free(ctx->oid->elements);
80 ctx->oid = xcalloc(1, sizeof(gss_OID_desc));
81 ctx->oid->length = len;
82 ctx->oid->elements = xmalloc(len);
83 memcpy(ctx->oid->elements, data, len);
86 /* Set the contexts OID */
88 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
90 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
93 /* All this effort to report an error ... */
95 ssh_gssapi_error(Gssctxt *ctxt)
99 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
105 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
106 OM_uint32 *minor_status)
109 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
115 if ((b = sshbuf_new()) == NULL)
116 fatal_f("sshbuf_new failed");
118 if (major_status != NULL)
119 *major_status = ctxt->major;
120 if (minor_status != NULL)
121 *minor_status = ctxt->minor;
124 /* The GSSAPI error */
126 gss_display_status(&lmin, ctxt->major,
127 GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
129 if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
130 (r = sshbuf_put_u8(b, '\n')) != 0)
131 fatal_fr(r, "assemble GSS_CODE");
133 gss_release_buffer(&lmin, &msg);
136 /* The mechanism specific error */
138 gss_display_status(&lmin, ctxt->minor,
139 GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
141 if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
142 (r = sshbuf_put_u8(b, '\n')) != 0)
143 fatal_fr(r, "assemble MECH_CODE");
145 gss_release_buffer(&lmin, &msg);
148 if ((r = sshbuf_put_u8(b, '\n')) != 0)
149 fatal_fr(r, "assemble newline");
150 ret = xstrdup((const char *)sshbuf_ptr(b));
156 * Initialise our GSSAPI context. We use this opaque structure to contain all
157 * of the data which both the client and server need to persist across
158 * {accept,init}_sec_context calls, so that when we do it from the userauth
159 * stuff life is a little easier
162 ssh_gssapi_build_ctx(Gssctxt **ctx)
164 *ctx = xcalloc(1, sizeof (Gssctxt));
165 (*ctx)->context = GSS_C_NO_CONTEXT;
166 (*ctx)->name = GSS_C_NO_NAME;
167 (*ctx)->oid = GSS_C_NO_OID;
168 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
169 (*ctx)->client = GSS_C_NO_NAME;
170 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
173 /* Delete our context, providing it has been built correctly */
175 ssh_gssapi_delete_ctx(Gssctxt **ctx)
181 if ((*ctx)->context != GSS_C_NO_CONTEXT)
182 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
183 if ((*ctx)->name != GSS_C_NO_NAME)
184 gss_release_name(&ms, &(*ctx)->name);
185 if ((*ctx)->oid != GSS_C_NO_OID) {
186 free((*ctx)->oid->elements);
188 (*ctx)->oid = GSS_C_NO_OID;
190 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
191 gss_release_cred(&ms, &(*ctx)->creds);
192 if ((*ctx)->client != GSS_C_NO_NAME)
193 gss_release_name(&ms, &(*ctx)->client);
194 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
195 gss_release_cred(&ms, &(*ctx)->client_creds);
202 * Wrapper to init_sec_context
203 * Requires that the context contains:
205 * server name (from ssh_gssapi_import_name)
208 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
209 gss_buffer_desc* send_tok, OM_uint32 *flags)
214 deleg_flag = GSS_C_DELEG_FLAG;
215 debug("Delegating credentials");
218 ctx->major = gss_init_sec_context(&ctx->minor,
219 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
220 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
221 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
223 if (GSS_ERROR(ctx->major))
224 ssh_gssapi_error(ctx);
229 /* Create a service name for the given host */
231 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
233 gss_buffer_desc gssbuf;
236 xasprintf(&val, "host@%s", host);
238 gssbuf.length = strlen(gssbuf.value);
240 if ((ctx->major = gss_import_name(&ctx->minor,
241 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
242 ssh_gssapi_error(ctx);
249 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
251 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
252 GSS_C_QOP_DEFAULT, buffer, hash)))
253 ssh_gssapi_error(ctx);
259 ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
260 const char *context, const struct sshbuf *session_id)
265 if ((r = sshbuf_put_stringb(b, session_id)) != 0 ||
266 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
267 (r = sshbuf_put_cstring(b, user)) != 0 ||
268 (r = sshbuf_put_cstring(b, service)) != 0 ||
269 (r = sshbuf_put_cstring(b, context)) != 0)
270 fatal_fr(r, "assemble buildmic");
274 ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
276 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
277 OM_uint32 major, minor;
278 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
280 /* RFC 4462 says we MUST NOT do SPNEGO */
281 if (oid->length == spnego_oid.length &&
282 (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
283 return 0; /* false */
285 ssh_gssapi_build_ctx(ctx);
286 ssh_gssapi_set_oid(*ctx, oid);
287 major = ssh_gssapi_import_name(*ctx, host);
288 if (!GSS_ERROR(major)) {
289 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
291 gss_release_buffer(&minor, &token);
292 if ((*ctx)->context != GSS_C_NO_CONTEXT)
293 gss_delete_sec_context(&minor, &(*ctx)->context,
297 if (GSS_ERROR(major))
298 ssh_gssapi_delete_ctx(ctx);
300 return (!GSS_ERROR(major));