17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate * are met:
77c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate /*
25*c15e4e4bSjp161948 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include "includes.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #ifdef GSSAPI
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
367c478bd9Sstevel@tonic-gate #include <openssl/bn.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include "xmalloc.h"
397c478bd9Sstevel@tonic-gate #include "buffer.h"
407c478bd9Sstevel@tonic-gate #include "bufaux.h"
417c478bd9Sstevel@tonic-gate #include "compat.h"
427c478bd9Sstevel@tonic-gate #include "kex.h"
437c478bd9Sstevel@tonic-gate #include "log.h"
447c478bd9Sstevel@tonic-gate #include "packet.h"
457c478bd9Sstevel@tonic-gate #include "dh.h"
467c478bd9Sstevel@tonic-gate #include "ssh2.h"
477c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
4855837012Snw141292 #include "auth.h"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate Gssctxt *xxx_gssctxt;
5155837012Snw141292 extern Authctxt *x_authctxt;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate static void kex_gss_send_error(Gssctxt *ctxt);
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate void
kexgss_server(Kex * kex)567c478bd9Sstevel@tonic-gate kexgss_server(Kex *kex)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate OM_uint32 maj_status, min_status;
597c478bd9Sstevel@tonic-gate gss_buffer_desc gssbuf, send_tok, recv_tok, msg_tok;
607c478bd9Sstevel@tonic-gate Gssctxt *ctxt = NULL;
617c478bd9Sstevel@tonic-gate unsigned int klen, kout;
627c478bd9Sstevel@tonic-gate unsigned int sbloblen = 0;
637c478bd9Sstevel@tonic-gate unsigned char *kbuf, *hash;
647c478bd9Sstevel@tonic-gate unsigned char *server_host_key_blob = NULL;
657c478bd9Sstevel@tonic-gate DH *dh;
667c478bd9Sstevel@tonic-gate Key *server_host_key = NULL;
677c478bd9Sstevel@tonic-gate BIGNUM *shared_secret = NULL;
687c478bd9Sstevel@tonic-gate BIGNUM *dh_client_pub = NULL;
697c478bd9Sstevel@tonic-gate int type = 0;
70*c15e4e4bSjp161948 uint_t slen;
717c478bd9Sstevel@tonic-gate gss_OID oid;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * Load host key to advertise in a SSH_MSG_KEXGSS_HOSTKEY packet
757c478bd9Sstevel@tonic-gate * -- unlike KEX_DH/KEX_GEX no host key, no problem since it's
767c478bd9Sstevel@tonic-gate * the GSS-API that provides for server host authentication.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate if (kex->load_host_key != NULL &&
797c478bd9Sstevel@tonic-gate !(datafellows & SSH_BUG_GSSKEX_HOSTKEY))
807c478bd9Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type);
817c478bd9Sstevel@tonic-gate if (server_host_key != NULL)
827c478bd9Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* Initialise GSSAPI */
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(kex->name, &oid);
887c478bd9Sstevel@tonic-gate if (oid == GSS_C_NULL_OID) {
897c478bd9Sstevel@tonic-gate fatal("Couldn't match the negotiated GSS key exchange");
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
9255837012Snw141292 ssh_gssapi_build_ctx(&xxx_gssctxt, 0, oid);
937c478bd9Sstevel@tonic-gate
9455837012Snw141292 ctxt = xxx_gssctxt;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate do {
977c478bd9Sstevel@tonic-gate debug("Wait SSH2_MSG_GSSAPI_INIT");
987c478bd9Sstevel@tonic-gate type = packet_read();
997c478bd9Sstevel@tonic-gate switch (type) {
1007c478bd9Sstevel@tonic-gate case SSH2_MSG_KEXGSS_INIT:
1017c478bd9Sstevel@tonic-gate if (dh_client_pub != NULL)
102*c15e4e4bSjp161948 fatal("Received KEXGSS_INIT after "
103*c15e4e4bSjp161948 "initialising");
1047c478bd9Sstevel@tonic-gate recv_tok.value = packet_get_string(&slen);
1057c478bd9Sstevel@tonic-gate recv_tok.length = slen; /* int vs. size_t */
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate dh_client_pub = BN_new();
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (dh_client_pub == NULL)
1107c478bd9Sstevel@tonic-gate fatal("dh_client_pub == NULL");
1117c478bd9Sstevel@tonic-gate packet_get_bignum2(dh_client_pub);
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
1147c478bd9Sstevel@tonic-gate if (sbloblen) {
1157c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_HOSTKEY);
116*c15e4e4bSjp161948 packet_put_string(server_host_key_blob,
117*c15e4e4bSjp161948 sbloblen);
1187c478bd9Sstevel@tonic-gate packet_send();
1197c478bd9Sstevel@tonic-gate packet_write_wait();
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate break;
1227c478bd9Sstevel@tonic-gate case SSH2_MSG_KEXGSS_CONTINUE:
1237c478bd9Sstevel@tonic-gate recv_tok.value = packet_get_string(&slen);
1247c478bd9Sstevel@tonic-gate recv_tok.length = slen; /* int vs. size_t */
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate default:
127*c15e4e4bSjp161948 packet_disconnect("Protocol error: didn't expect "
128*c15e4e4bSjp161948 "packet type %d", type);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1319a8058b5Sjp161948 maj_status = ssh_gssapi_accept_ctx(ctxt, &recv_tok, &send_tok);
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate xfree(recv_tok.value); /* We allocated this, not gss */
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate if (dh_client_pub == NULL)
1367c478bd9Sstevel@tonic-gate fatal("No client public key");
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (maj_status == GSS_S_CONTINUE_NEEDED) {
1397c478bd9Sstevel@tonic-gate debug("Sending GSSAPI_CONTINUE");
1407c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1417c478bd9Sstevel@tonic-gate packet_put_string(send_tok.value, send_tok.length);
1427c478bd9Sstevel@tonic-gate packet_send();
1437c478bd9Sstevel@tonic-gate packet_write_wait();
1447c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate } while (maj_status == GSS_S_CONTINUE_NEEDED);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (GSS_ERROR(maj_status)) {
1497c478bd9Sstevel@tonic-gate kex_gss_send_error(ctxt);
1507c478bd9Sstevel@tonic-gate if (send_tok.length > 0) {
1517c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_CONTINUE);
1527c478bd9Sstevel@tonic-gate packet_put_string(send_tok.value, send_tok.length);
1537c478bd9Sstevel@tonic-gate packet_send();
1547c478bd9Sstevel@tonic-gate packet_write_wait();
1557c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate fatal("accept_ctx died");
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate debug("gss_complete");
1617c478bd9Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_MUTUAL_FLAG))
1627c478bd9Sstevel@tonic-gate fatal("Mutual authentication flag wasn't set");
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (!(ctxt->flags & GSS_C_INTEG_FLAG))
1657c478bd9Sstevel@tonic-gate fatal("Integrity flag wasn't set");
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate dh = dh_new_group1();
1687c478bd9Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub))
1717c478bd9Sstevel@tonic-gate packet_disconnect("bad client public DH value");
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate klen = DH_size(dh);
1747c478bd9Sstevel@tonic-gate kbuf = xmalloc(klen);
1757c478bd9Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh);
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate shared_secret = BN_new();
1787c478bd9Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret);
1797c478bd9Sstevel@tonic-gate (void) memset(kbuf, 0, klen);
1807c478bd9Sstevel@tonic-gate xfree(kbuf);
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /* The GSSAPI hash is identical to the Diffie Helman one */
1837c478bd9Sstevel@tonic-gate hash = kex_dh_hash(
1847c478bd9Sstevel@tonic-gate kex->client_version_string,
1857c478bd9Sstevel@tonic-gate kex->server_version_string,
1867c478bd9Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer),
1877c478bd9Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my),
1887c478bd9Sstevel@tonic-gate server_host_key_blob, sbloblen,
1897c478bd9Sstevel@tonic-gate dh_client_pub,
1907c478bd9Sstevel@tonic-gate dh->pub_key,
191*c15e4e4bSjp161948 shared_secret);
192*c15e4e4bSjp161948
1937c478bd9Sstevel@tonic-gate BN_free(dh_client_pub);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if (kex->session_id == NULL) {
1967c478bd9Sstevel@tonic-gate kex->session_id_len = 20;
1977c478bd9Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len);
1987c478bd9Sstevel@tonic-gate (void) memcpy(kex->session_id, hash, kex->session_id_len);
19955837012Snw141292 } else if (x_authctxt != NULL && x_authctxt->success) {
20055837012Snw141292 ssh_gssapi_storecreds(ctxt, x_authctxt);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* Should fix kex_dh_hash to output hash length */
2047c478bd9Sstevel@tonic-gate gssbuf.length = 20; /* yes, it's always 20 (SHA-1) */
2057c478bd9Sstevel@tonic-gate gssbuf.value = hash; /* and it's static constant storage */
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate if (GSS_ERROR(ssh_gssapi_get_mic(ctxt, &gssbuf, &msg_tok))) {
2087c478bd9Sstevel@tonic-gate kex_gss_send_error(ctxt);
2097c478bd9Sstevel@tonic-gate fatal("Couldn't get MIC");
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_COMPLETE);
2137c478bd9Sstevel@tonic-gate packet_put_bignum2(dh->pub_key);
2147c478bd9Sstevel@tonic-gate packet_put_string((char *)msg_tok.value, msg_tok.length);
2157c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &msg_tok);
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate if (send_tok.length != 0) {
2187c478bd9Sstevel@tonic-gate packet_put_char(1); /* true */
2197c478bd9Sstevel@tonic-gate packet_put_string((char *)send_tok.value, send_tok.length);
2207c478bd9Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok);
2217c478bd9Sstevel@tonic-gate } else {
2227c478bd9Sstevel@tonic-gate packet_put_char(0); /* false */
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate packet_send();
2257c478bd9Sstevel@tonic-gate packet_write_wait();
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate DH_free(dh);
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret);
2307c478bd9Sstevel@tonic-gate BN_clear_free(shared_secret);
2317c478bd9Sstevel@tonic-gate kex_finish(kex);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate static void
kex_gss_send_error(Gssctxt * ctxt)2357c478bd9Sstevel@tonic-gate kex_gss_send_error(Gssctxt *ctxt) {
2367c478bd9Sstevel@tonic-gate char *errstr;
2377c478bd9Sstevel@tonic-gate OM_uint32 maj, min;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate errstr = ssh_gssapi_last_error(ctxt, &maj, &min);
2407c478bd9Sstevel@tonic-gate if (errstr) {
2417c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEXGSS_ERROR);
2427c478bd9Sstevel@tonic-gate packet_put_int(maj);
2437c478bd9Sstevel@tonic-gate packet_put_int(min);
2447c478bd9Sstevel@tonic-gate packet_put_cstring(errstr);
2457c478bd9Sstevel@tonic-gate packet_put_cstring("");
2467c478bd9Sstevel@tonic-gate packet_send();
2477c478bd9Sstevel@tonic-gate packet_write_wait();
2487c478bd9Sstevel@tonic-gate /* XXX - We should probably log the error locally here */
2497c478bd9Sstevel@tonic-gate xfree(errstr);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
253