svr-kex.c 6.02 KB
Newer Older
1
2
3
4
/*
 * Dropbear - a SSH2 server
 * 
 * Copyright (c) 2002,2003 Matt Johnston
5
 * Copyright (c) 2004 by Mihnea Stoenescu
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. */

#include "includes.h"
#include "dbutil.h"
#include "algo.h"
#include "buffer.h"
#include "session.h"
#include "kex.h"
#include "ssh.h"
#include "packet.h"
#include "bignum.h"
35
#include "dbrandom.h"
Matt Johnston's avatar
Matt Johnston committed
36
#include "runopts.h"
37
#include "ecc.h"
38
#include "gensignkey.h"
39

40
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
41
42
43
44
45
46
47

/* Handle a diffie-hellman key exchange initialisation. This involves
 * calculating a session key reply value, and corresponding hash. These
 * are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls
 * that function, then brings the new keys into use */
void recv_msg_kexdh_init() {

48
	DEF_MP_INT(dh_e);
49
	buffer *ecdh_qs = NULL;
50

51
	TRACE(("enter recv_msg_kexdh_init"))
52
53
54
55
	if (!ses.kexstate.recvkexinit) {
		dropbear_exit("Premature kexdh_init message received");
	}

Matt Johnston's avatar
Matt Johnston committed
56
	switch (ses.newkeys->algo_kex->mode) {
57
#if DROPBEAR_NORMAL_DH
Matt Johnston's avatar
Matt Johnston committed
58
59
60
61
62
63
		case DROPBEAR_KEX_NORMAL_DH:
			m_mp_init(&dh_e);
			if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
				dropbear_exit("Bad kex value");
			}
			break;
64
#endif
65
#if DROPBEAR_ECDH
Matt Johnston's avatar
Matt Johnston committed
66
		case DROPBEAR_KEX_ECDH:
67
#endif
68
#if DROPBEAR_CURVE25519
Matt Johnston's avatar
Matt Johnston committed
69
		case DROPBEAR_KEX_CURVE25519:
70
#endif
71
#if DROPBEAR_ECDH || DROPBEAR_CURVE25519
Matt Johnston's avatar
Matt Johnston committed
72
73
			ecdh_qs = buf_getstringbuf(ses.payload);
			break;
74
#endif
Matt Johnston's avatar
Matt Johnston committed
75
	}
76
77
78
	if (ses.payload->pos != ses.payload->len) {
		dropbear_exit("Bad kex value");
	}
79

80
	send_msg_kexdh_reply(&dh_e, ecdh_qs);
81
82

	mp_clear(&dh_e);
83
84
	if (ecdh_qs) {
		buf_free(ecdh_qs);
85
		ecdh_qs = NULL;
86
	}
87
88

	send_msg_newkeys();
89
	ses.requirenext = SSH_MSG_NEWKEYS;
90
	TRACE(("leave recv_msg_kexdh_init"))
91
}
92

93

94
#if DROPBEAR_DELAY_HOSTKEY
95

