1 /*
   2  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  * 1. Redistributions of source code must retain the above copyright
   8  *    notice, this list of conditions and the following disclaimer.
   9  * 2. Redistributions in binary form must reproduce the above copyright
  10  *    notice, this list of conditions and the following disclaimer in the
  11  *    documentation and/or other materials provided with the distribution.
  12  *
  13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
  14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23  */
  24 /*
  25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 #pragma ident   "%Z%%M% %I%     %E% SMI"
  30 
  31 
  32 #include "includes.h"
  33 
  34 #ifdef GSSAPI
  35 
  36 #include <openssl/opensslconf.h>
  37 #include <openssl/crypto.h>
  38 #include <openssl/bn.h>
  39 
  40 #include "xmalloc.h"
  41 #include "buffer.h"
  42 #include "bufaux.h"
  43 #include "kex.h"
  44 #include "log.h"
  45 #include "packet.h"
  46 #include "dh.h"
  47 #include "canohost.h"
  48 #include "ssh2.h"
  49 #include "ssh-gss.h"
  50 
  51 extern char *xxx_host;
  52 
  53 Gssctxt *xxx_gssctxt;
  54 
  55 static void kexgss_verbose_cleanup(void *arg);
  56 
  57 void
  58 kexgss_client(Kex *kex)
  59 {
  60         gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok;
  61         gss_buffer_t token_ptr;
  62         gss_OID mech = GSS_C_NULL_OID;
  63         Gssctxt *ctxt = NULL;
  64         OM_uint32 maj_status, min_status, smaj_status, smin_status;
  65         unsigned int klen, kout;
  66         DH *dh;
  67         BIGNUM *dh_server_pub = 0;
  68         BIGNUM *shared_secret = 0;
  69         Key *server_host_key = NULL;
  70         unsigned char *kbuf;
  71         unsigned char *hash;
  72         unsigned char *server_host_key_blob = NULL;
  73         char *msg, *lang;
  74         int type = 0;
  75         int first = 1;
  76         uint_t sbloblen = 0;
  77         uint_t strlen;
  78 
  79         /* Map the negotiated kex name to a mech OID */
  80         ssh_gssapi_oid_of_kexname(kex->name, &mech);
  81         if (mech == GSS_C_NULL_OID)
  82                 fatal("Couldn't match the negotiated GSS key exchange");
  83 
  84         ssh_gssapi_build_ctx(&ctxt, 1, mech);
  85 
  86         /* This code should match that in ssh_dh1_client */
  87 
  88         /* Step 1 - e is dh->pub_key */
  89         dh = dh_new_group1();
  90         dh_gen_key(dh, kex->we_need * 8);
  91 
  92         /* This is f, we initialise it now to make life easier */
  93         dh_server_pub = BN_new();
  94         if (dh_server_pub == NULL) {
  95                 fatal("dh_server_pub == NULL");
  96         }
  97 
  98         token_ptr = GSS_C_NO_BUFFER;
  99 
 100         recv_tok.value = NULL;
 101         recv_tok.length = 0;
 102 
 103         do {
 104                 debug("Calling gss_init_sec_context");
 105 
 106                 maj_status = ssh_gssapi_init_ctx(ctxt, xxx_host,
 107                     kex->options.gss_deleg_creds, token_ptr, &send_tok);
 108 
 109                 if (GSS_ERROR(maj_status)) {
 110                         ssh_gssapi_error(ctxt, "performing GSS-API protected "
 111                             "SSHv2 key exchange");
 112                         (void) gss_release_buffer(&min_status, &send_tok);
 113                         packet_disconnect("A GSS-API error occurred during "
 114                             "GSS-API protected SSHv2 key exchange\n");
 115                 }
 116 
 117                 /* If we've got an old receive buffer get rid of it */
 118                 if (token_ptr != GSS_C_NO_BUFFER) {
 119                         /* We allocated recv_tok */
 120                         xfree(recv_tok.value);
 121                         recv_tok.value = NULL;
 122                         recv_tok.length = 0;
 123                         token_ptr = GSS_C_NO_BUFFER;
 124                 }
 125 
 126                 if (maj_status == GSS_S_COMPLETE) {
 127                         /* If mutual state flag is not true, kex fails */
 128                         if (!(ctxt->flags & GSS_C_MUTUAL_FLAG)) {
 129                                 fatal("Mutual authentication failed");
 130                         }
 131                         /* If integ avail flag is not true kex fails */
 132                         if (!(ctxt->flags & GSS_C_INTEG_FLAG)) {
 133                                 fatal("Integrity check failed");
 134                         }
 135                 }
 136 
 137                 /*
 138                  * If we have data to send, then the last message that we
 139                  * received cannot have been a 'complete'.
 140                  */
 141                 if (send_tok.length != 0) {
 142                         if (first) {
 143                                 packet_start(SSH2_MSG_KEXGSS_INIT);
 144                                 packet_put_string(send_tok.value,
 145                                     send_tok.length);
 146                                 packet_put_bignum2(dh->pub_key);
 147                                 first = 0;
 148                         } else {
 149                                 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
 150                                 packet_put_string(send_tok.value,
 151                                     send_tok.length);
 152                         }
 153                         (void) gss_release_buffer(&min_status, &send_tok);
 154                         packet_send();
 155                         packet_write_wait();
 156 
 157 
 158                         /*
 159                          * If we've sent them data, they'd better be polite and
 160                          * reply.
 161                          */
 162 
 163 next_packet:
 164                         /*
 165                          * We need to catch connection closing w/o error
 166                          * tokens or messages so we can tell the user
 167                          * _something_ more useful than "Connection
 168                          * closed by ..."
 169                          *
 170                          * We use a fatal cleanup function as that's
 171                          * all, really, that we can do for now.
 172                          */
 173                         fatal_add_cleanup(kexgss_verbose_cleanup, NULL);
 174                         type = packet_read();
 175                         fatal_remove_cleanup(kexgss_verbose_cleanup, NULL);
 176                         switch (type) {
 177                         case SSH2_MSG_KEXGSS_HOSTKEY:
 178                                 debug("Received KEXGSS_HOSTKEY");
 179                                 server_host_key_blob =
 180                                     packet_get_string(&sbloblen);
 181                                 server_host_key =
 182                                     key_from_blob(server_host_key_blob,
 183                                     sbloblen);
 184                                 goto next_packet; /* there MUSt be another */
 185                                 break;
 186                         case SSH2_MSG_KEXGSS_CONTINUE:
 187                                 debug("Received GSSAPI_CONTINUE");
 188                                 if (maj_status == GSS_S_COMPLETE)
 189                                         packet_disconnect("Protocol error: "
 190                                             "received GSS-API context token "
 191                                             "though the context was already "
 192                                             "established");
 193                                 recv_tok.value = packet_get_string(&strlen);
 194                                 recv_tok.length = strlen; /* u_int vs. size_t */
 195                                 break;
 196                         case SSH2_MSG_KEXGSS_COMPLETE:
 197                                 debug("Received GSSAPI_COMPLETE");
 198                                 packet_get_bignum2(dh_server_pub);
 199                                 msg_tok.value = packet_get_string(&strlen);
 200                                 msg_tok.length = strlen; /* u_int vs. size_t */
 201 
 202                                 /* Is there a token included? */
 203                                 if (packet_get_char()) {
 204                                         recv_tok.value =
 205                                             packet_get_string(&strlen);
 206                                         /* u_int/size_t */
 207                                         recv_tok.length = strlen;
 208                                 }
 209                                 if (recv_tok.length > 0 &&
 210                                     maj_status == GSS_S_COMPLETE) {
 211                                         packet_disconnect("Protocol error: "
 212                                             "received GSS-API context token "
 213                                             "though the context was already "
 214                                             "established");
 215                                 } else if (recv_tok.length == 0 &&
 216                                     maj_status == GSS_S_CONTINUE_NEEDED) {
 217                                         /* No token included */
 218                                         packet_disconnect("Protocol error: "
 219                                             "did not receive expected "
 220                                             "GSS-API context token");
 221                                 }
 222                                 break;
 223                         case SSH2_MSG_KEXGSS_ERROR:
 224                                 smaj_status = packet_get_int();
 225                                 smin_status = packet_get_int();
 226                                 msg = packet_get_string(NULL);
 227                                 lang = packet_get_string(NULL);
 228                                 xfree(lang);
 229                                 error("Server had a GSS-API error; the "
 230                                     "connection will close (%d/%d):\n%s",
 231                                     smaj_status, smin_status, msg);
 232                                 error("Use the GssKeyEx option to disable "
 233                                     "GSS-API key exchange and try again.");
 234                                 packet_disconnect("The server had a GSS-API "
 235                                     "error during GSS-API protected SSHv2 "
 236                                     "key exchange\n");
 237                                 break;
 238                         default:
 239                                 packet_disconnect("Protocol error: "
 240                                     "didn't expect packet type %d", type);
 241                         }
 242                         if (recv_tok.value)
 243                                 token_ptr = &recv_tok;
 244                 } else {
 245                         /* No data, and not complete */
 246                         if (maj_status != GSS_S_COMPLETE) {
 247                                 fatal("Not complete, and no token output");
 248                         }
 249                 }
 250         } while (maj_status == GSS_S_CONTINUE_NEEDED);
 251 
 252         /*
 253          * We _must_ have received a COMPLETE message in reply from the
 254          * server, which will have set dh_server_pub and msg_tok.
 255          */
 256         if (type != SSH2_MSG_KEXGSS_COMPLETE)
 257                 fatal("Expected SSH2_MSG_KEXGSS_COMPLETE never arrived");
 258         if (maj_status != GSS_S_COMPLETE)
 259                 fatal("Internal error in GSS-API protected SSHv2 key exchange");
 260 
 261         /* Check f in range [1, p-1] */
 262         if (!dh_pub_is_valid(dh, dh_server_pub))
 263                 packet_disconnect("bad server public DH value");
 264 
 265         /* compute K=f^x mod p */
 266         klen = DH_size(dh);
 267         kbuf = xmalloc(klen);
 268         kout = DH_compute_key(kbuf, dh_server_pub, dh);
 269 
 270         shared_secret = BN_new();
 271         BN_bin2bn(kbuf, kout, shared_secret);
 272         (void) memset(kbuf, 0, klen);
 273         xfree(kbuf);
 274 
 275         /* The GSS hash is identical to the DH one */
 276         hash = kex_dh_hash(
 277             kex->client_version_string,
 278             kex->server_version_string,
 279             buffer_ptr(&kex->my), buffer_len(&kex->my),
 280             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
 281             server_host_key_blob, sbloblen, /* server host key */
 282             dh->pub_key,     /* e */
 283             dh_server_pub,      /* f */
 284             shared_secret);     /* K */
 285 
 286         gssbuf.value = hash;
 287         gssbuf.length = 20;
 288 
 289         /* Verify that H matches the token we just got. */
 290         if ((maj_status = gss_verify_mic(&min_status, ctxt->context, &gssbuf,
 291             &msg_tok, NULL))) {
 292                 packet_disconnect("Hash's MIC didn't verify");
 293         }
 294 
 295         if (server_host_key && kex->accept_host_key != NULL)
 296                 (void) kex->accept_host_key(server_host_key);
 297 
 298         DH_free(dh);
 299 
 300         xxx_gssctxt = ctxt; /* for gss keyex w/ mic userauth */
 301 
 302         /* save session id */
 303         if (kex->session_id == NULL) {
 304                 kex->session_id_len = 20;
 305                 kex->session_id = xmalloc(kex->session_id_len);
 306                 (void) memcpy(kex->session_id, hash, kex->session_id_len);
 307         }
 308 
 309         kex_derive_keys(kex, hash, shared_secret);
 310         BN_clear_free(shared_secret);
 311         kex_finish(kex);
 312 }
 313 
 314 /* ARGSUSED */
 315 static
 316 void
 317 kexgss_verbose_cleanup(void *arg)
 318 {
 319         error("The GSS-API protected key exchange has failed without "
 320             "indication\nfrom the server, possibly due to misconfiguration "
 321             "of the server.");
 322         error("Use the GssKeyEx option to disable GSS-API key exchange "
 323             "and try again.");
 324 }
 325 
 326 #endif /* GSSAPI */