1264377Sdes/* $OpenBSD: kexdhs.c,v 1.18 2014/02/02 03:44:31 djm Exp $ */
2113908Sdes/*
3113908Sdes * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4113908Sdes *
5113908Sdes * Redistribution and use in source and binary forms, with or without
6113908Sdes * modification, are permitted provided that the following conditions
7113908Sdes * are met:
8113908Sdes * 1. Redistributions of source code must retain the above copyright
9113908Sdes *    notice, this list of conditions and the following disclaimer.
10113908Sdes * 2. Redistributions in binary form must reproduce the above copyright
11113908Sdes *    notice, this list of conditions and the following disclaimer in the
12113908Sdes *    documentation and/or other materials provided with the distribution.
13113908Sdes *
14113908Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15113908Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16113908Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17113908Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18113908Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19113908Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20113908Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21113908Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22113908Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23113908Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24113908Sdes */
25113908Sdes
26113908Sdes#include "includes.h"
27113908Sdes
28162852Sdes#include <sys/types.h>
29162852Sdes
30162852Sdes#include <stdarg.h>
31162852Sdes#include <string.h>
32162852Sdes#include <signal.h>
33162852Sdes
34221420Sdes#include <openssl/dh.h>
35221420Sdes
36113908Sdes#include "xmalloc.h"
37162852Sdes#include "buffer.h"
38113908Sdes#include "key.h"
39162852Sdes#include "cipher.h"
40113908Sdes#include "kex.h"
41113908Sdes#include "log.h"
42113908Sdes#include "packet.h"
43113908Sdes#include "dh.h"
44113908Sdes#include "ssh2.h"
45113908Sdes
46113908Sdesvoid
47113908Sdeskexdh_server(Kex *kex)
48113908Sdes{
49113908Sdes	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
50113908Sdes	DH *dh;
51204917Sdes	Key *server_host_public, *server_host_private;
52113908Sdes	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
53164146Sdes	u_int sbloblen, klen, hashlen, slen;
54164146Sdes	int kout;
55113908Sdes
56113908Sdes	/* generate server DH public key */
57137015Sdes	switch (kex->kex_type) {
58137015Sdes	case KEX_DH_GRP1_SHA1:
59137015Sdes		dh = dh_new_group1();
60137015Sdes		break;
61137015Sdes	case KEX_DH_GRP14_SHA1:
62137015Sdes		dh = dh_new_group14();
63137015Sdes		break;
64137015Sdes	default:
65137015Sdes		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
66137015Sdes	}
67113908Sdes	dh_gen_key(dh, kex->we_need * 8);
68113908Sdes
69113908Sdes	debug("expecting SSH2_MSG_KEXDH_INIT");
70113908Sdes	packet_read_expect(SSH2_MSG_KEXDH_INIT);
71113908Sdes
72204917Sdes	if (kex->load_host_public_key == NULL ||
73204917Sdes	    kex->load_host_private_key == NULL)
74113908Sdes		fatal("Cannot load hostkey");
75204917Sdes	server_host_public = kex->load_host_public_key(kex->hostkey_type);
76204917Sdes	if (server_host_public == NULL)
77113908Sdes		fatal("Unsupported hostkey type %d", kex->hostkey_type);
78204917Sdes	server_host_private = kex->load_host_private_key(kex->hostkey_type);
79113908Sdes
80113908Sdes	/* key, cert */
81113908Sdes	if ((dh_client_pub = BN_new()) == NULL)
82113908Sdes		fatal("dh_client_pub == NULL");
83113908Sdes	packet_get_bignum2(dh_client_pub);
84113908Sdes	packet_check_eom();
85113908Sdes
86113908Sdes#ifdef DEBUG_KEXDH
87113908Sdes	fprintf(stderr, "dh_client_pub= ");
88113908Sdes	BN_print_fp(stderr, dh_client_pub);
89113908Sdes	fprintf(stderr, "\n");
90113908Sdes	debug("bits %d", BN_num_bits(dh_client_pub));
91113908Sdes#endif
92113908Sdes
93113908Sdes#ifdef DEBUG_KEXDH
94113908Sdes	DHparams_print_fp(stderr, dh);
95113908Sdes	fprintf(stderr, "pub= ");
96113908Sdes	BN_print_fp(stderr, dh->pub_key);
97113908Sdes	fprintf(stderr, "\n");
98113908Sdes#endif
99113908Sdes	if (!dh_pub_is_valid(dh, dh_client_pub))
100113908Sdes		packet_disconnect("bad client public DH value");
101113908Sdes
102113908Sdes	klen = DH_size(dh);
103113908Sdes	kbuf = xmalloc(klen);
104164146Sdes	if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
105164146Sdes		fatal("DH_compute_key: failed");
106113908Sdes#ifdef DEBUG_KEXDH
107113908Sdes	dump_digest("shared secret", kbuf, kout);
108113908Sdes#endif
109113908Sdes	if ((shared_secret = BN_new()) == NULL)
110113908Sdes		fatal("kexdh_server: BN_new failed");
111164146Sdes	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
112164146Sdes		fatal("kexdh_server: BN_bin2bn failed");
113264377Sdes	explicit_bzero(kbuf, klen);
114255767Sdes	free(kbuf);
115113908Sdes
116204917Sdes	key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
117113908Sdes
118113908Sdes	/* calc H */
119157016Sdes	kex_dh_hash(
120113908Sdes	    kex->client_version_string,
121113908Sdes	    kex->server_version_string,
122113908Sdes	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
123113908Sdes	    buffer_ptr(&kex->my), buffer_len(&kex->my),
124113908Sdes	    server_host_key_blob, sbloblen,
125113908Sdes	    dh_client_pub,
126113908Sdes	    dh->pub_key,
127157016Sdes	    shared_secret,
128157016Sdes	    &hash, &hashlen
129113908Sdes	);
130113908Sdes	BN_clear_free(dh_client_pub);
131113908Sdes
132113908Sdes	/* save session id := H */
133113908Sdes	if (kex->session_id == NULL) {
134157016Sdes		kex->session_id_len = hashlen;
135113908Sdes		kex->session_id = xmalloc(kex->session_id_len);
136113908Sdes		memcpy(kex->session_id, hash, kex->session_id_len);
137113908Sdes	}
138113908Sdes
139113908Sdes	/* sign H */
140255767Sdes	kex->sign(server_host_private, server_host_public, &signature, &slen,
141255767Sdes	    hash, hashlen);
142113908Sdes
143113908Sdes	/* destroy_sensitive_data(); */
144113908Sdes
145113908Sdes	/* send server hostkey, DH pubkey 'f' and singed H */
146113908Sdes	packet_start(SSH2_MSG_KEXDH_REPLY);
147113908Sdes	packet_put_string(server_host_key_blob, sbloblen);
148113908Sdes	packet_put_bignum2(dh->pub_key);	/* f */
149113908Sdes	packet_put_string(signature, slen);
150113908Sdes	packet_send();
151113908Sdes
152255767Sdes	free(signature);
153255767Sdes	free(server_host_key_blob);
154113908Sdes	/* have keys, free DH */
155113908Sdes	DH_free(dh);
156113908Sdes
157262566Sdes	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
158113908Sdes	BN_clear_free(shared_secret);
159113908Sdes	kex_finish(kex);
160113908Sdes}
161