96
97
98
99
100
101
102
103
104
105
106
107
108
static void svr_ensure_hostkey() {

	const char* fn = NULL;
	enum signkey_type type = ses.newkeys->algo_hostkey;
	void **hostkey = signkey_key_ptr(svr_opts.hostkey, type);
	int ret = DROPBEAR_FAILURE;

	if (hostkey && *hostkey) {
		return;
	}

	switch (type)
	{
109
#if DROPBEAR_RSA
110
111
112
113
		case DROPBEAR_SIGNKEY_RSA:
			fn = RSA_PRIV_FILENAME;
			break;
#endif
114
#if DROPBEAR_DSS
115
116
117
118
		case DROPBEAR_SIGNKEY_DSS:
			fn = DSS_PRIV_FILENAME;
			break;
#endif
119
#if DROPBEAR_ECDSA
120
121
122
123
124
125
126
		case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
		case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
		case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
			fn = ECDSA_PRIV_FILENAME;
			break;
#endif
		default:
127
			dropbear_assert(0);
128
129
130
131
132
133
	}

	if (readhostkey(fn, svr_opts.hostkey, &type) == DROPBEAR_SUCCESS) {
		return;
	}

134
	if (signkey_generate(type, 0, fn, 1) == DROPBEAR_FAILURE) {
135
136
		goto out;
	}
137
	
138
139
	ret = readhostkey(fn, svr_opts.hostkey, &type);

Matt Johnston's avatar
Matt Johnston committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	if (ret == DROPBEAR_SUCCESS) {
		char *fp = NULL;
		unsigned int len;
		buffer *key_buf = buf_new(MAX_PUBKEY_SIZE);
		buf_put_pub_key(key_buf, svr_opts.hostkey, type);
		buf_setpos(key_buf, 4);
		len = key_buf->len - key_buf->pos;
		fp = sign_key_fingerprint(buf_getptr(key_buf, len), len);
		dropbear_log(LOG_INFO, "Generated hostkey %s, fingerprint is %s",
			fn, fp);
		m_free(fp);
		buf_free(key_buf);
	}

154
155
156
out:
	if (ret == DROPBEAR_FAILURE)
	{
157
		dropbear_exit("Couldn't read or generate hostkey %s", fn);
158
159
	}
}
160
#endif
161
162
163
164
165
166
	
/* Generate our side of the diffie-hellman key exchange value (dh_f), and
 * calculate the session key using the diffie-hellman algorithm. Following
 * that, the session hash is calculated, and signed with RSA or DSS. The
 * result is sent to the client. 
 *
167
168
169
 * See the transport RFC4253 section 8 for details
 * or RFC5656 section 4 for elliptic curve variant. */
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
170
	TRACE(("enter send_msg_kexdh_reply"))
171
172
173

	/* we can start creating the kexdh_reply packet */
	CHECKCLEARTOWRITE();
174

175
#if DROPBEAR_DELAY_HOSTKEY
176
177
178
179
180
	if (svr_opts.delay_hostkey)
	{
		svr_ensure_hostkey();
	}
#endif
181

182
	buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
Matt Johnston's avatar
Matt Johnston committed
183
	buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
184
185
			ses.newkeys->algo_hostkey);

Matt Johnston's avatar
Matt Johnston committed
186
	switch (ses.newkeys->algo_kex->mode) {
187
#if DROPBEAR_NORMAL_DH
Matt Johnston's avatar
Matt Johnston committed
188
189
190
191
192
193
194
195
196
197
		case DROPBEAR_KEX_NORMAL_DH:
			{
			struct kex_dh_param * dh_param = gen_kexdh_param();
			kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);

			/* put f */
			buf_putmpint(ses.writepayload, &dh_param->pub);
			free_kexdh_param(dh_param);
			}
			break;
198
#endif
199
#if DROPBEAR_ECDH
200
		case DROPBEAR_KEX_ECDH:
Matt Johnston's avatar
Matt Johnston committed
201
202
203
			{
			struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
			kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
204

Matt Johnston's avatar
Matt Johnston committed
205
206
207
208
			buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
			free_kexecdh_param(ecdh_param);
			}
			break;
209
#endif
210
#if DROPBEAR_CURVE25519
211
		case DROPBEAR_KEX_CURVE25519:
Matt Johnston's avatar
Matt Johnston committed
212
			{
Matt Johnston's avatar
Merge    
Matt Johnston committed
213
			struct kex_curve25519_param *param = gen_kexcurve25519_param();
Matt Johnston's avatar
Matt Johnston committed
214
			kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
215
			buf_putstring(ses.writepayload, (const char*)param->pub, CURVE25519_LEN);
Matt Johnston's avatar
Matt Johnston committed
216
217
218
			free_kexcurve25519_param(param);
			}
			break;
219
#endif
220
	}
221
222

	/* calc the signature */
Matt Johnston's avatar
Matt Johnston committed
223
	buf_put_sign(ses.writepayload, svr_opts.hostkey, 
224
			ses.newkeys->algo_hostkey, ses.hash);
225
226
227
228

	/* the SSH_MSG_KEXDH_REPLY is done */
	encrypt_packet();

229
	TRACE(("leave send_msg_kexdh_reply"))
230
231
}