cli-kex.c 10.9 KB
Newer Older
Matt Johnston's avatar
Matt Johnston committed
1
2
3
/*
 * Dropbear - a SSH2 server
 * 
4
5
 * Copyright (c) 2002-2004 Matt Johnston
 * Copyright (c) 2004 by Mihnea Stoenescu
Matt Johnston's avatar
Matt Johnston committed
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
35
 * 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 "session.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"
36
#include "dbrandom.h"
Matt Johnston's avatar
Matt Johnston committed
37
#include "runopts.h"
Matt Johnston's avatar
Matt Johnston committed
38
#include "signkey.h"
39
#include "ecc.h"
Matt Johnston's avatar
Matt Johnston committed
40
41


42
43
static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen);
#define MAX_KNOWNHOSTS_LINE 4500
Matt Johnston's avatar
Matt Johnston committed
44
45

void send_msg_kexdh_init() {
46
	TRACE(("send_msg_kexdh_init()"))	
Matt Johnston's avatar
Matt Johnston committed
47
48
49

	CHECKCLEARTOWRITE();
	buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_INIT);
Matt Johnston's avatar
Matt Johnston committed
50
	switch (ses.newkeys->algo_kex->mode) {
51
#if DROPBEAR_NORMAL_DH
Matt Johnston's avatar
Matt Johnston committed
52
53
54
55
56
57
58
		case DROPBEAR_KEX_NORMAL_DH:
			if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
				|| !cli_ses.dh_param) {
				if (cli_ses.dh_param) {
					free_kexdh_param(cli_ses.dh_param);
				}
				cli_ses.dh_param = gen_kexdh_param();
Matt Johnston's avatar
Matt Johnston committed
59
			}
Matt Johnston's avatar
Matt Johnston committed
60
61
			buf_putmpint(ses.writepayload, &cli_ses.dh_param->pub);
			break;
62
#endif
63
#if DROPBEAR_ECDH
64
		case DROPBEAR_KEX_ECDH:
Matt Johnston's avatar
Matt Johnston committed
65
66
67
68
69
70
			if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
				|| !cli_ses.ecdh_param) {
				if (cli_ses.ecdh_param) {
					free_kexecdh_param(cli_ses.ecdh_param);
				}
				cli_ses.ecdh_param = gen_kexecdh_param();
Matt Johnston's avatar
Matt Johnston committed
71
			}
Matt Johnston's avatar
Matt Johnston committed
72
73
			buf_put_ecc_raw_pubkey_string(ses.writepayload, &cli_ses.ecdh_param->key);
			break;
74
#endif
75
#if DROPBEAR_CURVE25519
Matt Johnston's avatar
Matt Johnston committed
76
77
78
79
80
81
82
83
		case DROPBEAR_KEX_CURVE25519:
			if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
				|| !cli_ses.curve25519_param) {
				if (cli_ses.curve25519_param) {
					free_kexcurve25519_param(cli_ses.curve25519_param);
				}
				cli_ses.curve25519_param = gen_kexcurve25519_param();
			}
84
			buf_putstring(ses.writepayload, (const char*)cli_ses.curve25519_param->pub, CURVE25519_LEN);
Matt Johnston's avatar
Matt Johnston committed
85
			break;
86
#endif
87
	}
Matt Johnston's avatar
Matt Johnston committed
88

Matt Johnston's avatar
Matt Johnston committed
89
	cli_ses.param_kex_algo = ses.newkeys->algo_kex;
Matt Johnston's avatar
Matt Johnston committed
90
91
92
93
94
95
96
	encrypt_packet();
}

/* Handle a diffie-hellman key exchange reply. */
void recv_msg_kexdh_reply() {

	sign_key *hostkey = NULL;
97
98
99
	unsigned int type, keybloblen;
	unsigned char* keyblob = NULL;

100
	TRACE(("enter recv_msg_kexdh_reply"))
101
102
103
104

	if (cli_ses.kex_state != KEXDH_INIT_SENT) {
		dropbear_exit("Received out-of-order kexdhreply");
	}
Matt Johnston's avatar
Matt Johnston committed
105
	type = ses.newkeys->algo_hostkey;
106
	TRACE(("type is %d", type))
Matt Johnston's avatar
Matt Johnston committed
107
108

	hostkey = new_sign_key();
109
110
111
112
113
114
115
	keybloblen = buf_getint(ses.payload);

	keyblob = buf_getptr(ses.payload, keybloblen);
	if (!ses.kexstate.donefirstkex) {
		/* Only makes sense the first time */
		checkhostkey(keyblob, keybloblen);
	}
116

Matt Johnston's avatar
Matt Johnston committed
117
	if (buf_get_pub_key(ses.payload, hostkey, &type) != DROPBEAR_SUCCESS) {
118
		TRACE(("failed getting pubkey"))
Matt Johnston's avatar
Matt Johnston committed
119
120
121
		dropbear_exit("Bad KEX packet");
	}

Matt Johnston's avatar
Matt Johnston committed
122
	switch (ses.newkeys->algo_kex->mode) {
123
#if DROPBEAR_NORMAL_DH
Matt Johnston's avatar
Matt Johnston committed
124
125
126
127
128
129
130
131
132
133
134
135
136
		case DROPBEAR_KEX_NORMAL_DH:
			{
			DEF_MP_INT(dh_f);
			m_mp_init(&dh_f);
			if (buf_getmpint(ses.payload, &dh_f) != DROPBEAR_SUCCESS) {
				TRACE(("failed getting mpint"))
				dropbear_exit("Bad KEX packet");
			}

			kexdh_comb_key(cli_ses.dh_param, &dh_f, hostkey);
			mp_clear(&dh_f);
			}
			break;
137
#endif
138
#if DROPBEAR_ECDH
139
		case DROPBEAR_KEX_ECDH:
Matt Johnston's avatar
Matt Johnston committed
140
141
142
143
144
145
			{
			buffer *ecdh_qs = buf_getstringbuf(ses.payload);
			kexecdh_comb_key(cli_ses.ecdh_param, ecdh_qs, hostkey);
			buf_free(ecdh_qs);
			}
			break;
146
#endif
147
#if DROPBEAR_CURVE25519
Matt Johnston's avatar
Matt Johnston committed
148
149
150
151
152
153
154
		case DROPBEAR_KEX_CURVE25519:
			{
			buffer *ecdh_qs = buf_getstringbuf(ses.payload);
			kexcurve25519_comb_key(cli_ses.curve25519_param, ecdh_qs, hostkey);
			buf_free(ecdh_qs);
			}
			break;
155
#endif
Matt Johnston's avatar
Matt Johnston committed
156
157
	}

Matt Johnston's avatar
Matt Johnston committed
158
159
160
161
	if (cli_ses.dh_param) {
		free_kexdh_param(cli_ses.dh_param);
		cli_ses.dh_param = NULL;
	}
162
#if DROPBEAR_ECDH
Matt Johnston's avatar
Matt Johnston committed
163
164
165
166
167
	if (cli_ses.ecdh_param) {
		free_kexecdh_param(cli_ses.ecdh_param);
		cli_ses.ecdh_param = NULL;
	}
#endif
168
#if DROPBEAR_CURVE25519
Matt Johnston's avatar
Matt Johnston committed
169
170
171
172
173
	if (cli_ses.curve25519_param) {
		free_kexcurve25519_param(cli_ses.curve25519_param);
		cli_ses.curve25519_param = NULL;
	}
#endif
Matt Johnston's avatar
Matt Johnston committed
174

Matt Johnston's avatar
Matt Johnston committed
175
176
	cli_ses.param_kex_algo = NULL;
	if (buf_verify(ses.payload, hostkey, ses.hash) != DROPBEAR_SUCCESS) {
Matt Johnston's avatar
Matt Johnston committed
177
178
179
180
181
182
183
		dropbear_exit("Bad hostkey signature");
	}

	sign_key_free(hostkey);
	hostkey = NULL;

	send_msg_newkeys();
184
	ses.requirenext = SSH_MSG_NEWKEYS;
185
	TRACE(("leave recv_msg_kexdh_init"))
Matt Johnston's avatar
Matt Johnston committed
186
}
187

