1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 5*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 6*0Sstevel@tonic-gate * are met: 7*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 8*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 9*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 11*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 14*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 26*0Sstevel@tonic-gate * Use is subject to license terms. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include "includes.h" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #ifdef GSSAPI 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <openssl/crypto.h> 36*0Sstevel@tonic-gate #include <openssl/bn.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #include "xmalloc.h" 39*0Sstevel@tonic-gate #include "buffer.h" 40*0Sstevel@tonic-gate #include "bufaux.h" 41*0Sstevel@tonic-gate #include "compat.h" 42*0Sstevel@tonic-gate #include "kex.h" 43*0Sstevel@tonic-gate #include "log.h" 44*0Sstevel@tonic-gate #include "packet.h" 45*0Sstevel@tonic-gate #include "dh.h" 46*0Sstevel@tonic-gate #include "ssh2.h" 47*0Sstevel@tonic-gate #include "ssh-gss.h" 48*0Sstevel@tonic-gate #include "monitor_wrap.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate Gssctxt *xxx_gssctxt; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate static void kex_gss_send_error(Gssctxt *ctxt); 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate void 55*0Sstevel@tonic-gate kexgss_server(Kex *kex) 56*0Sstevel@tonic-gate { 57*0Sstevel@tonic-gate OM_uint32 maj_status, min_status; 58*0Sstevel@tonic-gate gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok; 59*0Sstevel@tonic-gate Gssctxt *ctxt = NULL; 60*0Sstevel@tonic-gate unsigned int klen, kout; 61*0Sstevel@tonic-gate unsigned int sbloblen = 0; 62*0Sstevel@tonic-gate unsigned char *kbuf, *hash; 63*0Sstevel@tonic-gate unsigned char *server_host_key_blob = NULL; 64*0Sstevel@tonic-gate DH *dh; 65*0Sstevel@tonic-gate Key *server_host_key = NULL; 66*0Sstevel@tonic-gate BIGNUM *shared_secret = NULL; 67*0Sstevel@tonic-gate BIGNUM *dh_client_pub = NULL; 68*0Sstevel@tonic-gate int type =0; 69*0Sstevel@tonic-gate u_int slen; 70*0Sstevel@tonic-gate gss_OID oid; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* 73*0Sstevel@tonic-gate * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet 74*0Sstevel@tonic-gate * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's 75*0Sstevel@tonic-gate * the GSS-API that provides for server host authentication. 76*0Sstevel@tonic-gate */ 77*0Sstevel@tonic-gate if (kex->load_host_key != NULL && 78*0Sstevel@tonic-gate !(datafellows & SSH_BUG_GSSKEX_HOSTKEY)) 79*0Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type); 80*0Sstevel@tonic-gate if (server_host_key != NULL) 81*0Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate /* Initialise GSSAPI */ 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(kex->name, &oid); 87*0Sstevel@tonic-gate if (oid == GSS_C_NULL_OID) { 88*0Sstevel@tonic-gate fatal("Couldn't match the negotiated GSS key exchange"); 89*0Sstevel@tonic-gate } 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate ssh_gssapi_build_ctx(&ctxt, 0, oid); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate xxx_gssctxt = ctxt; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate do { 96*0Sstevel@tonic-gate debug("Wait SSH2_MSG_GSSAPI_INIT"); 97*0Sstevel@tonic-gate type = packet_read(); 98*0Sstevel@tonic-gate switch(type) { 99*0Sstevel@tonic-gate case SSH2_MSG_KEXGSS_INIT: 100*0Sstevel@tonic-gate if (dh_client_pub!=NULL) 101*0Sstevel@tonic-gate fatal("Received KEXGSS_INIT after initialising"); 102*0Sstevel@tonic-gate recv_tok.value=packet_get_string(&slen); 103*0Sstevel@tonic-gate recv_tok.length=slen; /* int vs. size_t */ 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate dh_client_pub = BN_new(); 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate if (dh_client_pub == NULL) 108*0Sstevel@tonic-gate fatal("dh_client_pub == NULL"); 109*0Sstevel@tonic-gate packet_get_bignum2(dh_client_pub); 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ 112*0Sstevel@tonic-gate if (sbloblen) { 113*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_HOSTKEY); 114*0Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen); 115*0Sstevel@tonic-gate packet_send(); 116*0Sstevel@tonic-gate packet_write_wait(); 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate break; 119*0Sstevel@tonic-gate case SSH2_MSG_KEXGSS_CONTINUE: 120*0Sstevel@tonic-gate recv_tok.value=packet_get_string(&slen); 121*0Sstevel@tonic-gate recv_tok.length=slen; /* int vs. size_t */ 122*0Sstevel@tonic-gate break; 123*0Sstevel@tonic-gate default: 124*0Sstevel@tonic-gate packet_disconnect("Protocol error: didn't expect packet type %d", 125*0Sstevel@tonic-gate type); 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok, 129*0Sstevel@tonic-gate &send_tok)); 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate xfree(recv_tok.value); /* We allocated this, not gss */ 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate if (dh_client_pub == NULL) 134*0Sstevel@tonic-gate fatal("No client public key"); 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate if (maj_status == GSS_S_CONTINUE_NEEDED) { 137*0Sstevel@tonic-gate debug("Sending GSSAPI_CONTINUE"); 138*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE); 139*0Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length); 140*0Sstevel@tonic-gate packet_send(); 141*0Sstevel@tonic-gate packet_write_wait(); 142*0Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate } while (maj_status == GSS_S_CONTINUE_NEEDED); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if (GSS_ERROR(maj_status)) { 147*0Sstevel@tonic-gate kex_gss_send_error(ctxt); 148*0Sstevel@tonic-gate if (send_tok.length>0) { 149*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE); 150*0Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length); 151*0Sstevel@tonic-gate packet_send(); 152*0Sstevel@tonic-gate packet_write_wait(); 153*0Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate fatal("accept_ctx died"); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate debug("gss_complete"); 159*0Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_MUTUAL_FLAG)) 160*0Sstevel@tonic-gate fatal("Mutual authentication flag wasn't set"); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_INTEG_FLAG)) 163*0Sstevel@tonic-gate fatal("Integrity flag wasn't set"); 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate dh = dh_new_group1(); 166*0Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub)) 169*0Sstevel@tonic-gate packet_disconnect("bad client public DH value"); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate klen = DH_size(dh); 172*0Sstevel@tonic-gate kbuf = xmalloc(klen); 173*0Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh); 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate shared_secret = BN_new(); 176*0Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret); 177*0Sstevel@tonic-gate (void) memset(kbuf, 0, klen); 178*0Sstevel@tonic-gate xfree(kbuf); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* The GSSAPI hash is identical to the Diffie Helman one */ 181*0Sstevel@tonic-gate hash = kex_dh_hash( 182*0Sstevel@tonic-gate kex->client_version_string, 183*0Sstevel@tonic-gate kex->server_version_string, 184*0Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer), 185*0Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my), 186*0Sstevel@tonic-gate server_host_key_blob, sbloblen, 187*0Sstevel@tonic-gate dh_client_pub, 188*0Sstevel@tonic-gate dh->pub_key, 189*0Sstevel@tonic-gate shared_secret 190*0Sstevel@tonic-gate ); 191*0Sstevel@tonic-gate BN_free(dh_client_pub); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate if (kex->session_id == NULL) { 194*0Sstevel@tonic-gate kex->session_id_len = 20; 195*0Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len); 196*0Sstevel@tonic-gate (void) memcpy(kex->session_id, hash, kex->session_id_len); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate /* Should fix kex_dh_hash to output hash length */ 200*0Sstevel@tonic-gate gssbuf.length = 20; /* yes, it's always 20 (SHA-1) */ 201*0Sstevel@tonic-gate gssbuf.value = hash; /* and it's static constant storage */ 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate if (GSS_ERROR(ssh_gssapi_get_mic(ctxt,&gssbuf,&msg_tok))) { 204*0Sstevel@tonic-gate kex_gss_send_error(ctxt); 205*0Sstevel@tonic-gate fatal("Couldn't get MIC"); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_COMPLETE); 209*0Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); 210*0Sstevel@tonic-gate packet_put_string((char *)msg_tok.value,msg_tok.length); 211*0Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &msg_tok); 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate if (send_tok.length != 0) { 214*0Sstevel@tonic-gate packet_put_char(1); /* true */ 215*0Sstevel@tonic-gate packet_put_string((char *)send_tok.value,send_tok.length); 216*0Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 217*0Sstevel@tonic-gate } else { 218*0Sstevel@tonic-gate packet_put_char(0); /* false */ 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate packet_send(); 221*0Sstevel@tonic-gate packet_write_wait(); 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate DH_free(dh); 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret); 226*0Sstevel@tonic-gate BN_clear_free(shared_secret); 227*0Sstevel@tonic-gate kex_finish(kex); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate static void 231*0Sstevel@tonic-gate kex_gss_send_error(Gssctxt *ctxt) { 232*0Sstevel@tonic-gate char *errstr; 233*0Sstevel@tonic-gate OM_uint32 maj,min; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate errstr = ssh_gssapi_last_error(ctxt,&maj,&min); 236*0Sstevel@tonic-gate if (errstr) { 237*0Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_ERROR); 238*0Sstevel@tonic-gate packet_put_int(maj); 239*0Sstevel@tonic-gate packet_put_int(min); 240*0Sstevel@tonic-gate packet_put_cstring(errstr); 241*0Sstevel@tonic-gate packet_put_cstring(""); 242*0Sstevel@tonic-gate packet_send(); 243*0Sstevel@tonic-gate packet_write_wait(); 244*0Sstevel@tonic-gate /* XXX - We should probably log the error locally here */ 245*0Sstevel@tonic-gate xfree(errstr); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate #endif /* GSSAPI */ 249