17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Niels Provos. All rights reserved.
37c478bd9Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate * are met:
87c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
157c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
167c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
187c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
197c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
207c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
227c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
237c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include "includes.h"
277c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: kexgex.c,v 1.22 2002/03/24 17:27:03 stevesk Exp $");
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <openssl/bn.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "xmalloc.h"
347c478bd9Sstevel@tonic-gate #include "buffer.h"
357c478bd9Sstevel@tonic-gate #include "bufaux.h"
367c478bd9Sstevel@tonic-gate #include "key.h"
377c478bd9Sstevel@tonic-gate #include "kex.h"
387c478bd9Sstevel@tonic-gate #include "log.h"
397c478bd9Sstevel@tonic-gate #include "packet.h"
407c478bd9Sstevel@tonic-gate #include "dh.h"
417c478bd9Sstevel@tonic-gate #include "ssh2.h"
427c478bd9Sstevel@tonic-gate #include "compat.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate void
kexgex_server(Kex * kex)457c478bd9Sstevel@tonic-gate kexgex_server(Kex *kex)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
487c478bd9Sstevel@tonic-gate Key *server_host_key;
497c478bd9Sstevel@tonic-gate DH *dh;
507c478bd9Sstevel@tonic-gate u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
517c478bd9Sstevel@tonic-gate u_int sbloblen, klen, kout, slen;
527c478bd9Sstevel@tonic-gate int min = -1, max = -1, nbits = -1, type;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if (kex->load_host_key == NULL)
557c478bd9Sstevel@tonic-gate fatal("Cannot load hostkey");
567c478bd9Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type);
577c478bd9Sstevel@tonic-gate if (server_host_key == NULL)
587c478bd9Sstevel@tonic-gate fatal("Unsupported hostkey type %d", kex->hostkey_type);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate type = packet_read();
617c478bd9Sstevel@tonic-gate switch (type) {
627c478bd9Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST:
637c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
647c478bd9Sstevel@tonic-gate min = packet_get_int();
657c478bd9Sstevel@tonic-gate nbits = packet_get_int();
667c478bd9Sstevel@tonic-gate max = packet_get_int();
677c478bd9Sstevel@tonic-gate min = MAX(DH_GRP_MIN, min);
687c478bd9Sstevel@tonic-gate max = MIN(DH_GRP_MAX, max);
697c478bd9Sstevel@tonic-gate break;
707c478bd9Sstevel@tonic-gate case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
717c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
727c478bd9Sstevel@tonic-gate nbits = packet_get_int();
737c478bd9Sstevel@tonic-gate min = DH_GRP_MIN;
747c478bd9Sstevel@tonic-gate max = DH_GRP_MAX;
757c478bd9Sstevel@tonic-gate /* unused for old GEX */
767c478bd9Sstevel@tonic-gate break;
777c478bd9Sstevel@tonic-gate default:
787c478bd9Sstevel@tonic-gate fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate packet_check_eom();
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate if (max < min || nbits < min || max < nbits)
837c478bd9Sstevel@tonic-gate fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
847c478bd9Sstevel@tonic-gate min, nbits, max);
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /* Contact privileged parent */
87*9a8058b5Sjp161948 dh = choose_dh(min, nbits, max);
887c478bd9Sstevel@tonic-gate if (dh == NULL)
897c478bd9Sstevel@tonic-gate packet_disconnect("Protocol error: no matching DH grp found");
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
927c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
937c478bd9Sstevel@tonic-gate packet_put_bignum2(dh->p);
947c478bd9Sstevel@tonic-gate packet_put_bignum2(dh->g);
957c478bd9Sstevel@tonic-gate packet_send();
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* flush */
987c478bd9Sstevel@tonic-gate packet_write_wait();
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /* Compute our exchange value in parallel with the client */
1017c478bd9Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8);
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
1047c478bd9Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /* key, cert */
1077c478bd9Sstevel@tonic-gate if ((dh_client_pub = BN_new()) == NULL)
1087c478bd9Sstevel@tonic-gate fatal("dh_client_pub == NULL");
1097c478bd9Sstevel@tonic-gate packet_get_bignum2(dh_client_pub);
1107c478bd9Sstevel@tonic-gate packet_check_eom();
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
1137c478bd9Sstevel@tonic-gate fprintf(stderr, "dh_client_pub= ");
1147c478bd9Sstevel@tonic-gate BN_print_fp(stderr, dh_client_pub);
1157c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
1167c478bd9Sstevel@tonic-gate debug("bits %d", BN_num_bits(dh_client_pub));
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
1207c478bd9Sstevel@tonic-gate DHparams_print_fp(stderr, dh);
1217c478bd9Sstevel@tonic-gate fprintf(stderr, "pub= ");
1227c478bd9Sstevel@tonic-gate BN_print_fp(stderr, dh->pub_key);
1237c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub))
1267c478bd9Sstevel@tonic-gate packet_disconnect("bad client public DH value");
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate klen = DH_size(dh);
1297c478bd9Sstevel@tonic-gate kbuf = xmalloc(klen);
1307c478bd9Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh);
1317c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
1327c478bd9Sstevel@tonic-gate dump_digest("shared secret", kbuf, kout);
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate if ((shared_secret = BN_new()) == NULL)
1357c478bd9Sstevel@tonic-gate fatal("kexgex_server: BN_new failed");
1367c478bd9Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret);
1377c478bd9Sstevel@tonic-gate memset(kbuf, 0, klen);
1387c478bd9Sstevel@tonic-gate xfree(kbuf);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
1437c478bd9Sstevel@tonic-gate min = max = -1;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* calc H */ /* XXX depends on 'kex' */
1467c478bd9Sstevel@tonic-gate hash = kexgex_hash(
1477c478bd9Sstevel@tonic-gate kex->client_version_string,
1487c478bd9Sstevel@tonic-gate kex->server_version_string,
1497c478bd9Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer),
1507c478bd9Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my),
1517c478bd9Sstevel@tonic-gate server_host_key_blob, sbloblen,
1527c478bd9Sstevel@tonic-gate min, nbits, max,
1537c478bd9Sstevel@tonic-gate dh->p, dh->g,
1547c478bd9Sstevel@tonic-gate dh_client_pub,
1557c478bd9Sstevel@tonic-gate dh->pub_key,
1567c478bd9Sstevel@tonic-gate shared_secret
1577c478bd9Sstevel@tonic-gate );
1587c478bd9Sstevel@tonic-gate BN_clear_free(dh_client_pub);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* save session id := H */
1617c478bd9Sstevel@tonic-gate /* XXX hashlen depends on KEX */
1627c478bd9Sstevel@tonic-gate if (kex->session_id == NULL) {
1637c478bd9Sstevel@tonic-gate kex->session_id_len = 20;
1647c478bd9Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len);
1657c478bd9Sstevel@tonic-gate memcpy(kex->session_id, hash, kex->session_id_len);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /* sign H */
1697c478bd9Sstevel@tonic-gate /* XXX hashlen depends on KEX */
170*9a8058b5Sjp161948 key_sign(server_host_key, &signature, &slen, hash, 20);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* destroy_sensitive_data(); */
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* send server hostkey, DH pubkey 'f' and singed H */
1757c478bd9Sstevel@tonic-gate debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
1767c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
1777c478bd9Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen);
1787c478bd9Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); /* f */
1797c478bd9Sstevel@tonic-gate packet_put_string(signature, slen);
1807c478bd9Sstevel@tonic-gate packet_send();
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate xfree(signature);
1837c478bd9Sstevel@tonic-gate xfree(server_host_key_blob);
1847c478bd9Sstevel@tonic-gate /* have keys, free DH */
1857c478bd9Sstevel@tonic-gate DH_free(dh);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret);
1887c478bd9Sstevel@tonic-gate BN_clear_free(shared_secret);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate kex_finish(kex);
1917c478bd9Sstevel@tonic-gate }
192