djm@openbsd.org | 0f3958c | 2018-07-10 09:13:30 +0000 | [diff] [blame] | 1 | /* $OpenBSD: gss-genr.c,v 1.26 2018/07/10 09:13:30 djm Exp $ */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2 | |
| 3 | /* |
Darren Tucker | b1e128f | 2007-06-12 23:44:36 +1000 | [diff] [blame] | 4 | * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved. |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 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 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 32 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 33 | #include <limits.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 35 | #include <string.h> |
Darren Tucker | aa72196 | 2015-05-22 17:49:46 +1000 | [diff] [blame] | 36 | #include <signal.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 37 | #include <unistd.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 40 | #include "ssherr.h" |
| 41 | #include "sshbuf.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 42 | #include "log.h" |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 43 | #include "ssh2.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 44 | |
| 45 | #include "ssh-gss.h" |
| 46 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 47 | extern u_char *session_id2; |
| 48 | extern u_int session_id2_len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 49 | |
djm@openbsd.org | 0f3958c | 2018-07-10 09:13:30 +0000 | [diff] [blame] | 50 | /* sshbuf_get for gss_buffer_desc */ |
| 51 | int |
| 52 | ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g) |
| 53 | { |
| 54 | int r; |
| 55 | u_char *p; |
| 56 | size_t len; |
| 57 | |
| 58 | if ((r = sshbuf_get_string(b, &p, &len)) != 0) |
| 59 | return r; |
| 60 | g->value = p; |
| 61 | g->length = len; |
| 62 | return 0; |
| 63 | } |
| 64 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 65 | /* Check that the OID in a data stream matches that in the context */ |
| 66 | int |
| 67 | ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len) |
| 68 | { |
| 69 | return (ctx != NULL && ctx->oid != GSS_C_NO_OID && |
| 70 | ctx->oid->length == len && |
| 71 | memcmp(ctx->oid->elements, data, len) == 0); |
| 72 | } |
| 73 | |
| 74 | /* Set the contexts OID from a data stream */ |
| 75 | void |
| 76 | ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) |
| 77 | { |
| 78 | if (ctx->oid != GSS_C_NO_OID) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 79 | free(ctx->oid->elements); |
| 80 | free(ctx->oid); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 81 | } |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 82 | ctx->oid = xcalloc(1, sizeof(gss_OID_desc)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 83 | ctx->oid->length = len; |
| 84 | ctx->oid->elements = xmalloc(len); |
| 85 | memcpy(ctx->oid->elements, data, len); |
| 86 | } |
| 87 | |
| 88 | /* Set the contexts OID */ |
| 89 | void |
| 90 | ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) |
| 91 | { |
| 92 | ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length); |
| 93 | } |
| 94 | |
| 95 | /* All this effort to report an error ... */ |
| 96 | void |
| 97 | ssh_gssapi_error(Gssctxt *ctxt) |
| 98 | { |
Damien Miller | a66cf68 | 2006-03-26 00:05:23 +1100 | [diff] [blame] | 99 | char *s; |
| 100 | |
| 101 | s = ssh_gssapi_last_error(ctxt, NULL, NULL); |
| 102 | debug("%s", s); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 103 | free(s); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | char * |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 107 | ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, |
| 108 | OM_uint32 *minor_status) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 109 | { |
| 110 | OM_uint32 lmin; |
| 111 | gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; |
| 112 | OM_uint32 ctx; |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 113 | struct sshbuf *b; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 114 | char *ret; |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 115 | int r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 116 | |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 117 | if ((b = sshbuf_new()) == NULL) |
| 118 | fatal("%s: sshbuf_new failed", __func__); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 119 | |
| 120 | if (major_status != NULL) |
| 121 | *major_status = ctxt->major; |
| 122 | if (minor_status != NULL) |
| 123 | *minor_status = ctxt->minor; |
| 124 | |
| 125 | ctx = 0; |
| 126 | /* The GSSAPI error */ |
| 127 | do { |
| 128 | gss_display_status(&lmin, ctxt->major, |
Darren Tucker | b1e128f | 2007-06-12 23:44:36 +1000 | [diff] [blame] | 129 | GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 130 | |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 131 | if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 || |
| 132 | (r = sshbuf_put_u8(b, '\n')) != 0) |
| 133 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 134 | |
| 135 | gss_release_buffer(&lmin, &msg); |
| 136 | } while (ctx != 0); |
| 137 | |
| 138 | /* The mechanism specific error */ |
| 139 | do { |
| 140 | gss_display_status(&lmin, ctxt->minor, |
Darren Tucker | b1e128f | 2007-06-12 23:44:36 +1000 | [diff] [blame] | 141 | GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 142 | |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 143 | if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 || |
| 144 | (r = sshbuf_put_u8(b, '\n')) != 0) |
| 145 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 146 | |
| 147 | gss_release_buffer(&lmin, &msg); |
| 148 | } while (ctx != 0); |
| 149 | |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 150 | if ((r = sshbuf_put_u8(b, '\n')) != 0) |
| 151 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 152 | ret = xstrdup((const char *)sshbuf_ptr(b)); |
| 153 | sshbuf_free(b); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 154 | return (ret); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Initialise our GSSAPI context. We use this opaque structure to contain all |
| 159 | * of the data which both the client and server need to persist across |
| 160 | * {accept,init}_sec_context calls, so that when we do it from the userauth |
| 161 | * stuff life is a little easier |
| 162 | */ |
| 163 | void |
| 164 | ssh_gssapi_build_ctx(Gssctxt **ctx) |
| 165 | { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 166 | *ctx = xcalloc(1, sizeof (Gssctxt)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 167 | (*ctx)->context = GSS_C_NO_CONTEXT; |
| 168 | (*ctx)->name = GSS_C_NO_NAME; |
| 169 | (*ctx)->oid = GSS_C_NO_OID; |
| 170 | (*ctx)->creds = GSS_C_NO_CREDENTIAL; |
| 171 | (*ctx)->client = GSS_C_NO_NAME; |
| 172 | (*ctx)->client_creds = GSS_C_NO_CREDENTIAL; |
| 173 | } |
| 174 | |
| 175 | /* Delete our context, providing it has been built correctly */ |
| 176 | void |
| 177 | ssh_gssapi_delete_ctx(Gssctxt **ctx) |
| 178 | { |
| 179 | OM_uint32 ms; |
| 180 | |
| 181 | if ((*ctx) == NULL) |
| 182 | return; |
| 183 | if ((*ctx)->context != GSS_C_NO_CONTEXT) |
| 184 | gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER); |
| 185 | if ((*ctx)->name != GSS_C_NO_NAME) |
| 186 | gss_release_name(&ms, &(*ctx)->name); |
| 187 | if ((*ctx)->oid != GSS_C_NO_OID) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 188 | free((*ctx)->oid->elements); |
| 189 | free((*ctx)->oid); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 190 | (*ctx)->oid = GSS_C_NO_OID; |
| 191 | } |
| 192 | if ((*ctx)->creds != GSS_C_NO_CREDENTIAL) |
| 193 | gss_release_cred(&ms, &(*ctx)->creds); |
| 194 | if ((*ctx)->client != GSS_C_NO_NAME) |
| 195 | gss_release_name(&ms, &(*ctx)->client); |
| 196 | if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL) |
| 197 | gss_release_cred(&ms, &(*ctx)->client_creds); |
| 198 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 199 | free(*ctx); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 200 | *ctx = NULL; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Wrapper to init_sec_context |
| 205 | * Requires that the context contains: |
| 206 | * oid |
| 207 | * server name (from ssh_gssapi_import_name) |
| 208 | */ |
| 209 | OM_uint32 |
| 210 | ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok, |
| 211 | gss_buffer_desc* send_tok, OM_uint32 *flags) |
| 212 | { |
| 213 | int deleg_flag = 0; |
| 214 | |
| 215 | if (deleg_creds) { |
| 216 | deleg_flag = GSS_C_DELEG_FLAG; |
| 217 | debug("Delegating credentials"); |
| 218 | } |
| 219 | |
| 220 | ctx->major = gss_init_sec_context(&ctx->minor, |
| 221 | GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid, |
| 222 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag, |
| 223 | 0, NULL, recv_tok, NULL, send_tok, flags, NULL); |
| 224 | |
| 225 | if (GSS_ERROR(ctx->major)) |
| 226 | ssh_gssapi_error(ctx); |
| 227 | |
| 228 | return (ctx->major); |
| 229 | } |
| 230 | |
| 231 | /* Create a service name for the given host */ |
| 232 | OM_uint32 |
| 233 | ssh_gssapi_import_name(Gssctxt *ctx, const char *host) |
| 234 | { |
| 235 | gss_buffer_desc gssbuf; |
Damien Miller | 63e437f | 2006-04-23 12:05:46 +1000 | [diff] [blame] | 236 | char *val; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 237 | |
Damien Miller | 63e437f | 2006-04-23 12:05:46 +1000 | [diff] [blame] | 238 | xasprintf(&val, "host@%s", host); |
| 239 | gssbuf.value = val; |
| 240 | gssbuf.length = strlen(gssbuf.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 241 | |
| 242 | if ((ctx->major = gss_import_name(&ctx->minor, |
| 243 | &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name))) |
| 244 | ssh_gssapi_error(ctx); |
| 245 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 246 | free(gssbuf.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 247 | return (ctx->major); |
| 248 | } |
| 249 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 250 | OM_uint32 |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 251 | ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash) |
| 252 | { |
| 253 | if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context, |
| 254 | GSS_C_QOP_DEFAULT, buffer, hash))) |
| 255 | ssh_gssapi_error(ctx); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 256 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 257 | return (ctx->major); |
| 258 | } |
| 259 | |
| 260 | void |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 261 | ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service, |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 262 | const char *context) |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 263 | { |
markus@openbsd.org | b8d9214 | 2018-07-09 21:37:55 +0000 | [diff] [blame] | 264 | int r; |
| 265 | |
| 266 | sshbuf_reset(b); |
| 267 | if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || |
| 268 | (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 269 | (r = sshbuf_put_cstring(b, user)) != 0 || |
| 270 | (r = sshbuf_put_cstring(b, service)) != 0 || |
| 271 | (r = sshbuf_put_cstring(b, context)) != 0) |
| 272 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 273 | } |
| 274 | |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 275 | int |
Damien Miller | 3d2d6e9 | 2006-08-19 00:46:43 +1000 | [diff] [blame] | 276 | ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host) |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 277 | { |
| 278 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; |
| 279 | OM_uint32 major, minor; |
| 280 | gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"}; |
| 281 | |
| 282 | /* RFC 4462 says we MUST NOT do SPNEGO */ |
| 283 | if (oid->length == spnego_oid.length && |
| 284 | (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0)) |
Damien Miller | deccaa7 | 2006-08-19 08:50:57 +1000 | [diff] [blame] | 285 | return 0; /* false */ |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 286 | |
| 287 | ssh_gssapi_build_ctx(ctx); |
| 288 | ssh_gssapi_set_oid(*ctx, oid); |
| 289 | major = ssh_gssapi_import_name(*ctx, host); |
| 290 | if (!GSS_ERROR(major)) { |
| 291 | major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, |
| 292 | NULL); |
| 293 | gss_release_buffer(&minor, &token); |
Damien Miller | 76758b6 | 2006-08-30 11:08:04 +1000 | [diff] [blame] | 294 | if ((*ctx)->context != GSS_C_NO_CONTEXT) |
| 295 | gss_delete_sec_context(&minor, &(*ctx)->context, |
| 296 | GSS_C_NO_BUFFER); |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (GSS_ERROR(major)) |
| 300 | ssh_gssapi_delete_ctx(ctx); |
| 301 | |
| 302 | return (!GSS_ERROR(major)); |
| 303 | } |
| 304 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 305 | #endif /* GSSAPI */ |