1/* $OpenBSD: kexdhs.c,v 1.12 2010/11/10 01:33:07 djm Exp $ */
2/*
3 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
27
28#include <sys/types.h>
29
30#include <stdarg.h>
31#include <string.h>
32#include <signal.h>
33
34#ifdef __APPLE_CRYPTO__
35#include "ossl-dh.h"
36#else
37#include <openssl/dh.h>
38#endif
39
40#include "xmalloc.h"
41#include "buffer.h"
42#include "key.h"
43#include "cipher.h"
44#include "kex.h"
45#include "log.h"
46#include "packet.h"
47#include "dh.h"
48#include "ssh2.h"
49#ifdef GSSAPI
50#include "ssh-gss.h"
51#endif
52#include "monitor_wrap.h"
53
54void
55kexdh_server(Kex *kex)
56{
57	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
58	DH *dh;
59	Key *server_host_public, *server_host_private;
60	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
61	u_int sbloblen, klen, hashlen, slen;
62	int kout;
63
64	/* generate server DH public key */
65	switch (kex->kex_type) {
66	case KEX_DH_GRP1_SHA1:
67		dh = dh_new_group1();
68		break;
69	case KEX_DH_GRP14_SHA1:
70		dh = dh_new_group14();
71		break;
72	default:
73		fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
74	}
75	dh_gen_key(dh, kex->we_need * 8);
76
77	debug("expecting SSH2_MSG_KEXDH_INIT");
78	packet_read_expect(SSH2_MSG_KEXDH_INIT);
79
80	if (kex->load_host_public_key == NULL ||
81	    kex->load_host_private_key == NULL)
82		fatal("Cannot load hostkey");
83	server_host_public = kex->load_host_public_key(kex->hostkey_type);
84	if (server_host_public == NULL)
85		fatal("Unsupported hostkey type %d", kex->hostkey_type);
86	server_host_private = kex->load_host_private_key(kex->hostkey_type);
87	if (server_host_private == NULL)
88		fatal("Missing private key for hostkey type %d",
89		    kex->hostkey_type);
90
91	/* key, cert */
92	if ((dh_client_pub = BN_new()) == NULL)
93		fatal("dh_client_pub == NULL");
94	packet_get_bignum2(dh_client_pub);
95	packet_check_eom();
96
97#ifdef DEBUG_KEXDH
98	fprintf(stderr, "dh_client_pub= ");
99	BN_print_fp(stderr, dh_client_pub);
100	fprintf(stderr, "\n");
101	debug("bits %d", BN_num_bits(dh_client_pub));
102#endif
103
104#ifdef DEBUG_KEXDH
105	DHparams_print_fp(stderr, dh);
106	fprintf(stderr, "pub= ");
107	BN_print_fp(stderr, dh->pub_key);
108	fprintf(stderr, "\n");
109#endif
110	if (!dh_pub_is_valid(dh, dh_client_pub))
111		packet_disconnect("bad client public DH value");
112
113	klen = DH_size(dh);
114	kbuf = xmalloc(klen);
115	if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
116		fatal("DH_compute_key: failed");
117#ifdef DEBUG_KEXDH
118	dump_digest("shared secret", kbuf, kout);
119#endif
120	if ((shared_secret = BN_new()) == NULL)
121		fatal("kexdh_server: BN_new failed");
122	if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
123		fatal("kexdh_server: BN_bin2bn failed");
124	memset(kbuf, 0, klen);
125	xfree(kbuf);
126
127	key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
128
129	/* calc H */
130	kex_dh_hash(
131	    kex->client_version_string,
132	    kex->server_version_string,
133	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
134	    buffer_ptr(&kex->my), buffer_len(&kex->my),
135	    server_host_key_blob, sbloblen,
136	    dh_client_pub,
137	    dh->pub_key,
138	    shared_secret,
139	    &hash, &hashlen
140	);
141	BN_clear_free(dh_client_pub);
142
143	/* save session id := H */
144	if (kex->session_id == NULL) {
145		kex->session_id_len = hashlen;
146		kex->session_id = xmalloc(kex->session_id_len);
147		memcpy(kex->session_id, hash, kex->session_id_len);
148	}
149
150	/* sign H */
151	if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash,
152	    hashlen)) < 0)
153		fatal("kexdh_server: key_sign failed");
154
155	/* destroy_sensitive_data(); */
156
157	/* send server hostkey, DH pubkey 'f' and singed H */
158	packet_start(SSH2_MSG_KEXDH_REPLY);
159	packet_put_string(server_host_key_blob, sbloblen);
160	packet_put_bignum2(dh->pub_key);	/* f */
161	packet_put_string(signature, slen);
162	packet_send();
163
164	xfree(signature);
165	xfree(server_host_key_blob);
166	/* have keys, free DH */
167	DH_free(dh);
168
169	kex_derive_keys(kex, hash, hashlen, shared_secret);
170	BN_clear_free(shared_secret);
171	kex_finish(kex);
172}
173