xref: /titanic_44/usr/src/cmd/ssh/libssh/common/kexdhs.c (revision 9a8058b57457911fab0e3b4b6f0a97740e7a816d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 Markus Friedl.  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 
257c478bd9Sstevel@tonic-gate #include "includes.h"
267c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: kexdh.c,v 1.18 2002/03/18 17:50:31 provos Exp $");
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
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 
437c478bd9Sstevel@tonic-gate void
kexdh_server(Kex * kex)447c478bd9Sstevel@tonic-gate kexdh_server(Kex *kex)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
477c478bd9Sstevel@tonic-gate 	DH *dh;
487c478bd9Sstevel@tonic-gate 	Key *server_host_key;
497c478bd9Sstevel@tonic-gate 	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
507c478bd9Sstevel@tonic-gate 	u_int sbloblen, klen, kout;
517c478bd9Sstevel@tonic-gate 	u_int slen;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	/* generate server DH public key */
547c478bd9Sstevel@tonic-gate 	dh = dh_new_group1();
557c478bd9Sstevel@tonic-gate 	dh_gen_key(dh, kex->we_need * 8);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	debug("expecting SSH2_MSG_KEXDH_INIT");
587c478bd9Sstevel@tonic-gate 	packet_read_expect(SSH2_MSG_KEXDH_INIT);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (kex->load_host_key == NULL)
617c478bd9Sstevel@tonic-gate 		fatal("Cannot load hostkey");
627c478bd9Sstevel@tonic-gate 	server_host_key = kex->load_host_key(kex->hostkey_type);
637c478bd9Sstevel@tonic-gate 	if (server_host_key == NULL)
647c478bd9Sstevel@tonic-gate 		fatal("Unsupported hostkey type %d", kex->hostkey_type);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/* key, cert */
677c478bd9Sstevel@tonic-gate 	if ((dh_client_pub = BN_new()) == NULL)
687c478bd9Sstevel@tonic-gate 		fatal("dh_client_pub == NULL");
697c478bd9Sstevel@tonic-gate 	packet_get_bignum2(dh_client_pub);
707c478bd9Sstevel@tonic-gate 	packet_check_eom();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
737c478bd9Sstevel@tonic-gate 	fprintf(stderr, "dh_client_pub= ");
747c478bd9Sstevel@tonic-gate 	BN_print_fp(stderr, dh_client_pub);
757c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
767c478bd9Sstevel@tonic-gate 	debug("bits %d", BN_num_bits(dh_client_pub));
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
807c478bd9Sstevel@tonic-gate 	DHparams_print_fp(stderr, dh);
817c478bd9Sstevel@tonic-gate 	fprintf(stderr, "pub= ");
827c478bd9Sstevel@tonic-gate 	BN_print_fp(stderr, dh->pub_key);
837c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 	if (!dh_pub_is_valid(dh, dh_client_pub))
867c478bd9Sstevel@tonic-gate 		packet_disconnect("bad client public DH value");
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	klen = DH_size(dh);
897c478bd9Sstevel@tonic-gate 	kbuf = xmalloc(klen);
907c478bd9Sstevel@tonic-gate 	kout = DH_compute_key(kbuf, dh_client_pub, dh);
917c478bd9Sstevel@tonic-gate #ifdef DEBUG_KEXDH
927c478bd9Sstevel@tonic-gate 	dump_digest("shared secret", kbuf, kout);
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 	if ((shared_secret = BN_new()) == NULL)
957c478bd9Sstevel@tonic-gate 		fatal("kexdh_server: BN_new failed");
967c478bd9Sstevel@tonic-gate 	BN_bin2bn(kbuf, kout, shared_secret);
977c478bd9Sstevel@tonic-gate 	memset(kbuf, 0, klen);
987c478bd9Sstevel@tonic-gate 	xfree(kbuf);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/* calc H */
1037c478bd9Sstevel@tonic-gate 	hash = kex_dh_hash(
1047c478bd9Sstevel@tonic-gate 	    kex->client_version_string,
1057c478bd9Sstevel@tonic-gate 	    kex->server_version_string,
1067c478bd9Sstevel@tonic-gate 	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
1077c478bd9Sstevel@tonic-gate 	    buffer_ptr(&kex->my), buffer_len(&kex->my),
1087c478bd9Sstevel@tonic-gate 	    server_host_key_blob, sbloblen,
1097c478bd9Sstevel@tonic-gate 	    dh_client_pub,
1107c478bd9Sstevel@tonic-gate 	    dh->pub_key,
1117c478bd9Sstevel@tonic-gate 	    shared_secret
1127c478bd9Sstevel@tonic-gate 	);
1137c478bd9Sstevel@tonic-gate 	BN_clear_free(dh_client_pub);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* save session id := H */
1167c478bd9Sstevel@tonic-gate 	/* XXX hashlen depends on KEX */
1177c478bd9Sstevel@tonic-gate 	if (kex->session_id == NULL) {
1187c478bd9Sstevel@tonic-gate 		kex->session_id_len = 20;
1197c478bd9Sstevel@tonic-gate 		kex->session_id = xmalloc(kex->session_id_len);
1207c478bd9Sstevel@tonic-gate 		memcpy(kex->session_id, hash, kex->session_id_len);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/* sign H */
1247c478bd9Sstevel@tonic-gate 	/* XXX hashlen depends on KEX */
125*9a8058b5Sjp161948 	key_sign(server_host_key, &signature, &slen, hash, 20);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* destroy_sensitive_data(); */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* send server hostkey, DH pubkey 'f' and singed H */
1307c478bd9Sstevel@tonic-gate 	packet_start(SSH2_MSG_KEXDH_REPLY);
1317c478bd9Sstevel@tonic-gate 	packet_put_string(server_host_key_blob, sbloblen);
1327c478bd9Sstevel@tonic-gate 	packet_put_bignum2(dh->pub_key);	/* f */
1337c478bd9Sstevel@tonic-gate 	packet_put_string(signature, slen);
1347c478bd9Sstevel@tonic-gate 	packet_send();
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	xfree(signature);
1377c478bd9Sstevel@tonic-gate 	xfree(server_host_key_blob);
1387c478bd9Sstevel@tonic-gate 	/* have keys, free DH */
1397c478bd9Sstevel@tonic-gate 	DH_free(dh);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	kex_derive_keys(kex, hash, shared_secret);
1427c478bd9Sstevel@tonic-gate 	BN_clear_free(shared_secret);
1437c478bd9Sstevel@tonic-gate 	kex_finish(kex);
1447c478bd9Sstevel@tonic-gate }
145