1295367Sdes/* $OpenBSD: auth2-gss.c,v 1.22 2015/01/19 20:07:45 markus Exp $ */ 2124208Sdes 3124208Sdes/* 4124208Sdes * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. 5124208Sdes * 6124208Sdes * Redistribution and use in source and binary forms, with or without 7124208Sdes * modification, are permitted provided that the following conditions 8124208Sdes * are met: 9124208Sdes * 1. Redistributions of source code must retain the above copyright 10124208Sdes * notice, this list of conditions and the following disclaimer. 11124208Sdes * 2. Redistributions in binary form must reproduce the above copyright 12124208Sdes * notice, this list of conditions and the following disclaimer in the 13124208Sdes * documentation and/or other materials provided with the distribution. 14124208Sdes * 15124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR 16124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25124208Sdes */ 26124208Sdes 27124208Sdes#include "includes.h" 28124208Sdes 29124208Sdes#ifdef GSSAPI 30124208Sdes 31162852Sdes#include <sys/types.h> 32162852Sdes 33162852Sdes#include <stdarg.h> 34162852Sdes 35162852Sdes#include "xmalloc.h" 36162852Sdes#include "key.h" 37162852Sdes#include "hostfile.h" 38124208Sdes#include "auth.h" 39124208Sdes#include "ssh2.h" 40124208Sdes#include "log.h" 41124208Sdes#include "dispatch.h" 42162852Sdes#include "buffer.h" 43295367Sdes#include "misc.h" 44124208Sdes#include "servconf.h" 45124208Sdes#include "packet.h" 46162852Sdes#include "ssh-gss.h" 47124208Sdes#include "monitor_wrap.h" 48124208Sdes 49124208Sdesextern ServerOptions options; 50124208Sdes 51295367Sdesstatic int input_gssapi_token(int type, u_int32_t plen, void *ctxt); 52295367Sdesstatic int input_gssapi_mic(int type, u_int32_t plen, void *ctxt); 53295367Sdesstatic int input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt); 54295367Sdesstatic int input_gssapi_errtok(int, u_int32_t, void *); 55124208Sdes 56124208Sdes/* 57124208Sdes * We only support those mechanisms that we know about (ie ones that we know 58157016Sdes * how to check local user kuserok and the like) 59124208Sdes */ 60124208Sdesstatic int 61124208Sdesuserauth_gssapi(Authctxt *authctxt) 62124208Sdes{ 63137015Sdes gss_OID_desc goid = {0, NULL}; 64124208Sdes Gssctxt *ctxt = NULL; 65124208Sdes int mechs; 66124208Sdes int present; 67124208Sdes OM_uint32 ms; 68124208Sdes u_int len; 69149749Sdes u_char *doid = NULL; 70124208Sdes 71124208Sdes if (!authctxt->valid || authctxt->user == NULL) 72124208Sdes return (0); 73124208Sdes 74124208Sdes mechs = packet_get_int(); 75124208Sdes if (mechs == 0) { 76124208Sdes debug("Mechanism negotiation is not supported"); 77124208Sdes return (0); 78124208Sdes } 79124208Sdes 80124208Sdes do { 81124208Sdes mechs--; 82124208Sdes 83255767Sdes free(doid); 84124208Sdes 85126274Sdes present = 0; 86124208Sdes doid = packet_get_string(&len); 87124208Sdes 88149749Sdes if (len > 2 && doid[0] == SSH_GSS_OIDTYPE && 89149749Sdes doid[1] == len - 2) { 90137015Sdes goid.elements = doid + 2; 91137015Sdes goid.length = len - 2; 92264377Sdes ssh_gssapi_test_oid_supported(&ms, &goid, &present); 93126274Sdes } else { 94126274Sdes logit("Badly formed OID received"); 95124208Sdes } 96124208Sdes } while (mechs > 0 && !present); 97124208Sdes 98124208Sdes if (!present) { 99255767Sdes free(doid); 100226046Sdes authctxt->server_caused_failure = 1; 101124208Sdes return (0); 102124208Sdes } 103124208Sdes 104137015Sdes if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) { 105162852Sdes if (ctxt != NULL) 106162852Sdes ssh_gssapi_delete_ctx(&ctxt); 107255767Sdes free(doid); 108226046Sdes authctxt->server_caused_failure = 1; 109124208Sdes return (0); 110124208Sdes } 111124208Sdes 112157016Sdes authctxt->methoddata = (void *)ctxt; 113124208Sdes 114124208Sdes packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE); 115124208Sdes 116126274Sdes /* Return the OID that we received */ 117124208Sdes packet_put_string(doid, len); 118124208Sdes 119124208Sdes packet_send(); 120255767Sdes free(doid); 121124208Sdes 122124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 123124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 124124208Sdes authctxt->postponed = 1; 125124208Sdes 126124208Sdes return (0); 127124208Sdes} 128124208Sdes 129295367Sdesstatic int 130124208Sdesinput_gssapi_token(int type, u_int32_t plen, void *ctxt) 131124208Sdes{ 132124208Sdes Authctxt *authctxt = ctxt; 133124208Sdes Gssctxt *gssctxt; 134124208Sdes gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 135124208Sdes gss_buffer_desc recv_tok; 136126274Sdes OM_uint32 maj_status, min_status, flags; 137124208Sdes u_int len; 138124208Sdes 139124208Sdes if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) 140124208Sdes fatal("No authentication or GSSAPI context"); 141124208Sdes 142124208Sdes gssctxt = authctxt->methoddata; 143124208Sdes recv_tok.value = packet_get_string(&len); 144124208Sdes recv_tok.length = len; /* u_int vs. size_t */ 145124208Sdes 146124208Sdes packet_check_eom(); 147124208Sdes 148124208Sdes maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, 149126274Sdes &send_tok, &flags)); 150124208Sdes 151255767Sdes free(recv_tok.value); 152124208Sdes 153124208Sdes if (GSS_ERROR(maj_status)) { 154124208Sdes if (send_tok.length != 0) { 155124208Sdes packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); 156124208Sdes packet_put_string(send_tok.value, send_tok.length); 157124208Sdes packet_send(); 158124208Sdes } 159124208Sdes authctxt->postponed = 0; 160124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 161248619Sdes userauth_finish(authctxt, 0, "gssapi-with-mic", NULL); 162124208Sdes } else { 163124208Sdes if (send_tok.length != 0) { 164124208Sdes packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); 165124208Sdes packet_put_string(send_tok.value, send_tok.length); 166124208Sdes packet_send(); 167124208Sdes } 168124208Sdes if (maj_status == GSS_S_COMPLETE) { 169124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 170126274Sdes if (flags & GSS_C_INTEG_FLAG) 171126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, 172126274Sdes &input_gssapi_mic); 173126274Sdes else 174126274Sdes dispatch_set( 175126274Sdes SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, 176126274Sdes &input_gssapi_exchange_complete); 177124208Sdes } 178124208Sdes } 179124208Sdes 180124208Sdes gss_release_buffer(&min_status, &send_tok); 181295367Sdes return 0; 182124208Sdes} 183124208Sdes 184295367Sdesstatic int 185124208Sdesinput_gssapi_errtok(int type, u_int32_t plen, void *ctxt) 186124208Sdes{ 187124208Sdes Authctxt *authctxt = ctxt; 188124208Sdes Gssctxt *gssctxt; 189124208Sdes gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 190124208Sdes gss_buffer_desc recv_tok; 191124208Sdes OM_uint32 maj_status; 192124208Sdes u_int len; 193124208Sdes 194124208Sdes if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) 195124208Sdes fatal("No authentication or GSSAPI context"); 196124208Sdes 197124208Sdes gssctxt = authctxt->methoddata; 198124208Sdes recv_tok.value = packet_get_string(&len); 199124208Sdes recv_tok.length = len; 200124208Sdes 201124208Sdes packet_check_eom(); 202124208Sdes 203124208Sdes /* Push the error token into GSSAPI to see what it says */ 204124208Sdes maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, 205124208Sdes &send_tok, NULL)); 206124208Sdes 207255767Sdes free(recv_tok.value); 208124208Sdes 209124208Sdes /* We can't return anything to the client, even if we wanted to */ 210124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 211124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 212124208Sdes 213124208Sdes /* The client will have already moved on to the next auth */ 214124208Sdes 215124208Sdes gss_release_buffer(&maj_status, &send_tok); 216295367Sdes return 0; 217124208Sdes} 218124208Sdes 219124208Sdes/* 220124208Sdes * This is called when the client thinks we've completed authentication. 221124208Sdes * It should only be enabled in the dispatch handler by the function above, 222124208Sdes * which only enables it once the GSSAPI exchange is complete. 223124208Sdes */ 224124208Sdes 225295367Sdesstatic int 226124208Sdesinput_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt) 227124208Sdes{ 228124208Sdes Authctxt *authctxt = ctxt; 229124208Sdes int authenticated; 230124208Sdes 231124208Sdes if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) 232124208Sdes fatal("No authentication or GSSAPI context"); 233124208Sdes 234124208Sdes /* 235126274Sdes * We don't need to check the status, because we're only enabled in 236126274Sdes * the dispatcher once the exchange is complete 237124208Sdes */ 238124208Sdes 239124208Sdes packet_check_eom(); 240124208Sdes 241124208Sdes authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); 242124208Sdes 243124208Sdes authctxt->postponed = 0; 244124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 245124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 246126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); 247124208Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 248248619Sdes userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL); 249295367Sdes return 0; 250124208Sdes} 251124208Sdes 252295367Sdesstatic int 253126274Sdesinput_gssapi_mic(int type, u_int32_t plen, void *ctxt) 254126274Sdes{ 255126274Sdes Authctxt *authctxt = ctxt; 256126274Sdes Gssctxt *gssctxt; 257126274Sdes int authenticated = 0; 258126274Sdes Buffer b; 259126274Sdes gss_buffer_desc mic, gssbuf; 260126274Sdes u_int len; 261126274Sdes 262126274Sdes if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) 263126274Sdes fatal("No authentication or GSSAPI context"); 264126274Sdes 265126274Sdes gssctxt = authctxt->methoddata; 266126274Sdes 267126274Sdes mic.value = packet_get_string(&len); 268126274Sdes mic.length = len; 269126274Sdes 270126274Sdes ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service, 271126274Sdes "gssapi-with-mic"); 272126274Sdes 273126274Sdes gssbuf.value = buffer_ptr(&b); 274126274Sdes gssbuf.length = buffer_len(&b); 275126274Sdes 276126274Sdes if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic)))) 277126274Sdes authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); 278126274Sdes else 279126274Sdes logit("GSSAPI MIC check failed"); 280126274Sdes 281126274Sdes buffer_free(&b); 282255767Sdes free(mic.value); 283126274Sdes 284126274Sdes authctxt->postponed = 0; 285126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 286126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 287126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); 288126274Sdes dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 289248619Sdes userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL); 290295367Sdes return 0; 291126274Sdes} 292126274Sdes 293124208SdesAuthmethod method_gssapi = { 294126274Sdes "gssapi-with-mic", 295124208Sdes userauth_gssapi, 296124208Sdes &options.gss_authentication 297124208Sdes}; 298124208Sdes 299124208Sdes#endif /* GSSAPI */ 300