188
189
static void ask_to_confirm(unsigned char* keyblob, unsigned int keybloblen,
	const char* algoname) {
190
191

	char* fp = NULL;
192
	FILE *tty = NULL;
193
	int response = 'z';
194
195

	fp = sign_key_fingerprint(keyblob, keybloblen);
196
	if (cli_opts.always_accept_key) {
197
		dropbear_log(LOG_INFO, "\nHost '%s' key accepted unconditionally.\n(%s fingerprint %s)\n",
198
				cli_opts.remotehost,
199
				algoname,
200
201
202
203
				fp);
		m_free(fp);
		return;
	}
204
	fprintf(stderr, "\nHost '%s' is not in the trusted hosts file.\n(%s fingerprint %s)\nDo you want to continue connecting? (y/n) ", 
205
			cli_opts.remotehost, 
206
			algoname,
207
			fp);
Matt Johnston's avatar
Matt Johnston committed
208
	m_free(fp);
209

210
211
212
213
214
215
216
217
218
	tty = fopen(_PATH_TTY, "r");
	if (tty) {
		response = getc(tty);
		fclose(tty);
	} else {
		response = getc(stdin);
	}

	if (response == 'y') {
219
220
221
222
223
224
		return;
	}

	dropbear_exit("Didn't validate host key");
}

