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*6080Sjp161948 * Copyright 2008 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" 481776Snw141292 #include "auth.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate Gssctxt *xxx_gssctxt; 511776Snw141292 extern Authctxt *x_authctxt; 520Sstevel@tonic-gate 530Sstevel@tonic-gate static void kex_gss_send_error(Gssctxt *ctxt); 540Sstevel@tonic-gate 550Sstevel@tonic-gate void 560Sstevel@tonic-gate kexgss_server(Kex *kex) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate OM_uint32 maj_status, min_status; 590Sstevel@tonic-gate gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok; 600Sstevel@tonic-gate Gssctxt *ctxt = NULL; 61*6080Sjp161948 unsigned int klen, kout; 62*6080Sjp161948 unsigned int sbloblen = 0; 63*6080Sjp161948 unsigned char *kbuf, *hash; 640Sstevel@tonic-gate unsigned char *server_host_key_blob = NULL; 65*6080Sjp161948 DH *dh; 660Sstevel@tonic-gate Key *server_host_key = NULL; 67*6080Sjp161948 BIGNUM *shared_secret = NULL; 68*6080Sjp161948 BIGNUM *dh_client_pub = NULL; 69*6080Sjp161948 int type = 0; 70*6080Sjp161948 uint_t slen; 710Sstevel@tonic-gate gss_OID oid; 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet 750Sstevel@tonic-gate * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's 760Sstevel@tonic-gate * the GSS-API that provides for server host authentication. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate if (kex->load_host_key != NULL && 790Sstevel@tonic-gate !(datafellows & SSH_BUG_GSSKEX_HOSTKEY)) 800Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type); 810Sstevel@tonic-gate if (server_host_key != NULL) 820Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); 830Sstevel@tonic-gate 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* Initialise GSSAPI */ 860Sstevel@tonic-gate 870Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(kex->name, &oid); 880Sstevel@tonic-gate if (oid == GSS_C_NULL_OID) { 890Sstevel@tonic-gate fatal("Couldn't match the negotiated GSS key exchange"); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 921776Snw141292 ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid); 930Sstevel@tonic-gate 941776Snw141292 ctxt = xxx_gssctxt; 950Sstevel@tonic-gate 960Sstevel@tonic-gate do { 970Sstevel@tonic-gate debug("Wait SSH2_MSG_GSSAPI_INIT"); 980Sstevel@tonic-gate type = packet_read(); 99*6080Sjp161948 switch (type) { 1000Sstevel@tonic-gate case SSH2_MSG_KEXGSS_INIT: 101*6080Sjp161948 if (dh_client_pub != NULL) 102*6080Sjp161948 fatal("Received KEXGSS_INIT after " 103*6080Sjp161948 "initialising"); 104*6080Sjp161948 recv_tok.value = packet_get_string(&slen); 105*6080Sjp161948 recv_tok.length = slen; /* int vs. size_t */ 1060Sstevel@tonic-gate 107*6080Sjp161948 dh_client_pub = BN_new(); 1080Sstevel@tonic-gate 109*6080Sjp161948 if (dh_client_pub == NULL) 110*6080Sjp161948 fatal("dh_client_pub == NULL"); 111*6080Sjp161948 packet_get_bignum2(dh_client_pub); 1120Sstevel@tonic-gate 113*6080Sjp161948 /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */ 1140Sstevel@tonic-gate if (sbloblen) { 1150Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_HOSTKEY); 116*6080Sjp161948 packet_put_string(server_host_key_blob, 117*6080Sjp161948 sbloblen); 1180Sstevel@tonic-gate packet_send(); 1190Sstevel@tonic-gate packet_write_wait(); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate break; 1220Sstevel@tonic-gate case SSH2_MSG_KEXGSS_CONTINUE: 123*6080Sjp161948 recv_tok.value = packet_get_string(&slen); 124*6080Sjp161948 recv_tok.length = slen; /* int vs. size_t */ 1250Sstevel@tonic-gate break; 1260Sstevel@tonic-gate default: 127*6080Sjp161948 packet_disconnect("Protocol error: didn't expect " 128*6080Sjp161948 "packet type %d", type); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 131*6080Sjp161948 maj_status = ssh_gssapi_accept_ctx(ctxt, &recv_tok, &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); 141*6080Sjp161948 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); 150*6080Sjp161948 if (send_tok.length > 0) { 1510Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE); 152*6080Sjp161948 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 170*6080Sjp161948 if (!dh_pub_is_valid(dh, dh_client_pub)) 171*6080Sjp161948 packet_disconnect("bad client public DH value"); 1720Sstevel@tonic-gate 173*6080Sjp161948 klen = DH_size(dh); 174*6080Sjp161948 kbuf = xmalloc(klen); 175*6080Sjp161948 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 */ 183*6080Sjp161948 hash = kex_dh_hash( 184*6080Sjp161948 kex->client_version_string, 185*6080Sjp161948 kex->server_version_string, 186*6080Sjp161948 buffer_ptr(&kex->peer), buffer_len(&kex->peer), 187*6080Sjp161948 buffer_ptr(&kex->my), buffer_len(&kex->my), 188*6080Sjp161948 server_host_key_blob, sbloblen, 189*6080Sjp161948 dh_client_pub, 190*6080Sjp161948 dh->pub_key, 191*6080Sjp161948 shared_secret); 192*6080Sjp161948 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); 1991776Snw141292 } else if (x_authctxt != NULL && x_authctxt->success) { 2001776Snw141292 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 207*6080Sjp161948 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); 214*6080Sjp161948 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 */ 219*6080Sjp161948 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 } 224*6080Sjp161948 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; 237*6080Sjp161948 OM_uint32 maj, min; 2380Sstevel@tonic-gate 239*6080Sjp161948 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