Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 1 | /* $OpenBSD: gss-genr.c,v 1.6 2005/10/13 22:24:31 stevesk Exp $ */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2 | |
| 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 | |
| 29 | #ifdef GSSAPI |
| 30 | |
| 31 | #include "xmalloc.h" |
| 32 | #include "bufaux.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 33 | #include "log.h" |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 34 | #include "ssh2.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 35 | |
| 36 | #include "ssh-gss.h" |
| 37 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 38 | extern u_char *session_id2; |
| 39 | extern u_int session_id2_len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 40 | |
| 41 | /* Check that the OID in a data stream matches that in the context */ |
| 42 | int |
| 43 | ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len) |
| 44 | { |
| 45 | return (ctx != NULL && ctx->oid != GSS_C_NO_OID && |
| 46 | ctx->oid->length == len && |
| 47 | memcmp(ctx->oid->elements, data, len) == 0); |
| 48 | } |
| 49 | |
| 50 | /* Set the contexts OID from a data stream */ |
| 51 | void |
| 52 | ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) |
| 53 | { |
| 54 | if (ctx->oid != GSS_C_NO_OID) { |
| 55 | xfree(ctx->oid->elements); |
| 56 | xfree(ctx->oid); |
| 57 | } |
| 58 | ctx->oid = xmalloc(sizeof(gss_OID_desc)); |
| 59 | ctx->oid->length = len; |
| 60 | ctx->oid->elements = xmalloc(len); |
| 61 | memcpy(ctx->oid->elements, data, len); |
| 62 | } |
| 63 | |
| 64 | /* Set the contexts OID */ |
| 65 | void |
| 66 | ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) |
| 67 | { |
| 68 | ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length); |
| 69 | } |
| 70 | |
| 71 | /* All this effort to report an error ... */ |
| 72 | void |
| 73 | ssh_gssapi_error(Gssctxt *ctxt) |
| 74 | { |
| 75 | debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL)); |
| 76 | } |
| 77 | |
| 78 | char * |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 79 | ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, |
| 80 | OM_uint32 *minor_status) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 81 | { |
| 82 | OM_uint32 lmin; |
| 83 | gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; |
| 84 | OM_uint32 ctx; |
| 85 | Buffer b; |
| 86 | char *ret; |
| 87 | |
| 88 | buffer_init(&b); |
| 89 | |
| 90 | if (major_status != NULL) |
| 91 | *major_status = ctxt->major; |
| 92 | if (minor_status != NULL) |
| 93 | *minor_status = ctxt->minor; |
| 94 | |
| 95 | ctx = 0; |
| 96 | /* The GSSAPI error */ |
| 97 | do { |
| 98 | gss_display_status(&lmin, ctxt->major, |
| 99 | GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg); |
| 100 | |
| 101 | buffer_append(&b, msg.value, msg.length); |
| 102 | buffer_put_char(&b, '\n'); |
| 103 | |
| 104 | gss_release_buffer(&lmin, &msg); |
| 105 | } while (ctx != 0); |
| 106 | |
| 107 | /* The mechanism specific error */ |
| 108 | do { |
| 109 | gss_display_status(&lmin, ctxt->minor, |
| 110 | GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg); |
| 111 | |
| 112 | buffer_append(&b, msg.value, msg.length); |
| 113 | buffer_put_char(&b, '\n'); |
| 114 | |
| 115 | gss_release_buffer(&lmin, &msg); |
| 116 | } while (ctx != 0); |
| 117 | |
| 118 | buffer_put_char(&b, '\0'); |
| 119 | ret = xmalloc(buffer_len(&b)); |
| 120 | buffer_get(&b, ret, buffer_len(&b)); |
| 121 | buffer_free(&b); |
| 122 | return (ret); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Initialise our GSSAPI context. We use this opaque structure to contain all |
| 127 | * of the data which both the client and server need to persist across |
| 128 | * {accept,init}_sec_context calls, so that when we do it from the userauth |
| 129 | * stuff life is a little easier |
| 130 | */ |
| 131 | void |
| 132 | ssh_gssapi_build_ctx(Gssctxt **ctx) |
| 133 | { |
| 134 | *ctx = xmalloc(sizeof (Gssctxt)); |
| 135 | (*ctx)->major = 0; |
| 136 | (*ctx)->minor = 0; |
| 137 | (*ctx)->context = GSS_C_NO_CONTEXT; |
| 138 | (*ctx)->name = GSS_C_NO_NAME; |
| 139 | (*ctx)->oid = GSS_C_NO_OID; |
| 140 | (*ctx)->creds = GSS_C_NO_CREDENTIAL; |
| 141 | (*ctx)->client = GSS_C_NO_NAME; |
| 142 | (*ctx)->client_creds = GSS_C_NO_CREDENTIAL; |
| 143 | } |
| 144 | |
| 145 | /* Delete our context, providing it has been built correctly */ |
| 146 | void |
| 147 | ssh_gssapi_delete_ctx(Gssctxt **ctx) |
| 148 | { |
| 149 | OM_uint32 ms; |
| 150 | |
| 151 | if ((*ctx) == NULL) |
| 152 | return; |
| 153 | if ((*ctx)->context != GSS_C_NO_CONTEXT) |
| 154 | gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER); |
| 155 | if ((*ctx)->name != GSS_C_NO_NAME) |
| 156 | gss_release_name(&ms, &(*ctx)->name); |
| 157 | if ((*ctx)->oid != GSS_C_NO_OID) { |
| 158 | xfree((*ctx)->oid->elements); |
| 159 | xfree((*ctx)->oid); |
| 160 | (*ctx)->oid = GSS_C_NO_OID; |
| 161 | } |
| 162 | if ((*ctx)->creds != GSS_C_NO_CREDENTIAL) |
| 163 | gss_release_cred(&ms, &(*ctx)->creds); |
| 164 | if ((*ctx)->client != GSS_C_NO_NAME) |
| 165 | gss_release_name(&ms, &(*ctx)->client); |
| 166 | if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL) |
| 167 | gss_release_cred(&ms, &(*ctx)->client_creds); |
| 168 | |
| 169 | xfree(*ctx); |
| 170 | *ctx = NULL; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Wrapper to init_sec_context |
| 175 | * Requires that the context contains: |
| 176 | * oid |
| 177 | * server name (from ssh_gssapi_import_name) |
| 178 | */ |
| 179 | OM_uint32 |
| 180 | ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok, |
| 181 | gss_buffer_desc* send_tok, OM_uint32 *flags) |
| 182 | { |
| 183 | int deleg_flag = 0; |
| 184 | |
| 185 | if (deleg_creds) { |
| 186 | deleg_flag = GSS_C_DELEG_FLAG; |
| 187 | debug("Delegating credentials"); |
| 188 | } |
| 189 | |
| 190 | ctx->major = gss_init_sec_context(&ctx->minor, |
| 191 | GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid, |
| 192 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag, |
| 193 | 0, NULL, recv_tok, NULL, send_tok, flags, NULL); |
| 194 | |
| 195 | if (GSS_ERROR(ctx->major)) |
| 196 | ssh_gssapi_error(ctx); |
| 197 | |
| 198 | return (ctx->major); |
| 199 | } |
| 200 | |
| 201 | /* Create a service name for the given host */ |
| 202 | OM_uint32 |
| 203 | ssh_gssapi_import_name(Gssctxt *ctx, const char *host) |
| 204 | { |
| 205 | gss_buffer_desc gssbuf; |
| 206 | |
| 207 | gssbuf.length = sizeof("host@") + strlen(host); |
| 208 | gssbuf.value = xmalloc(gssbuf.length); |
| 209 | snprintf(gssbuf.value, gssbuf.length, "host@%s", host); |
| 210 | |
| 211 | if ((ctx->major = gss_import_name(&ctx->minor, |
| 212 | &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name))) |
| 213 | ssh_gssapi_error(ctx); |
| 214 | |
| 215 | xfree(gssbuf.value); |
| 216 | return (ctx->major); |
| 217 | } |
| 218 | |
| 219 | /* Acquire credentials for a server running on the current host. |
| 220 | * Requires that the context structure contains a valid OID |
| 221 | */ |
| 222 | |
| 223 | /* Returns a GSSAPI error code */ |
| 224 | OM_uint32 |
| 225 | ssh_gssapi_acquire_cred(Gssctxt *ctx) |
| 226 | { |
| 227 | OM_uint32 status; |
| 228 | char lname[MAXHOSTNAMELEN]; |
| 229 | gss_OID_set oidset; |
| 230 | |
| 231 | gss_create_empty_oid_set(&status, &oidset); |
| 232 | gss_add_oid_set_member(&status, ctx->oid, &oidset); |
| 233 | |
| 234 | if (gethostname(lname, MAXHOSTNAMELEN)) |
| 235 | return (-1); |
| 236 | |
| 237 | if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) |
| 238 | return (ctx->major); |
| 239 | |
| 240 | if ((ctx->major = gss_acquire_cred(&ctx->minor, |
| 241 | ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL))) |
| 242 | ssh_gssapi_error(ctx); |
| 243 | |
| 244 | gss_release_oid_set(&status, &oidset); |
| 245 | return (ctx->major); |
| 246 | } |
| 247 | |
| 248 | OM_uint32 |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 249 | ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash) |
| 250 | { |
| 251 | if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context, |
| 252 | GSS_C_QOP_DEFAULT, buffer, hash))) |
| 253 | ssh_gssapi_error(ctx); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 254 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 255 | return (ctx->major); |
| 256 | } |
| 257 | |
| 258 | void |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 259 | ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service, |
| 260 | const char *context) |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 261 | { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 262 | buffer_init(b); |
| 263 | buffer_put_string(b, session_id2, session_id2_len); |
| 264 | buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST); |
| 265 | buffer_put_cstring(b, user); |
| 266 | buffer_put_cstring(b, service); |
| 267 | buffer_put_cstring(b, context); |
| 268 | } |
| 269 | |
| 270 | OM_uint32 |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 271 | ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) |
| 272 | { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 273 | if (*ctx) |
| 274 | ssh_gssapi_delete_ctx(ctx); |
| 275 | ssh_gssapi_build_ctx(ctx); |
| 276 | ssh_gssapi_set_oid(*ctx, oid); |
| 277 | return (ssh_gssapi_acquire_cred(*ctx)); |
| 278 | } |
| 279 | |
| 280 | #endif /* GSSAPI */ |