1*ba1276acSMatthew Dillon /* $OpenBSD: gss-genr.c,v 1.29 2024/02/01 02:37:33 djm Exp $ */
2*ba1276acSMatthew Dillon
3*ba1276acSMatthew Dillon /*
4*ba1276acSMatthew Dillon * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
5*ba1276acSMatthew Dillon *
6*ba1276acSMatthew Dillon * Redistribution and use in source and binary forms, with or without
7*ba1276acSMatthew Dillon * modification, are permitted provided that the following conditions
8*ba1276acSMatthew Dillon * are met:
9*ba1276acSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
10*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer.
11*ba1276acSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
12*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
13*ba1276acSMatthew Dillon * documentation and/or other materials provided with the distribution.
14*ba1276acSMatthew Dillon *
15*ba1276acSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
16*ba1276acSMatthew Dillon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*ba1276acSMatthew Dillon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*ba1276acSMatthew Dillon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*ba1276acSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*ba1276acSMatthew Dillon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*ba1276acSMatthew Dillon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*ba1276acSMatthew Dillon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*ba1276acSMatthew Dillon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*ba1276acSMatthew Dillon * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*ba1276acSMatthew Dillon */
26*ba1276acSMatthew Dillon
27*ba1276acSMatthew Dillon #include "includes.h"
28*ba1276acSMatthew Dillon
29*ba1276acSMatthew Dillon #ifdef GSSAPI
30*ba1276acSMatthew Dillon
31*ba1276acSMatthew Dillon #include <sys/types.h>
32*ba1276acSMatthew Dillon
33*ba1276acSMatthew Dillon #include <limits.h>
34*ba1276acSMatthew Dillon #include <stdarg.h>
35*ba1276acSMatthew Dillon #include <string.h>
36*ba1276acSMatthew Dillon #include <signal.h>
37*ba1276acSMatthew Dillon #include <stdlib.h>
38*ba1276acSMatthew Dillon #include <unistd.h>
39*ba1276acSMatthew Dillon
40*ba1276acSMatthew Dillon #include "xmalloc.h"
41*ba1276acSMatthew Dillon #include "ssherr.h"
42*ba1276acSMatthew Dillon #include "sshbuf.h"
43*ba1276acSMatthew Dillon #include "log.h"
44*ba1276acSMatthew Dillon #include "ssh2.h"
45*ba1276acSMatthew Dillon
46*ba1276acSMatthew Dillon #include "ssh-gss.h"
47*ba1276acSMatthew Dillon
48*ba1276acSMatthew Dillon /* sshbuf_get for gss_buffer_desc */
49*ba1276acSMatthew Dillon int
ssh_gssapi_get_buffer_desc(struct sshbuf * b,gss_buffer_desc * g)50*ba1276acSMatthew Dillon ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
51*ba1276acSMatthew Dillon {
52*ba1276acSMatthew Dillon int r;
53*ba1276acSMatthew Dillon u_char *p;
54*ba1276acSMatthew Dillon size_t len;
55*ba1276acSMatthew Dillon
56*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(b, &p, &len)) != 0)
57*ba1276acSMatthew Dillon return r;
58*ba1276acSMatthew Dillon g->value = p;
59*ba1276acSMatthew Dillon g->length = len;
60*ba1276acSMatthew Dillon return 0;
61*ba1276acSMatthew Dillon }
62*ba1276acSMatthew Dillon
63*ba1276acSMatthew Dillon /* Check that the OID in a data stream matches that in the context */
64*ba1276acSMatthew Dillon int
ssh_gssapi_check_oid(Gssctxt * ctx,void * data,size_t len)65*ba1276acSMatthew Dillon ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
66*ba1276acSMatthew Dillon {
67*ba1276acSMatthew Dillon return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
68*ba1276acSMatthew Dillon ctx->oid->length == len &&
69*ba1276acSMatthew Dillon memcmp(ctx->oid->elements, data, len) == 0);
70*ba1276acSMatthew Dillon }
71*ba1276acSMatthew Dillon
72*ba1276acSMatthew Dillon /* Set the contexts OID from a data stream */
73*ba1276acSMatthew Dillon void
ssh_gssapi_set_oid_data(Gssctxt * ctx,void * data,size_t len)74*ba1276acSMatthew Dillon ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
75*ba1276acSMatthew Dillon {
76*ba1276acSMatthew Dillon if (ctx->oid != GSS_C_NO_OID) {
77*ba1276acSMatthew Dillon free(ctx->oid->elements);
78*ba1276acSMatthew Dillon free(ctx->oid);
79*ba1276acSMatthew Dillon }
80*ba1276acSMatthew Dillon ctx->oid = xcalloc(1, sizeof(gss_OID_desc));
81*ba1276acSMatthew Dillon ctx->oid->length = len;
82*ba1276acSMatthew Dillon ctx->oid->elements = xmalloc(len);
83*ba1276acSMatthew Dillon memcpy(ctx->oid->elements, data, len);
84*ba1276acSMatthew Dillon }
85*ba1276acSMatthew Dillon
86*ba1276acSMatthew Dillon /* Set the contexts OID */
87*ba1276acSMatthew Dillon void
ssh_gssapi_set_oid(Gssctxt * ctx,gss_OID oid)88*ba1276acSMatthew Dillon ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
89*ba1276acSMatthew Dillon {
90*ba1276acSMatthew Dillon ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
91*ba1276acSMatthew Dillon }
92*ba1276acSMatthew Dillon
93*ba1276acSMatthew Dillon /* All this effort to report an error ... */
94*ba1276acSMatthew Dillon void
ssh_gssapi_error(Gssctxt * ctxt)95*ba1276acSMatthew Dillon ssh_gssapi_error(Gssctxt *ctxt)
96*ba1276acSMatthew Dillon {
97*ba1276acSMatthew Dillon char *s;
98*ba1276acSMatthew Dillon
99*ba1276acSMatthew Dillon s = ssh_gssapi_last_error(ctxt, NULL, NULL);
100*ba1276acSMatthew Dillon debug("%s", s);
101*ba1276acSMatthew Dillon free(s);
102*ba1276acSMatthew Dillon }
103*ba1276acSMatthew Dillon
104*ba1276acSMatthew Dillon char *
ssh_gssapi_last_error(Gssctxt * ctxt,OM_uint32 * major_status,OM_uint32 * minor_status)105*ba1276acSMatthew Dillon ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
106*ba1276acSMatthew Dillon OM_uint32 *minor_status)
107*ba1276acSMatthew Dillon {
108*ba1276acSMatthew Dillon OM_uint32 lmin;
109*ba1276acSMatthew Dillon gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
110*ba1276acSMatthew Dillon OM_uint32 ctx;
111*ba1276acSMatthew Dillon struct sshbuf *b;
112*ba1276acSMatthew Dillon char *ret;
113*ba1276acSMatthew Dillon int r;
114*ba1276acSMatthew Dillon
115*ba1276acSMatthew Dillon if ((b = sshbuf_new()) == NULL)
116*ba1276acSMatthew Dillon fatal_f("sshbuf_new failed");
117*ba1276acSMatthew Dillon
118*ba1276acSMatthew Dillon if (major_status != NULL)
119*ba1276acSMatthew Dillon *major_status = ctxt->major;
120*ba1276acSMatthew Dillon if (minor_status != NULL)
121*ba1276acSMatthew Dillon *minor_status = ctxt->minor;
122*ba1276acSMatthew Dillon
123*ba1276acSMatthew Dillon ctx = 0;
124*ba1276acSMatthew Dillon /* The GSSAPI error */
125*ba1276acSMatthew Dillon do {
126*ba1276acSMatthew Dillon gss_display_status(&lmin, ctxt->major,
127*ba1276acSMatthew Dillon GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
128*ba1276acSMatthew Dillon
129*ba1276acSMatthew Dillon if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
130*ba1276acSMatthew Dillon (r = sshbuf_put_u8(b, '\n')) != 0)
131*ba1276acSMatthew Dillon fatal_fr(r, "assemble GSS_CODE");
132*ba1276acSMatthew Dillon
133*ba1276acSMatthew Dillon gss_release_buffer(&lmin, &msg);
134*ba1276acSMatthew Dillon } while (ctx != 0);
135*ba1276acSMatthew Dillon
136*ba1276acSMatthew Dillon /* The mechanism specific error */
137*ba1276acSMatthew Dillon do {
138*ba1276acSMatthew Dillon gss_display_status(&lmin, ctxt->minor,
139*ba1276acSMatthew Dillon GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
140*ba1276acSMatthew Dillon
141*ba1276acSMatthew Dillon if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
142*ba1276acSMatthew Dillon (r = sshbuf_put_u8(b, '\n')) != 0)
143*ba1276acSMatthew Dillon fatal_fr(r, "assemble MECH_CODE");
144*ba1276acSMatthew Dillon
145*ba1276acSMatthew Dillon gss_release_buffer(&lmin, &msg);
146*ba1276acSMatthew Dillon } while (ctx != 0);
147*ba1276acSMatthew Dillon
148*ba1276acSMatthew Dillon if ((r = sshbuf_put_u8(b, '\n')) != 0)
149*ba1276acSMatthew Dillon fatal_fr(r, "assemble newline");
150*ba1276acSMatthew Dillon ret = xstrdup((const char *)sshbuf_ptr(b));
151*ba1276acSMatthew Dillon sshbuf_free(b);
152*ba1276acSMatthew Dillon return (ret);
153*ba1276acSMatthew Dillon }
154*ba1276acSMatthew Dillon
155*ba1276acSMatthew Dillon /*
156*ba1276acSMatthew Dillon * Initialise our GSSAPI context. We use this opaque structure to contain all
157*ba1276acSMatthew Dillon * of the data which both the client and server need to persist across
158*ba1276acSMatthew Dillon * {accept,init}_sec_context calls, so that when we do it from the userauth
159*ba1276acSMatthew Dillon * stuff life is a little easier
160*ba1276acSMatthew Dillon */
161*ba1276acSMatthew Dillon void
ssh_gssapi_build_ctx(Gssctxt ** ctx)162*ba1276acSMatthew Dillon ssh_gssapi_build_ctx(Gssctxt **ctx)
163*ba1276acSMatthew Dillon {
164*ba1276acSMatthew Dillon *ctx = xcalloc(1, sizeof (Gssctxt));
165*ba1276acSMatthew Dillon (*ctx)->context = GSS_C_NO_CONTEXT;
166*ba1276acSMatthew Dillon (*ctx)->name = GSS_C_NO_NAME;
167*ba1276acSMatthew Dillon (*ctx)->oid = GSS_C_NO_OID;
168*ba1276acSMatthew Dillon (*ctx)->creds = GSS_C_NO_CREDENTIAL;
169*ba1276acSMatthew Dillon (*ctx)->client = GSS_C_NO_NAME;
170*ba1276acSMatthew Dillon (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
171*ba1276acSMatthew Dillon }
172*ba1276acSMatthew Dillon
173*ba1276acSMatthew Dillon /* Delete our context, providing it has been built correctly */
174*ba1276acSMatthew Dillon void
ssh_gssapi_delete_ctx(Gssctxt ** ctx)175*ba1276acSMatthew Dillon ssh_gssapi_delete_ctx(Gssctxt **ctx)
176*ba1276acSMatthew Dillon {
177*ba1276acSMatthew Dillon OM_uint32 ms;
178*ba1276acSMatthew Dillon
179*ba1276acSMatthew Dillon if ((*ctx) == NULL)
180*ba1276acSMatthew Dillon return;
181*ba1276acSMatthew Dillon if ((*ctx)->context != GSS_C_NO_CONTEXT)
182*ba1276acSMatthew Dillon gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
183*ba1276acSMatthew Dillon if ((*ctx)->name != GSS_C_NO_NAME)
184*ba1276acSMatthew Dillon gss_release_name(&ms, &(*ctx)->name);
185*ba1276acSMatthew Dillon if ((*ctx)->oid != GSS_C_NO_OID) {
186*ba1276acSMatthew Dillon free((*ctx)->oid->elements);
187*ba1276acSMatthew Dillon free((*ctx)->oid);
188*ba1276acSMatthew Dillon (*ctx)->oid = GSS_C_NO_OID;
189*ba1276acSMatthew Dillon }
190*ba1276acSMatthew Dillon if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
191*ba1276acSMatthew Dillon gss_release_cred(&ms, &(*ctx)->creds);
192*ba1276acSMatthew Dillon if ((*ctx)->client != GSS_C_NO_NAME)
193*ba1276acSMatthew Dillon gss_release_name(&ms, &(*ctx)->client);
194*ba1276acSMatthew Dillon if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
195*ba1276acSMatthew Dillon gss_release_cred(&ms, &(*ctx)->client_creds);
196*ba1276acSMatthew Dillon
197*ba1276acSMatthew Dillon free(*ctx);
198*ba1276acSMatthew Dillon *ctx = NULL;
199*ba1276acSMatthew Dillon }
200*ba1276acSMatthew Dillon
201*ba1276acSMatthew Dillon /*
202*ba1276acSMatthew Dillon * Wrapper to init_sec_context
203*ba1276acSMatthew Dillon * Requires that the context contains:
204*ba1276acSMatthew Dillon * oid
205*ba1276acSMatthew Dillon * server name (from ssh_gssapi_import_name)
206*ba1276acSMatthew Dillon */
207*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_init_ctx(Gssctxt * ctx,int deleg_creds,gss_buffer_desc * recv_tok,gss_buffer_desc * send_tok,OM_uint32 * flags)208*ba1276acSMatthew Dillon ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
209*ba1276acSMatthew Dillon gss_buffer_desc* send_tok, OM_uint32 *flags)
210*ba1276acSMatthew Dillon {
211*ba1276acSMatthew Dillon int deleg_flag = 0;
212*ba1276acSMatthew Dillon
213*ba1276acSMatthew Dillon if (deleg_creds) {
214*ba1276acSMatthew Dillon deleg_flag = GSS_C_DELEG_FLAG;
215*ba1276acSMatthew Dillon debug("Delegating credentials");
216*ba1276acSMatthew Dillon }
217*ba1276acSMatthew Dillon
218*ba1276acSMatthew Dillon ctx->major = gss_init_sec_context(&ctx->minor,
219*ba1276acSMatthew Dillon GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
220*ba1276acSMatthew Dillon GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
221*ba1276acSMatthew Dillon 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
222*ba1276acSMatthew Dillon
223*ba1276acSMatthew Dillon if (GSS_ERROR(ctx->major))
224*ba1276acSMatthew Dillon ssh_gssapi_error(ctx);
225*ba1276acSMatthew Dillon
226*ba1276acSMatthew Dillon return (ctx->major);
227*ba1276acSMatthew Dillon }
228*ba1276acSMatthew Dillon
229*ba1276acSMatthew Dillon /* Create a service name for the given host */
230*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_import_name(Gssctxt * ctx,const char * host)231*ba1276acSMatthew Dillon ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
232*ba1276acSMatthew Dillon {
233*ba1276acSMatthew Dillon gss_buffer_desc gssbuf;
234*ba1276acSMatthew Dillon char *val;
235*ba1276acSMatthew Dillon
236*ba1276acSMatthew Dillon xasprintf(&val, "host@%s", host);
237*ba1276acSMatthew Dillon gssbuf.value = val;
238*ba1276acSMatthew Dillon gssbuf.length = strlen(gssbuf.value);
239*ba1276acSMatthew Dillon
240*ba1276acSMatthew Dillon if ((ctx->major = gss_import_name(&ctx->minor,
241*ba1276acSMatthew Dillon &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
242*ba1276acSMatthew Dillon ssh_gssapi_error(ctx);
243*ba1276acSMatthew Dillon
244*ba1276acSMatthew Dillon free(gssbuf.value);
245*ba1276acSMatthew Dillon return (ctx->major);
246*ba1276acSMatthew Dillon }
247*ba1276acSMatthew Dillon
248*ba1276acSMatthew Dillon OM_uint32
ssh_gssapi_sign(Gssctxt * ctx,gss_buffer_t buffer,gss_buffer_t hash)249*ba1276acSMatthew Dillon ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
250*ba1276acSMatthew Dillon {
251*ba1276acSMatthew Dillon if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
252*ba1276acSMatthew Dillon GSS_C_QOP_DEFAULT, buffer, hash)))
253*ba1276acSMatthew Dillon ssh_gssapi_error(ctx);
254*ba1276acSMatthew Dillon
255*ba1276acSMatthew Dillon return (ctx->major);
256*ba1276acSMatthew Dillon }
257*ba1276acSMatthew Dillon
258*ba1276acSMatthew Dillon void
ssh_gssapi_buildmic(struct sshbuf * b,const char * user,const char * service,const char * context,const struct sshbuf * session_id)259*ba1276acSMatthew Dillon ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
260*ba1276acSMatthew Dillon const char *context, const struct sshbuf *session_id)
261*ba1276acSMatthew Dillon {
262*ba1276acSMatthew Dillon int r;
263*ba1276acSMatthew Dillon
264*ba1276acSMatthew Dillon sshbuf_reset(b);
265*ba1276acSMatthew Dillon if ((r = sshbuf_put_stringb(b, session_id)) != 0 ||
266*ba1276acSMatthew Dillon (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
267*ba1276acSMatthew Dillon (r = sshbuf_put_cstring(b, user)) != 0 ||
268*ba1276acSMatthew Dillon (r = sshbuf_put_cstring(b, service)) != 0 ||
269*ba1276acSMatthew Dillon (r = sshbuf_put_cstring(b, context)) != 0)
270*ba1276acSMatthew Dillon fatal_fr(r, "assemble buildmic");
271*ba1276acSMatthew Dillon }
272*ba1276acSMatthew Dillon
273*ba1276acSMatthew Dillon int
ssh_gssapi_check_mechanism(Gssctxt ** ctx,gss_OID oid,const char * host)274*ba1276acSMatthew Dillon ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
275*ba1276acSMatthew Dillon {
276*ba1276acSMatthew Dillon gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
277*ba1276acSMatthew Dillon OM_uint32 major, minor;
278*ba1276acSMatthew Dillon gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
279*ba1276acSMatthew Dillon
280*ba1276acSMatthew Dillon /* RFC 4462 says we MUST NOT do SPNEGO */
281*ba1276acSMatthew Dillon if (oid->length == spnego_oid.length &&
282*ba1276acSMatthew Dillon (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
283*ba1276acSMatthew Dillon return 0; /* false */
284*ba1276acSMatthew Dillon
285*ba1276acSMatthew Dillon ssh_gssapi_build_ctx(ctx);
286*ba1276acSMatthew Dillon ssh_gssapi_set_oid(*ctx, oid);
287*ba1276acSMatthew Dillon major = ssh_gssapi_import_name(*ctx, host);
288*ba1276acSMatthew Dillon if (!GSS_ERROR(major)) {
289*ba1276acSMatthew Dillon major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
290*ba1276acSMatthew Dillon NULL);
291*ba1276acSMatthew Dillon gss_release_buffer(&minor, &token);
292*ba1276acSMatthew Dillon if ((*ctx)->context != GSS_C_NO_CONTEXT)
293*ba1276acSMatthew Dillon gss_delete_sec_context(&minor, &(*ctx)->context,
294*ba1276acSMatthew Dillon GSS_C_NO_BUFFER);
295*ba1276acSMatthew Dillon }
296*ba1276acSMatthew Dillon
297*ba1276acSMatthew Dillon if (GSS_ERROR(major))
298*ba1276acSMatthew Dillon ssh_gssapi_delete_ctx(ctx);
299*ba1276acSMatthew Dillon
300*ba1276acSMatthew Dillon return (!GSS_ERROR(major));
301*ba1276acSMatthew Dillon }
302*ba1276acSMatthew Dillon
303*ba1276acSMatthew Dillon #endif /* GSSAPI */
304