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 #include "includes.h"
  32 
  33 #ifdef GSSAPI
  34 
  35 #include <openssl/crypto.h>
  36 #include <openssl/bn.h>
  37 
  38 #include "xmalloc.h"
  39 #include "buffer.h"
  40 #include "bufaux.h"
  41 #include "compat.h"
  42 #include "kex.h"
  43 #include "log.h"
  44 #include "packet.h"
  45 #include "dh.h"
  46 #include "ssh2.h"
  47 #include "ssh-gss.h"
  48 #include "auth.h"
  49 
  50 Gssctxt *xxx_gssctxt;
  51 extern Authctxt *x_authctxt;
  52 
  53 static void kex_gss_send_error(Gssctxt *ctxt);
  54 
  55 void
  56 kexgss_server(Kex *kex)
  57 {
  58         OM_uint32 maj_status, min_status;
  59         gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok;
  60         Gssctxt *ctxt = NULL;
  61         unsigned int klen, kout;
  62         unsigned int sbloblen = 0;
  63         unsigned char *kbuf, *hash;
  64         unsigned char *server_host_key_blob = NULL;
  65         DH *dh;
  66         Key *server_host_key = NULL;
  67         BIGNUM *shared_secret = NULL;
  68         BIGNUM *dh_client_pub = NULL;
  69         int type = 0;
  70         uint_t slen;
  71         gss_OID oid;
  72 
  73         /*
  74          * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet
  75          * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's
  76          * the GSS-API that provides for server host authentication.
  77          */
  78         if (kex->load_host_key != NULL &&
  79             !(datafellows & SSH_BUG_GSSKEX_HOSTKEY))
  80                 server_host_key = kex->load_host_key(kex->hostkey_type);
  81         if (server_host_key != NULL)
  82                 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
  83 
  84 
  85         /* Initialise GSSAPI */
  86 
  87         ssh_gssapi_oid_of_kexname(kex->name, &oid);
  88         if (oid == GSS_C_NULL_OID) {
  89                 fatal("Couldn't match the negotiated GSS key exchange");
  90         }
  91 
  92         ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid);
  93 
  94         ctxt = xxx_gssctxt;
  95 
  96         do {
  97                 debug("Wait SSH2_MSG_GSSAPI_INIT");
  98                 type = packet_read();
  99                 switch (type) {
 100                 case SSH2_MSG_KEXGSS_INIT:
 101                         if (dh_client_pub != NULL)
 102                                 fatal("Received KEXGSS_INIT after "
 103                                     "initialising");
 104                         recv_tok.value = packet_get_string(&slen);
 105                         recv_tok.length = slen; /* int vs. size_t */
 106 
 107                         dh_client_pub = BN_new();
 108 
 109                         if (dh_client_pub == NULL)
 110                                 fatal("dh_client_pub == NULL");
 111                         packet_get_bignum2(dh_client_pub);
 112 
 113                         /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
 114                         if (sbloblen) {
 115                                 packet_start(SSH2_MSG_KEXGSS_HOSTKEY);
 116                                 packet_put_string(server_host_key_blob,
 117                                     sbloblen);
 118                                 packet_send();
 119                                 packet_write_wait();
 120                         }
 121                         break;
 122                 case SSH2_MSG_KEXGSS_CONTINUE:
 123                         recv_tok.value = packet_get_string(&slen);
 124                         recv_tok.length = slen; /* int vs. size_t */
 125                         break;
 126                 default:
 127                         packet_disconnect("Protocol error: didn't expect "
 128                             "packet type %d", type);
 129                 }
 130 
 131                 maj_status = ssh_gssapi_accept_ctx(ctxt, &recv_tok, &send_tok);
 132 
 133                 xfree(recv_tok.value); /* We allocated this, not gss */
 134 
 135                 if (dh_client_pub == NULL)
 136                         fatal("No client public key");
 137 
 138                 if (maj_status == GSS_S_CONTINUE_NEEDED) {
 139                         debug("Sending GSSAPI_CONTINUE");
 140                         packet_start(SSH2_MSG_KEXGSS_CONTINUE);
 141                         packet_put_string(send_tok.value, send_tok.length);
 142                         packet_send();
 143                         packet_write_wait();
 144                         (void) gss_release_buffer(&min_status, &send_tok);
 145                 }
 146         } while (maj_status == GSS_S_CONTINUE_NEEDED);
 147 
 148         if (GSS_ERROR(maj_status)) {
 149                 kex_gss_send_error(ctxt);
 150                 if (send_tok.length > 0) {
 151                         packet_start(SSH2_MSG_KEXGSS_CONTINUE);
 152                         packet_put_string(send_tok.value, send_tok.length);
 153                         packet_send();
 154                         packet_write_wait();
 155                         (void) gss_release_buffer(&min_status, &send_tok);
 156                 }
 157                 fatal("accept_ctx died");
 158         }
 159 
 160         debug("gss_complete");
 161         if (!(ctxt->flags & GSS_C_MUTUAL_FLAG))
 162                 fatal("Mutual authentication flag wasn't set");
 163 
 164         if (!(ctxt->flags & GSS_C_INTEG_FLAG))
 165                 fatal("Integrity flag wasn't set");
 166 
 167         dh = dh_new_group1();
 168         dh_gen_key(dh, kex->we_need * 8);
 169 
 170         if (!dh_pub_is_valid(dh, dh_client_pub))
 171                 packet_disconnect("bad client public DH value");
 172 
 173         klen = DH_size(dh);
 174         kbuf = xmalloc(klen);
 175         kout = DH_compute_key(kbuf, dh_client_pub, dh);
 176 
 177         shared_secret = BN_new();
 178         BN_bin2bn(kbuf, kout, shared_secret);
 179         (void) memset(kbuf, 0, klen);
 180         xfree(kbuf);
 181 
 182         /* The GSSAPI hash is identical to the Diffie Helman one */
 183         hash = kex_dh_hash(
 184             kex->client_version_string,
 185             kex->server_version_string,
 186             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
 187             buffer_ptr(&kex->my), buffer_len(&kex->my),
 188             server_host_key_blob, sbloblen,
 189             dh_client_pub,
 190             dh->pub_key,
 191             shared_secret);
 192 
 193         BN_free(dh_client_pub);
 194 
 195         if (kex->session_id == NULL) {
 196                 kex->session_id_len = 20;
 197                 kex->session_id = xmalloc(kex->session_id_len);
 198                 (void) memcpy(kex->session_id, hash, kex->session_id_len);
 199         } else if (x_authctxt != NULL && x_authctxt->success) {
 200                 ssh_gssapi_storecreds(ctxt, x_authctxt);
 201         }
 202 
 203         /* Should fix kex_dh_hash to output hash length */
 204         gssbuf.length = 20;     /* yes, it's always 20 (SHA-1) */
 205         gssbuf.value = hash;    /* and it's static constant storage */
 206 
 207         if (GSS_ERROR(ssh_gssapi_get_mic(ctxt, &gssbuf, &msg_tok))) {
 208                 kex_gss_send_error(ctxt);
 209                 fatal("Couldn't get MIC");
 210         }
 211 
 212         packet_start(SSH2_MSG_KEXGSS_COMPLETE);
 213         packet_put_bignum2(dh->pub_key);
 214         packet_put_string((char *)msg_tok.value, msg_tok.length);
 215         (void) gss_release_buffer(&min_status, &msg_tok);
 216 
 217         if (send_tok.length != 0) {
 218                 packet_put_char(1); /* true */
 219                 packet_put_string((char *)send_tok.value, send_tok.length);
 220                 (void) gss_release_buffer(&min_status, &send_tok);
 221         } else {
 222                 packet_put_char(0); /* false */
 223         }
 224         packet_send();
 225         packet_write_wait();
 226 
 227         DH_free(dh);
 228 
 229         kex_derive_keys(kex, hash, shared_secret);
 230         BN_clear_free(shared_secret);
 231         kex_finish(kex);
 232 }
 233 
 234 static void
 235 kex_gss_send_error(Gssctxt *ctxt) {
 236         char *errstr;
 237         OM_uint32 maj, min;
 238 
 239         errstr = ssh_gssapi_last_error(ctxt, &maj, &min);
 240         if (errstr) {
 241                 packet_start(SSH2_MSG_KEXGSS_ERROR);
 242                 packet_put_int(maj);
 243                 packet_put_int(min);
 244                 packet_put_cstring(errstr);
 245                 packet_put_cstring("");
 246                 packet_send();
 247                 packet_write_wait();
 248                 /* XXX - We should probably log the error locally here */
 249                 xfree(errstr);
 250         }
 251 }
 252 #endif /* GSSAPI */