Matt Johnston's avatar
Matt Johnston committed
225
226
227
static FILE* open_known_hosts_file(int * readonly)
{
	FILE * hostsfile = NULL;
228
	char * filename = NULL;
229
	char * homedir = NULL;
230
	
231
	homedir = getenv("HOME");
232

233
	if (!homedir) {
Matt Johnston's avatar
Matt Johnston committed
234
		struct passwd * pw = NULL;
235
236
237
238
		pw = getpwuid(getuid());
		if (pw) {
			homedir = pw->pw_dir;
		}
239
240
	}

241
	if (homedir) {
Matt Johnston's avatar
Matt Johnston committed
242
		unsigned int len;
243
244
245
246
247
248
249
		len = strlen(homedir);
		filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/

		snprintf(filename, len+18, "%s/.ssh", homedir);
		/* Check that ~/.ssh exists - easiest way is just to mkdir */
		if (mkdir(filename, S_IRWXU) != 0) {
			if (errno != EEXIST) {
250
251
				dropbear_log(LOG_INFO, "Warning: failed creating %s/.ssh: %s",
						homedir, strerror(errno));
252
				TRACE(("mkdir didn't work: %s", strerror(errno)))
Matt Johnston's avatar
Matt Johnston committed
253
				goto out;
254
255
256
257
258
259
260
			}
		}

		snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
		hostsfile = fopen(filename, "a+");
		
		if (hostsfile != NULL) {
Matt Johnston's avatar
Matt Johnston committed
261
			*readonly = 0;
262
263
264
265
266
			fseek(hostsfile, 0, SEEK_SET);
		} else {
			/* We mightn't have been able to open it if it was read-only */
			if (errno == EACCES || errno == EROFS) {
					TRACE(("trying readonly: %s", strerror(errno)))
Matt Johnston's avatar
Matt Johnston committed
267
					*readonly = 1;
268
269
					hostsfile = fopen(filename, "r");
			}
Matt Johnston's avatar
Matt Johnston committed
270
		}
Matt Johnston's avatar
Matt Johnston committed
271
272
	}

273
	if (hostsfile == NULL) {
274
		TRACE(("hostsfile didn't open: %s", strerror(errno)))
275
276
		dropbear_log(LOG_WARNING, "Failed to open %s/.ssh/known_hosts",
				homedir);
Matt Johnston's avatar
Matt Johnston committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
		goto out;
	}	

out:
	m_free(filename);
	return hostsfile;
}

