10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. 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
250Sstevel@tonic-gate #include "includes.h"
260Sstevel@tonic-gate RCSID("$OpenBSD: kexdh.c,v 1.18 2002/03/18 17:50:31 provos Exp $");
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <openssl/crypto.h>
310Sstevel@tonic-gate #include <openssl/bn.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "xmalloc.h"
340Sstevel@tonic-gate #include "buffer.h"
350Sstevel@tonic-gate #include "bufaux.h"
360Sstevel@tonic-gate #include "key.h"
370Sstevel@tonic-gate #include "kex.h"
380Sstevel@tonic-gate #include "log.h"
390Sstevel@tonic-gate #include "packet.h"
400Sstevel@tonic-gate #include "dh.h"
410Sstevel@tonic-gate #include "ssh2.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate void
kexdh_server(Kex * kex)440Sstevel@tonic-gate kexdh_server(Kex *kex)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
470Sstevel@tonic-gate DH *dh;
480Sstevel@tonic-gate Key *server_host_key;
490Sstevel@tonic-gate u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
500Sstevel@tonic-gate u_int sbloblen, klen, kout;
510Sstevel@tonic-gate u_int slen;
520Sstevel@tonic-gate
530Sstevel@tonic-gate /* generate server DH public key */
540Sstevel@tonic-gate dh = dh_new_group1();
550Sstevel@tonic-gate dh_gen_key(dh, kex->we_need * 8);
560Sstevel@tonic-gate
570Sstevel@tonic-gate debug("expecting SSH2_MSG_KEXDH_INIT");
580Sstevel@tonic-gate packet_read_expect(SSH2_MSG_KEXDH_INIT);
590Sstevel@tonic-gate
600Sstevel@tonic-gate if (kex->load_host_key == NULL)
610Sstevel@tonic-gate fatal("Cannot load hostkey");
620Sstevel@tonic-gate server_host_key = kex->load_host_key(kex->hostkey_type);
630Sstevel@tonic-gate if (server_host_key == NULL)
640Sstevel@tonic-gate fatal("Unsupported hostkey type %d", kex->hostkey_type);
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* key, cert */
670Sstevel@tonic-gate if ((dh_client_pub = BN_new()) == NULL)
680Sstevel@tonic-gate fatal("dh_client_pub == NULL");
690Sstevel@tonic-gate packet_get_bignum2(dh_client_pub);
700Sstevel@tonic-gate packet_check_eom();
710Sstevel@tonic-gate
720Sstevel@tonic-gate #ifdef DEBUG_KEXDH
730Sstevel@tonic-gate fprintf(stderr, "dh_client_pub= ");
740Sstevel@tonic-gate BN_print_fp(stderr, dh_client_pub);
750Sstevel@tonic-gate fprintf(stderr, "\n");
760Sstevel@tonic-gate debug("bits %d", BN_num_bits(dh_client_pub));
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate
790Sstevel@tonic-gate #ifdef DEBUG_KEXDH
800Sstevel@tonic-gate DHparams_print_fp(stderr, dh);
810Sstevel@tonic-gate fprintf(stderr, "pub= ");
820Sstevel@tonic-gate BN_print_fp(stderr, dh->pub_key);
830Sstevel@tonic-gate fprintf(stderr, "\n");
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate if (!dh_pub_is_valid(dh, dh_client_pub))
860Sstevel@tonic-gate packet_disconnect("bad client public DH value");
870Sstevel@tonic-gate
880Sstevel@tonic-gate klen = DH_size(dh);
890Sstevel@tonic-gate kbuf = xmalloc(klen);
900Sstevel@tonic-gate kout = DH_compute_key(kbuf, dh_client_pub, dh);
910Sstevel@tonic-gate #ifdef DEBUG_KEXDH
920Sstevel@tonic-gate dump_digest("shared secret", kbuf, kout);
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate if ((shared_secret = BN_new()) == NULL)
950Sstevel@tonic-gate fatal("kexdh_server: BN_new failed");
960Sstevel@tonic-gate BN_bin2bn(kbuf, kout, shared_secret);
970Sstevel@tonic-gate memset(kbuf, 0, klen);
980Sstevel@tonic-gate xfree(kbuf);
990Sstevel@tonic-gate
1000Sstevel@tonic-gate key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /* calc H */
1030Sstevel@tonic-gate hash = kex_dh_hash(
1040Sstevel@tonic-gate kex->client_version_string,
1050Sstevel@tonic-gate kex->server_version_string,
1060Sstevel@tonic-gate buffer_ptr(&kex->peer), buffer_len(&kex->peer),
1070Sstevel@tonic-gate buffer_ptr(&kex->my), buffer_len(&kex->my),
1080Sstevel@tonic-gate server_host_key_blob, sbloblen,
1090Sstevel@tonic-gate dh_client_pub,
1100Sstevel@tonic-gate dh->pub_key,
1110Sstevel@tonic-gate shared_secret
1120Sstevel@tonic-gate );
1130Sstevel@tonic-gate BN_clear_free(dh_client_pub);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /* save session id := H */
1160Sstevel@tonic-gate /* XXX hashlen depends on KEX */
1170Sstevel@tonic-gate if (kex->session_id == NULL) {
1180Sstevel@tonic-gate kex->session_id_len = 20;
1190Sstevel@tonic-gate kex->session_id = xmalloc(kex->session_id_len);
1200Sstevel@tonic-gate memcpy(kex->session_id, hash, kex->session_id_len);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /* sign H */
1240Sstevel@tonic-gate /* XXX hashlen depends on KEX */
125*5562Sjp161948 key_sign(server_host_key, &signature, &slen, hash, 20);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* destroy_sensitive_data(); */
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /* send server hostkey, DH pubkey 'f' and singed H */
1300Sstevel@tonic-gate packet_start(SSH2_MSG_KEXDH_REPLY);
1310Sstevel@tonic-gate packet_put_string(server_host_key_blob, sbloblen);
1320Sstevel@tonic-gate packet_put_bignum2(dh->pub_key); /* f */
1330Sstevel@tonic-gate packet_put_string(signature, slen);
1340Sstevel@tonic-gate packet_send();
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate xfree(signature);
1370Sstevel@tonic-gate xfree(server_host_key_blob);
1380Sstevel@tonic-gate /* have keys, free DH */
1390Sstevel@tonic-gate DH_free(dh);
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate kex_derive_keys(kex, hash, shared_secret);
1420Sstevel@tonic-gate BN_clear_free(shared_secret);
1430Sstevel@tonic-gate kex_finish(kex);
1440Sstevel@tonic-gate }
145