annotate cli-kex.c @ 849:754d7bee1068 ecc

Merge
author Matt Johnston <matt@ucc.asn.au>
date Fri, 08 Nov 2013 23:32:13 +0800
parents 6c69e7df3621 f4bb964c8678
children 7507b174bba0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Dropbear - a SSH2 server
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 *
74
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 59
diff changeset
4 * Copyright (c) 2002-2004 Matt Johnston
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 59
diff changeset
5 * Copyright (c) 2004 by Mihnea Stoenescu
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * All rights reserved.
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 *
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * of this software and associated documentation files (the "Software"), to deal
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * in the Software without restriction, including without limitation the rights
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * copies of the Software, and to permit persons to whom the Software is
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * furnished to do so, subject to the following conditions:
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 *
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * The above copyright notice and this permission notice shall be included in
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * all copies or substantial portions of the Software.
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 *
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * SOFTWARE. */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include "includes.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "session.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "dbutil.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include "algo.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "buffer.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #include "session.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #include "kex.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "ssh.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "packet.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #include "bignum.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include "random.h"
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #include "runopts.h"
33
f789045062e6 Progressing client support
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
38 #include "signkey.h"
761
ac2158e3e403 ecc kind of works, needs fixing/testing
Matt Johnston <matt@ucc.asn.au>
parents: 759
diff changeset
39 #include "ecc.h"
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
42 static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
43 #define MAX_KNOWNHOSTS_LINE 4500
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 void send_msg_kexdh_init() {
739
d44325108d0e first_kex_packet_follows working, needs tidying
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
46 TRACE(("send_msg_kexdh_init()"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 CHECKCLEARTOWRITE();
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_INIT);
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
50 switch (ses.newkeys->algo_kex->mode) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
51 case DROPBEAR_KEX_NORMAL_DH:
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
52 if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
53 || !cli_ses.dh_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
54 if (cli_ses.dh_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
55 free_kexdh_param(cli_ses.dh_param);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
56 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
57 cli_ses.dh_param = gen_kexdh_param();
801
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
58 }
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
59 buf_putmpint(ses.writepayload, &cli_ses.dh_param->pub);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
60 break;
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
61 case DROPBEAR_KEX_ECDH:
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
62 #ifdef DROPBEAR_ECDH
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
63 if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
64 || !cli_ses.ecdh_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
65 if (cli_ses.ecdh_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
66 free_kexecdh_param(cli_ses.ecdh_param);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
67 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
68 cli_ses.ecdh_param = gen_kexecdh_param();
801
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
69 }
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
70 buf_put_ecc_raw_pubkey_string(ses.writepayload, &cli_ses.ecdh_param->key);
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
71 #endif
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
72 break;
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
73 #ifdef DROPBEAR_CURVE25519
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
74 case DROPBEAR_KEX_CURVE25519:
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
75 if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
76 || !cli_ses.curve25519_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
77 if (cli_ses.curve25519_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
78 free_kexcurve25519_param(cli_ses.curve25519_param);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
79 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
80 cli_ses.curve25519_param = gen_kexcurve25519_param();
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
81 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
82 buf_putstring(ses.writepayload, cli_ses.curve25519_param->priv, CURVE25519_LEN);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
83 #endif
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
84 break;
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
85 }
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
86
801
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
87 cli_ses.param_kex_algo = ses.newkeys->algo_kex;
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 encrypt_packet();
775
2f1c199b6e4b requirenext fixup for firstkexfollows
Matt Johnston <matt@ucc.asn.au>
parents: 773
diff changeset
89 ses.requirenext[0] = SSH_MSG_KEXDH_REPLY;
2f1c199b6e4b requirenext fixup for firstkexfollows
Matt Johnston <matt@ucc.asn.au>
parents: 773
diff changeset
90 ses.requirenext[1] = SSH_MSG_KEXINIT;
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 /* Handle a diffie-hellman key exchange reply. */
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 void recv_msg_kexdh_reply() {
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 sign_key *hostkey = NULL;
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
97 unsigned int type, keybloblen;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
98 unsigned char* keyblob = NULL;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
99
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
100 TRACE(("enter recv_msg_kexdh_reply"))
84
29a5c7c62350 default initialisers for mp_ints
Matt Johnston <matt@ucc.asn.au>
parents: 80
diff changeset
101
29a5c7c62350 default initialisers for mp_ints
Matt Johnston <matt@ucc.asn.au>
parents: 80
diff changeset
102 if (cli_ses.kex_state != KEXDH_INIT_SENT) {
29a5c7c62350 default initialisers for mp_ints
Matt Johnston <matt@ucc.asn.au>
parents: 80
diff changeset
103 dropbear_exit("Received out-of-order kexdhreply");
29a5c7c62350 default initialisers for mp_ints
Matt Johnston <matt@ucc.asn.au>
parents: 80
diff changeset
104 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 type = ses.newkeys->algo_hostkey;
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
106 TRACE(("type is %d", type))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 hostkey = new_sign_key();
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
109 keybloblen = buf_getint(ses.payload);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
110
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
111 keyblob = buf_getptr(ses.payload, keybloblen);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
112 if (!ses.kexstate.donefirstkex) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
113 /* Only makes sense the first time */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
114 checkhostkey(keyblob, keybloblen);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
115 }
34
e2a1eaa19f22 Client mostly works up to password auth
Matt Johnston <matt@ucc.asn.au>
parents: 33
diff changeset
116
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 if (buf_get_pub_key(ses.payload, hostkey, &type) != DROPBEAR_SUCCESS) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
118 TRACE(("failed getting pubkey"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 dropbear_exit("Bad KEX packet");
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
122 switch (ses.newkeys->algo_kex->mode) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
123 case DROPBEAR_KEX_NORMAL_DH:
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
124 {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
125 DEF_MP_INT(dh_f);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
126 m_mp_init(&dh_f);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
127 if (buf_getmpint(ses.payload, &dh_f) != DROPBEAR_SUCCESS) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
128 TRACE(("failed getting mpint"))
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
129 dropbear_exit("Bad KEX packet");
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
130 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
132 kexdh_comb_key(cli_ses.dh_param, &dh_f, hostkey);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
133 mp_clear(&dh_f);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
134 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
135 break;
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
136 case DROPBEAR_KEX_ECDH:
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
137 #ifdef DROPBEAR_ECDH
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
138 {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
139 buffer *ecdh_qs = buf_getstringbuf(ses.payload);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
140 kexecdh_comb_key(cli_ses.ecdh_param, ecdh_qs, hostkey);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
141 buf_free(ecdh_qs);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
142 }
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 723
diff changeset
143 #endif
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
144 break;
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
145 #ifdef DROPBEAR_CURVE25519
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
146 case DROPBEAR_KEX_CURVE25519:
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
147 {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
148 buffer *ecdh_qs = buf_getstringbuf(ses.payload);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
149 kexcurve25519_comb_key(cli_ses.curve25519_param, ecdh_qs, hostkey);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
150 buf_free(ecdh_qs);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
151 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
152 #endif
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
153 break;
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
801
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
156 if (cli_ses.dh_param) {
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
157 free_kexdh_param(cli_ses.dh_param);
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
158 cli_ses.dh_param = NULL;
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
159 }
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
160 #ifdef DROPBEAR_ECDH
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
161 if (cli_ses.ecdh_param) {
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
162 free_kexecdh_param(cli_ses.ecdh_param);
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
163 cli_ses.ecdh_param = NULL;
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
164 }
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
165 #endif
848
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
166 #ifdef DROPBEAR_CURVE25519
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
167 if (cli_ses.curve25519_param) {
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
168 free_kexcurve25519_param(cli_ses.curve25519_param);
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
169 cli_ses.curve25519_param = NULL;
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
170 }
6c69e7df3621 curve25519
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
171 #endif
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
801
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
173 cli_ses.param_kex_algo = NULL;
7dcb46da72d9 merge in HEAD
Matt Johnston <matt@ucc.asn.au>
parents: 765 775
diff changeset
174 if (buf_verify(ses.payload, hostkey, ses.hash) != DROPBEAR_SUCCESS) {
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 dropbear_exit("Bad hostkey signature");
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 }
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 sign_key_free(hostkey);
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 hostkey = NULL;
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 send_msg_newkeys();
775
2f1c199b6e4b requirenext fixup for firstkexfollows
Matt Johnston <matt@ucc.asn.au>
parents: 773
diff changeset
182 ses.requirenext[0] = SSH_MSG_NEWKEYS;
2f1c199b6e4b requirenext fixup for firstkexfollows
Matt Johnston <matt@ucc.asn.au>
parents: 773
diff changeset
183 ses.requirenext[1] = 0;
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
184 TRACE(("leave recv_msg_kexdh_init"))
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
186
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
187 static void ask_to_confirm(unsigned char* keyblob, unsigned int keybloblen,
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
188 const char* algoname) {
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
189
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
190 char* fp = NULL;
170
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
191 FILE *tty = NULL;
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
192 char response = 'z';
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
193
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
194 fp = sign_key_fingerprint(keyblob, keybloblen);
418
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
195 if (cli_opts.always_accept_key) {
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
196 fprintf(stderr, "\nHost '%s' key accepted unconditionally.\n(%s fingerprint %s)\n",
418
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
197 cli_opts.remotehost,
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
198 algoname,
418
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
199 fp);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
200 m_free(fp);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
201 return;
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
202 }
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
203 fprintf(stderr, "\nHost '%s' is not in the trusted hosts file.\n(%s fingerprint %s)\nDo you want to continue connecting? (y/n) ",
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
204 cli_opts.remotehost,
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
205 algoname,
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
206 fp);
340
454a34b2dfd1 Fixes from Erik Hovland:
Matt Johnston <matt@ucc.asn.au>
parents: 322
diff changeset
207 m_free(fp);
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
208
170
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
209 tty = fopen(_PATH_TTY, "r");
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
210 if (tty) {
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
211 response = getc(tty);
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
212 fclose(tty);
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
213 } else {
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
214 response = getc(stdin);
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
215 }
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
216
a62cb364f615 Read "y/n" response for fingerprints from /dev/tty directly so that dbclient
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
217 if (response == 'y') {
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
218 return;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
219 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
220
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
221 dropbear_exit("Didn't validate host key");
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
222 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
223
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
224 static FILE* open_known_hosts_file(int * readonly)
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
225 {
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
226 FILE * hostsfile = NULL;
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
227 char * filename = NULL;
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
228 char * homedir = NULL;
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
229
322
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
230 homedir = getenv("HOME");
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
231
322
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
232 if (!homedir) {
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
233 struct passwd * pw = NULL;
322
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
234 pw = getpwuid(getuid());
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
235 if (pw) {
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
236 homedir = pw->pw_dir;
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
237 }
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
238 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
239
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
240 if (homedir) {
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
241 unsigned int len;
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
242 len = strlen(homedir);
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
243 filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
244
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
245 snprintf(filename, len+18, "%s/.ssh", homedir);
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
246 /* Check that ~/.ssh exists - easiest way is just to mkdir */
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
247 if (mkdir(filename, S_IRWXU) != 0) {
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
248 if (errno != EEXIST) {
322
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
249 dropbear_log(LOG_INFO, "Warning: failed creating %s/.ssh: %s",
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
250 homedir, strerror(errno));
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
251 TRACE(("mkdir didn't work: %s", strerror(errno)))
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
252 goto out;
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
253 }
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
254 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
255
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
256 snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
257 hostsfile = fopen(filename, "a+");
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
258
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
259 if (hostsfile != NULL) {
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
260 *readonly = 0;
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
261 fseek(hostsfile, 0, SEEK_SET);
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
262 } else {
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
263 /* We mightn't have been able to open it if it was read-only */
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
264 if (errno == EACCES || errno == EROFS) {
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
265 TRACE(("trying readonly: %s", strerror(errno)))
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
266 *readonly = 1;
318
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
267 hostsfile = fopen(filename, "r");
9916350d7d8b don't fail fatally if the client can't get homedir from getpwuid(), fallback
Matt Johnston <matt@ucc.asn.au>
parents: 170
diff changeset
268 }
106
e13f8a712a1c Fix if the first write fails
Matt Johnston <matt@ucc.asn.au>
parents: 84
diff changeset
269 }
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
270 }
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
271
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
272 if (hostsfile == NULL) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
273 TRACE(("hostsfile didn't open: %s", strerror(errno)))
322
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
274 dropbear_log(LOG_WARNING, "Failed to open %s/.ssh/known_hosts",
84aa4e60bd3c Look at HOME before /etc/passwd when looking for ~/.ssh/known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 318
diff changeset
275 homedir);
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
276 goto out;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
277 }
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
278
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
279 out:
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
280 m_free(filename);
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
281 return hostsfile;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
282 }
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
283
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
284 static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
285
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
286 FILE *hostsfile = NULL;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
287 int readonly = 0;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
288 unsigned int hostlen, algolen;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
289 unsigned long len;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
290 const char *algoname = NULL;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
291 char * fingerprint = NULL;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
292 buffer * line = NULL;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
293 int ret;
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
294
772
7fc0aeada79c -y -y to disable hostkey checking
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
295 if (cli_opts.no_hostkey_check) {
7fc0aeada79c -y -y to disable hostkey checking
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
296 fprintf(stderr, "Caution, skipping hostkey check for %s\n", cli_opts.remotehost);
7fc0aeada79c -y -y to disable hostkey checking
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
297 return;
7fc0aeada79c -y -y to disable hostkey checking
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
298 }
7fc0aeada79c -y -y to disable hostkey checking
Matt Johnston <matt@ucc.asn.au>
parents: 568
diff changeset
299
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
300 algoname = signkey_name_from_type(ses.newkeys->algo_hostkey, &algolen);
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
301
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
302 hostsfile = open_known_hosts_file(&readonly);
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
303 if (!hostsfile) {
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
304 ask_to_confirm(keyblob, keybloblen, algoname);
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
305 /* ask_to_confirm will exit upon failure */
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
306 return;
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
307 }
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
308
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
309 line = buf_new(MAX_KNOWNHOSTS_LINE);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
310 hostlen = strlen(cli_opts.remotehost);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
311
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
312 do {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
313 if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
314 TRACE(("failed reading line: prob EOF"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
315 break;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
316 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
317
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
318 /* The line is too short to be sensible */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
319 /* "30" is 'enough to hold ssh-dss plus the spaces, ie so we don't
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
320 * buf_getfoo() past the end and die horribly - the base64 parsing
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
321 * code is what tiptoes up to the end nicely */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
322 if (line->len < (hostlen+30) ) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
323 TRACE(("line is too short to be sensible"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
324 continue;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
325 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
326
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
327 /* Compare hostnames */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
328 if (strncmp(cli_opts.remotehost, buf_getptr(line, hostlen),
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
329 hostlen) != 0) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
330 continue;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
331 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
332
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
333 buf_incrpos(line, hostlen);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
334 if (buf_getbyte(line) != ' ') {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
335 /* there wasn't a space after the hostname, something dodgy */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
336 TRACE(("missing space afte matching hostname"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
337 continue;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
338 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
339
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
340 if (strncmp(buf_getptr(line, algolen), algoname, algolen) != 0) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
341 TRACE(("algo doesn't match"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
342 continue;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
343 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
344
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
345 buf_incrpos(line, algolen);
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
346 if (buf_getbyte(line) != ' ') {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
347 TRACE(("missing space after algo"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
348 continue;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
349 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
350
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
351 /* Now we're at the interesting hostkey */
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
352 ret = cmp_base64_key(keyblob, keybloblen, algoname, algolen,
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
353 line, &fingerprint);
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
354
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
355 if (ret == DROPBEAR_SUCCESS) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
356 /* Good matching key */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
357 TRACE(("good matching key"))
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
358 goto out;
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
359 }
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
360
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
361 /* The keys didn't match. eep. Note that we're "leaking"
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
362 the fingerprint strings here, but we're exiting anyway */
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
363 dropbear_exit("\n\n%s host key mismatch for %s !\n"
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
364 "Fingerprint is %s\n"
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
365 "Expected %s\n"
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
366 "If you know that the host key is correct you can\nremove the bad entry from ~/.ssh/known_hosts",
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
367 algoname,
436
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
368 cli_opts.remotehost,
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
369 sign_key_fingerprint(keyblob, keybloblen),
7282370416a0 Improve known_hosts checking.
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
370 fingerprint ? fingerprint : "UNKNOWN");
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
371 } while (1); /* keep going 'til something happens */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
372
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
373 /* Key doesn't exist yet */
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 801
diff changeset
374 ask_to_confirm(keyblob, keybloblen, algoname);
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
375
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
376 /* If we get here, they said yes */
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
377
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
378 if (readonly) {
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 106
diff changeset
379 TRACE(("readonly"))
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
380 goto out;
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
381 }
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
382
418
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
383 if (!cli_opts.always_accept_key) {
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
384 /* put the new entry in the file */
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
385 fseek(hostsfile, 0, SEEK_END); /* In case it wasn't opened append */
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
386 buf_setpos(line, 0);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
387 buf_setlen(line, 0);
568
005530560594 Rearrange getaddrstring() etc
Matt Johnston <matt@ucc.asn.au>
parents: 544
diff changeset
388 buf_putbytes(line, cli_opts.remotehost, hostlen);
418
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
389 buf_putbyte(line, ' ');
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
390 buf_putbytes(line, algoname, algolen);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
391 buf_putbyte(line, ' ');
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
392 len = line->size - line->pos;
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
393 /* The only failure with base64 is buffer_overflow, but buf_getwriteptr
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
394 * will die horribly in the case anyway */
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
395 base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
396 buf_incrwritepos(line, len);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
397 buf_putbyte(line, '\n');
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
398 buf_setpos(line, 0);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
399 fwrite(buf_getptr(line, line->len), line->len, 1, hostsfile);
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
400 /* We ignore errors, since there's not much we can do about them */
ab57ba0cb667 Add '-y' option to dbclient to accept the host key without checking
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
401 }
59
bdc97a5719f4 add new entries to known_hosts
Matt Johnston <matt@ucc.asn.au>
parents: 51
diff changeset
402
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
403 out:
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
404 if (hostsfile != NULL) {
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
405 fclose(hostsfile);
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
406 }
79
5a55bd66707f - don't crash when trying to add to known_hosts if it doesn't exist
Matt Johnston <matt@ucc.asn.au>
parents: 74
diff changeset
407 if (line != NULL) {
5a55bd66707f - don't crash when trying to add to known_hosts if it doesn't exist
Matt Johnston <matt@ucc.asn.au>
parents: 74
diff changeset
408 buf_free(line);
5a55bd66707f - don't crash when trying to add to known_hosts if it doesn't exist
Matt Johnston <matt@ucc.asn.au>
parents: 74
diff changeset
409 }
544
9e51707cd6f2 - Make -i and -W pass through multihop arguments
Matt Johnston <matt@ucc.asn.au>
parents: 440
diff changeset
410 m_free(fingerprint);
51
095d689fed16 - Hostkey checking is mostly there, just aren't appending yet.
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
411 }