static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {

	FILE *hostsfile = NULL;
	int readonly = 0;
	unsigned int hostlen, algolen;
	unsigned long len;
	const char *algoname = NULL;
	char * fingerprint = NULL;
	buffer * line = NULL;
	int ret;

296
	if (cli_opts.no_hostkey_check) {
297
		dropbear_log(LOG_INFO, "Caution, skipping hostkey check for %s\n", cli_opts.remotehost);
298
299
300
		return;
	}

301
302
	algoname = signkey_name_from_type(ses.newkeys->algo_hostkey, &algolen);

Matt Johnston's avatar
Matt Johnston committed
303
304
	hostsfile = open_known_hosts_file(&readonly);
	if (!hostsfile)	{
305
		ask_to_confirm(keyblob, keybloblen, algoname);
Matt Johnston's avatar
Matt Johnston committed
306
307
		/* ask_to_confirm will exit upon failure */
		return;
308
	}
Matt Johnston's avatar
Matt Johnston committed
309
	
310
311
312
313
314
	line = buf_new(MAX_KNOWNHOSTS_LINE);
	hostlen = strlen(cli_opts.remotehost);

	do {
		if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) {
315
			TRACE(("failed reading line: prob EOF"))
316
317
318
319
320
321
322
323
			break;
		}

		/* The line is too short to be sensible */
		/* "30" is 'enough to hold ssh-dss plus the spaces, ie so we don't
		 * buf_getfoo() past the end and die horribly - the base64 parsing
		 * code is what tiptoes up to the end nicely */
		if (line->len < (hostlen+30) ) {
324
			TRACE(("line is too short to be sensible"))
325
326
327
328
			continue;
		}

		/* Compare hostnames */
329
		if (strncmp(cli_opts.remotehost, (const char *) buf_getptr(line, hostlen),
330
331
332
333
334
335
336
					hostlen) != 0) {
			continue;
		}

		buf_incrpos(line, hostlen);
		if (buf_getbyte(line) != ' ') {
			/* there wasn't a space after the hostname, something dodgy */
337
			TRACE(("missing space afte matching hostname"))
338
339
340
			continue;
		}

341
		if (strncmp((const char *) buf_getptr(line, algolen), algoname, algolen) != 0) {
342
			TRACE(("algo doesn't match"))
343
344
345
			continue;
		}

Matt Johnston's avatar
Matt Johnston committed
346
		buf_incrpos(line, algolen);
347
		if (buf_getbyte(line) != ' ') {
348
			TRACE(("missing space after algo"))
349
350
351
352
			continue;
		}

		/* Now we're at the interesting hostkey */
353
		ret = cmp_base64_key(keyblob, keybloblen, (const unsigned char *) algoname, algolen,
Matt Johnston's avatar
Matt Johnston committed
354
						line, &fingerprint);
355
356
357

		if (ret == DROPBEAR_SUCCESS) {
			/* Good matching key */
358
			TRACE(("good matching key"))
359
360
361
			goto out;
		}

Matt Johnston's avatar
Matt Johnston committed
362
363
		/* The keys didn't match. eep. Note that we're "leaking"
		   the fingerprint strings here, but we're exiting anyway */
364
		dropbear_exit("\n\n%s host key mismatch for %s !\n"
Matt Johnston's avatar
Matt Johnston committed
365
366
367
					"Fingerprint is %s\n"
					"Expected %s\n"
					"If you know that the host key is correct you can\nremove the bad entry from ~/.ssh/known_hosts", 
368
					algoname,
Matt Johnston's avatar
Matt Johnston committed
369
370
371
					cli_opts.remotehost,
					sign_key_fingerprint(keyblob, keybloblen),
					fingerprint ? fingerprint : "UNKNOWN");
372
373
374
	} while (1); /* keep going 'til something happens */

	/* Key doesn't exist yet */
375
	ask_to_confirm(keyblob, keybloblen, algoname);
Matt Johnston's avatar
Matt Johnston committed
376

377
378
	/* If we get here, they said yes */

Matt Johnston's avatar
Matt Johnston committed
379
	if (readonly) {
380
		TRACE(("readonly"))
Matt Johnston's avatar
Matt Johnston committed
381
382
383
		goto out;
	}

384
385
386
387
388
	if (!cli_opts.always_accept_key) {
		/* put the new entry in the file */
		fseek(hostsfile, 0, SEEK_END); /* In case it wasn't opened append */
		buf_setpos(line, 0);
		buf_setlen(line, 0);
389
		buf_putbytes(line, (const unsigned char *) cli_opts.remotehost, hostlen);
390
		buf_putbyte(line, ' ');
391
		buf_putbytes(line, (const unsigned char *) algoname, algolen);
392
393
394
395
396
397
398
399
400
401
402
		buf_putbyte(line, ' ');
		len = line->size - line->pos;
		/* The only failure with base64 is buffer_overflow, but buf_getwriteptr
		 * will die horribly in the case anyway */
		base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len);
		buf_incrwritepos(line, len);
		buf_putbyte(line, '\n');
		buf_setpos(line, 0);
		fwrite(buf_getptr(line, line->len), line->len, 1, hostsfile);
		/* We ignore errors, since there's not much we can do about them */
	}
Matt Johnston's avatar
Matt Johnston committed
403

404
405
406
407
out:
	if (hostsfile != NULL) {
		fclose(hostsfile);
	}
408
409
410
	if (line != NULL) {
		buf_free(line);
	}
411
	m_free(fingerprint);
412
}