Damien Miller | 3fcdfd5 | 2011-05-05 14:04:11 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-gss.c,v 1.17 2011/03/10 02:52:57 djm 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 | |
Darren Tucker | 8c6feda | 2006-08-05 15:24:59 +1000 | [diff] [blame] | 29 | #ifdef GSSAPI |
| 30 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 31 | #include <sys/types.h> |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 32 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
| 34 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include "xmalloc.h" |
| 36 | #include "key.h" |
| 37 | #include "hostfile.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 38 | #include "auth.h" |
| 39 | #include "ssh2.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 40 | #include "log.h" |
| 41 | #include "dispatch.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 42 | #include "buffer.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 43 | #include "servconf.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 44 | #include "packet.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 45 | #include "ssh-gss.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "monitor_wrap.h" |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 47 | |
| 48 | extern ServerOptions options; |
| 49 | |
| 50 | static void input_gssapi_token(int type, u_int32_t plen, void *ctxt); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 51 | static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 52 | static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt); |
| 53 | static void input_gssapi_errtok(int, u_int32_t, void *); |
| 54 | |
| 55 | /* |
| 56 | * We only support those mechanisms that we know about (ie ones that we know |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 57 | * how to check local user kuserok and the like) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 58 | */ |
| 59 | static int |
| 60 | userauth_gssapi(Authctxt *authctxt) |
| 61 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 62 | gss_OID_desc goid = {0, NULL}; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 63 | Gssctxt *ctxt = NULL; |
| 64 | int mechs; |
| 65 | gss_OID_set supported; |
| 66 | int present; |
| 67 | OM_uint32 ms; |
| 68 | u_int len; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 69 | u_char *doid = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 70 | |
| 71 | if (!authctxt->valid || authctxt->user == NULL) |
| 72 | return (0); |
| 73 | |
| 74 | mechs = packet_get_int(); |
| 75 | if (mechs == 0) { |
| 76 | debug("Mechanism negotiation is not supported"); |
| 77 | return (0); |
| 78 | } |
| 79 | |
| 80 | ssh_gssapi_supported_oids(&supported); |
| 81 | do { |
| 82 | mechs--; |
| 83 | |
| 84 | if (doid) |
| 85 | xfree(doid); |
| 86 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 87 | present = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 88 | doid = packet_get_string(&len); |
| 89 | |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 90 | if (len > 2 && doid[0] == SSH_GSS_OIDTYPE && |
| 91 | doid[1] == len - 2) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 92 | goid.elements = doid + 2; |
| 93 | goid.length = len - 2; |
| 94 | gss_test_oid_set_member(&ms, &goid, supported, |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 95 | &present); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 96 | } else { |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 97 | logit("Badly formed OID received"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 98 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 99 | } while (mechs > 0 && !present); |
| 100 | |
| 101 | gss_release_oid_set(&ms, &supported); |
| 102 | |
| 103 | if (!present) { |
| 104 | xfree(doid); |
Damien Miller | 3fcdfd5 | 2011-05-05 14:04:11 +1000 | [diff] [blame] | 105 | authctxt->server_caused_failure = 1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 106 | return (0); |
| 107 | } |
| 108 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 109 | if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) { |
Damien Miller | f23c096 | 2006-03-26 00:04:53 +1100 | [diff] [blame] | 110 | if (ctxt != NULL) |
| 111 | ssh_gssapi_delete_ctx(&ctxt); |
Damien Miller | 982d326 | 2003-09-02 22:59:01 +1000 | [diff] [blame] | 112 | xfree(doid); |
Damien Miller | 3fcdfd5 | 2011-05-05 14:04:11 +1000 | [diff] [blame] | 113 | authctxt->server_caused_failure = 1; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 114 | return (0); |
Damien Miller | 982d326 | 2003-09-02 22:59:01 +1000 | [diff] [blame] | 115 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 116 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 117 | authctxt->methoddata = (void *)ctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 118 | |
| 119 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE); |
| 120 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 121 | /* Return the OID that we received */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 122 | packet_put_string(doid, len); |
| 123 | |
| 124 | packet_send(); |
| 125 | xfree(doid); |
| 126 | |
| 127 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); |
| 128 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); |
| 129 | authctxt->postponed = 1; |
| 130 | |
| 131 | return (0); |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
| 136 | { |
| 137 | Authctxt *authctxt = ctxt; |
| 138 | Gssctxt *gssctxt; |
| 139 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 140 | gss_buffer_desc recv_tok; |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 141 | OM_uint32 maj_status, min_status, flags; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 142 | u_int len; |
| 143 | |
| 144 | if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) |
| 145 | fatal("No authentication or GSSAPI context"); |
| 146 | |
| 147 | gssctxt = authctxt->methoddata; |
| 148 | recv_tok.value = packet_get_string(&len); |
| 149 | recv_tok.length = len; /* u_int vs. size_t */ |
| 150 | |
| 151 | packet_check_eom(); |
| 152 | |
| 153 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 154 | &send_tok, &flags)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 155 | |
| 156 | xfree(recv_tok.value); |
| 157 | |
| 158 | if (GSS_ERROR(maj_status)) { |
| 159 | if (send_tok.length != 0) { |
| 160 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); |
| 161 | packet_put_string(send_tok.value, send_tok.length); |
| 162 | packet_send(); |
| 163 | } |
| 164 | authctxt->postponed = 0; |
| 165 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 166 | userauth_finish(authctxt, 0, "gssapi-with-mic"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 167 | } else { |
| 168 | if (send_tok.length != 0) { |
| 169 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); |
| 170 | packet_put_string(send_tok.value, send_tok.length); |
| 171 | packet_send(); |
| 172 | } |
| 173 | if (maj_status == GSS_S_COMPLETE) { |
| 174 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 175 | if (flags & GSS_C_INTEG_FLAG) |
| 176 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, |
| 177 | &input_gssapi_mic); |
| 178 | else |
| 179 | dispatch_set( |
| 180 | SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, |
| 181 | &input_gssapi_exchange_complete); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | gss_release_buffer(&min_status, &send_tok); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) |
| 190 | { |
| 191 | Authctxt *authctxt = ctxt; |
| 192 | Gssctxt *gssctxt; |
| 193 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 194 | gss_buffer_desc recv_tok; |
| 195 | OM_uint32 maj_status; |
Damien Miller | 55c47ed | 2003-09-02 22:14:07 +1000 | [diff] [blame] | 196 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 197 | |
| 198 | if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) |
| 199 | fatal("No authentication or GSSAPI context"); |
| 200 | |
| 201 | gssctxt = authctxt->methoddata; |
Damien Miller | 55c47ed | 2003-09-02 22:14:07 +1000 | [diff] [blame] | 202 | recv_tok.value = packet_get_string(&len); |
| 203 | recv_tok.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 204 | |
| 205 | packet_check_eom(); |
| 206 | |
| 207 | /* Push the error token into GSSAPI to see what it says */ |
| 208 | maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, |
| 209 | &send_tok, NULL)); |
| 210 | |
| 211 | xfree(recv_tok.value); |
| 212 | |
| 213 | /* We can't return anything to the client, even if we wanted to */ |
| 214 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 215 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); |
| 216 | |
| 217 | /* The client will have already moved on to the next auth */ |
| 218 | |
| 219 | gss_release_buffer(&maj_status, &send_tok); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * This is called when the client thinks we've completed authentication. |
| 224 | * It should only be enabled in the dispatch handler by the function above, |
| 225 | * which only enables it once the GSSAPI exchange is complete. |
| 226 | */ |
| 227 | |
| 228 | static void |
| 229 | input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt) |
| 230 | { |
| 231 | Authctxt *authctxt = ctxt; |
| 232 | Gssctxt *gssctxt; |
| 233 | int authenticated; |
| 234 | |
| 235 | if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) |
| 236 | fatal("No authentication or GSSAPI context"); |
| 237 | |
| 238 | gssctxt = authctxt->methoddata; |
| 239 | |
| 240 | /* |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 241 | * We don't need to check the status, because we're only enabled in |
| 242 | * the dispatcher once the exchange is complete |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 243 | */ |
| 244 | |
| 245 | packet_check_eom(); |
| 246 | |
| 247 | authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); |
| 248 | |
| 249 | authctxt->postponed = 0; |
| 250 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 251 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 252 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 253 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 254 | userauth_finish(authctxt, authenticated, "gssapi-with-mic"); |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | input_gssapi_mic(int type, u_int32_t plen, void *ctxt) |
| 259 | { |
| 260 | Authctxt *authctxt = ctxt; |
| 261 | Gssctxt *gssctxt; |
| 262 | int authenticated = 0; |
| 263 | Buffer b; |
| 264 | gss_buffer_desc mic, gssbuf; |
| 265 | u_int len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 266 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 267 | if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) |
| 268 | fatal("No authentication or GSSAPI context"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 269 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 270 | gssctxt = authctxt->methoddata; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 271 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 272 | mic.value = packet_get_string(&len); |
| 273 | mic.length = len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 274 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 275 | ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service, |
| 276 | "gssapi-with-mic"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 277 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 278 | gssbuf.value = buffer_ptr(&b); |
| 279 | gssbuf.length = buffer_len(&b); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 280 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 281 | if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic)))) |
| 282 | authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); |
| 283 | else |
| 284 | logit("GSSAPI MIC check failed"); |
| 285 | |
| 286 | buffer_free(&b); |
| 287 | xfree(mic.value); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 288 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 289 | authctxt->postponed = 0; |
| 290 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
| 291 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); |
| 292 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); |
| 293 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); |
| 294 | userauth_finish(authctxt, authenticated, "gssapi-with-mic"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | Authmethod method_gssapi = { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 298 | "gssapi-with-mic", |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 299 | userauth_gssapi, |
| 300 | &options.gss_authentication |
| 301 | }; |
Darren Tucker | 8c6feda | 2006-08-05 15:24:59 +1000 | [diff] [blame] | 302 | |
| 303 | #endif /* GSSAPI */ |