10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate /* 25*1776Snw141292 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 260Sstevel@tonic-gate * Use is subject to license terms. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include "includes.h" 320Sstevel@tonic-gate 330Sstevel@tonic-gate #ifdef GSSAPI 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <openssl/crypto.h> 360Sstevel@tonic-gate #include <openssl/bn.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include "xmalloc.h" 390Sstevel@tonic-gate #include "buffer.h" 400Sstevel@tonic-gate #include "bufaux.h" 410Sstevel@tonic-gate #include "compat.h" 420Sstevel@tonic-gate #include "kex.h" 430Sstevel@tonic-gate #include "log.h" 440Sstevel@tonic-gate #include "packet.h" 450Sstevel@tonic-gate #include "dh.h" 460Sstevel@tonic-gate #include "ssh2.h" 470Sstevel@tonic-gate #include "ssh-gss.h" 480Sstevel@tonic-gate #include "monitor_wrap.h" 49*1776Snw141292 #include "auth.h" 500Sstevel@tonic-gate 510Sstevel@tonic-gate Gssctxt *xxx_gssctxt; 52*1776Snw141292 extern Authctxt *x_authctxt; 530Sstevel@tonic-gate 540Sstevel@tonic-gate static void kex_gss_send_error(Gssctxt *ctxt); 550Sstevel@tonic-gate 560Sstevel@tonic-gate void 570Sstevel@tonic-gate kexgss_server(Kex *kex) 580Sstevel@tonic-gate { 590Sstevel@tonic-gate OM_uint32 maj_status, min_status; 600Sstevel@tonic-gate gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok; 610Sstevel@tonic-gate Gssctxt *ctxt = NULL; 620Sstevel@tonic-gate unsigned int klen, kout; 630Sstevel@tonic-gate unsigned int sbloblen = 0; 640Sstevel@tonic-gate unsigned char *kbuf, *hash; 650Sstevel@tonic-gate unsigned char *server_host_key_blob = NULL; 660Sstevel@tonic-gate DH *dh; 670Sstevel@tonic-gate Key *server_host_key = NULL; 680Sstevel@tonic-gate BIGNUM *shared_secret = NULL; 690Sstevel@tonic-gate BIGNUM *dh_client_pub = NULL; 700Sstevel@tonic-gate int type =0; 710Sstevel@tonic-gate u_int slen; 720Sstevel@tonic-gate gss_OID oid; 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet 760Sstevel@tonic-gate * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's 770Sstevel@tonic-gate * the GSS-API that provides for server host authentication. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate if (kex->load_host_key != NULL && 800Sstevel@tonic-gate !(datafellows & SSH_BUG_GSSKEX_HOSTKEY)) 810Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type); 820Sstevel@tonic-gate if (server_host_key != NULL) 830Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); 840Sstevel@tonic-gate 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* Initialise GSSAPI */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(kex->name, &oid); 890Sstevel@tonic-gate if (oid == GSS_C_NULL_OID) { 900Sstevel@tonic-gate fatal("Couldn't match the negotiated GSS key exchange"); 910Sstevel@tonic-gate } 920Sstevel@tonic-gate 93*1776Snw141292 ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid); 940Sstevel@tonic-gate 95*1776Snw141292 ctxt = xxx_gssctxt; 960Sstevel@tonic-gate 970Sstevel@tonic-gate do { 980Sstevel@tonic-gate debug("Wait SSH2_MSG_GSSAPI_INIT"); 990Sstevel@tonic-gate type = packet_read(); 1000Sstevel@tonic-gate switch(type) { 1010Sstevel@tonic-gate case SSH2_MSG_KEXGSS_INIT: 1020Sstevel@tonic-gate if (dh_client_pub!=NULL) 1030Sstevel@tonic-gate fatal("Received KEXGSS_INIT after initialising"); 1040Sstevel@tonic-gate recv_tok.value=packet_get_string(&slen); 1050Sstevel@tonic-gate recv_tok.length=slen; /* int vs. size_t */ 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate dh_client_pub = BN_new(); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if (dh_client_pub == NULL) 1100Sstevel@tonic-gate fatal("dh_client_pub == NULL"); 1110Sstevel@tonic-gate packet_get_bignum2(dh_client_pub); 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ 1140Sstevel@tonic-gate if (sbloblen) { 1150Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_HOSTKEY); 1160Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen); 1170Sstevel@tonic-gate packet_send(); 1180Sstevel@tonic-gate packet_write_wait(); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate break; 1210Sstevel@tonic-gate case SSH2_MSG_KEXGSS_CONTINUE: 1220Sstevel@tonic-gate recv_tok.value=packet_get_string(&slen); 1230Sstevel@tonic-gate recv_tok.length=slen; /* int vs. size_t */ 1240Sstevel@tonic-gate break; 1250Sstevel@tonic-gate default: 1260Sstevel@tonic-gate packet_disconnect("Protocol error: didn't expect packet type %d", 1270Sstevel@tonic-gate type); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate maj_status=PRIVSEP(ssh_gssapi_accept_ctx(ctxt,&recv_tok, 1310Sstevel@tonic-gate &send_tok)); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate xfree(recv_tok.value); /* We allocated this, not gss */ 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate if (dh_client_pub == NULL) 1360Sstevel@tonic-gate fatal("No client public key"); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate if (maj_status == GSS_S_CONTINUE_NEEDED) { 1390Sstevel@tonic-gate debug("Sending GSSAPI_CONTINUE"); 1400Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE); 1410Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length); 1420Sstevel@tonic-gate packet_send(); 1430Sstevel@tonic-gate packet_write_wait(); 1440Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate } while (maj_status == GSS_S_CONTINUE_NEEDED); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (GSS_ERROR(maj_status)) { 1490Sstevel@tonic-gate kex_gss_send_error(ctxt); 1500Sstevel@tonic-gate if (send_tok.length>0) { 1510Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE); 1520Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length); 1530Sstevel@tonic-gate packet_send(); 1540Sstevel@tonic-gate packet_write_wait(); 1550Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate fatal("accept_ctx died"); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate debug("gss_complete"); 1610Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_MUTUAL_FLAG)) 1620Sstevel@tonic-gate fatal("Mutual authentication flag wasn't set"); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_INTEG_FLAG)) 1650Sstevel@tonic-gate fatal("Integrity flag wasn't set"); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate dh = dh_new_group1(); 1680Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub)) 1710Sstevel@tonic-gate packet_disconnect("bad client public DH value"); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate klen = DH_size(dh); 1740Sstevel@tonic-gate kbuf = xmalloc(klen); 1750Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate shared_secret = BN_new(); 1780Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret); 1790Sstevel@tonic-gate (void) memset(kbuf, 0, klen); 1800Sstevel@tonic-gate xfree(kbuf); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* The GSSAPI hash is identical to the Diffie Helman one */ 1830Sstevel@tonic-gate hash = kex_dh_hash( 1840Sstevel@tonic-gate kex->client_version_string, 1850Sstevel@tonic-gate kex->server_version_string, 1860Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer), 1870Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my), 1880Sstevel@tonic-gate server_host_key_blob, sbloblen, 1890Sstevel@tonic-gate dh_client_pub, 1900Sstevel@tonic-gate dh->pub_key, 1910Sstevel@tonic-gate shared_secret 1920Sstevel@tonic-gate ); 1930Sstevel@tonic-gate BN_free(dh_client_pub); 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate if (kex->session_id == NULL) { 1960Sstevel@tonic-gate kex->session_id_len = 20; 1970Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len); 1980Sstevel@tonic-gate (void) memcpy(kex->session_id, hash, kex->session_id_len); 199*1776Snw141292 } else if (x_authctxt != NULL && x_authctxt->success) { 200*1776Snw141292 ssh_gssapi_storecreds(ctxt, x_authctxt); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* Should fix kex_dh_hash to output hash length */ 2040Sstevel@tonic-gate gssbuf.length = 20; /* yes, it's always 20 (SHA-1) */ 2050Sstevel@tonic-gate gssbuf.value = hash; /* and it's static constant storage */ 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (GSS_ERROR(ssh_gssapi_get_mic(ctxt,&gssbuf,&msg_tok))) { 2080Sstevel@tonic-gate kex_gss_send_error(ctxt); 2090Sstevel@tonic-gate fatal("Couldn't get MIC"); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_COMPLETE); 2130Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); 2140Sstevel@tonic-gate packet_put_string((char *)msg_tok.value,msg_tok.length); 2150Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &msg_tok); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (send_tok.length != 0) { 2180Sstevel@tonic-gate packet_put_char(1); /* true */ 2190Sstevel@tonic-gate packet_put_string((char *)send_tok.value,send_tok.length); 2200Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok); 2210Sstevel@tonic-gate } else { 2220Sstevel@tonic-gate packet_put_char(0); /* false */ 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate packet_send(); 2250Sstevel@tonic-gate packet_write_wait(); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate DH_free(dh); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret); 2300Sstevel@tonic-gate BN_clear_free(shared_secret); 2310Sstevel@tonic-gate kex_finish(kex); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate static void 2350Sstevel@tonic-gate kex_gss_send_error(Gssctxt *ctxt) { 2360Sstevel@tonic-gate char *errstr; 2370Sstevel@tonic-gate OM_uint32 maj,min; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate errstr = ssh_gssapi_last_error(ctxt,&maj,&min); 2400Sstevel@tonic-gate if (errstr) { 2410Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_ERROR); 2420Sstevel@tonic-gate packet_put_int(maj); 2430Sstevel@tonic-gate packet_put_int(min); 2440Sstevel@tonic-gate packet_put_cstring(errstr); 2450Sstevel@tonic-gate packet_put_cstring(""); 2460Sstevel@tonic-gate packet_send(); 2470Sstevel@tonic-gate packet_write_wait(); 2480Sstevel@tonic-gate /* XXX - We should probably log the error locally here */ 2490Sstevel@tonic-gate xfree(errstr); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate #endif /* GSSAPI */ 253