8df7a775 |
1 | #include <windows.h> |
374330e2 |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
fb09bf1c |
4 | #include <stdarg.h> |
5 | #include <assert.h> |
374330e2 |
6 | |
7 | #include "putty.h" |
dacbd0e8 |
8 | #include "tree234.h" |
fb09bf1c |
9 | #include "ssh.h" |
374330e2 |
10 | |
11 | #ifndef FALSE |
12 | #define FALSE 0 |
13 | #endif |
14 | #ifndef TRUE |
15 | #define TRUE 1 |
16 | #endif |
17 | |
fb09bf1c |
18 | #define logevent(s) { logevent(s); \ |
67779be7 |
19 | if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) \ |
fb09bf1c |
20 | fprintf(stderr, "%s\n", s); } |
21 | |
3cd52910 |
22 | #define bombout(msg) ( ssh_state = SSH_STATE_CLOSED, \ |
4a8fc3c4 |
23 | (s ? sk_close(s), s = NULL : 0), \ |
3cd52910 |
24 | connection_fatal msg ) |
8d5de777 |
25 | |
d211621f |
26 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
27 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
28 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
29 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
30 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
31 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
32 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
33 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
34 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
35 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
36 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
37 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
38 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
39 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
40 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
41 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
42 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
43 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
44 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
45 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
46 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
47 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
48 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
49 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
9c964e85 |
50 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
51 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
52 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
d211621f |
53 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
54 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
d211621f |
55 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
9c964e85 |
56 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
57 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
58 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
d211621f |
59 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
4ba9b64b |
60 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
d211621f |
61 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
62 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
63 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
64 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
65 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
66 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
67 | |
68 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
69 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
70 | |
b96dc54c |
71 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
72 | /* Mask for protoflags we will echo back to server if seen */ |
73 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
74 | |
d211621f |
75 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
76 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
77 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
78 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
79 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
80 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
81 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
82 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
83 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
84 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
a92dd380 |
85 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
86 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
87 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
88 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
d211621f |
89 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
90 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
91 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
92 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
93 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
94 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
95 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
96 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
97 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
98 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
99 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
100 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
101 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
102 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
103 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
104 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
105 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
106 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
107 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
108 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
109 | |
9005f3ba |
110 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
111 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
112 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
113 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
114 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
115 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
116 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
117 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
118 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
119 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
120 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
121 | |
d211621f |
122 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
123 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
124 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
125 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
126 | |
127 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
128 | |
7d503c31 |
129 | /* |
130 | * Various remote-bug flags. |
131 | */ |
132 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
133 | #define BUG_SSH2_HMAC 2 |
134 | |
fb09bf1c |
135 | #define GET_32BIT(cp) \ |
136 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
137 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
138 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
139 | ((unsigned long)(unsigned char)(cp)[3])) |
140 | |
141 | #define PUT_32BIT(cp, value) { \ |
142 | (cp)[0] = (unsigned char)((value) >> 24); \ |
143 | (cp)[1] = (unsigned char)((value) >> 16); \ |
144 | (cp)[2] = (unsigned char)((value) >> 8); \ |
145 | (cp)[3] = (unsigned char)(value); } |
146 | |
7cca0d81 |
147 | enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM }; |
972a41c8 |
148 | |
374330e2 |
149 | /* Coroutine mechanics for the sillier bits of the code */ |
150 | #define crBegin1 static int crLine = 0; |
151 | #define crBegin2 switch(crLine) { case 0:; |
152 | #define crBegin crBegin1; crBegin2; |
153 | #define crFinish(z) } crLine = 0; return (z) |
154 | #define crFinishV } crLine = 0; return |
155 | #define crReturn(z) \ |
156 | do {\ |
157 | crLine=__LINE__; return (z); case __LINE__:;\ |
158 | } while (0) |
159 | #define crReturnV \ |
160 | do {\ |
161 | crLine=__LINE__; return; case __LINE__:;\ |
162 | } while (0) |
163 | #define crStop(z) do{ crLine = 0; return (z); }while(0) |
164 | #define crStopV do{ crLine = 0; return; }while(0) |
fb09bf1c |
165 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
166 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
167 | |
0c6c9a70 |
168 | extern char *x11_init (Socket *, char *, void *); |
9c964e85 |
169 | extern void x11_close (Socket); |
170 | extern void x11_send (Socket , char *, int); |
171 | extern void x11_invent_auth(char *, int, char *, int); |
172 | |
5e8358ad |
173 | /* |
174 | * Ciphers for SSH2. We miss out single-DES because it isn't |
175 | * supported; also 3DES and Blowfish are both done differently from |
176 | * SSH1. (3DES uses outer chaining; Blowfish has the opposite |
177 | * endianness and different-sized keys.) |
5e8358ad |
178 | */ |
0a3f1d48 |
179 | const static struct ssh2_ciphers *ciphers[] = { |
180 | &ssh2_aes, |
181 | &ssh2_blowfish, |
182 | &ssh2_3des, |
8479e33c |
183 | }; |
e5574168 |
184 | |
a92dd380 |
185 | const static struct ssh_kex *kex_algs[] = { |
a92dd380 |
186 | &ssh_diffiehellman_gex, |
a92dd380 |
187 | &ssh_diffiehellman }; |
e5574168 |
188 | |
85cc02bb |
189 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
190 | |
d39f364a |
191 | static void nullmac_key(unsigned char *key) { } |
e5574168 |
192 | static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { } |
193 | static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; } |
57476f6b |
194 | const static struct ssh_mac ssh_mac_none = { |
d39f364a |
195 | nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0 |
e5574168 |
196 | }; |
8b2715b2 |
197 | const static struct ssh_mac *macs[] = { |
198 | &ssh_sha1, &ssh_md5, &ssh_mac_none }; |
199 | const static struct ssh_mac *buggymacs[] = { |
200 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none }; |
e5574168 |
201 | |
4ba9b64b |
202 | static void ssh_comp_none_init(void) { } |
203 | static int ssh_comp_none_block(unsigned char *block, int len, |
204 | unsigned char **outblock, int *outlen) { |
205 | return 0; |
206 | } |
63b00c83 |
207 | static int ssh_comp_none_disable(void) { return 0; } |
57476f6b |
208 | const static struct ssh_compress ssh_comp_none = { |
4ba9b64b |
209 | "none", |
210 | ssh_comp_none_init, ssh_comp_none_block, |
63b00c83 |
211 | ssh_comp_none_init, ssh_comp_none_block, |
212 | ssh_comp_none_disable |
e5574168 |
213 | }; |
4ba9b64b |
214 | extern const struct ssh_compress ssh_zlib; |
215 | const static struct ssh_compress *compressions[] = { |
216 | &ssh_zlib, &ssh_comp_none }; |
374330e2 |
217 | |
783415f8 |
218 | enum { /* channel types */ |
219 | CHAN_MAINSESSION, |
220 | CHAN_X11, |
221 | CHAN_AGENT, |
222 | }; |
223 | |
dacbd0e8 |
224 | /* |
225 | * 2-3-4 tree storing channels. |
226 | */ |
227 | struct ssh_channel { |
d211621f |
228 | unsigned remoteid, localid; |
dacbd0e8 |
229 | int type; |
230 | int closes; |
783415f8 |
231 | struct ssh2_data_channel { |
232 | unsigned char *outbuffer; |
233 | unsigned outbuflen, outbufsize; |
234 | unsigned remwindow, remmaxpkt; |
235 | } v2; |
dacbd0e8 |
236 | union { |
237 | struct ssh_agent_channel { |
238 | unsigned char *message; |
239 | unsigned char msglen[4]; |
240 | int lensofar, totallen; |
241 | } a; |
9c964e85 |
242 | struct ssh_x11_channel { |
243 | Socket s; |
783415f8 |
244 | } x11; |
dacbd0e8 |
245 | } u; |
246 | }; |
57476f6b |
247 | |
248 | struct Packet { |
249 | long length; |
250 | int type; |
251 | unsigned char *data; |
252 | unsigned char *body; |
253 | long savedpos; |
254 | long maxlen; |
255 | }; |
256 | |
0db56f73 |
257 | static SHA_State exhash, exhashbase; |
57476f6b |
258 | |
8df7a775 |
259 | static Socket s = NULL; |
57476f6b |
260 | |
261 | static unsigned char session_key[32]; |
4ba9b64b |
262 | static int ssh1_compressing; |
b96dc54c |
263 | static int ssh1_remote_protoflags; |
264 | static int ssh1_local_protoflags; |
db7d555c |
265 | static int ssh_agentfwd_enabled; |
9c964e85 |
266 | static int ssh_X11_fwd_enabled; |
7d503c31 |
267 | static int ssh_remote_bugs; |
57476f6b |
268 | static const struct ssh_cipher *cipher = NULL; |
0a3f1d48 |
269 | static const struct ssh2_cipher *cscipher = NULL; |
270 | static const struct ssh2_cipher *sccipher = NULL; |
57476f6b |
271 | static const struct ssh_mac *csmac = NULL; |
272 | static const struct ssh_mac *scmac = NULL; |
273 | static const struct ssh_compress *cscomp = NULL; |
274 | static const struct ssh_compress *sccomp = NULL; |
275 | static const struct ssh_kex *kex = NULL; |
e055a386 |
276 | static const struct ssh_signkey *hostkey = NULL; |
65a22376 |
277 | static unsigned char ssh2_session_id[20]; |
fa17a66e |
278 | int (*ssh_get_line)(const char *prompt, char *str, int maxlen, |
279 | int is_pw) = NULL; |
57476f6b |
280 | |
281 | static char *savedhost; |
282 | static int savedport; |
283 | static int ssh_send_ok; |
0965bee0 |
284 | static int ssh_echoing, ssh_editing; |
57476f6b |
285 | |
dacbd0e8 |
286 | static tree234 *ssh_channels; /* indexed by local id */ |
57476f6b |
287 | static struct ssh_channel *mainchan; /* primary session channel */ |
288 | |
289 | static enum { |
3687d221 |
290 | SSH_STATE_PREPACKET, |
57476f6b |
291 | SSH_STATE_BEFORE_SIZE, |
292 | SSH_STATE_INTERMED, |
293 | SSH_STATE_SESSION, |
294 | SSH_STATE_CLOSED |
3687d221 |
295 | } ssh_state = SSH_STATE_PREPACKET; |
57476f6b |
296 | |
3687d221 |
297 | static int size_needed = FALSE, eof_needed = FALSE; |
57476f6b |
298 | |
299 | static struct Packet pktin = { 0, 0, NULL, NULL, 0 }; |
300 | static struct Packet pktout = { 0, 0, NULL, NULL, 0 }; |
b185170a |
301 | static unsigned char *deferred_send_data = NULL; |
302 | static int deferred_len = 0, deferred_size = 0; |
57476f6b |
303 | |
304 | static int ssh_version; |
305 | static void (*ssh_protocol)(unsigned char *in, int inlen, int ispkt); |
306 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt); |
307 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt); |
308 | static void ssh_size(void); |
3687d221 |
309 | static void ssh_special (Telnet_Special); |
783415f8 |
310 | static void ssh2_try_send(struct ssh_channel *c); |
311 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
57476f6b |
312 | |
313 | static int (*s_rdpkt)(unsigned char **data, int *datalen); |
314 | |
315 | static struct rdpkt1_state_tag { |
316 | long len, pad, biglen, to_read; |
317 | unsigned long realcrc, gotcrc; |
318 | unsigned char *p; |
319 | int i; |
320 | int chunk; |
321 | } rdpkt1_state; |
322 | |
960e736a |
323 | static struct rdpkt2_state_tag { |
324 | long len, pad, payload, packetlen, maclen; |
325 | int i; |
326 | int cipherblk; |
327 | unsigned long incoming_sequence; |
328 | } rdpkt2_state; |
329 | |
dacbd0e8 |
330 | static int ssh_channelcmp(void *av, void *bv) { |
331 | struct ssh_channel *a = (struct ssh_channel *)av; |
332 | struct ssh_channel *b = (struct ssh_channel *)bv; |
333 | if (a->localid < b->localid) return -1; |
334 | if (a->localid > b->localid) return +1; |
335 | return 0; |
336 | } |
337 | static int ssh_channelfind(void *av, void *bv) { |
d211621f |
338 | unsigned *a = (unsigned *)av; |
dacbd0e8 |
339 | struct ssh_channel *b = (struct ssh_channel *)bv; |
340 | if (*a < b->localid) return -1; |
341 | if (*a > b->localid) return +1; |
342 | return 0; |
343 | } |
344 | |
374330e2 |
345 | static void c_write (char *buf, int len) { |
67779be7 |
346 | if ((flags & FLAG_STDERR)) { |
4017be6d |
347 | int i; |
348 | for (i = 0; i < len; i++) |
349 | if (buf[i] != '\r') |
350 | fputc(buf[i], stderr); |
fb09bf1c |
351 | return; |
352 | } |
fe50e814 |
353 | from_backend(1, buf, len); |
3bdaf79d |
354 | } |
355 | |
a209e957 |
356 | static void c_write_untrusted(char *buf, int len) { |
357 | int i; |
358 | for (i = 0; i < len; i++) { |
359 | if (buf[i] == '\n') |
360 | c_write("\r\n", 2); |
361 | else if ((buf[i] & 0x60) || (buf[i] == '\r')) |
362 | c_write(buf+i, 1); |
363 | } |
364 | } |
365 | |
1408a877 |
366 | static void c_write_str (char *buf) { |
367 | c_write(buf, strlen(buf)); |
368 | } |
369 | |
fb09bf1c |
370 | /* |
371 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
372 | * Decipher and verify the packet when it is completely read. |
373 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
374 | * Update the *data and *datalen variables. |
375 | * Return the additional nr of bytes needed, or 0 when |
376 | * a complete packet is available. |
377 | */ |
e5574168 |
378 | static int ssh1_rdpkt(unsigned char **data, int *datalen) |
fb09bf1c |
379 | { |
57476f6b |
380 | struct rdpkt1_state_tag *st = &rdpkt1_state; |
374330e2 |
381 | |
382 | crBegin; |
374330e2 |
383 | |
fb09bf1c |
384 | next_packet: |
37508af4 |
385 | |
fb09bf1c |
386 | pktin.type = 0; |
387 | pktin.length = 0; |
374330e2 |
388 | |
57476f6b |
389 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
390 | while ((*datalen) == 0) |
57476f6b |
391 | crReturn(4-st->i); |
392 | st->len = (st->len << 8) + **data; |
fb09bf1c |
393 | (*data)++, (*datalen)--; |
394 | } |
374330e2 |
395 | |
fb09bf1c |
396 | #ifdef FWHACK |
57476f6b |
397 | if (st->len == 0x52656d6f) { /* "Remo"te server has closed ... */ |
398 | st->len = 0x300; /* big enough to carry to end */ |
fb09bf1c |
399 | } |
400 | #endif |
374330e2 |
401 | |
57476f6b |
402 | st->pad = 8 - (st->len % 8); |
403 | st->biglen = st->len + st->pad; |
404 | pktin.length = st->len - 5; |
fb09bf1c |
405 | |
57476f6b |
406 | if (pktin.maxlen < st->biglen) { |
407 | pktin.maxlen = st->biglen; |
dcbde236 |
408 | pktin.data = (pktin.data == NULL ? smalloc(st->biglen+APIEXTRA) : |
409 | srealloc(pktin.data, st->biglen+APIEXTRA)); |
fb09bf1c |
410 | if (!pktin.data) |
411 | fatalbox("Out of memory"); |
412 | } |
374330e2 |
413 | |
57476f6b |
414 | st->to_read = st->biglen; |
415 | st->p = pktin.data; |
416 | while (st->to_read > 0) { |
417 | st->chunk = st->to_read; |
fb09bf1c |
418 | while ((*datalen) == 0) |
57476f6b |
419 | crReturn(st->to_read); |
420 | if (st->chunk > (*datalen)) |
421 | st->chunk = (*datalen); |
422 | memcpy(st->p, *data, st->chunk); |
423 | *data += st->chunk; |
424 | *datalen -= st->chunk; |
425 | st->p += st->chunk; |
426 | st->to_read -= st->chunk; |
fb09bf1c |
427 | } |
374330e2 |
428 | |
fb09bf1c |
429 | if (cipher) |
57476f6b |
430 | cipher->decrypt(pktin.data, st->biglen); |
698a5108 |
431 | #if 0 |
432 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
433 | for (st->i = 0; st->i < st->biglen; st->i++) |
434 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
435 | debug(("\r\n")); |
436 | #endif |
374330e2 |
437 | |
57476f6b |
438 | st->realcrc = crc32(pktin.data, st->biglen-4); |
439 | st->gotcrc = GET_32BIT(pktin.data+st->biglen-4); |
440 | if (st->gotcrc != st->realcrc) { |
8d5de777 |
441 | bombout(("Incorrect CRC received on packet")); |
442 | crReturn(0); |
fb09bf1c |
443 | } |
572f871e |
444 | |
4ba9b64b |
445 | pktin.body = pktin.data + st->pad + 1; |
446 | |
447 | if (ssh1_compressing) { |
448 | unsigned char *decompblk; |
449 | int decomplen; |
450 | #if 0 |
b96dc54c |
451 | { |
452 | int i; |
453 | debug(("Packet payload pre-decompression:\n")); |
454 | for (i = -1; i < pktin.length; i++) |
455 | debug((" %02x", (unsigned char)pktin.body[i])); |
456 | debug(("\r\n")); |
457 | } |
4ba9b64b |
458 | #endif |
459 | zlib_decompress_block(pktin.body-1, pktin.length+1, |
460 | &decompblk, &decomplen); |
461 | |
462 | if (pktin.maxlen < st->pad + decomplen) { |
463 | pktin.maxlen = st->pad + decomplen; |
dcbde236 |
464 | pktin.data = srealloc(pktin.data, pktin.maxlen+APIEXTRA); |
58298665 |
465 | pktin.body = pktin.data + st->pad + 1; |
4ba9b64b |
466 | if (!pktin.data) |
467 | fatalbox("Out of memory"); |
468 | } |
469 | |
470 | memcpy(pktin.body-1, decompblk, decomplen); |
dcbde236 |
471 | sfree(decompblk); |
4ba9b64b |
472 | pktin.length = decomplen-1; |
473 | #if 0 |
b96dc54c |
474 | { |
475 | int i; |
476 | debug(("Packet payload post-decompression:\n")); |
477 | for (i = -1; i < pktin.length; i++) |
478 | debug((" %02x", (unsigned char)pktin.body[i])); |
479 | debug(("\r\n")); |
480 | } |
4ba9b64b |
481 | #endif |
482 | } |
483 | |
e5574168 |
484 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
485 | pktin.type == SSH1_SMSG_STDERR_DATA || |
486 | pktin.type == SSH1_MSG_DEBUG || |
18515c51 |
487 | pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE || |
488 | pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
fb09bf1c |
489 | long strlen = GET_32BIT(pktin.body); |
8d5de777 |
490 | if (strlen + 4 != pktin.length) { |
491 | bombout(("Received data packet with bogus string length")); |
492 | crReturn(0); |
493 | } |
fb09bf1c |
494 | } |
495 | |
4ba9b64b |
496 | pktin.type = pktin.body[-1]; |
497 | |
e5574168 |
498 | if (pktin.type == SSH1_MSG_DEBUG) { |
fb09bf1c |
499 | /* log debug message */ |
500 | char buf[80]; |
501 | int strlen = GET_32BIT(pktin.body); |
502 | strcpy(buf, "Remote: "); |
503 | if (strlen > 70) strlen = 70; |
504 | memcpy(buf+8, pktin.body+4, strlen); |
505 | buf[8+strlen] = '\0'; |
506 | logevent(buf); |
507 | goto next_packet; |
e5574168 |
508 | } else if (pktin.type == SSH1_MSG_IGNORE) { |
fb09bf1c |
509 | /* do nothing */ |
510 | goto next_packet; |
511 | } |
512 | |
513 | crFinish(0); |
514 | } |
515 | |
e5574168 |
516 | static int ssh2_rdpkt(unsigned char **data, int *datalen) |
517 | { |
960e736a |
518 | struct rdpkt2_state_tag *st = &rdpkt2_state; |
e5574168 |
519 | |
520 | crBegin; |
521 | |
522 | next_packet: |
e5574168 |
523 | pktin.type = 0; |
524 | pktin.length = 0; |
960e736a |
525 | if (sccipher) |
526 | st->cipherblk = sccipher->blksize; |
e5574168 |
527 | else |
960e736a |
528 | st->cipherblk = 8; |
529 | if (st->cipherblk < 8) |
530 | st->cipherblk = 8; |
531 | |
532 | if (pktin.maxlen < st->cipherblk) { |
533 | pktin.maxlen = st->cipherblk; |
dcbde236 |
534 | pktin.data = (pktin.data == NULL ? smalloc(st->cipherblk+APIEXTRA) : |
535 | srealloc(pktin.data, st->cipherblk+APIEXTRA)); |
e5574168 |
536 | if (!pktin.data) |
537 | fatalbox("Out of memory"); |
538 | } |
539 | |
540 | /* |
541 | * Acquire and decrypt the first block of the packet. This will |
542 | * contain the length and padding details. |
543 | */ |
960e736a |
544 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
545 | while ((*datalen) == 0) |
960e736a |
546 | crReturn(st->cipherblk-st->i); |
547 | pktin.data[st->i] = *(*data)++; |
e5574168 |
548 | (*datalen)--; |
549 | } |
550 | #ifdef FWHACK |
551 | if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */ |
552 | /* FIXME */ |
553 | } |
554 | #endif |
555 | if (sccipher) |
960e736a |
556 | sccipher->decrypt(pktin.data, st->cipherblk); |
e5574168 |
557 | |
558 | /* |
559 | * Now get the length and padding figures. |
560 | */ |
960e736a |
561 | st->len = GET_32BIT(pktin.data); |
562 | st->pad = pktin.data[4]; |
e5574168 |
563 | |
564 | /* |
565 | * This enables us to deduce the payload length. |
566 | */ |
960e736a |
567 | st->payload = st->len - st->pad - 1; |
e5574168 |
568 | |
960e736a |
569 | pktin.length = st->payload + 5; |
e5574168 |
570 | |
571 | /* |
572 | * So now we can work out the total packet length. |
573 | */ |
960e736a |
574 | st->packetlen = st->len + 4; |
575 | st->maclen = scmac ? scmac->len : 0; |
e5574168 |
576 | |
577 | /* |
578 | * Adjust memory allocation if packet is too big. |
579 | */ |
960e736a |
580 | if (pktin.maxlen < st->packetlen+st->maclen) { |
581 | pktin.maxlen = st->packetlen+st->maclen; |
dcbde236 |
582 | pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) : |
583 | srealloc(pktin.data, pktin.maxlen+APIEXTRA)); |
e5574168 |
584 | if (!pktin.data) |
585 | fatalbox("Out of memory"); |
586 | } |
587 | |
588 | /* |
589 | * Read and decrypt the remainder of the packet. |
590 | */ |
960e736a |
591 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) { |
e5574168 |
592 | while ((*datalen) == 0) |
960e736a |
593 | crReturn(st->packetlen + st->maclen - st->i); |
594 | pktin.data[st->i] = *(*data)++; |
e5574168 |
595 | (*datalen)--; |
596 | } |
597 | /* Decrypt everything _except_ the MAC. */ |
598 | if (sccipher) |
960e736a |
599 | sccipher->decrypt(pktin.data + st->cipherblk, |
600 | st->packetlen - st->cipherblk); |
e5574168 |
601 | |
7cca0d81 |
602 | #if 0 |
960e736a |
603 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
604 | for (st->i = 0; st->i < st->packetlen; st->i++) |
605 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
d39f364a |
606 | debug(("\r\n")); |
7cca0d81 |
607 | #endif |
d39f364a |
608 | |
e5574168 |
609 | /* |
610 | * Check the MAC. |
611 | */ |
960e736a |
612 | if (scmac && !scmac->verify(pktin.data, st->len+4, st->incoming_sequence)) { |
8d5de777 |
613 | bombout(("Incorrect MAC received on packet")); |
614 | crReturn(0); |
615 | } |
960e736a |
616 | st->incoming_sequence++; /* whether or not we MACed */ |
e5574168 |
617 | |
4ba9b64b |
618 | /* |
619 | * Decompress packet payload. |
620 | */ |
621 | { |
622 | unsigned char *newpayload; |
623 | int newlen; |
624 | if (sccomp && sccomp->decompress(pktin.data+5, pktin.length-5, |
625 | &newpayload, &newlen)) { |
626 | if (pktin.maxlen < newlen+5) { |
627 | pktin.maxlen = newlen+5; |
dcbde236 |
628 | pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) : |
629 | srealloc(pktin.data, pktin.maxlen+APIEXTRA)); |
4ba9b64b |
630 | if (!pktin.data) |
631 | fatalbox("Out of memory"); |
632 | } |
633 | pktin.length = 5 + newlen; |
634 | memcpy(pktin.data+5, newpayload, newlen); |
635 | #if 0 |
636 | debug(("Post-decompression payload:\r\n")); |
637 | for (st->i = 0; st->i < newlen; st->i++) |
638 | debug((" %02x", (unsigned char)pktin.data[5+st->i])); |
639 | debug(("\r\n")); |
640 | #endif |
641 | |
dcbde236 |
642 | sfree(newpayload); |
4ba9b64b |
643 | } |
644 | } |
645 | |
e5574168 |
646 | pktin.savedpos = 6; |
647 | pktin.type = pktin.data[5]; |
e5574168 |
648 | |
7cca0d81 |
649 | if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG) |
650 | goto next_packet; /* FIXME: print DEBUG message */ |
e5574168 |
651 | |
652 | crFinish(0); |
653 | } |
654 | |
4ba9b64b |
655 | static void ssh1_pktout_size(int len) { |
374330e2 |
656 | int pad, biglen; |
657 | |
658 | len += 5; /* type and CRC */ |
659 | pad = 8 - (len%8); |
660 | biglen = len + pad; |
661 | |
662 | pktout.length = len-5; |
663 | if (pktout.maxlen < biglen) { |
664 | pktout.maxlen = biglen; |
8f203108 |
665 | #ifdef MSCRYPTOAPI |
fb09bf1c |
666 | /* Allocate enough buffer space for extra block |
8f203108 |
667 | * for MS CryptEncrypt() */ |
dcbde236 |
668 | pktout.data = (pktout.data == NULL ? smalloc(biglen+12) : |
669 | srealloc(pktout.data, biglen+12)); |
8f203108 |
670 | #else |
dcbde236 |
671 | pktout.data = (pktout.data == NULL ? smalloc(biglen+4) : |
672 | srealloc(pktout.data, biglen+4)); |
8f203108 |
673 | #endif |
374330e2 |
674 | if (!pktout.data) |
675 | fatalbox("Out of memory"); |
676 | } |
4ba9b64b |
677 | pktout.body = pktout.data+4+pad+1; |
678 | } |
374330e2 |
679 | |
4ba9b64b |
680 | static void s_wrpkt_start(int type, int len) { |
681 | ssh1_pktout_size(len); |
374330e2 |
682 | pktout.type = type; |
374330e2 |
683 | } |
684 | |
39065bed |
685 | static int s_wrpkt_prepare(void) { |
374330e2 |
686 | int pad, len, biglen, i; |
687 | unsigned long crc; |
688 | |
4ba9b64b |
689 | pktout.body[-1] = pktout.type; |
690 | |
7d503c31 |
691 | #if 0 |
692 | debug(("Packet payload pre-compression:\n")); |
693 | for (i = -1; i < pktout.length; i++) |
694 | debug((" %02x", (unsigned char)pktout.body[i])); |
695 | debug(("\r\n")); |
696 | #endif |
697 | |
4ba9b64b |
698 | if (ssh1_compressing) { |
699 | unsigned char *compblk; |
700 | int complen; |
4ba9b64b |
701 | zlib_compress_block(pktout.body-1, pktout.length+1, |
702 | &compblk, &complen); |
703 | ssh1_pktout_size(complen-1); |
704 | memcpy(pktout.body-1, compblk, complen); |
dcbde236 |
705 | sfree(compblk); |
4ba9b64b |
706 | #if 0 |
707 | debug(("Packet payload post-compression:\n")); |
708 | for (i = -1; i < pktout.length; i++) |
709 | debug((" %02x", (unsigned char)pktout.body[i])); |
710 | debug(("\r\n")); |
711 | #endif |
712 | } |
713 | |
374330e2 |
714 | len = pktout.length + 5; /* type and CRC */ |
715 | pad = 8 - (len%8); |
716 | biglen = len + pad; |
717 | |
374330e2 |
718 | for (i=0; i<pad; i++) |
719 | pktout.data[i+4] = random_byte(); |
720 | crc = crc32(pktout.data+4, biglen-4); |
fb09bf1c |
721 | PUT_32BIT(pktout.data+biglen, crc); |
722 | PUT_32BIT(pktout.data, len); |
374330e2 |
723 | |
698a5108 |
724 | #if 0 |
725 | debug(("Sending packet len=%d\r\n", biglen+4)); |
726 | for (i = 0; i < biglen+4; i++) |
727 | debug((" %02x", (unsigned char)pktout.data[i])); |
728 | debug(("\r\n")); |
729 | #endif |
374330e2 |
730 | if (cipher) |
731 | cipher->encrypt(pktout.data+4, biglen); |
732 | |
39065bed |
733 | return biglen+4; |
734 | } |
735 | |
736 | static void s_wrpkt(void) { |
737 | int len; |
738 | len = s_wrpkt_prepare(); |
739 | sk_write(s, pktout.data, len); |
740 | } |
741 | |
742 | static void s_wrpkt_defer(void) { |
743 | int len; |
744 | len = s_wrpkt_prepare(); |
745 | if (deferred_len + len > deferred_size) { |
746 | deferred_size = deferred_len + len + 128; |
747 | deferred_send_data = srealloc(deferred_send_data, deferred_size); |
748 | } |
749 | memcpy(deferred_send_data+deferred_len, pktout.data, len); |
750 | deferred_len += len; |
374330e2 |
751 | } |
752 | |
fb09bf1c |
753 | /* |
39065bed |
754 | * Construct a packet with the specified contents. |
fb09bf1c |
755 | */ |
39065bed |
756 | static void construct_packet(int pkttype, va_list ap1, va_list ap2) |
fb09bf1c |
757 | { |
fb09bf1c |
758 | unsigned char *p, *argp, argchar; |
759 | unsigned long argint; |
760 | int pktlen, argtype, arglen; |
7cca0d81 |
761 | Bignum bn; |
fb09bf1c |
762 | |
763 | pktlen = 0; |
39065bed |
764 | while ((argtype = va_arg(ap1, int)) != PKT_END) { |
fb09bf1c |
765 | switch (argtype) { |
766 | case PKT_INT: |
39065bed |
767 | (void) va_arg(ap1, int); |
fb09bf1c |
768 | pktlen += 4; |
769 | break; |
770 | case PKT_CHAR: |
39065bed |
771 | (void) va_arg(ap1, char); |
fb09bf1c |
772 | pktlen++; |
773 | break; |
774 | case PKT_DATA: |
39065bed |
775 | (void) va_arg(ap1, unsigned char *); |
776 | arglen = va_arg(ap1, int); |
fb09bf1c |
777 | pktlen += arglen; |
778 | break; |
779 | case PKT_STR: |
39065bed |
780 | argp = va_arg(ap1, unsigned char *); |
fb09bf1c |
781 | arglen = strlen(argp); |
782 | pktlen += 4 + arglen; |
783 | break; |
7cca0d81 |
784 | case PKT_BIGNUM: |
39065bed |
785 | bn = va_arg(ap1, Bignum); |
5c58ad2d |
786 | pktlen += ssh1_bignum_length(bn); |
7cca0d81 |
787 | break; |
fb09bf1c |
788 | default: |
789 | assert(0); |
790 | } |
791 | } |
fb09bf1c |
792 | |
793 | s_wrpkt_start(pkttype, pktlen); |
794 | p = pktout.body; |
795 | |
39065bed |
796 | while ((argtype = va_arg(ap2, int)) != PKT_END) { |
fb09bf1c |
797 | switch (argtype) { |
798 | case PKT_INT: |
39065bed |
799 | argint = va_arg(ap2, int); |
fb09bf1c |
800 | PUT_32BIT(p, argint); |
801 | p += 4; |
802 | break; |
803 | case PKT_CHAR: |
39065bed |
804 | argchar = va_arg(ap2, unsigned char); |
fb09bf1c |
805 | *p = argchar; |
806 | p++; |
807 | break; |
808 | case PKT_DATA: |
39065bed |
809 | argp = va_arg(ap2, unsigned char *); |
810 | arglen = va_arg(ap2, int); |
fb09bf1c |
811 | memcpy(p, argp, arglen); |
812 | p += arglen; |
813 | break; |
814 | case PKT_STR: |
39065bed |
815 | argp = va_arg(ap2, unsigned char *); |
fb09bf1c |
816 | arglen = strlen(argp); |
817 | PUT_32BIT(p, arglen); |
818 | memcpy(p + 4, argp, arglen); |
819 | p += 4 + arglen; |
820 | break; |
7cca0d81 |
821 | case PKT_BIGNUM: |
39065bed |
822 | bn = va_arg(ap2, Bignum); |
5c58ad2d |
823 | p += ssh1_write_bignum(p, bn); |
7cca0d81 |
824 | break; |
fb09bf1c |
825 | } |
826 | } |
39065bed |
827 | } |
fb09bf1c |
828 | |
39065bed |
829 | static void send_packet(int pkttype, ...) { |
830 | va_list ap1, ap2; |
831 | va_start(ap1, pkttype); |
832 | va_start(ap2, pkttype); |
833 | construct_packet(pkttype, ap1, ap2); |
fb09bf1c |
834 | s_wrpkt(); |
835 | } |
836 | |
39065bed |
837 | static void defer_packet(int pkttype, ...) { |
838 | va_list ap1, ap2; |
839 | va_start(ap1, pkttype); |
840 | va_start(ap2, pkttype); |
841 | construct_packet(pkttype, ap1, ap2); |
842 | s_wrpkt_defer(); |
843 | } |
844 | |
9697bfd2 |
845 | static int ssh_versioncmp(char *a, char *b) { |
846 | char *ae, *be; |
847 | unsigned long av, bv; |
848 | |
43aa02a7 |
849 | av = strtoul(a, &ae, 10); |
850 | bv = strtoul(b, &be, 10); |
9697bfd2 |
851 | if (av != bv) return (av < bv ? -1 : +1); |
852 | if (*ae == '.') ae++; |
853 | if (*be == '.') be++; |
43aa02a7 |
854 | av = strtoul(ae, &ae, 10); |
855 | bv = strtoul(be, &be, 10); |
9697bfd2 |
856 | if (av != bv) return (av < bv ? -1 : +1); |
857 | return 0; |
858 | } |
859 | |
e5574168 |
860 | |
861 | /* |
a92dd380 |
862 | * Utility routines for putting an SSH-protocol `string' and |
863 | * `uint32' into a SHA state. |
e5574168 |
864 | */ |
865 | #include <stdio.h> |
75cab814 |
866 | static void sha_string(SHA_State *s, void *str, int len) { |
e5574168 |
867 | unsigned char lenblk[4]; |
e5574168 |
868 | PUT_32BIT(lenblk, len); |
e5574168 |
869 | SHA_Bytes(s, lenblk, 4); |
e5574168 |
870 | SHA_Bytes(s, str, len); |
871 | } |
872 | |
a92dd380 |
873 | static void sha_uint32(SHA_State *s, unsigned i) { |
874 | unsigned char intblk[4]; |
875 | PUT_32BIT(intblk, i); |
876 | SHA_Bytes(s, intblk, 4); |
877 | } |
878 | |
7cca0d81 |
879 | /* |
880 | * SSH2 packet construction functions. |
881 | */ |
783415f8 |
882 | static void ssh2_pkt_ensure(int length) { |
883 | if (pktout.maxlen < length) { |
884 | pktout.maxlen = length + 256; |
dcbde236 |
885 | pktout.data = (pktout.data == NULL ? smalloc(pktout.maxlen+APIEXTRA) : |
886 | srealloc(pktout.data, pktout.maxlen+APIEXTRA)); |
7cca0d81 |
887 | if (!pktout.data) |
888 | fatalbox("Out of memory"); |
889 | } |
783415f8 |
890 | } |
891 | static void ssh2_pkt_adddata(void *data, int len) { |
892 | pktout.length += len; |
893 | ssh2_pkt_ensure(pktout.length); |
7cca0d81 |
894 | memcpy(pktout.data+pktout.length-len, data, len); |
895 | } |
75cab814 |
896 | static void ssh2_pkt_addbyte(unsigned char byte) { |
7cca0d81 |
897 | ssh2_pkt_adddata(&byte, 1); |
898 | } |
75cab814 |
899 | static void ssh2_pkt_init(int pkt_type) { |
7cca0d81 |
900 | pktout.length = 5; |
901 | ssh2_pkt_addbyte((unsigned char)pkt_type); |
902 | } |
75cab814 |
903 | static void ssh2_pkt_addbool(unsigned char value) { |
7cca0d81 |
904 | ssh2_pkt_adddata(&value, 1); |
905 | } |
75cab814 |
906 | static void ssh2_pkt_adduint32(unsigned long value) { |
7cca0d81 |
907 | unsigned char x[4]; |
908 | PUT_32BIT(x, value); |
909 | ssh2_pkt_adddata(x, 4); |
910 | } |
75cab814 |
911 | static void ssh2_pkt_addstring_start(void) { |
7cca0d81 |
912 | ssh2_pkt_adduint32(0); |
913 | pktout.savedpos = pktout.length; |
914 | } |
75cab814 |
915 | static void ssh2_pkt_addstring_str(char *data) { |
7cca0d81 |
916 | ssh2_pkt_adddata(data, strlen(data)); |
917 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
918 | pktout.length - pktout.savedpos); |
919 | } |
75cab814 |
920 | static void ssh2_pkt_addstring_data(char *data, int len) { |
7cca0d81 |
921 | ssh2_pkt_adddata(data, len); |
922 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
923 | pktout.length - pktout.savedpos); |
924 | } |
75cab814 |
925 | static void ssh2_pkt_addstring(char *data) { |
7cca0d81 |
926 | ssh2_pkt_addstring_start(); |
927 | ssh2_pkt_addstring_str(data); |
928 | } |
75cab814 |
929 | static char *ssh2_mpint_fmt(Bignum b, int *len) { |
7cca0d81 |
930 | unsigned char *p; |
3709bfe9 |
931 | int i, n = (ssh1_bignum_bitcount(b)+7)/8; |
932 | p = smalloc(n + 1); |
7cca0d81 |
933 | if (!p) |
934 | fatalbox("out of memory"); |
935 | p[0] = 0; |
3709bfe9 |
936 | for (i = 1; i <= n; i++) |
937 | p[i] = bignum_byte(b, n-i); |
7cca0d81 |
938 | i = 0; |
3709bfe9 |
939 | while (i <= n && p[i] == 0 && (p[i+1] & 0x80) == 0) |
7cca0d81 |
940 | i++; |
3709bfe9 |
941 | memmove(p, p+i, n+1-i); |
942 | *len = n+1-i; |
7cca0d81 |
943 | return p; |
944 | } |
75cab814 |
945 | static void ssh2_pkt_addmp(Bignum b) { |
7cca0d81 |
946 | unsigned char *p; |
947 | int len; |
948 | p = ssh2_mpint_fmt(b, &len); |
949 | ssh2_pkt_addstring_start(); |
950 | ssh2_pkt_addstring_data(p, len); |
dcbde236 |
951 | sfree(p); |
7cca0d81 |
952 | } |
b185170a |
953 | |
954 | /* |
955 | * Construct an SSH2 final-form packet: compress it, encrypt it, |
956 | * put the MAC on it. Final packet, ready to be sent, is stored in |
957 | * pktout.data. Total length is returned. |
958 | */ |
959 | static int ssh2_pkt_construct(void) { |
7cca0d81 |
960 | int cipherblk, maclen, padding, i; |
961 | static unsigned long outgoing_sequence = 0; |
962 | |
963 | /* |
4ba9b64b |
964 | * Compress packet payload. |
965 | */ |
966 | #if 0 |
967 | debug(("Pre-compression payload:\r\n")); |
968 | for (i = 5; i < pktout.length; i++) |
969 | debug((" %02x", (unsigned char)pktout.data[i])); |
970 | debug(("\r\n")); |
971 | #endif |
972 | { |
973 | unsigned char *newpayload; |
974 | int newlen; |
975 | if (cscomp && cscomp->compress(pktout.data+5, pktout.length-5, |
976 | &newpayload, &newlen)) { |
977 | pktout.length = 5; |
978 | ssh2_pkt_adddata(newpayload, newlen); |
dcbde236 |
979 | sfree(newpayload); |
4ba9b64b |
980 | } |
981 | } |
982 | |
983 | /* |
7cca0d81 |
984 | * Add padding. At least four bytes, and must also bring total |
985 | * length (minus MAC) up to a multiple of the block size. |
986 | */ |
8479e33c |
987 | cipherblk = cscipher ? cscipher->blksize : 8; /* block size */ |
7cca0d81 |
988 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
989 | padding = 4; |
990 | padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk; |
783415f8 |
991 | maclen = csmac ? csmac->len : 0; |
992 | ssh2_pkt_ensure(pktout.length + padding + maclen); |
7cca0d81 |
993 | pktout.data[4] = padding; |
994 | for (i = 0; i < padding; i++) |
995 | pktout.data[pktout.length + i] = random_byte(); |
996 | PUT_32BIT(pktout.data, pktout.length + padding - 4); |
997 | if (csmac) |
998 | csmac->generate(pktout.data, pktout.length + padding, |
999 | outgoing_sequence); |
1000 | outgoing_sequence++; /* whether or not we MACed */ |
1001 | |
1002 | #if 0 |
1003 | debug(("Sending packet len=%d\r\n", pktout.length+padding)); |
1004 | for (i = 0; i < pktout.length+padding; i++) |
1005 | debug((" %02x", (unsigned char)pktout.data[i])); |
1006 | debug(("\r\n")); |
1007 | #endif |
1008 | |
1009 | if (cscipher) |
1010 | cscipher->encrypt(pktout.data, pktout.length + padding); |
7cca0d81 |
1011 | |
b185170a |
1012 | /* Ready-to-send packet starts at pktout.data. We return length. */ |
1013 | return pktout.length + padding + maclen; |
1014 | } |
1015 | |
1016 | /* |
1017 | * Construct and send an SSH2 packet immediately. |
1018 | */ |
1019 | static void ssh2_pkt_send(void) { |
1020 | int len = ssh2_pkt_construct(); |
1021 | sk_write(s, pktout.data, len); |
1022 | } |
1023 | |
1024 | /* |
1025 | * Construct an SSH2 packet and add it to a deferred data block. |
1026 | * Useful for sending multiple packets in a single sk_write() call, |
1027 | * to prevent a traffic-analysing listener from being able to work |
1028 | * out the length of any particular packet (such as the password |
1029 | * packet). |
1030 | * |
1031 | * Note that because SSH2 sequence-numbers its packets, this can |
1032 | * NOT be used as an m4-style `defer' allowing packets to be |
1033 | * constructed in one order and sent in another. |
1034 | */ |
1035 | static void ssh2_pkt_defer(void) { |
1036 | int len = ssh2_pkt_construct(); |
1037 | if (deferred_len + len > deferred_size) { |
1038 | deferred_size = deferred_len + len + 128; |
1039 | deferred_send_data = srealloc(deferred_send_data, deferred_size); |
1040 | } |
1041 | memcpy(deferred_send_data+deferred_len, pktout.data, len); |
1042 | deferred_len += len; |
1043 | } |
1044 | |
1045 | /* |
1046 | * Send the whole deferred data block constructed by |
39065bed |
1047 | * ssh2_pkt_defer() or SSH1's defer_packet(). |
b185170a |
1048 | */ |
39065bed |
1049 | static void ssh_pkt_defersend(void) { |
b185170a |
1050 | sk_write(s, deferred_send_data, deferred_len); |
1051 | deferred_len = deferred_size = 0; |
1052 | sfree(deferred_send_data); |
1053 | deferred_send_data = NULL; |
7cca0d81 |
1054 | } |
1055 | |
1056 | #if 0 |
1057 | void bndebug(char *string, Bignum b) { |
1058 | unsigned char *p; |
1059 | int i, len; |
1060 | p = ssh2_mpint_fmt(b, &len); |
1061 | debug(("%s", string)); |
1062 | for (i = 0; i < len; i++) |
1063 | debug((" %02x", p[i])); |
1064 | debug(("\r\n")); |
dcbde236 |
1065 | sfree(p); |
7cca0d81 |
1066 | } |
1067 | #endif |
1068 | |
75cab814 |
1069 | static void sha_mpint(SHA_State *s, Bignum b) { |
7cca0d81 |
1070 | unsigned char *p; |
1071 | int len; |
1072 | p = ssh2_mpint_fmt(b, &len); |
1073 | sha_string(s, p, len); |
dcbde236 |
1074 | sfree(p); |
7cca0d81 |
1075 | } |
1076 | |
1077 | /* |
1078 | * SSH2 packet decode functions. |
1079 | */ |
75cab814 |
1080 | static unsigned long ssh2_pkt_getuint32(void) { |
7cca0d81 |
1081 | unsigned long value; |
1082 | if (pktin.length - pktin.savedpos < 4) |
1083 | return 0; /* arrgh, no way to decline (FIXME?) */ |
1084 | value = GET_32BIT(pktin.data+pktin.savedpos); |
1085 | pktin.savedpos += 4; |
1086 | return value; |
1087 | } |
65a22376 |
1088 | static int ssh2_pkt_getbool(void) { |
1089 | unsigned long value; |
1090 | if (pktin.length - pktin.savedpos < 1) |
1091 | return 0; /* arrgh, no way to decline (FIXME?) */ |
1092 | value = pktin.data[pktin.savedpos] != 0; |
1093 | pktin.savedpos++; |
1094 | return value; |
1095 | } |
75cab814 |
1096 | static void ssh2_pkt_getstring(char **p, int *length) { |
7cca0d81 |
1097 | *p = NULL; |
1098 | if (pktin.length - pktin.savedpos < 4) |
1099 | return; |
1100 | *length = GET_32BIT(pktin.data+pktin.savedpos); |
1101 | pktin.savedpos += 4; |
1102 | if (pktin.length - pktin.savedpos < *length) |
1103 | return; |
1104 | *p = pktin.data+pktin.savedpos; |
1105 | pktin.savedpos += *length; |
1106 | } |
75cab814 |
1107 | static Bignum ssh2_pkt_getmp(void) { |
7cca0d81 |
1108 | char *p; |
3709bfe9 |
1109 | int length; |
7cca0d81 |
1110 | Bignum b; |
1111 | |
1112 | ssh2_pkt_getstring(&p, &length); |
1113 | if (!p) |
1114 | return NULL; |
8d5de777 |
1115 | if (p[0] & 0x80) { |
1116 | bombout(("internal error: Can't handle negative mpints")); |
1117 | return NULL; |
1118 | } |
3709bfe9 |
1119 | b = bignum_from_bytes(p, length); |
7cca0d81 |
1120 | return b; |
1121 | } |
1122 | |
7d503c31 |
1123 | /* |
1124 | * Examine the remote side's version string and compare it against |
1125 | * a list of known buggy implementations. |
1126 | */ |
1127 | static void ssh_detect_bugs(char *vstring) { |
1128 | char *imp; /* pointer to implementation part */ |
1129 | imp = vstring; |
1130 | imp += strcspn(imp, "-"); |
620f0caf |
1131 | if (*imp) imp++; |
7d503c31 |
1132 | imp += strcspn(imp, "-"); |
620f0caf |
1133 | if (*imp) imp++; |
7d503c31 |
1134 | |
1135 | ssh_remote_bugs = 0; |
1136 | |
1137 | if (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
1138 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
1139 | !strcmp(imp, "1.2.22")) { |
1140 | /* |
1141 | * These versions don't support SSH1_MSG_IGNORE, so we have |
1142 | * to use a different defence against password length |
1143 | * sniffing. |
1144 | */ |
1145 | ssh_remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
1146 | logevent("We believe remote version has SSH1 ignore bug"); |
1147 | } |
1148 | |
1149 | if (!strncmp(imp, "2.1.0", 5) || !strncmp(imp, "2.0.", 4) || |
1150 | !strncmp(imp, "2.2.0", 5) || !strncmp(imp, "2.3.0", 5) || |
1151 | !strncmp(imp, "2.1 ", 4)) { |
1152 | /* |
1153 | * These versions have the HMAC bug. |
1154 | */ |
1155 | ssh_remote_bugs |= BUG_SSH2_HMAC; |
1156 | logevent("We believe remote version has SSH2 HMAC bug"); |
1157 | } |
1158 | } |
1159 | |
8df7a775 |
1160 | static int do_ssh_init(unsigned char c) { |
50526e47 |
1161 | static char vslen; |
8df7a775 |
1162 | static char version[10]; |
50526e47 |
1163 | static char *vstring; |
1164 | static int vstrsize; |
1165 | static char *vlog; |
8df7a775 |
1166 | static int i; |
374330e2 |
1167 | |
8df7a775 |
1168 | crBegin; |
1169 | |
1170 | /* Search for the string "SSH-" in the input. */ |
374330e2 |
1171 | i = 0; |
8df7a775 |
1172 | while (1) { |
1173 | static const int transS[] = { 1, 2, 2, 1 }; |
1174 | static const int transH[] = { 0, 0, 3, 0 }; |
1175 | static const int transminus[] = { 0, 0, 0, -1 }; |
1176 | if (c == 'S') i = transS[i]; |
1177 | else if (c == 'H') i = transH[i]; |
1178 | else if (c == '-') i = transminus[i]; |
374330e2 |
1179 | else i = 0; |
8df7a775 |
1180 | if (i < 0) |
1181 | break; |
1182 | crReturn(1); /* get another character */ |
374330e2 |
1183 | } |
8df7a775 |
1184 | |
50526e47 |
1185 | vstring = smalloc(16); |
1186 | vstrsize = 16; |
c5e9c988 |
1187 | strcpy(vstring, "SSH-"); |
50526e47 |
1188 | vslen = 4; |
374330e2 |
1189 | i = 0; |
1190 | while (1) { |
8df7a775 |
1191 | crReturn(1); /* get another char */ |
50526e47 |
1192 | if (vslen >= vstrsize-1) { |
1193 | vstrsize += 16; |
1194 | vstring = srealloc(vstring, vstrsize); |
1195 | } |
1196 | vstring[vslen++] = c; |
374330e2 |
1197 | if (i >= 0) { |
1198 | if (c == '-') { |
1199 | version[i] = '\0'; |
1200 | i = -1; |
1201 | } else if (i < sizeof(version)-1) |
1202 | version[i++] = c; |
1203 | } |
1204 | else if (c == '\n') |
1205 | break; |
1206 | } |
1207 | |
db7d555c |
1208 | ssh_agentfwd_enabled = FALSE; |
960e736a |
1209 | rdpkt2_state.incoming_sequence = 0; |
1210 | |
50526e47 |
1211 | vstring[vslen] = 0; |
9275ca8a |
1212 | vlog = smalloc(20 + vslen); |
c5e9c988 |
1213 | sprintf(vlog, "Server version: %s", vstring); |
7d503c31 |
1214 | ssh_detect_bugs(vstring); |
c5e9c988 |
1215 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
1216 | logevent(vlog); |
9275ca8a |
1217 | sfree(vlog); |
c5e9c988 |
1218 | |
adf799dd |
1219 | /* |
1220 | * Server version "1.99" means we can choose whether we use v1 |
1221 | * or v2 protocol. Choice is based on cfg.sshprot. |
1222 | */ |
1223 | if (ssh_versioncmp(version, cfg.sshprot == 1 ? "2.0" : "1.99") >= 0) { |
e5574168 |
1224 | /* |
1225 | * This is a v2 server. Begin v2 protocol. |
1226 | */ |
9275ca8a |
1227 | char verstring[80], vlog[100]; |
900a4ee6 |
1228 | sprintf(verstring, "SSH-2.0-%s", sshver); |
0db56f73 |
1229 | SHA_Init(&exhashbase); |
e5574168 |
1230 | /* |
1231 | * Hash our version string and their version string. |
1232 | */ |
0db56f73 |
1233 | sha_string(&exhashbase, verstring, strlen(verstring)); |
1234 | sha_string(&exhashbase, vstring, strcspn(vstring, "\r\n")); |
e5574168 |
1235 | sprintf(vlog, "We claim version: %s", verstring); |
1236 | logevent(vlog); |
9275ca8a |
1237 | strcat(verstring, "\n"); |
e5574168 |
1238 | logevent("Using SSH protocol version 2"); |
9275ca8a |
1239 | sk_write(s, verstring, strlen(verstring)); |
e5574168 |
1240 | ssh_protocol = ssh2_protocol; |
6abbf9e3 |
1241 | ssh_version = 2; |
e5574168 |
1242 | s_rdpkt = ssh2_rdpkt; |
1243 | } else { |
1244 | /* |
1245 | * This is a v1 server. Begin v1 protocol. |
1246 | */ |
9275ca8a |
1247 | char verstring[80], vlog[100]; |
1248 | sprintf(verstring, "SSH-%s-%s", |
900a4ee6 |
1249 | (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"), |
1250 | sshver); |
9275ca8a |
1251 | sprintf(vlog, "We claim version: %s", verstring); |
e5574168 |
1252 | logevent(vlog); |
9275ca8a |
1253 | strcat(verstring, "\n"); |
e5574168 |
1254 | logevent("Using SSH protocol version 1"); |
9275ca8a |
1255 | sk_write(s, verstring, strlen(verstring)); |
e5574168 |
1256 | ssh_protocol = ssh1_protocol; |
6abbf9e3 |
1257 | ssh_version = 1; |
e5574168 |
1258 | s_rdpkt = ssh1_rdpkt; |
1259 | } |
3687d221 |
1260 | ssh_state = SSH_STATE_BEFORE_SIZE; |
8df7a775 |
1261 | |
50526e47 |
1262 | sfree(vstring); |
50526e47 |
1263 | |
8df7a775 |
1264 | crFinish(0); |
1265 | } |
1266 | |
1267 | static void ssh_gotdata(unsigned char *data, int datalen) |
1268 | { |
1269 | crBegin; |
1270 | |
1271 | /* |
1272 | * To begin with, feed the characters one by one to the |
1273 | * protocol initialisation / selection function do_ssh_init(). |
1274 | * When that returns 0, we're done with the initial greeting |
1275 | * exchange and can move on to packet discipline. |
1276 | */ |
1277 | while (1) { |
1278 | int ret; |
1279 | if (datalen == 0) |
1280 | crReturnV; /* more data please */ |
1281 | ret = do_ssh_init(*data); |
1282 | data++; datalen--; |
1283 | if (ret == 0) |
1284 | break; |
1285 | } |
1286 | |
1287 | /* |
1288 | * We emerge from that loop when the initial negotiation is |
1289 | * over and we have selected an s_rdpkt function. Now pass |
1290 | * everything to s_rdpkt, and then pass the resulting packets |
1291 | * to the proper protocol handler. |
1292 | */ |
1293 | if (datalen == 0) |
1294 | crReturnV; |
1295 | while (1) { |
1296 | while (datalen > 0) { |
1297 | if ( s_rdpkt(&data, &datalen) == 0 ) { |
1298 | ssh_protocol(NULL, 0, 1); |
1299 | if (ssh_state == SSH_STATE_CLOSED) { |
1300 | return; |
1301 | } |
1302 | } |
1303 | } |
1304 | crReturnV; |
1305 | } |
1306 | crFinishV; |
1307 | } |
1308 | |
7e78000d |
1309 | static int ssh_closing (Plug plug, char *error_msg, int error_code, int calling_back) { |
1310 | ssh_state = SSH_STATE_CLOSED; |
1311 | sk_close(s); |
1312 | s = NULL; |
1313 | if (error_msg) { |
2c94fd1c |
1314 | /* A socket error has occurred. */ |
7e78000d |
1315 | connection_fatal (error_msg); |
1316 | } else { |
1317 | /* Otherwise, the remote side closed the connection normally. */ |
8df7a775 |
1318 | } |
7e78000d |
1319 | return 0; |
1320 | } |
1321 | |
1322 | static int ssh_receive(Plug plug, int urgent, char *data, int len) { |
8df7a775 |
1323 | ssh_gotdata (data, len); |
3257deae |
1324 | if (ssh_state == SSH_STATE_CLOSED) { |
1325 | if (s) { |
1326 | sk_close(s); |
1327 | s = NULL; |
1328 | } |
1329 | return 0; |
1330 | } |
fef97f43 |
1331 | return 1; |
374330e2 |
1332 | } |
1333 | |
fb09bf1c |
1334 | /* |
8df7a775 |
1335 | * Connect to specified host and port. |
1336 | * Returns an error message, or NULL on success. |
1337 | * Also places the canonical host name into `realhost'. |
1338 | */ |
1339 | static char *connect_to_host(char *host, int port, char **realhost) |
1340 | { |
7e78000d |
1341 | static struct plug_function_table fn_table = { |
1342 | ssh_closing, |
1343 | ssh_receive |
1344 | }, *fn_table_ptr = &fn_table; |
1345 | |
8df7a775 |
1346 | SockAddr addr; |
1347 | char *err; |
1348 | #ifdef FWHACK |
1349 | char *FWhost; |
1350 | int FWport; |
1351 | #endif |
1352 | |
dcbde236 |
1353 | savedhost = smalloc(1+strlen(host)); |
8df7a775 |
1354 | if (!savedhost) |
1355 | fatalbox("Out of memory"); |
1356 | strcpy(savedhost, host); |
1357 | |
1358 | if (port < 0) |
1359 | port = 22; /* default ssh port */ |
1360 | savedport = port; |
1361 | |
1362 | #ifdef FWHACK |
1363 | FWhost = host; |
1364 | FWport = port; |
1365 | host = FWSTR; |
1366 | port = 23; |
1367 | #endif |
1368 | |
1369 | /* |
1370 | * Try to find host. |
1371 | */ |
1372 | addr = sk_namelookup(host, realhost); |
1373 | if ( (err = sk_addr_error(addr)) ) |
1374 | return err; |
1375 | |
1376 | #ifdef FWHACK |
1377 | *realhost = FWhost; |
1378 | #endif |
1379 | |
1380 | /* |
1381 | * Open socket. |
1382 | */ |
7e78000d |
1383 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
8df7a775 |
1384 | if ( (err = sk_socket_error(s)) ) |
1385 | return err; |
1386 | |
1387 | #ifdef FWHACK |
1388 | sk_write(s, "connect ", 8); |
1389 | sk_write(s, FWhost, strlen(FWhost)); |
1390 | { |
1391 | char buf[20]; |
1392 | sprintf(buf, " %d\n", FWport); |
1393 | sk_write(s, buf, strlen(buf)); |
1394 | } |
1395 | #endif |
1396 | |
1397 | return NULL; |
1398 | } |
1399 | |
1400 | /* |
fb09bf1c |
1401 | * Handle the key exchange and user authentication phases. |
1402 | */ |
e5574168 |
1403 | static int do_ssh1_login(unsigned char *in, int inlen, int ispkt) |
fb09bf1c |
1404 | { |
374330e2 |
1405 | int i, j, len; |
374330e2 |
1406 | unsigned char *rsabuf, *keystr1, *keystr2; |
1407 | unsigned char cookie[8]; |
1408 | struct RSAKey servkey, hostkey; |
1409 | struct MD5Context md5c; |
ccbfb941 |
1410 | static unsigned long supported_ciphers_mask, supported_auths_mask; |
7cca0d81 |
1411 | static int tried_publickey; |
1412 | static unsigned char session_id[16]; |
bea1ef5f |
1413 | int cipher_type; |
b585ec46 |
1414 | static char username[100]; |
374330e2 |
1415 | |
374330e2 |
1416 | crBegin; |
1417 | |
fb09bf1c |
1418 | if (!ispkt) crWaitUntil(ispkt); |
374330e2 |
1419 | |
8d5de777 |
1420 | if (pktin.type != SSH1_SMSG_PUBLIC_KEY) { |
1421 | bombout(("Public key packet not received")); |
1422 | crReturn(0); |
1423 | } |
374330e2 |
1424 | |
c5e9c988 |
1425 | logevent("Received public keys"); |
374330e2 |
1426 | |
c5e9c988 |
1427 | memcpy(cookie, pktin.body, 8); |
374330e2 |
1428 | |
7cca0d81 |
1429 | i = makekey(pktin.body+8, &servkey, &keystr1, 0); |
1430 | j = makekey(pktin.body+8+i, &hostkey, &keystr2, 0); |
374330e2 |
1431 | |
c5e9c988 |
1432 | /* |
1c2a93c4 |
1433 | * Log the host key fingerprint. |
c5e9c988 |
1434 | */ |
c5e9c988 |
1435 | { |
1436 | char logmsg[80]; |
1c2a93c4 |
1437 | logevent("Host key fingerprint is:"); |
c5e9c988 |
1438 | strcpy(logmsg, " "); |
1c2a93c4 |
1439 | hostkey.comment = NULL; |
1440 | rsa_fingerprint(logmsg+strlen(logmsg), sizeof(logmsg)-strlen(logmsg), |
1441 | &hostkey); |
c5e9c988 |
1442 | logevent(logmsg); |
1443 | } |
1444 | |
b96dc54c |
1445 | ssh1_remote_protoflags = GET_32BIT(pktin.body+8+i+j); |
fb09bf1c |
1446 | supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j); |
1447 | supported_auths_mask = GET_32BIT(pktin.body+16+i+j); |
bea1ef5f |
1448 | |
b96dc54c |
1449 | ssh1_local_protoflags = ssh1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
1450 | ssh1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
1451 | |
c5e9c988 |
1452 | MD5Init(&md5c); |
374330e2 |
1453 | MD5Update(&md5c, keystr2, hostkey.bytes); |
1454 | MD5Update(&md5c, keystr1, servkey.bytes); |
1455 | MD5Update(&md5c, pktin.body, 8); |
374330e2 |
1456 | MD5Final(session_id, &md5c); |
1457 | |
1458 | for (i=0; i<32; i++) |
1459 | session_key[i] = random_byte(); |
1460 | |
1461 | len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
1462 | |
dcbde236 |
1463 | rsabuf = smalloc(len); |
374330e2 |
1464 | if (!rsabuf) |
1465 | fatalbox("Out of memory"); |
1466 | |
89ee5268 |
1467 | /* |
1468 | * Verify the host key. |
1469 | */ |
1470 | { |
1471 | /* |
1472 | * First format the key into a string. |
1473 | */ |
1474 | int len = rsastr_len(&hostkey); |
d5859615 |
1475 | char fingerprint[100]; |
dcbde236 |
1476 | char *keystr = smalloc(len); |
89ee5268 |
1477 | if (!keystr) |
1478 | fatalbox("Out of memory"); |
1479 | rsastr_fmt(keystr, &hostkey); |
d5859615 |
1480 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
d4857987 |
1481 | verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint); |
dcbde236 |
1482 | sfree(keystr); |
89ee5268 |
1483 | } |
374330e2 |
1484 | |
1485 | for (i=0; i<32; i++) { |
1486 | rsabuf[i] = session_key[i]; |
1487 | if (i < 16) |
1488 | rsabuf[i] ^= session_id[i]; |
1489 | } |
1490 | |
1491 | if (hostkey.bytes > servkey.bytes) { |
1492 | rsaencrypt(rsabuf, 32, &servkey); |
1493 | rsaencrypt(rsabuf, servkey.bytes, &hostkey); |
1494 | } else { |
1495 | rsaencrypt(rsabuf, 32, &hostkey); |
1496 | rsaencrypt(rsabuf, hostkey.bytes, &servkey); |
1497 | } |
1498 | |
c5e9c988 |
1499 | logevent("Encrypted session key"); |
1500 | |
0a3f1d48 |
1501 | switch (cfg.cipher) { |
1502 | case CIPHER_BLOWFISH: cipher_type = SSH_CIPHER_BLOWFISH; break; |
1503 | case CIPHER_DES: cipher_type = SSH_CIPHER_DES; break; |
1504 | case CIPHER_3DES: cipher_type = SSH_CIPHER_3DES; break; |
1505 | case CIPHER_AES: |
1408a877 |
1506 | c_write_str("AES not supported in SSH1, falling back to 3DES\r\n"); |
0a3f1d48 |
1507 | cipher_type = SSH_CIPHER_3DES; |
1508 | break; |
1509 | } |
bea1ef5f |
1510 | if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { |
1408a877 |
1511 | c_write_str("Selected cipher not supported, falling back to 3DES\r\n"); |
bea1ef5f |
1512 | cipher_type = SSH_CIPHER_3DES; |
1513 | } |
c5e9c988 |
1514 | switch (cipher_type) { |
1515 | case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break; |
1516 | case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break; |
1517 | case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break; |
1518 | } |
bea1ef5f |
1519 | |
e5574168 |
1520 | send_packet(SSH1_CMSG_SESSION_KEY, |
fb09bf1c |
1521 | PKT_CHAR, cipher_type, |
1522 | PKT_DATA, cookie, 8, |
1523 | PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF, |
1524 | PKT_DATA, rsabuf, len, |
b96dc54c |
1525 | PKT_INT, ssh1_local_protoflags, |
fb09bf1c |
1526 | PKT_END); |
1527 | |
c5e9c988 |
1528 | logevent("Trying to enable encryption..."); |
374330e2 |
1529 | |
dcbde236 |
1530 | sfree(rsabuf); |
374330e2 |
1531 | |
5e8358ad |
1532 | cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
9697bfd2 |
1533 | cipher_type == SSH_CIPHER_DES ? &ssh_des : |
bea1ef5f |
1534 | &ssh_3des; |
374330e2 |
1535 | cipher->sesskey(session_key); |
1536 | |
fb09bf1c |
1537 | crWaitUntil(ispkt); |
374330e2 |
1538 | |
8d5de777 |
1539 | if (pktin.type != SSH1_SMSG_SUCCESS) { |
1540 | bombout(("Encryption not successfully enabled")); |
1541 | crReturn(0); |
1542 | } |
374330e2 |
1543 | |
c5e9c988 |
1544 | logevent("Successfully started encryption"); |
1545 | |
374330e2 |
1546 | fflush(stdout); |
1547 | { |
374330e2 |
1548 | static int pos = 0; |
1549 | static char c; |
67779be7 |
1550 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
fa17a66e |
1551 | if (ssh_get_line) { |
1552 | if (!ssh_get_line("login as: ", |
1553 | username, sizeof(username), FALSE)) { |
1554 | /* |
1555 | * get_line failed to get a username. |
1556 | * Terminate. |
1557 | */ |
1558 | logevent("No username provided. Abandoning session."); |
1559 | ssh_state = SSH_STATE_CLOSED; |
1560 | crReturn(1); |
1561 | } |
1562 | } else { |
1563 | c_write_str("login as: "); |
1564 | ssh_send_ok = 1; |
1565 | while (pos >= 0) { |
1566 | crWaitUntil(!ispkt); |
1567 | while (inlen--) switch (c = *in++) { |
1568 | case 10: case 13: |
1569 | username[pos] = 0; |
1570 | pos = -1; |
1571 | break; |
1572 | case 8: case 127: |
1573 | if (pos > 0) { |
1574 | c_write_str("\b \b"); |
1575 | pos--; |
1576 | } |
1577 | break; |
1578 | case 21: case 27: |
1579 | while (pos > 0) { |
1580 | c_write_str("\b \b"); |
1581 | pos--; |
1582 | } |
1583 | break; |
1584 | case 3: case 4: |
1585 | random_save_seed(); |
1586 | exit(0); |
1587 | break; |
1588 | default: |
1589 | if (((c >= ' ' && c <= '~') || |
1590 | ((unsigned char)c >= 160)) && pos < 40) { |
1591 | username[pos++] = c; |
1592 | c_write(&c, 1); |
1593 | } |
1594 | break; |
1595 | } |
1596 | } |
1597 | c_write_str("\r\n"); |
1598 | username[strcspn(username, "\n\r")] = '\0'; |
1599 | } |
1600 | } else { |
374330e2 |
1601 | strncpy(username, cfg.username, 99); |
1602 | username[99] = '\0'; |
374330e2 |
1603 | } |
fb09bf1c |
1604 | |
e5574168 |
1605 | send_packet(SSH1_CMSG_USER, PKT_STR, username, PKT_END); |
c5e9c988 |
1606 | { |
3c8e959b |
1607 | char userlog[22+sizeof(username)]; |
c5e9c988 |
1608 | sprintf(userlog, "Sent username \"%s\"", username); |
1609 | logevent(userlog); |
3c8e959b |
1610 | if (flags & FLAG_INTERACTIVE && |
1611 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
1612 | strcat(userlog, "\r\n"); |
1408a877 |
1613 | c_write_str(userlog); |
3c8e959b |
1614 | } |
c5e9c988 |
1615 | } |
374330e2 |
1616 | } |
1617 | |
fb09bf1c |
1618 | crWaitUntil(ispkt); |
374330e2 |
1619 | |
7cca0d81 |
1620 | tried_publickey = 0; |
1621 | |
e5574168 |
1622 | while (pktin.type == SSH1_SMSG_FAILURE) { |
374330e2 |
1623 | static char password[100]; |
a52f067e |
1624 | static char prompt[200]; |
374330e2 |
1625 | static int pos; |
1626 | static char c; |
ccbfb941 |
1627 | static int pwpkt_type; |
ccbfb941 |
1628 | /* |
1629 | * Show password prompt, having first obtained it via a TIS |
18515c51 |
1630 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
1631 | * authentication. |
ccbfb941 |
1632 | */ |
e5574168 |
1633 | pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
5c58ad2d |
1634 | if (agent_exists()) { |
1635 | /* |
1636 | * Attempt RSA authentication using Pageant. |
1637 | */ |
1638 | static unsigned char request[5], *response, *p; |
1639 | static int responselen; |
1640 | static int i, nkeys; |
1641 | static int authed = FALSE; |
1642 | void *r; |
1643 | |
1644 | logevent("Pageant is running. Requesting keys."); |
1645 | |
1646 | /* Request the keys held by the agent. */ |
1647 | PUT_32BIT(request, 1); |
1983e559 |
1648 | request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
5c58ad2d |
1649 | agent_query(request, 5, &r, &responselen); |
1650 | response = (unsigned char *)r; |
95797139 |
1651 | if (response && responselen >= 5 && |
1652 | response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
5c58ad2d |
1653 | p = response + 5; |
1654 | nkeys = GET_32BIT(p); p += 4; |
6328ea7a |
1655 | { char buf[64]; sprintf(buf, "Pageant has %d SSH1 keys", nkeys); |
5c58ad2d |
1656 | logevent(buf); } |
1657 | for (i = 0; i < nkeys; i++) { |
1658 | static struct RSAKey key; |
1659 | static Bignum challenge; |
b8bb811a |
1660 | static char *commentp; |
1661 | static int commentlen; |
5c58ad2d |
1662 | |
1663 | { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i); |
1664 | logevent(buf); } |
1665 | p += 4; |
1666 | p += ssh1_read_bignum(p, &key.exponent); |
1667 | p += ssh1_read_bignum(p, &key.modulus); |
b8bb811a |
1668 | commentlen = GET_32BIT(p); p += 4; |
1669 | commentp = p; p += commentlen; |
5c58ad2d |
1670 | send_packet(SSH1_CMSG_AUTH_RSA, |
1671 | PKT_BIGNUM, key.modulus, PKT_END); |
1672 | crWaitUntil(ispkt); |
1673 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1674 | logevent("Key refused"); |
1675 | continue; |
1676 | } |
1677 | logevent("Received RSA challenge"); |
1678 | ssh1_read_bignum(pktin.body, &challenge); |
1679 | { |
1680 | char *agentreq, *q, *ret; |
1681 | int len, retlen; |
1682 | len = 1 + 4; /* message type, bit count */ |
1683 | len += ssh1_bignum_length(key.exponent); |
1684 | len += ssh1_bignum_length(key.modulus); |
1685 | len += ssh1_bignum_length(challenge); |
1686 | len += 16; /* session id */ |
1687 | len += 4; /* response format */ |
dcbde236 |
1688 | agentreq = smalloc(4 + len); |
5c58ad2d |
1689 | PUT_32BIT(agentreq, len); |
1690 | q = agentreq + 4; |
1983e559 |
1691 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
5c58ad2d |
1692 | PUT_32BIT(q, ssh1_bignum_bitcount(key.modulus)); |
1693 | q += 4; |
1694 | q += ssh1_write_bignum(q, key.exponent); |
1695 | q += ssh1_write_bignum(q, key.modulus); |
1696 | q += ssh1_write_bignum(q, challenge); |
1697 | memcpy(q, session_id, 16); q += 16; |
1698 | PUT_32BIT(q, 1); /* response format */ |
1699 | agent_query(agentreq, len+4, &ret, &retlen); |
dcbde236 |
1700 | sfree(agentreq); |
5c58ad2d |
1701 | if (ret) { |
1983e559 |
1702 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
5c58ad2d |
1703 | logevent("Sending Pageant's response"); |
1704 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
1705 | PKT_DATA, ret+5, 16, PKT_END); |
dcbde236 |
1706 | sfree(ret); |
5c58ad2d |
1707 | crWaitUntil(ispkt); |
1708 | if (pktin.type == SSH1_SMSG_SUCCESS) { |
1709 | logevent("Pageant's response accepted"); |
c4fa2fce |
1710 | if (flags & FLAG_VERBOSE) { |
1408a877 |
1711 | c_write_str("Authenticated using RSA key \""); |
c4fa2fce |
1712 | c_write(commentp, commentlen); |
1408a877 |
1713 | c_write_str("\" from agent\r\n"); |
c4fa2fce |
1714 | } |
5c58ad2d |
1715 | authed = TRUE; |
1716 | } else |
1717 | logevent("Pageant's response not accepted"); |
1718 | } else { |
1719 | logevent("Pageant failed to answer challenge"); |
dcbde236 |
1720 | sfree(ret); |
5c58ad2d |
1721 | } |
1722 | } else { |
1723 | logevent("No reply received from Pageant"); |
1724 | } |
1725 | } |
1726 | freebn(key.exponent); |
1727 | freebn(key.modulus); |
1728 | freebn(challenge); |
1729 | if (authed) |
1730 | break; |
1731 | } |
1732 | } |
1733 | if (authed) |
1734 | break; |
1735 | } |
7cca0d81 |
1736 | if (*cfg.keyfile && !tried_publickey) |
1737 | pwpkt_type = SSH1_CMSG_AUTH_RSA; |
fb09bf1c |
1738 | |
a52f067e |
1739 | if (pktin.type == SSH1_SMSG_FAILURE && |
1740 | cfg.try_tis_auth && |
1741 | (supported_auths_mask & (1<<SSH1_AUTH_TIS))) { |
1742 | pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
1743 | logevent("Requested TIS authentication"); |
1744 | send_packet(SSH1_CMSG_AUTH_TIS, PKT_END); |
1745 | crWaitUntil(ispkt); |
1746 | if (pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
1747 | logevent("TIS authentication declined"); |
1748 | if (flags & FLAG_INTERACTIVE) |
1408a877 |
1749 | c_write_str("TIS authentication refused.\r\n"); |
a52f067e |
1750 | } else { |
1751 | int challengelen = ((pktin.body[0] << 24) | |
1752 | (pktin.body[1] << 16) | |
1753 | (pktin.body[2] << 8) | |
1754 | (pktin.body[3])); |
1755 | logevent("Received TIS challenge"); |
1756 | if (challengelen > sizeof(prompt)-1) |
1757 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1758 | memcpy(prompt, pktin.body+4, challengelen); |
1759 | prompt[challengelen] = '\0'; |
1760 | } |
1761 | } |
1762 | if (pktin.type == SSH1_SMSG_FAILURE && |
1763 | cfg.try_tis_auth && |
1764 | (supported_auths_mask & (1<<SSH1_AUTH_CCARD))) { |
1765 | pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
1766 | logevent("Requested CryptoCard authentication"); |
1767 | send_packet(SSH1_CMSG_AUTH_CCARD, PKT_END); |
1768 | crWaitUntil(ispkt); |
1769 | if (pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
1770 | logevent("CryptoCard authentication declined"); |
1408a877 |
1771 | c_write_str("CryptoCard authentication refused.\r\n"); |
a52f067e |
1772 | } else { |
1773 | int challengelen = ((pktin.body[0] << 24) | |
1774 | (pktin.body[1] << 16) | |
1775 | (pktin.body[2] << 8) | |
1776 | (pktin.body[3])); |
1777 | logevent("Received CryptoCard challenge"); |
1778 | if (challengelen > sizeof(prompt)-1) |
1779 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1780 | memcpy(prompt, pktin.body+4, challengelen); |
1781 | strncpy(prompt + challengelen, "\r\nResponse : ", |
1782 | sizeof(prompt)-challengelen); |
1783 | prompt[sizeof(prompt)-1] = '\0'; |
1784 | } |
1785 | } |
1786 | if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
1787 | sprintf(prompt, "%.90s@%.90s's password: ", |
b585ec46 |
1788 | username, savedhost); |
a52f067e |
1789 | } |
1790 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1791 | char *comment = NULL; |
1792 | if (flags & FLAG_VERBOSE) |
1408a877 |
1793 | c_write_str("Trying public key authentication.\r\n"); |
a52f067e |
1794 | if (!rsakey_encrypted(cfg.keyfile, &comment)) { |
1795 | if (flags & FLAG_VERBOSE) |
1408a877 |
1796 | c_write_str("No passphrase required.\r\n"); |
a52f067e |
1797 | goto tryauth; |
1798 | } |
1799 | sprintf(prompt, "Passphrase for key \"%.100s\": ", comment); |
dcbde236 |
1800 | sfree(comment); |
a52f067e |
1801 | } |
1802 | |
fa17a66e |
1803 | if (ssh_get_line) { |
1804 | if (!ssh_get_line(prompt, password, sizeof(password), TRUE)) { |
85ee8208 |
1805 | /* |
fa17a66e |
1806 | * get_line failed to get a password (for example |
1807 | * because one was supplied on the command line |
1808 | * which has already failed to work). Terminate. |
85ee8208 |
1809 | */ |
1810 | logevent("No more passwords to try"); |
1811 | ssh_state = SSH_STATE_CLOSED; |
1812 | crReturn(1); |
1813 | } |
fb09bf1c |
1814 | } else { |
1408a877 |
1815 | c_write_str(prompt); |
7cca0d81 |
1816 | pos = 0; |
67779be7 |
1817 | ssh_send_ok = 1; |
7cca0d81 |
1818 | while (pos >= 0) { |
1819 | crWaitUntil(!ispkt); |
1820 | while (inlen--) switch (c = *in++) { |
1821 | case 10: case 13: |
1822 | password[pos] = 0; |
1823 | pos = -1; |
1824 | break; |
1825 | case 8: case 127: |
1826 | if (pos > 0) |
1827 | pos--; |
1828 | break; |
1829 | case 21: case 27: |
1830 | pos = 0; |
1831 | break; |
1832 | case 3: case 4: |
1833 | random_save_seed(); |
1834 | exit(0); |
1835 | break; |
1836 | default: |
1837 | if (((c >= ' ' && c <= '~') || |
1838 | ((unsigned char)c >= 160)) && pos < sizeof(password)) |
1839 | password[pos++] = c; |
1840 | break; |
1841 | } |
1842 | } |
1408a877 |
1843 | c_write_str("\r\n"); |
a52f067e |
1844 | } |
fb09bf1c |
1845 | |
7cca0d81 |
1846 | tryauth: |
1847 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1848 | /* |
1849 | * Try public key authentication with the specified |
1850 | * key file. |
1851 | */ |
1852 | static struct RSAKey pubkey; |
1853 | static Bignum challenge, response; |
1854 | static int i; |
1855 | static unsigned char buffer[32]; |
1856 | |
1857 | tried_publickey = 1; |
65a22376 |
1858 | i = loadrsakey(cfg.keyfile, &pubkey, password); |
7cca0d81 |
1859 | if (i == 0) { |
1408a877 |
1860 | c_write_str("Couldn't load public key from "); |
1861 | c_write_str(cfg.keyfile); |
1862 | c_write_str(".\r\n"); |
7cca0d81 |
1863 | continue; /* go and try password */ |
1864 | } |
1865 | if (i == -1) { |
1408a877 |
1866 | c_write_str("Wrong passphrase.\r\n"); |
7cca0d81 |
1867 | tried_publickey = 0; |
1868 | continue; /* try again */ |
1869 | } |
1870 | |
1871 | /* |
1872 | * Send a public key attempt. |
1873 | */ |
1874 | send_packet(SSH1_CMSG_AUTH_RSA, |
1875 | PKT_BIGNUM, pubkey.modulus, PKT_END); |
1876 | |
1877 | crWaitUntil(ispkt); |
1878 | if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
1879 | c_write_str("Server refused our public key.\r\n"); |
7cca0d81 |
1880 | continue; /* go and try password */ |
1881 | } |
8d5de777 |
1882 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1883 | bombout(("Bizarre response to offer of public key")); |
1884 | crReturn(0); |
1885 | } |
7cca0d81 |
1886 | ssh1_read_bignum(pktin.body, &challenge); |
1887 | response = rsadecrypt(challenge, &pubkey); |
1888 | freebn(pubkey.private_exponent); /* burn the evidence */ |
1889 | |
3709bfe9 |
1890 | for (i = 0; i < 32; i++) { |
1891 | buffer[i] = bignum_byte(response, 31-i); |
7cca0d81 |
1892 | } |
1893 | |
1894 | MD5Init(&md5c); |
1895 | MD5Update(&md5c, buffer, 32); |
1896 | MD5Update(&md5c, session_id, 16); |
1897 | MD5Final(buffer, &md5c); |
1898 | |
1899 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
1900 | PKT_DATA, buffer, 16, PKT_END); |
1901 | |
1902 | crWaitUntil(ispkt); |
1903 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
1904 | if (flags & FLAG_VERBOSE) |
1408a877 |
1905 | c_write_str("Failed to authenticate with our public key.\r\n"); |
7cca0d81 |
1906 | continue; /* go and try password */ |
1907 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
1908 | bombout(("Bizarre response to RSA authentication response")); |
1909 | crReturn(0); |
7cca0d81 |
1910 | } |
1911 | |
1912 | break; /* we're through! */ |
1913 | } else { |
39065bed |
1914 | if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
1915 | /* |
1916 | * Defence against traffic analysis: we send a |
1917 | * whole bunch of packets containing strings of |
1918 | * different lengths. One of these strings is the |
1919 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
1920 | * The others are all random data in |
1921 | * SSH1_MSG_IGNORE packets. This way a passive |
1922 | * listener can't tell which is the password, and |
1923 | * hence can't deduce the password length. |
1924 | * |
1925 | * Anybody with a password length greater than 16 |
1926 | * bytes is going to have enough entropy in their |
1927 | * password that a listener won't find it _that_ |
1928 | * much help to know how long it is. So what we'll |
1929 | * do is: |
1930 | * |
1931 | * - if password length < 16, we send 15 packets |
1932 | * containing string lengths 1 through 15 |
1933 | * |
1934 | * - otherwise, we let N be the nearest multiple |
1935 | * of 8 below the password length, and send 8 |
1936 | * packets containing string lengths N through |
1937 | * N+7. This won't obscure the order of |
1938 | * magnitude of the password length, but it will |
1939 | * introduce a bit of extra uncertainty. |
7d503c31 |
1940 | * |
1941 | * A few servers (the old 1.2.18 through 1.2.22) |
1942 | * can't deal with SSH1_MSG_IGNORE. For these |
1943 | * servers, we need an alternative defence. We make |
1944 | * use of the fact that the password is interpreted |
1945 | * as a C string: so we can append a NUL, then some |
1946 | * random data. |
39065bed |
1947 | */ |
7d503c31 |
1948 | if (ssh_remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) { |
1949 | char string[64]; |
1950 | char *s; |
1951 | int len; |
1952 | |
1953 | len = strlen(password); |
1954 | if (len < sizeof(string)) { |
1955 | s = string; |
1956 | strcpy(string, password); |
1957 | len++; /* cover the zero byte */ |
1958 | while (len < sizeof(string)) { |
1959 | string[len++] = (char)random_byte(); |
1960 | } |
1961 | } else { |
1962 | s = password; |
1963 | } |
1964 | send_packet(pwpkt_type, PKT_INT, len, |
1965 | PKT_DATA, s, len, PKT_END); |
39065bed |
1966 | } else { |
7d503c31 |
1967 | int bottom, top, pwlen, i; |
1968 | char *randomstr; |
1969 | |
1970 | pwlen = strlen(password); |
1971 | if (pwlen < 16) { |
98c84eb4 |
1972 | bottom = 0; /* zero length passwords are OK! :-) */ |
7d503c31 |
1973 | top = 15; |
1974 | } else { |
1975 | bottom = pwlen &~ 7; |
1976 | top = bottom + 7; |
1977 | } |
39065bed |
1978 | |
7d503c31 |
1979 | assert(pwlen >= bottom && pwlen <= top); |
39065bed |
1980 | |
7d503c31 |
1981 | randomstr = smalloc(top+1); |
39065bed |
1982 | |
7d503c31 |
1983 | for (i = bottom; i <= top; i++) { |
1984 | if (i == pwlen) |
1985 | defer_packet(pwpkt_type, PKT_STR, password, PKT_END); |
1986 | else { |
1987 | for (j = 0; j < i; j++) { |
1988 | do { |
1989 | randomstr[j] = random_byte(); |
1990 | } while (randomstr[j] == '\0'); |
1991 | } |
1992 | randomstr[i] = '\0'; |
1993 | defer_packet(SSH1_MSG_IGNORE, |
1994 | PKT_STR, randomstr, PKT_END); |
39065bed |
1995 | } |
39065bed |
1996 | } |
7d503c31 |
1997 | ssh_pkt_defersend(); |
39065bed |
1998 | } |
39065bed |
1999 | } else { |
2000 | send_packet(pwpkt_type, PKT_STR, password, PKT_END); |
2001 | } |
7cca0d81 |
2002 | } |
c5e9c988 |
2003 | logevent("Sent password"); |
374330e2 |
2004 | memset(password, 0, strlen(password)); |
fb09bf1c |
2005 | crWaitUntil(ispkt); |
e5574168 |
2006 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
2007 | if (flags & FLAG_VERBOSE) |
1408a877 |
2008 | c_write_str("Access denied\r\n"); |
c5e9c988 |
2009 | logevent("Authentication refused"); |
e5574168 |
2010 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
fb09bf1c |
2011 | logevent("Received disconnect request"); |
85ee8208 |
2012 | ssh_state = SSH_STATE_CLOSED; |
2013 | crReturn(1); |
e5574168 |
2014 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
2015 | bombout(("Strange packet received, type %d", pktin.type)); |
2016 | crReturn(0); |
374330e2 |
2017 | } |
2018 | } |
2019 | |
c5e9c988 |
2020 | logevent("Authentication successful"); |
2021 | |
fb09bf1c |
2022 | crFinish(1); |
2023 | } |
2024 | |
9c964e85 |
2025 | void sshfwd_close(struct ssh_channel *c) { |
80e48603 |
2026 | if (c && !c->closes) { |
783415f8 |
2027 | if (ssh_version == 1) { |
2028 | send_packet(SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, PKT_END); |
2029 | } else { |
2030 | ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
2031 | ssh2_pkt_adduint32(c->remoteid); |
2032 | ssh2_pkt_send(); |
2033 | } |
2034 | c->closes = 1; |
2035 | if (c->type == CHAN_X11) { |
2036 | c->u.x11.s = NULL; |
2037 | logevent("X11 connection terminated"); |
2038 | } |
2039 | } |
9c964e85 |
2040 | } |
2041 | |
2042 | void sshfwd_write(struct ssh_channel *c, char *buf, int len) { |
783415f8 |
2043 | if (ssh_version == 1) { |
2044 | send_packet(SSH1_MSG_CHANNEL_DATA, |
2045 | PKT_INT, c->remoteid, |
2046 | PKT_INT, len, |
2047 | PKT_DATA, buf, len, |
2048 | PKT_END); |
2049 | } else { |
2050 | ssh2_add_channel_data(c, buf, len); |
2051 | ssh2_try_send(c); |
2052 | } |
9c964e85 |
2053 | } |
2054 | |
e5574168 |
2055 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) { |
fb09bf1c |
2056 | crBegin; |
2057 | |
2058 | random_init(); |
2059 | |
e5574168 |
2060 | while (!do_ssh1_login(in, inlen, ispkt)) { |
fb09bf1c |
2061 | crReturnV; |
85ee8208 |
2062 | } |
2063 | if (ssh_state == SSH_STATE_CLOSED) |
2064 | crReturnV; |
fb09bf1c |
2065 | |
979310f1 |
2066 | if (cfg.agentfwd && agent_exists()) { |
dacbd0e8 |
2067 | logevent("Requesting agent forwarding"); |
2068 | send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
2069 | do { crReturnV; } while (!ispkt); |
2070 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
2071 | bombout(("Protocol confusion")); |
2072 | crReturnV; |
dacbd0e8 |
2073 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
2074 | logevent("Agent forwarding refused"); |
db7d555c |
2075 | } else { |
dacbd0e8 |
2076 | logevent("Agent forwarding enabled"); |
db7d555c |
2077 | ssh_agentfwd_enabled = TRUE; |
2078 | } |
dacbd0e8 |
2079 | } |
2080 | |
9c964e85 |
2081 | if (cfg.x11_forward) { |
2082 | char proto[20], data[64]; |
2083 | logevent("Requesting X11 forwarding"); |
2084 | x11_invent_auth(proto, sizeof(proto), data, sizeof(data)); |
b96dc54c |
2085 | if (ssh1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
2086 | send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING, |
2087 | PKT_STR, proto, PKT_STR, data, |
2088 | PKT_INT, 0, |
2089 | PKT_END); |
2090 | } else { |
2091 | send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING, |
2092 | PKT_STR, proto, PKT_STR, data, |
2093 | PKT_END); |
2094 | } |
9c964e85 |
2095 | do { crReturnV; } while (!ispkt); |
2096 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
2097 | bombout(("Protocol confusion")); |
2098 | crReturnV; |
2099 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
2100 | logevent("X11 forwarding refused"); |
2101 | } else { |
2102 | logevent("X11 forwarding enabled"); |
2103 | ssh_X11_fwd_enabled = TRUE; |
2104 | } |
2105 | } |
2106 | |
fef97f43 |
2107 | if (!cfg.nopty) { |
e5574168 |
2108 | send_packet(SSH1_CMSG_REQUEST_PTY, |
fb09bf1c |
2109 | PKT_STR, cfg.termtype, |
2110 | PKT_INT, rows, PKT_INT, cols, |
2111 | PKT_INT, 0, PKT_INT, 0, |
2112 | PKT_CHAR, 0, |
2113 | PKT_END); |
fef97f43 |
2114 | ssh_state = SSH_STATE_INTERMED; |
2115 | do { crReturnV; } while (!ispkt); |
e5574168 |
2116 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
2117 | bombout(("Protocol confusion")); |
2118 | crReturnV; |
e5574168 |
2119 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
2120 | c_write_str("Server refused to allocate pty\r\n"); |
0965bee0 |
2121 | ssh_editing = ssh_echoing = 1; |
fef97f43 |
2122 | } |
c5e9c988 |
2123 | logevent("Allocated pty"); |
0965bee0 |
2124 | } else { |
2125 | ssh_editing = ssh_echoing = 1; |
374330e2 |
2126 | } |
2127 | |
4ba9b64b |
2128 | if (cfg.compression) { |
2129 | send_packet(SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
2130 | do { crReturnV; } while (!ispkt); |
2131 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
2132 | bombout(("Protocol confusion")); |
2133 | crReturnV; |
2134 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
2135 | c_write_str("Server refused to compress\r\n"); |
4ba9b64b |
2136 | } |
2137 | logevent("Started compression"); |
2138 | ssh1_compressing = TRUE; |
2139 | zlib_compress_init(); |
2140 | zlib_decompress_init(); |
2141 | } |
2142 | |
96621a84 |
2143 | if (*cfg.remote_cmd_ptr) |
2144 | send_packet(SSH1_CMSG_EXEC_CMD, PKT_STR, cfg.remote_cmd_ptr, PKT_END); |
6abbf9e3 |
2145 | else |
2146 | send_packet(SSH1_CMSG_EXEC_SHELL, PKT_END); |
c5e9c988 |
2147 | logevent("Started session"); |
374330e2 |
2148 | |
2149 | ssh_state = SSH_STATE_SESSION; |
2150 | if (size_needed) |
2151 | ssh_size(); |
3687d221 |
2152 | if (eof_needed) |
2153 | ssh_special(TS_EOF); |
374330e2 |
2154 | |
0965bee0 |
2155 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
8ccc75b0 |
2156 | ssh_send_ok = 1; |
dacbd0e8 |
2157 | ssh_channels = newtree234(ssh_channelcmp); |
374330e2 |
2158 | while (1) { |
2159 | crReturnV; |
2160 | if (ispkt) { |
e5574168 |
2161 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
2162 | pktin.type == SSH1_SMSG_STDERR_DATA) { |
fb09bf1c |
2163 | long len = GET_32BIT(pktin.body); |
fe50e814 |
2164 | from_backend(pktin.type == SSH1_SMSG_STDERR_DATA, |
2165 | pktin.body+4, len); |
e5574168 |
2166 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
21248260 |
2167 | ssh_state = SSH_STATE_CLOSED; |
c5e9c988 |
2168 | logevent("Received disconnect request"); |
3257deae |
2169 | crReturnV; |
9c964e85 |
2170 | } else if (pktin.type == SSH1_SMSG_X11_OPEN) { |
2171 | /* Remote side is trying to open a channel to talk to our |
2172 | * X-Server. Give them back a local channel number. */ |
2173 | unsigned i; |
2174 | struct ssh_channel *c, *d; |
2175 | enum234 e; |
2176 | |
2177 | logevent("Received X11 connect request"); |
2178 | /* Refuse if X11 forwarding is disabled. */ |
2179 | if (!ssh_X11_fwd_enabled) { |
2180 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2181 | PKT_INT, GET_32BIT(pktin.body), |
2182 | PKT_END); |
2183 | logevent("Rejected X11 connect request"); |
2184 | } else { |
9c964e85 |
2185 | c = smalloc(sizeof(struct ssh_channel)); |
2186 | |
0c6c9a70 |
2187 | if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) { |
9c964e85 |
2188 | logevent("opening X11 forward connection failed"); |
2189 | sfree(c); |
2190 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2191 | PKT_INT, GET_32BIT(pktin.body), |
2192 | PKT_END); |
2193 | } else { |
2194 | logevent("opening X11 forward connection succeeded"); |
2195 | for (i=1, d = first234(ssh_channels, &e); d; d = next234(&e)) { |
783415f8 |
2196 | if (d->localid > i) |
2197 | break; /* found a free number */ |
2198 | i = d->localid + 1; |
9c964e85 |
2199 | } |
2200 | c->remoteid = GET_32BIT(pktin.body); |
2201 | c->localid = i; |
2202 | c->closes = 0; |
783415f8 |
2203 | c->type = CHAN_X11; /* identify channel type */ |
9c964e85 |
2204 | add234(ssh_channels, c); |
2205 | send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
2206 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
2207 | PKT_END); |
2208 | logevent("Opened X11 forward channel"); |
2209 | } |
2210 | } |
dacbd0e8 |
2211 | } else if (pktin.type == SSH1_SMSG_AGENT_OPEN) { |
2212 | /* Remote side is trying to open a channel to talk to our |
2213 | * agent. Give them back a local channel number. */ |
db7d555c |
2214 | unsigned i; |
dacbd0e8 |
2215 | struct ssh_channel *c; |
2216 | enum234 e; |
db7d555c |
2217 | |
2218 | /* Refuse if agent forwarding is disabled. */ |
2219 | if (!ssh_agentfwd_enabled) { |
2220 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2221 | PKT_INT, GET_32BIT(pktin.body), |
2222 | PKT_END); |
2223 | } else { |
2224 | i = 1; |
2225 | for (c = first234(ssh_channels, &e); c; c = next234(&e)) { |
2226 | if (c->localid > i) |
2227 | break; /* found a free number */ |
2228 | i = c->localid + 1; |
2229 | } |
dcbde236 |
2230 | c = smalloc(sizeof(struct ssh_channel)); |
db7d555c |
2231 | c->remoteid = GET_32BIT(pktin.body); |
2232 | c->localid = i; |
2233 | c->closes = 0; |
783415f8 |
2234 | c->type = CHAN_AGENT; /* identify channel type */ |
db7d555c |
2235 | c->u.a.lensofar = 0; |
2236 | add234(ssh_channels, c); |
2237 | send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
2238 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
2239 | PKT_END); |
2240 | } |
2241 | } else if (pktin.type == SSH1_MSG_CHANNEL_CLOSE || |
2242 | pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) { |
dacbd0e8 |
2243 | /* Remote side closes a channel. */ |
d211621f |
2244 | unsigned i = GET_32BIT(pktin.body); |
dacbd0e8 |
2245 | struct ssh_channel *c; |
2246 | c = find234(ssh_channels, &i, ssh_channelfind); |
2247 | if (c) { |
2248 | int closetype; |
2249 | closetype = (pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
824e9f94 |
2250 | if (!(c->closes & closetype)) |
2251 | send_packet(pktin.type, PKT_INT, c->remoteid, PKT_END); |
783415f8 |
2252 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
9c964e85 |
2253 | logevent("X11 connection closed"); |
2254 | assert(c->u.x11.s != NULL); |
2255 | x11_close(c->u.x11.s); |
2256 | c->u.x11.s = NULL; |
2257 | } |
dacbd0e8 |
2258 | c->closes |= closetype; |
2259 | if (c->closes == 3) { |
2260 | del234(ssh_channels, c); |
dcbde236 |
2261 | sfree(c); |
dacbd0e8 |
2262 | } |
2263 | } |
2264 | } else if (pktin.type == SSH1_MSG_CHANNEL_DATA) { |
2265 | /* Data sent down one of our channels. */ |
2266 | int i = GET_32BIT(pktin.body); |
2267 | int len = GET_32BIT(pktin.body+4); |
2268 | unsigned char *p = pktin.body+8; |
2269 | struct ssh_channel *c; |
2270 | c = find234(ssh_channels, &i, ssh_channelfind); |
2271 | if (c) { |
2272 | switch(c->type) { |
783415f8 |
2273 | case CHAN_X11: |
9c964e85 |
2274 | x11_send(c->u.x11.s, p, len); |
2275 | break; |
783415f8 |
2276 | case CHAN_AGENT: |
dacbd0e8 |
2277 | /* Data for an agent message. Buffer it. */ |
2278 | while (len > 0) { |
2279 | if (c->u.a.lensofar < 4) { |
2280 | int l = min(4 - c->u.a.lensofar, len); |
2281 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, l); |
2282 | p += l; len -= l; c->u.a.lensofar += l; |
2283 | } |
2284 | if (c->u.a.lensofar == 4) { |
2285 | c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen); |
dcbde236 |
2286 | c->u.a.message = smalloc(c->u.a.totallen); |
dacbd0e8 |
2287 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
2288 | } |
2289 | if (c->u.a.lensofar >= 4 && len > 0) { |
2290 | int l = min(c->u.a.totallen - c->u.a.lensofar, len); |
2291 | memcpy(c->u.a.message + c->u.a.lensofar, p, l); |
2292 | p += l; len -= l; c->u.a.lensofar += l; |
2293 | } |
2294 | if (c->u.a.lensofar == c->u.a.totallen) { |
2295 | void *reply, *sentreply; |
2296 | int replylen; |
2297 | agent_query(c->u.a.message, c->u.a.totallen, |
2298 | &reply, &replylen); |
2299 | if (reply) |
2300 | sentreply = reply; |
2301 | else { |
2302 | /* Fake SSH_AGENT_FAILURE. */ |
2303 | sentreply = "\0\0\0\1\5"; |
2304 | replylen = 5; |
2305 | } |
2306 | send_packet(SSH1_MSG_CHANNEL_DATA, |
2307 | PKT_INT, c->remoteid, |
2308 | PKT_INT, replylen, |
2309 | PKT_DATA, sentreply, replylen, |
2310 | PKT_END); |
2311 | if (reply) |
dcbde236 |
2312 | sfree(reply); |
2313 | sfree(c->u.a.message); |
dacbd0e8 |
2314 | c->u.a.lensofar = 0; |
2315 | } |
2316 | } |
2317 | break; |
2318 | } |
2319 | } |
e5574168 |
2320 | } else if (pktin.type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
2321 | /* may be from EXEC_SHELL on some servers */ |
e5574168 |
2322 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
972a41c8 |
2323 | /* may be from EXEC_SHELL on some servers |
374330e2 |
2324 | * if no pty is available or in other odd cases. Ignore */ |
e5574168 |
2325 | } else if (pktin.type == SSH1_SMSG_EXIT_STATUS) { |
2326 | send_packet(SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
374330e2 |
2327 | } else { |
8d5de777 |
2328 | bombout(("Strange packet received: type %d", pktin.type)); |
2329 | crReturnV; |
374330e2 |
2330 | } |
2331 | } else { |
8df7a775 |
2332 | while (inlen > 0) { |
2333 | int len = min(inlen, 512); |
2334 | send_packet(SSH1_CMSG_STDIN_DATA, |
2335 | PKT_INT, len, PKT_DATA, in, len, PKT_END); |
2336 | in += len; |
2337 | inlen -= len; |
2338 | } |
374330e2 |
2339 | } |
2340 | } |
2341 | |
2342 | crFinishV; |
2343 | } |
2344 | |
2345 | /* |
e5574168 |
2346 | * Utility routine for decoding comma-separated strings in KEXINIT. |
2347 | */ |
75cab814 |
2348 | static int in_commasep_string(char *needle, char *haystack, int haylen) { |
e5574168 |
2349 | int needlen = strlen(needle); |
2350 | while (1) { |
2351 | /* |
2352 | * Is it at the start of the string? |
2353 | */ |
2354 | if (haylen >= needlen && /* haystack is long enough */ |
2355 | !memcmp(needle, haystack, needlen) && /* initial match */ |
2356 | (haylen == needlen || haystack[needlen] == ',') |
2357 | /* either , or EOS follows */ |
2358 | ) |
2359 | return 1; |
2360 | /* |
2361 | * If not, search for the next comma and resume after that. |
2362 | * If no comma found, terminate. |
2363 | */ |
2364 | while (haylen > 0 && *haystack != ',') |
2365 | haylen--, haystack++; |
2366 | if (haylen == 0) |
2367 | return 0; |
2368 | haylen--, haystack++; /* skip over comma itself */ |
2369 | } |
2370 | } |
2371 | |
2372 | /* |
d39f364a |
2373 | * SSH2 key creation method. |
2374 | */ |
5e0d7cb8 |
2375 | static void ssh2_mkkey(Bignum K, char *H, char *sessid, char chr, char *keyspace) { |
d39f364a |
2376 | SHA_State s; |
2377 | /* First 20 bytes. */ |
2378 | SHA_Init(&s); |
2379 | sha_mpint(&s, K); |
2380 | SHA_Bytes(&s, H, 20); |
2381 | SHA_Bytes(&s, &chr, 1); |
5e0d7cb8 |
2382 | SHA_Bytes(&s, sessid, 20); |
d39f364a |
2383 | SHA_Final(&s, keyspace); |
2384 | /* Next 20 bytes. */ |
2385 | SHA_Init(&s); |
2386 | sha_mpint(&s, K); |
2387 | SHA_Bytes(&s, H, 20); |
2388 | SHA_Bytes(&s, keyspace, 20); |
2389 | SHA_Final(&s, keyspace+20); |
2390 | } |
2391 | |
2392 | /* |
7cca0d81 |
2393 | * Handle the SSH2 transport layer. |
e5574168 |
2394 | */ |
7cca0d81 |
2395 | static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt) |
e5574168 |
2396 | { |
7bd5a860 |
2397 | static int i, j, len, nbits, pbits; |
e5574168 |
2398 | static char *str; |
a92dd380 |
2399 | static Bignum p, g, e, f, K; |
2400 | static int kex_init_value, kex_reply_value; |
7591b9ff |
2401 | static const struct ssh_mac **maclist; |
8b2715b2 |
2402 | static int nmacs; |
0a3f1d48 |
2403 | static const struct ssh2_cipher *cscipher_tobe = NULL; |
2404 | static const struct ssh2_cipher *sccipher_tobe = NULL; |
57476f6b |
2405 | static const struct ssh_mac *csmac_tobe = NULL; |
2406 | static const struct ssh_mac *scmac_tobe = NULL; |
2407 | static const struct ssh_compress *cscomp_tobe = NULL; |
2408 | static const struct ssh_compress *sccomp_tobe = NULL; |
d5859615 |
2409 | static char *hostkeydata, *sigdata, *keystr, *fingerprint; |
e5574168 |
2410 | static int hostkeylen, siglen; |
e055a386 |
2411 | static void *hkey; /* actual host key */ |
e5574168 |
2412 | static unsigned char exchange_hash[20]; |
d39f364a |
2413 | static unsigned char keyspace[40]; |
0a3f1d48 |
2414 | static const struct ssh2_ciphers *preferred_cipher; |
4ba9b64b |
2415 | static const struct ssh_compress *preferred_comp; |
0db56f73 |
2416 | static int first_kex; |
e5574168 |
2417 | |
2418 | crBegin; |
7cca0d81 |
2419 | random_init(); |
0db56f73 |
2420 | first_kex = 1; |
e5574168 |
2421 | |
5e8358ad |
2422 | /* |
4ba9b64b |
2423 | * Set up the preferred cipher and compression. |
5e8358ad |
2424 | */ |
2425 | if (cfg.cipher == CIPHER_BLOWFISH) { |
0a3f1d48 |
2426 | preferred_cipher = &ssh2_blowfish; |
5e8358ad |
2427 | } else if (cfg.cipher == CIPHER_DES) { |
2428 | logevent("Single DES not supported in SSH2; using 3DES"); |
0a3f1d48 |
2429 | preferred_cipher = &ssh2_3des; |
5e8358ad |
2430 | } else if (cfg.cipher == CIPHER_3DES) { |
0a3f1d48 |
2431 | preferred_cipher = &ssh2_3des; |
2432 | } else if (cfg.cipher == CIPHER_AES) { |
2433 | preferred_cipher = &ssh2_aes; |
5e8358ad |
2434 | } else { |
2435 | /* Shouldn't happen, but we do want to initialise to _something_. */ |
0a3f1d48 |
2436 | preferred_cipher = &ssh2_3des; |
5e8358ad |
2437 | } |
4ba9b64b |
2438 | if (cfg.compression) |
2439 | preferred_comp = &ssh_zlib; |
2440 | else |
2441 | preferred_comp = &ssh_comp_none; |
5e8358ad |
2442 | |
7591b9ff |
2443 | /* |
2444 | * Be prepared to work around the buggy MAC problem. |
2445 | */ |
7d503c31 |
2446 | if (cfg.buggymac || (ssh_remote_bugs & BUG_SSH2_HMAC)) |
8b2715b2 |
2447 | maclist = buggymacs, nmacs = lenof(buggymacs); |
7591b9ff |
2448 | else |
8b2715b2 |
2449 | maclist = macs, nmacs = lenof(macs); |
7591b9ff |
2450 | |
7cca0d81 |
2451 | begin_key_exchange: |
e5574168 |
2452 | /* |
2453 | * Construct and send our key exchange packet. |
2454 | */ |
2455 | ssh2_pkt_init(SSH2_MSG_KEXINIT); |
2456 | for (i = 0; i < 16; i++) |
2457 | ssh2_pkt_addbyte((unsigned char)random_byte()); |
2458 | /* List key exchange algorithms. */ |
2459 | ssh2_pkt_addstring_start(); |
2460 | for (i = 0; i < lenof(kex_algs); i++) { |
2461 | ssh2_pkt_addstring_str(kex_algs[i]->name); |
2462 | if (i < lenof(kex_algs)-1) |
2463 | ssh2_pkt_addstring_str(","); |
2464 | } |
2465 | /* List server host key algorithms. */ |
2466 | ssh2_pkt_addstring_start(); |
2467 | for (i = 0; i < lenof(hostkey_algs); i++) { |
2468 | ssh2_pkt_addstring_str(hostkey_algs[i]->name); |
2469 | if (i < lenof(hostkey_algs)-1) |
2470 | ssh2_pkt_addstring_str(","); |
2471 | } |
2472 | /* List client->server encryption algorithms. */ |
2473 | ssh2_pkt_addstring_start(); |
729e54b4 |
2474 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2475 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2476 | for (j = 0; j < c->nciphers; j++) { |
2477 | ssh2_pkt_addstring_str(c->list[j]->name); |
2478 | if (i < lenof(ciphers) || j < c->nciphers-1) |
2479 | ssh2_pkt_addstring_str(","); |
2480 | } |
e5574168 |
2481 | } |
2482 | /* List server->client encryption algorithms. */ |
2483 | ssh2_pkt_addstring_start(); |
729e54b4 |
2484 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2485 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2486 | for (j = 0; j < c->nciphers; j++) { |
2487 | ssh2_pkt_addstring_str(c->list[j]->name); |
2488 | if (i < lenof(ciphers) || j < c->nciphers-1) |
2489 | ssh2_pkt_addstring_str(","); |
2490 | } |
e5574168 |
2491 | } |
2492 | /* List client->server MAC algorithms. */ |
2493 | ssh2_pkt_addstring_start(); |
8b2715b2 |
2494 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2495 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
2496 | if (i < nmacs-1) |
e5574168 |
2497 | ssh2_pkt_addstring_str(","); |
2498 | } |
2499 | /* List server->client MAC algorithms. */ |
2500 | ssh2_pkt_addstring_start(); |
8b2715b2 |
2501 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2502 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
2503 | if (i < nmacs-1) |
e5574168 |
2504 | ssh2_pkt_addstring_str(","); |
2505 | } |
2506 | /* List client->server compression algorithms. */ |
2507 | ssh2_pkt_addstring_start(); |
4ba9b64b |
2508 | for (i = 0; i < lenof(compressions)+1; i++) { |
2509 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2510 | ssh2_pkt_addstring_str(c->name); |
2511 | if (i < lenof(compressions)) |
e5574168 |
2512 | ssh2_pkt_addstring_str(","); |
2513 | } |
2514 | /* List server->client compression algorithms. */ |
2515 | ssh2_pkt_addstring_start(); |
4ba9b64b |
2516 | for (i = 0; i < lenof(compressions)+1; i++) { |
2517 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2518 | ssh2_pkt_addstring_str(c->name); |
2519 | if (i < lenof(compressions)) |
e5574168 |
2520 | ssh2_pkt_addstring_str(","); |
2521 | } |
2522 | /* List client->server languages. Empty list. */ |
2523 | ssh2_pkt_addstring_start(); |
2524 | /* List server->client languages. Empty list. */ |
2525 | ssh2_pkt_addstring_start(); |
2526 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
2527 | ssh2_pkt_addbool(FALSE); |
2528 | /* Reserved. */ |
2529 | ssh2_pkt_adduint32(0); |
0db56f73 |
2530 | |
2531 | exhash = exhashbase; |
e5574168 |
2532 | sha_string(&exhash, pktout.data+5, pktout.length-5); |
0db56f73 |
2533 | |
e5574168 |
2534 | ssh2_pkt_send(); |
2535 | |
2536 | if (!ispkt) crWaitUntil(ispkt); |
2537 | sha_string(&exhash, pktin.data+5, pktin.length-5); |
2538 | |
2539 | /* |
2540 | * Now examine the other side's KEXINIT to see what we're up |
2541 | * to. |
2542 | */ |
7cca0d81 |
2543 | if (pktin.type != SSH2_MSG_KEXINIT) { |
8d5de777 |
2544 | bombout(("expected key exchange packet from server")); |
2545 | crReturn(0); |
7cca0d81 |
2546 | } |
e5574168 |
2547 | kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL; |
2548 | csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL; |
2549 | pktin.savedpos += 16; /* skip garbage cookie */ |
2550 | ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */ |
2551 | for (i = 0; i < lenof(kex_algs); i++) { |
2552 | if (in_commasep_string(kex_algs[i]->name, str, len)) { |
2553 | kex = kex_algs[i]; |
2554 | break; |
2555 | } |
2556 | } |
2557 | ssh2_pkt_getstring(&str, &len); /* host key algorithms */ |
2558 | for (i = 0; i < lenof(hostkey_algs); i++) { |
2559 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
2560 | hostkey = hostkey_algs[i]; |
2561 | break; |
2562 | } |
2563 | } |
2564 | ssh2_pkt_getstring(&str, &len); /* client->server cipher */ |
729e54b4 |
2565 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2566 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2567 | for (j = 0; j < c->nciphers; j++) { |
2568 | if (in_commasep_string(c->list[j]->name, str, len)) { |
2569 | cscipher_tobe = c->list[j]; |
2570 | break; |
2571 | } |
e5574168 |
2572 | } |
0a3f1d48 |
2573 | if (cscipher_tobe) |
2574 | break; |
e5574168 |
2575 | } |
2576 | ssh2_pkt_getstring(&str, &len); /* server->client cipher */ |
729e54b4 |
2577 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2578 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2579 | for (j = 0; j < c->nciphers; j++) { |
2580 | if (in_commasep_string(c->list[j]->name, str, len)) { |
2581 | sccipher_tobe = c->list[j]; |
2582 | break; |
2583 | } |
e5574168 |
2584 | } |
0a3f1d48 |
2585 | if (sccipher_tobe) |
2586 | break; |
e5574168 |
2587 | } |
2588 | ssh2_pkt_getstring(&str, &len); /* client->server mac */ |
8b2715b2 |
2589 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2590 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2591 | csmac_tobe = maclist[i]; |
e5574168 |
2592 | break; |
2593 | } |
2594 | } |
2595 | ssh2_pkt_getstring(&str, &len); /* server->client mac */ |
8b2715b2 |
2596 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2597 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2598 | scmac_tobe = maclist[i]; |
e5574168 |
2599 | break; |
2600 | } |
2601 | } |
2602 | ssh2_pkt_getstring(&str, &len); /* client->server compression */ |
4ba9b64b |
2603 | for (i = 0; i < lenof(compressions)+1; i++) { |
2604 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2605 | if (in_commasep_string(c->name, str, len)) { |
2606 | cscomp_tobe = c; |
e5574168 |
2607 | break; |
2608 | } |
2609 | } |
2610 | ssh2_pkt_getstring(&str, &len); /* server->client compression */ |
4ba9b64b |
2611 | for (i = 0; i < lenof(compressions)+1; i++) { |
2612 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2613 | if (in_commasep_string(c->name, str, len)) { |
2614 | sccomp_tobe = c; |
e5574168 |
2615 | break; |
2616 | } |
2617 | } |
e5574168 |
2618 | |
2619 | /* |
7bd5a860 |
2620 | * Work out the number of bits of key we will need from the key |
2621 | * exchange. We start with the maximum key length of either |
2622 | * cipher... |
2623 | */ |
2624 | { |
2625 | int csbits, scbits; |
2626 | |
2627 | csbits = cscipher_tobe->keylen; |
2628 | scbits = sccipher_tobe->keylen; |
2629 | nbits = (csbits > scbits ? csbits : scbits); |
2630 | } |
2631 | /* The keys only have 160-bit entropy, since they're based on |
2632 | * a SHA-1 hash. So cap the key size at 160 bits. */ |
2633 | if (nbits > 160) nbits = 160; |
2634 | |
2635 | /* |
a92dd380 |
2636 | * If we're doing Diffie-Hellman group exchange, start by |
2637 | * requesting a group. |
e5574168 |
2638 | */ |
a92dd380 |
2639 | if (kex == &ssh_diffiehellman_gex) { |
a92dd380 |
2640 | logevent("Doing Diffie-Hellman group exchange"); |
2641 | /* |
7bd5a860 |
2642 | * Work out how big a DH group we will need to allow that |
2643 | * much data. |
2644 | */ |
2645 | pbits = 512 << ((nbits-1) / 64); |
a92dd380 |
2646 | ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST); |
7bd5a860 |
2647 | ssh2_pkt_adduint32(pbits); |
a92dd380 |
2648 | ssh2_pkt_send(); |
2649 | |
2650 | crWaitUntil(ispkt); |
2651 | if (pktin.type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
2652 | bombout(("expected key exchange group packet from server")); |
2653 | crReturn(0); |
2654 | } |
2655 | p = ssh2_pkt_getmp(); |
2656 | g = ssh2_pkt_getmp(); |
2657 | dh_setup_group(p, g); |
2658 | kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
2659 | kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
2660 | } else { |
2661 | dh_setup_group1(); |
2662 | kex_init_value = SSH2_MSG_KEXDH_INIT; |
2663 | kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
8d5de777 |
2664 | } |
e5574168 |
2665 | |
a92dd380 |
2666 | logevent("Doing Diffie-Hellman key exchange"); |
e5574168 |
2667 | /* |
a92dd380 |
2668 | * Now generate and send e for Diffie-Hellman. |
e5574168 |
2669 | */ |
7bd5a860 |
2670 | e = dh_create_e(nbits*2); |
a92dd380 |
2671 | ssh2_pkt_init(kex_init_value); |
e5574168 |
2672 | ssh2_pkt_addmp(e); |
2673 | ssh2_pkt_send(); |
2674 | |
2675 | crWaitUntil(ispkt); |
a92dd380 |
2676 | if (pktin.type != kex_reply_value) { |
2677 | bombout(("expected key exchange reply packet from server")); |
8d5de777 |
2678 | crReturn(0); |
7cca0d81 |
2679 | } |
e5574168 |
2680 | ssh2_pkt_getstring(&hostkeydata, &hostkeylen); |
2681 | f = ssh2_pkt_getmp(); |
e5574168 |
2682 | ssh2_pkt_getstring(&sigdata, &siglen); |
2683 | |
2684 | K = dh_find_K(f); |
e5574168 |
2685 | |
2686 | sha_string(&exhash, hostkeydata, hostkeylen); |
a92dd380 |
2687 | if (kex == &ssh_diffiehellman_gex) { |
7bd5a860 |
2688 | sha_uint32(&exhash, pbits); |
a92dd380 |
2689 | sha_mpint(&exhash, p); |
2690 | sha_mpint(&exhash, g); |
2691 | } |
e5574168 |
2692 | sha_mpint(&exhash, e); |
2693 | sha_mpint(&exhash, f); |
2694 | sha_mpint(&exhash, K); |
2695 | SHA_Final(&exhash, exchange_hash); |
2696 | |
3709bfe9 |
2697 | dh_cleanup(); |
2698 | |
7cca0d81 |
2699 | #if 0 |
e5574168 |
2700 | debug(("Exchange hash is:\r\n")); |
2701 | for (i = 0; i < 20; i++) |
2702 | debug((" %02x", exchange_hash[i])); |
2703 | debug(("\r\n")); |
7cca0d81 |
2704 | #endif |
2705 | |
e055a386 |
2706 | hkey = hostkey->newkey(hostkeydata, hostkeylen); |
2707 | if (!hostkey->verifysig(hkey, sigdata, siglen, exchange_hash, 20)) { |
8d5de777 |
2708 | bombout(("Server failed host key check")); |
2709 | crReturn(0); |
2710 | } |
e5574168 |
2711 | |
2712 | /* |
7cca0d81 |
2713 | * Expect SSH2_MSG_NEWKEYS from server. |
d39f364a |
2714 | */ |
7cca0d81 |
2715 | crWaitUntil(ispkt); |
8d5de777 |
2716 | if (pktin.type != SSH2_MSG_NEWKEYS) { |
2717 | bombout(("expected new-keys packet from server")); |
2718 | crReturn(0); |
2719 | } |
d39f364a |
2720 | |
2721 | /* |
7cca0d81 |
2722 | * Authenticate remote host: verify host key. (We've already |
2723 | * checked the signature of the exchange hash.) |
e5574168 |
2724 | */ |
e055a386 |
2725 | keystr = hostkey->fmtkey(hkey); |
2726 | fingerprint = hostkey->fingerprint(hkey); |
d4857987 |
2727 | verify_ssh_host_key(savedhost, savedport, hostkey->keytype, |
2728 | keystr, fingerprint); |
a92dd380 |
2729 | if (first_kex) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
2730 | logevent("Host key fingerprint is:"); |
2731 | logevent(fingerprint); |
2732 | } |
dcbde236 |
2733 | sfree(fingerprint); |
2734 | sfree(keystr); |
e055a386 |
2735 | hostkey->freekey(hkey); |
d39f364a |
2736 | |
2737 | /* |
7cca0d81 |
2738 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
2739 | */ |
2740 | ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
2741 | ssh2_pkt_send(); |
d39f364a |
2742 | |
2743 | /* |
2744 | * Create and initialise session keys. |
2745 | */ |
2746 | cscipher = cscipher_tobe; |
2747 | sccipher = sccipher_tobe; |
2748 | csmac = csmac_tobe; |
2749 | scmac = scmac_tobe; |
2750 | cscomp = cscomp_tobe; |
2751 | sccomp = sccomp_tobe; |
4ba9b64b |
2752 | cscomp->compress_init(); |
2753 | sccomp->decompress_init(); |
d39f364a |
2754 | /* |
5e0d7cb8 |
2755 | * Set IVs after keys. Here we use the exchange hash from the |
2756 | * _first_ key exchange. |
d39f364a |
2757 | */ |
5e0d7cb8 |
2758 | if (first_kex) |
65a22376 |
2759 | memcpy(ssh2_session_id, exchange_hash, sizeof(exchange_hash)); |
2760 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'C', keyspace); |
5e0d7cb8 |
2761 | cscipher->setcskey(keyspace); |
65a22376 |
2762 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'D', keyspace); |
5e0d7cb8 |
2763 | sccipher->setsckey(keyspace); |
65a22376 |
2764 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'A', keyspace); |
5e0d7cb8 |
2765 | cscipher->setcsiv(keyspace); |
65a22376 |
2766 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'B', keyspace); |
5e0d7cb8 |
2767 | sccipher->setsciv(keyspace); |
65a22376 |
2768 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'E', keyspace); |
5e0d7cb8 |
2769 | csmac->setcskey(keyspace); |
65a22376 |
2770 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'F', keyspace); |
5e0d7cb8 |
2771 | scmac->setsckey(keyspace); |
d39f364a |
2772 | |
033b4cef |
2773 | /* |
0db56f73 |
2774 | * If this is the first key exchange phase, we must pass the |
2775 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
2776 | * wants to see it but because it will need time to initialise |
2777 | * itself before it sees an actual packet. In subsequent key |
2778 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
2779 | * it would only confuse the layer above. |
2780 | */ |
2781 | if (!first_kex) { |
2782 | crReturn(0); |
2783 | } |
2784 | first_kex = 0; |
2785 | |
2786 | /* |
7cca0d81 |
2787 | * Now we're encrypting. Begin returning 1 to the protocol main |
2788 | * function so that other things can run on top of the |
2789 | * transport. If we ever see a KEXINIT, we must go back to the |
2790 | * start. |
033b4cef |
2791 | */ |
e96adf72 |
2792 | while (!(ispkt && pktin.type == SSH2_MSG_KEXINIT)) { |
7cca0d81 |
2793 | crReturn(1); |
e96adf72 |
2794 | } |
5e0d7cb8 |
2795 | logevent("Server initiated key re-exchange"); |
7cca0d81 |
2796 | goto begin_key_exchange; |
e5574168 |
2797 | |
2798 | crFinish(1); |
2799 | } |
2800 | |
7cca0d81 |
2801 | /* |
783415f8 |
2802 | * Add data to an SSH2 channel output buffer. |
2803 | */ |
2804 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len) { |
2805 | if (c->v2.outbufsize < |
2806 | c->v2.outbuflen + len) { |
2807 | c->v2.outbufsize = |
2808 | c->v2.outbuflen + len + 1024; |
2809 | c->v2.outbuffer = srealloc(c->v2.outbuffer, |
2810 | c->v2.outbufsize); |
2811 | } |
2812 | memcpy(c->v2.outbuffer + c->v2.outbuflen, |
2813 | buf, len); |
2814 | c->v2.outbuflen += len; |
2815 | } |
2816 | |
2817 | /* |
2818 | * Attempt to send data on an SSH2 channel. |
2819 | */ |
2820 | static void ssh2_try_send(struct ssh_channel *c) { |
2821 | while (c->v2.remwindow > 0 && |
2822 | c->v2.outbuflen > 0) { |
2823 | unsigned len = c->v2.remwindow; |
2824 | if (len > c->v2.outbuflen) |
2825 | len = c->v2.outbuflen; |
2826 | if (len > c->v2.remmaxpkt) |
2827 | len = c->v2.remmaxpkt; |
2828 | ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
2829 | ssh2_pkt_adduint32(c->remoteid); |
2830 | ssh2_pkt_addstring_start(); |
2831 | ssh2_pkt_addstring_data(c->v2.outbuffer, len); |
2832 | ssh2_pkt_send(); |
2833 | c->v2.outbuflen -= len; |
2834 | memmove(c->v2.outbuffer, c->v2.outbuffer+len, |
2835 | c->v2.outbuflen); |
2836 | c->v2.remwindow -= len; |
2837 | } |
2838 | } |
2839 | |
2840 | /* |
7cca0d81 |
2841 | * Handle the SSH2 userauth and connection layers. |
2842 | */ |
2843 | static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt) |
2844 | { |
7cca0d81 |
2845 | static unsigned long remote_winsize; |
2846 | static unsigned long remote_maxpkt; |
65a22376 |
2847 | static enum { |
2848 | AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, AUTH_PASSWORD |
2849 | } method; |
1408a877 |
2850 | static enum { |
2851 | AUTH_TYPE_NONE, |
2852 | AUTH_TYPE_PUBLICKEY, |
2853 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
2854 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
2855 | AUTH_TYPE_PASSWORD |
2856 | } type; |
1983e559 |
2857 | static int gotit, need_pw, can_pubkey, can_passwd; |
2858 | static int tried_pubkey_config, tried_agent; |
1408a877 |
2859 | static int we_are_in; |
65a22376 |
2860 | static char username[100]; |
2861 | static char pwprompt[200]; |
2862 | static char password[100]; |
7cca0d81 |
2863 | |
e5574168 |
2864 | crBegin; |
2865 | |
7cca0d81 |
2866 | /* |
2867 | * Request userauth protocol, and await a response to it. |
2868 | */ |
2869 | ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
2870 | ssh2_pkt_addstring("ssh-userauth"); |
2871 | ssh2_pkt_send(); |
2872 | crWaitUntilV(ispkt); |
8d5de777 |
2873 | if (pktin.type != SSH2_MSG_SERVICE_ACCEPT) { |
2874 | bombout(("Server refused user authentication protocol")); |
2875 | crReturnV; |
2876 | } |
7cca0d81 |
2877 | |
2878 | /* |
1408a877 |
2879 | * We repeat this whole loop, including the username prompt, |
2880 | * until we manage a successful authentication. If the user |
2881 | * types the wrong _password_, they are sent back to the |
2882 | * beginning to try another username. (If they specify a |
2883 | * username in the config, they are never asked, even if they |
2884 | * do give a wrong password.) |
2885 | * |
2886 | * I think this best serves the needs of |
2887 | * |
2888 | * - the people who have no configuration, no keys, and just |
2889 | * want to try repeated (username,password) pairs until they |
2890 | * type both correctly |
2891 | * |
2892 | * - people who have keys and configuration but occasionally |
2893 | * need to fall back to passwords |
2894 | * |
2895 | * - people with a key held in Pageant, who might not have |
2896 | * logged in to a particular machine before; so they want to |
2897 | * type a username, and then _either_ their key will be |
2898 | * accepted, _or_ they will type a password. If they mistype |
2899 | * the username they will want to be able to get back and |
2900 | * retype it! |
7cca0d81 |
2901 | */ |
1408a877 |
2902 | do { |
2903 | static int pos; |
7cca0d81 |
2904 | static char c; |
e5574168 |
2905 | |
1408a877 |
2906 | /* |
2907 | * Get a username. |
2908 | */ |
2909 | pos = 0; |
67779be7 |
2910 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
fa17a66e |
2911 | if (ssh_get_line) { |
2912 | if (!ssh_get_line("login as: ", |
2913 | username, sizeof(username), FALSE)) { |
2914 | /* |
2915 | * get_line failed to get a username. |
2916 | * Terminate. |
2917 | */ |
2918 | logevent("No username provided. Abandoning session."); |
2919 | ssh_state = SSH_STATE_CLOSED; |
dae2b91f |
2920 | crReturnV; |
fa17a66e |
2921 | } |
2922 | } else { |
2923 | c_write_str("login as: "); |
2924 | ssh_send_ok = 1; |
2925 | while (pos >= 0) { |
2926 | crWaitUntilV(!ispkt); |
2927 | while (inlen--) switch (c = *in++) { |
2928 | case 10: case 13: |
2929 | username[pos] = 0; |
2930 | pos = -1; |
2931 | break; |
2932 | case 8: case 127: |
2933 | if (pos > 0) { |
2934 | c_write_str("\b \b"); |
2935 | pos--; |
2936 | } |
2937 | break; |
2938 | case 21: case 27: |
2939 | while (pos > 0) { |
2940 | c_write_str("\b \b"); |
2941 | pos--; |
2942 | } |
2943 | break; |
2944 | case 3: case 4: |
2945 | random_save_seed(); |
2946 | exit(0); |
2947 | break; |
2948 | default: |
2949 | if (((c >= ' ' && c <= '~') || |
2950 | ((unsigned char)c >= 160)) && pos < 40) { |
2951 | username[pos++] = c; |
2952 | c_write(&c, 1); |
2953 | } |
2954 | break; |
2955 | } |
2956 | } |
2957 | } |
1408a877 |
2958 | c_write_str("\r\n"); |
7cca0d81 |
2959 | username[strcspn(username, "\n\r")] = '\0'; |
2960 | } else { |
2961 | char stuff[200]; |
2962 | strncpy(username, cfg.username, 99); |
2963 | username[99] = '\0'; |
65a22376 |
2964 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
7cca0d81 |
2965 | sprintf(stuff, "Using username \"%s\".\r\n", username); |
1408a877 |
2966 | c_write_str(stuff); |
7cca0d81 |
2967 | } |
2968 | } |
2969 | |
65a22376 |
2970 | /* |
1408a877 |
2971 | * Send an authentication request using method "none": (a) |
2972 | * just in case it succeeds, and (b) so that we know what |
2973 | * authentication methods we can usefully try next. |
65a22376 |
2974 | */ |
1408a877 |
2975 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
2976 | ssh2_pkt_addstring(username); |
2977 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
2978 | ssh2_pkt_addstring("none"); /* method */ |
2979 | ssh2_pkt_send(); |
2980 | type = AUTH_TYPE_NONE; |
2981 | gotit = FALSE; |
2982 | we_are_in = FALSE; |
7cca0d81 |
2983 | |
1408a877 |
2984 | tried_pubkey_config = FALSE; |
1983e559 |
2985 | tried_agent = FALSE; |
65a22376 |
2986 | |
1408a877 |
2987 | while (1) { |
2988 | /* |
2989 | * Wait for the result of the last authentication request. |
2990 | */ |
2991 | if (!gotit) |
2992 | crWaitUntilV(ispkt); |
2993 | while (pktin.type == SSH2_MSG_USERAUTH_BANNER) { |
a209e957 |
2994 | char *banner; |
2995 | int size; |
10446215 |
2996 | /* |
2997 | * Don't show the banner if we're operating in |
2998 | * non-verbose non-interactive mode. (It's probably |
2999 | * a script, which means nobody will read the |
3000 | * banner _anyway_, and moreover the printing of |
3001 | * the banner will screw up processing on the |
3002 | * output of (say) plink.) |
3003 | */ |
3004 | if (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE)) { |
3005 | ssh2_pkt_getstring(&banner, &size); |
3006 | if (banner) |
3007 | c_write_untrusted(banner, size); |
3008 | } |
1408a877 |
3009 | crWaitUntilV(ispkt); |
3010 | } |
3011 | if (pktin.type == SSH2_MSG_USERAUTH_SUCCESS) { |
3012 | logevent("Access granted"); |
3013 | we_are_in = TRUE; |
3014 | break; |
3015 | } |
65a22376 |
3016 | |
1408a877 |
3017 | if (pktin.type != SSH2_MSG_USERAUTH_FAILURE) { |
3018 | bombout(("Strange packet received during authentication: type %d", |
3019 | pktin.type)); |
65a22376 |
3020 | } |
3021 | |
1408a877 |
3022 | gotit = FALSE; |
65a22376 |
3023 | |
1408a877 |
3024 | /* |
3025 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
3026 | * we can look at the string in it and know what we can |
3027 | * helpfully try next. |
3028 | */ |
3029 | { |
3030 | char *methods; |
3031 | int methlen; |
3032 | ssh2_pkt_getstring(&methods, &methlen); |
3033 | if (!ssh2_pkt_getbool()) { |
3034 | /* |
3035 | * We have received an unequivocal Access |
3036 | * Denied. This can translate to a variety of |
3037 | * messages: |
3038 | * |
3039 | * - if we'd just tried "none" authentication, |
3040 | * it's not worth printing anything at all |
3041 | * |
3042 | * - if we'd just tried a public key _offer_, |
3043 | * the message should be "Server refused our |
3044 | * key" (or no message at all if the key |
3045 | * came from Pageant) |
3046 | * |
3047 | * - if we'd just tried anything else, the |
3048 | * message really should be "Access denied". |
3049 | * |
3050 | * Additionally, if we'd just tried password |
3051 | * authentication, we should break out of this |
3052 | * whole loop so as to go back to the username |
3053 | * prompt. |
3054 | */ |
3055 | if (type == AUTH_TYPE_NONE) { |
3056 | /* do nothing */ |
3057 | } else if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
3058 | type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
3059 | if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
3060 | c_write_str("Server refused our key\r\n"); |
3061 | logevent("Server refused public key"); |
3062 | } else { |
3063 | c_write_str("Access denied\r\n"); |
3064 | logevent("Access denied"); |
3065 | if (type == AUTH_TYPE_PASSWORD) { |
3066 | we_are_in = FALSE; |
3067 | break; |
3068 | } |
3069 | } |
3070 | } else { |
3071 | c_write_str("Further authentication required\r\n"); |
3072 | logevent("Further authentication required"); |
3073 | } |
65a22376 |
3074 | |
1408a877 |
3075 | can_pubkey = in_commasep_string("publickey", methods, methlen); |
3076 | can_passwd = in_commasep_string("password", methods, methlen); |
3077 | } |
65a22376 |
3078 | |
1408a877 |
3079 | method = 0; |
65a22376 |
3080 | |
1983e559 |
3081 | if (!method && can_pubkey && agent_exists && !tried_agent) { |
3082 | /* |
3083 | * Attempt public-key authentication using Pageant. |
3084 | */ |
3085 | static unsigned char request[5], *response, *p; |
3086 | static int responselen; |
3087 | static int i, nkeys; |
3088 | static int authed = FALSE; |
3089 | void *r; |
3090 | |
3091 | tried_agent = TRUE; |
3092 | |
3093 | logevent("Pageant is running. Requesting keys."); |
3094 | |
3095 | /* Request the keys held by the agent. */ |
3096 | PUT_32BIT(request, 1); |
3097 | request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
3098 | agent_query(request, 5, &r, &responselen); |
3099 | response = (unsigned char *)r; |
95797139 |
3100 | if (response && responselen >= 5 && |
3101 | response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
1983e559 |
3102 | p = response + 5; |
3103 | nkeys = GET_32BIT(p); p += 4; |
6328ea7a |
3104 | { char buf[64]; sprintf(buf, "Pageant has %d SSH2 keys", nkeys); |
1983e559 |
3105 | logevent(buf); } |
3106 | for (i = 0; i < nkeys; i++) { |
3107 | static char *pkblob, *alg, *commentp; |
3108 | static int pklen, alglen, commentlen; |
3109 | static int siglen, retlen, len; |
3110 | static char *q, *agentreq, *ret; |
3111 | |
3112 | { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i); |
3113 | logevent(buf); } |
3114 | pklen = GET_32BIT(p); p += 4; |
3115 | pkblob = p; p += pklen; |
3116 | alglen = GET_32BIT(pkblob); |
3117 | alg = pkblob + 4; |
3118 | commentlen = GET_32BIT(p); p += 4; |
3119 | commentp = p; p += commentlen; |
3120 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3121 | ssh2_pkt_addstring(username); |
3122 | ssh2_pkt_addstring("ssh-connection");/* service requested */ |
3123 | ssh2_pkt_addstring("publickey");/* method */ |
3124 | ssh2_pkt_addbool(FALSE); /* no signature included */ |
3125 | ssh2_pkt_addstring_start(); |
3126 | ssh2_pkt_addstring_data(alg, alglen); |
3127 | ssh2_pkt_addstring_start(); |
3128 | ssh2_pkt_addstring_data(pkblob, pklen); |
3129 | ssh2_pkt_send(); |
3130 | |
3131 | crWaitUntilV(ispkt); |
3132 | if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
3133 | logevent("Key refused"); |
3134 | continue; |
3135 | } |
3136 | |
10446215 |
3137 | if (flags & FLAG_VERBOSE) { |
3138 | c_write_str("Authenticating with public key \""); |
3139 | c_write(commentp, commentlen); |
3140 | c_write_str("\" from agent\r\n"); |
3141 | } |
1983e559 |
3142 | |
3143 | /* |
3144 | * Server is willing to accept the key. |
3145 | * Construct a SIGN_REQUEST. |
3146 | */ |
3147 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3148 | ssh2_pkt_addstring(username); |
3149 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3150 | ssh2_pkt_addstring("publickey"); /* method */ |
3151 | ssh2_pkt_addbool(TRUE); |
3152 | ssh2_pkt_addstring_start(); |
3153 | ssh2_pkt_addstring_data(alg, alglen); |
3154 | ssh2_pkt_addstring_start(); |
3155 | ssh2_pkt_addstring_data(pkblob, pklen); |
3156 | |
3157 | siglen = pktout.length - 5 + 4 + 20; |
3158 | len = 1; /* message type */ |
3159 | len += 4 + pklen; /* key blob */ |
3160 | len += 4 + siglen; /* data to sign */ |
3161 | len += 4; /* flags */ |
3162 | agentreq = smalloc(4 + len); |
3163 | PUT_32BIT(agentreq, len); |
3164 | q = agentreq + 4; |
3165 | *q++ = SSH2_AGENTC_SIGN_REQUEST; |
3166 | PUT_32BIT(q, pklen); q += 4; |
3167 | memcpy(q, pkblob, pklen); q += pklen; |
3168 | PUT_32BIT(q, siglen); q += 4; |
3169 | /* Now the data to be signed... */ |
3170 | PUT_32BIT(q, 20); q += 4; |
3171 | memcpy(q, ssh2_session_id, 20); q += 20; |
3172 | memcpy(q, pktout.data+5, pktout.length-5); |
3173 | q += pktout.length-5; |
3174 | /* And finally the (zero) flags word. */ |
3175 | PUT_32BIT(q, 0); |
3176 | agent_query(agentreq, len+4, &ret, &retlen); |
3177 | sfree(agentreq); |
3178 | if (ret) { |
3179 | if (ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
3180 | logevent("Sending Pageant's response"); |
3181 | ssh2_pkt_addstring_start(); |
3182 | ssh2_pkt_addstring_data(ret+9, GET_32BIT(ret+5)); |
3183 | ssh2_pkt_send(); |
3184 | authed = TRUE; |
3185 | break; |
3186 | } else { |
3187 | logevent("Pageant failed to answer challenge"); |
3188 | sfree(ret); |
3189 | } |
3190 | } |
3191 | } |
3192 | if (authed) |
3193 | continue; |
3194 | } |
3195 | } |
3196 | |
1408a877 |
3197 | if (!method && can_pubkey && *cfg.keyfile && !tried_pubkey_config) { |
3198 | unsigned char *pub_blob; |
3199 | char *algorithm, *comment; |
3200 | int pub_blob_len; |
65a22376 |
3201 | |
1408a877 |
3202 | tried_pubkey_config = TRUE; |
65a22376 |
3203 | |
65a22376 |
3204 | /* |
1408a877 |
3205 | * Try the public key supplied in the configuration. |
3206 | * |
3207 | * First, offer the public blob to see if the server is |
3208 | * willing to accept it. |
65a22376 |
3209 | */ |
1408a877 |
3210 | pub_blob = ssh2_userkey_loadpub(cfg.keyfile, &algorithm, |
3211 | &pub_blob_len); |
3212 | if (pub_blob) { |
3213 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3214 | ssh2_pkt_addstring(username); |
3215 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3216 | ssh2_pkt_addstring("publickey");/* method */ |
3217 | ssh2_pkt_addbool(FALSE); /* no signature included */ |
3218 | ssh2_pkt_addstring(algorithm); |
3219 | ssh2_pkt_addstring_start(); |
3220 | ssh2_pkt_addstring_data(pub_blob, pub_blob_len); |
3221 | ssh2_pkt_send(); |
3222 | logevent("Offered public key"); /* FIXME */ |
3223 | |
3224 | crWaitUntilV(ispkt); |
3225 | if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
3226 | gotit = TRUE; |
3227 | type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
3228 | continue; /* key refused; give up on it */ |
3229 | } |
65a22376 |
3230 | |
1408a877 |
3231 | logevent("Offer of public key accepted"); |
65a22376 |
3232 | /* |
1408a877 |
3233 | * Actually attempt a serious authentication using |
3234 | * the key. |
65a22376 |
3235 | */ |
1408a877 |
3236 | if (ssh2_userkey_encrypted(cfg.keyfile, &comment)) { |
3237 | sprintf(pwprompt, "Passphrase for key \"%.100s\": ", comment); |
3238 | need_pw = TRUE; |
3239 | } else { |
3240 | need_pw = FALSE; |
3241 | } |
3242 | c_write_str("Authenticating with public key \""); |
3243 | c_write_str(comment); |
3244 | c_write_str("\"\r\n"); |
3245 | method = AUTH_PUBLICKEY_FILE; |
65a22376 |
3246 | } |
1408a877 |
3247 | } |
3248 | |
3249 | if (!method && can_passwd) { |
3250 | method = AUTH_PASSWORD; |
3251 | sprintf(pwprompt, "%.90s@%.90s's password: ", username, savedhost); |
3252 | need_pw = TRUE; |
3253 | } |
3254 | |
3255 | if (need_pw) { |
fa17a66e |
3256 | if (ssh_get_line) { |
3257 | if (!ssh_get_line(pwprompt, password, |
3258 | sizeof(password), TRUE)) { |
1408a877 |
3259 | /* |
fa17a66e |
3260 | * get_line failed to get a password (for |
3261 | * example because one was supplied on the |
3262 | * command line which has already failed to |
3263 | * work). Terminate. |
1408a877 |
3264 | */ |
3265 | logevent("No more passwords to try"); |
3266 | ssh_state = SSH_STATE_CLOSED; |
3267 | crReturnV; |
3268 | } |
3269 | } else { |
3270 | static int pos = 0; |
3271 | static char c; |
3272 | |
3273 | c_write_str(pwprompt); |
3274 | ssh_send_ok = 1; |
3275 | |
3276 | pos = 0; |
3277 | while (pos >= 0) { |
3278 | crWaitUntilV(!ispkt); |
3279 | while (inlen--) switch (c = *in++) { |
3280 | case 10: case 13: |
3281 | password[pos] = 0; |
3282 | pos = -1; |
3283 | break; |
3284 | case 8: case 127: |
3285 | if (pos > 0) |
3286 | pos--; |
3287 | break; |
3288 | case 21: case 27: |
3289 | pos = 0; |
3290 | break; |
3291 | case 3: case 4: |
3292 | random_save_seed(); |
3293 | exit(0); |
3294 | break; |
3295 | default: |
3296 | if (((c >= ' ' && c <= '~') || |
3297 | ((unsigned char)c >= 160)) && pos < 40) |
3298 | password[pos++] = c; |
3299 | break; |
3300 | } |
65a22376 |
3301 | } |
1408a877 |
3302 | c_write_str("\r\n"); |
65a22376 |
3303 | } |
65a22376 |
3304 | } |
65a22376 |
3305 | |
1408a877 |
3306 | if (method == AUTH_PUBLICKEY_FILE) { |
3307 | /* |
3308 | * We have our passphrase. Now try the actual authentication. |
3309 | */ |
3310 | struct ssh2_userkey *key; |
65a22376 |
3311 | |
1408a877 |
3312 | key = ssh2_load_userkey(cfg.keyfile, password); |
3313 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
3314 | if (key == SSH2_WRONG_PASSPHRASE) { |
3315 | c_write_str("Wrong passphrase\r\n"); |
3316 | tried_pubkey_config = FALSE; |
3317 | } else { |
3318 | c_write_str("Unable to load private key\r\n"); |
3319 | tried_pubkey_config = TRUE; |
3320 | } |
3321 | /* Send a spurious AUTH_NONE to return to the top. */ |
3322 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3323 | ssh2_pkt_addstring(username); |
3324 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3325 | ssh2_pkt_addstring("none"); /* method */ |
3326 | ssh2_pkt_send(); |
3327 | type = AUTH_TYPE_NONE; |
3328 | } else { |
3329 | unsigned char *blob, *sigdata; |
3330 | int blob_len, sigdata_len; |
65a22376 |
3331 | |
1408a877 |
3332 | /* |
3333 | * We have loaded the private key and the server |
3334 | * has announced that it's willing to accept it. |
3335 | * Hallelujah. Generate a signature and send it. |
3336 | */ |
3337 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3338 | ssh2_pkt_addstring(username); |
3339 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3340 | ssh2_pkt_addstring("publickey"); /* method */ |
3341 | ssh2_pkt_addbool(TRUE); |
3342 | ssh2_pkt_addstring(key->alg->name); |
3343 | blob = key->alg->public_blob(key->data, &blob_len); |
3344 | ssh2_pkt_addstring_start(); |
3345 | ssh2_pkt_addstring_data(blob, blob_len); |
3346 | sfree(blob); |
3347 | |
3348 | /* |
3349 | * The data to be signed is: |
3350 | * |
3351 | * string session-id |
3352 | * |
3353 | * followed by everything so far placed in the |
3354 | * outgoing packet. |
3355 | */ |
3356 | sigdata_len = pktout.length - 5 + 4 + 20; |
3357 | sigdata = smalloc(sigdata_len); |
3358 | PUT_32BIT(sigdata, 20); |
3359 | memcpy(sigdata+4, ssh2_session_id, 20); |
3360 | memcpy(sigdata+24, pktout.data+5, pktout.length-5); |
3361 | blob = key->alg->sign(key->data, sigdata, sigdata_len, &blob_len); |
3362 | ssh2_pkt_addstring_start(); |
3363 | ssh2_pkt_addstring_data(blob, blob_len); |
3364 | sfree(blob); |
3365 | sfree(sigdata); |
3366 | |
3367 | ssh2_pkt_send(); |
3368 | type = AUTH_TYPE_PUBLICKEY; |
3369 | } |
3370 | } else if (method == AUTH_PASSWORD) { |
65a22376 |
3371 | /* |
1408a877 |
3372 | * We send the password packet lumped tightly together with |
3373 | * an SSH_MSG_IGNORE packet. The IGNORE packet contains a |
3374 | * string long enough to make the total length of the two |
3375 | * packets constant. This should ensure that a passive |
3376 | * listener doing traffic analyis can't work out the length |
3377 | * of the password. |
3378 | * |
3379 | * For this to work, we need an assumption about the |
3380 | * maximum length of the password packet. I think 256 is |
3381 | * pretty conservative. Anyone using a password longer than |
3382 | * that probably doesn't have much to worry about from |
3383 | * people who find out how long their password is! |
65a22376 |
3384 | */ |
3385 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3386 | ssh2_pkt_addstring(username); |
3387 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
1408a877 |
3388 | ssh2_pkt_addstring("password"); |
3389 | ssh2_pkt_addbool(FALSE); |
3390 | ssh2_pkt_addstring(password); |
3391 | ssh2_pkt_defer(); |
65a22376 |
3392 | /* |
1408a877 |
3393 | * We'll include a string that's an exact multiple of the |
3394 | * cipher block size. If the cipher is NULL for some |
3395 | * reason, we don't do this trick at all because we gain |
3396 | * nothing by it. |
65a22376 |
3397 | */ |
6e9e9520 |
3398 | if (cscipher) { |
3399 | int stringlen, i; |
3400 | |
3401 | stringlen = (256 - deferred_len); |
3402 | stringlen += cscipher->blksize - 1; |
3403 | stringlen -= (stringlen % cscipher->blksize); |
3404 | if (cscomp) { |
3405 | /* |
3406 | * Temporarily disable actual compression, |
3407 | * so we can guarantee to get this string |
3408 | * exactly the length we want it. The |
3409 | * compression-disabling routine should |
3410 | * return an integer indicating how many |
3411 | * bytes we should adjust our string length |
3412 | * by. |
3413 | */ |
3414 | stringlen -= cscomp->disable_compression(); |
3415 | } |
1408a877 |
3416 | ssh2_pkt_init(SSH2_MSG_IGNORE); |
3417 | ssh2_pkt_addstring_start(); |
6e9e9520 |
3418 | for (i = 0; i < stringlen; i++) { |
3419 | char c = (char)random_byte(); |
3420 | ssh2_pkt_addstring_data(&c, 1); |
65a22376 |
3421 | } |
1408a877 |
3422 | ssh2_pkt_defer(); |
65a22376 |
3423 | } |
39065bed |
3424 | ssh_pkt_defersend(); |
0d43337a |
3425 | logevent("Sent password"); |
1408a877 |
3426 | type = AUTH_TYPE_PASSWORD; |
3427 | } else { |
3428 | c_write_str("No supported authentication methods left to try!\r\n"); |
3429 | logevent("No supported authentications offered. Disconnecting"); |
3430 | ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
3431 | ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION); |
3432 | ssh2_pkt_addstring("No supported authentication methods available"); |
3433 | ssh2_pkt_addstring("en"); /* language tag */ |
3434 | ssh2_pkt_send(); |
3435 | ssh_state = SSH_STATE_CLOSED; |
3436 | crReturnV; |
65a22376 |
3437 | } |
65a22376 |
3438 | } |
1408a877 |
3439 | } while (!we_are_in); |
7cca0d81 |
3440 | |
3441 | /* |
3442 | * Now we're authenticated for the connection protocol. The |
3443 | * connection protocol will automatically have started at this |
3444 | * point; there's no need to send SERVICE_REQUEST. |
3445 | */ |
3446 | |
3447 | /* |
3448 | * So now create a channel with a session in it. |
3449 | */ |
dcbde236 |
3450 | mainchan = smalloc(sizeof(struct ssh_channel)); |
d211621f |
3451 | mainchan->localid = 100; /* as good as any */ |
7cca0d81 |
3452 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
3453 | ssh2_pkt_addstring("session"); |
d211621f |
3454 | ssh2_pkt_adduint32(mainchan->localid); |
cf6e59d6 |
3455 | ssh2_pkt_adduint32(0x8000UL); /* our window size */ |
d211621f |
3456 | ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */ |
7cca0d81 |
3457 | ssh2_pkt_send(); |
3458 | crWaitUntilV(ispkt); |
3459 | if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
8d5de777 |
3460 | bombout(("Server refused to open a session")); |
3461 | crReturnV; |
7cca0d81 |
3462 | /* FIXME: error data comes back in FAILURE packet */ |
3463 | } |
d211621f |
3464 | if (ssh2_pkt_getuint32() != mainchan->localid) { |
8d5de777 |
3465 | bombout(("Server's channel confirmation cited wrong channel")); |
3466 | crReturnV; |
7cca0d81 |
3467 | } |
d211621f |
3468 | mainchan->remoteid = ssh2_pkt_getuint32(); |
783415f8 |
3469 | mainchan->type = CHAN_MAINSESSION; |
3470 | mainchan->closes = 0; |
3471 | mainchan->v2.remwindow = ssh2_pkt_getuint32(); |
3472 | mainchan->v2.remmaxpkt = ssh2_pkt_getuint32(); |
3473 | mainchan->v2.outbuffer = NULL; |
3474 | mainchan->v2.outbuflen = mainchan->v2.outbufsize = 0; |
3475 | ssh_channels = newtree234(ssh_channelcmp); |
3476 | add234(ssh_channels, mainchan); |
7cca0d81 |
3477 | logevent("Opened channel for session"); |
3478 | |
3479 | /* |
783415f8 |
3480 | * Potentially enable X11 forwarding. |
3481 | */ |
3482 | if (cfg.x11_forward) { |
3483 | char proto[20], data[64]; |
3484 | logevent("Requesting X11 forwarding"); |
3485 | x11_invent_auth(proto, sizeof(proto), data, sizeof(data)); |
3486 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3487 | ssh2_pkt_adduint32(mainchan->remoteid); |
3488 | ssh2_pkt_addstring("x11-req"); |
3489 | ssh2_pkt_addbool(1); /* want reply */ |
3490 | ssh2_pkt_addbool(0); /* many connections */ |
3491 | ssh2_pkt_addstring(proto); |
3492 | ssh2_pkt_addstring(data); |
3493 | ssh2_pkt_adduint32(0); /* screen number */ |
3494 | ssh2_pkt_send(); |
3495 | |
3496 | do { |
3497 | crWaitUntilV(ispkt); |
3498 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
3499 | unsigned i = ssh2_pkt_getuint32(); |
3500 | struct ssh_channel *c; |
3501 | c = find234(ssh_channels, &i, ssh_channelfind); |
3502 | if (!c) |
3503 | continue; /* nonexistent channel */ |
3504 | c->v2.remwindow += ssh2_pkt_getuint32(); |
3505 | } |
3506 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3507 | |
3508 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3509 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
3510 | bombout(("Server got confused by X11 forwarding request")); |
3511 | crReturnV; |
3512 | } |
3513 | logevent("X11 forwarding refused"); |
3514 | } else { |
3515 | logevent("X11 forwarding enabled"); |
3516 | ssh_X11_fwd_enabled = TRUE; |
3517 | } |
3518 | } |
3519 | |
3520 | /* |
36c2a3e9 |
3521 | * Potentially enable agent forwarding. |
3522 | */ |
3523 | if (cfg.agentfwd && agent_exists()) { |
36c2a3e9 |
3524 | logevent("Requesting OpenSSH-style agent forwarding"); |
36c2a3e9 |
3525 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3526 | ssh2_pkt_adduint32(mainchan->remoteid); |
3527 | ssh2_pkt_addstring("auth-agent-req@openssh.com"); |
3528 | ssh2_pkt_addbool(1); /* want reply */ |
3529 | ssh2_pkt_send(); |
3530 | |
3531 | do { |
3532 | crWaitUntilV(ispkt); |
3533 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
3534 | unsigned i = ssh2_pkt_getuint32(); |
3535 | struct ssh_channel *c; |
3536 | c = find234(ssh_channels, &i, ssh_channelfind); |
3537 | if (!c) |
3538 | continue; /* nonexistent channel */ |
3539 | c->v2.remwindow += ssh2_pkt_getuint32(); |
3540 | } |
3541 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3542 | |
3543 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3544 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
3545 | bombout(("Server got confused by agent forwarding request")); |
3546 | crReturnV; |
3547 | } |
3548 | logevent("Agent forwarding refused"); |
3549 | } else { |
3550 | logevent("Agent forwarding enabled"); |
3551 | ssh_agentfwd_enabled = TRUE; |
3552 | } |
3553 | } |
3554 | |
3555 | /* |
7cca0d81 |
3556 | * Now allocate a pty for the session. |
3557 | */ |
67779be7 |
3558 | if (!cfg.nopty) { |
3559 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3560 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
3561 | ssh2_pkt_addstring("pty-req"); |
3562 | ssh2_pkt_addbool(1); /* want reply */ |
3563 | ssh2_pkt_addstring(cfg.termtype); |
3564 | ssh2_pkt_adduint32(cols); |
3565 | ssh2_pkt_adduint32(rows); |
3566 | ssh2_pkt_adduint32(0); /* pixel width */ |
3567 | ssh2_pkt_adduint32(0); /* pixel height */ |
3568 | ssh2_pkt_addstring_start(); |
3569 | ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */ |
3570 | ssh2_pkt_send(); |
6e48c3fe |
3571 | ssh_state = SSH_STATE_INTERMED; |
7cca0d81 |
3572 | |
67779be7 |
3573 | do { |
3574 | crWaitUntilV(ispkt); |
3575 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3576 | unsigned i = ssh2_pkt_getuint32(); |
3577 | struct ssh_channel *c; |
3578 | c = find234(ssh_channels, &i, ssh_channelfind); |
3579 | if (!c) |
3580 | continue; /* nonexistent channel */ |
3581 | c->v2.remwindow += ssh2_pkt_getuint32(); |
67779be7 |
3582 | } |
3583 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
7cca0d81 |
3584 | |
67779be7 |
3585 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3586 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
3587 | bombout(("Server got confused by pty request")); |
3588 | crReturnV; |
67779be7 |
3589 | } |
1408a877 |
3590 | c_write_str("Server refused to allocate pty\r\n"); |
0965bee0 |
3591 | ssh_editing = ssh_echoing = 1; |
67779be7 |
3592 | } else { |
3593 | logevent("Allocated pty"); |
7cca0d81 |
3594 | } |
0965bee0 |
3595 | } else { |
3596 | ssh_editing = ssh_echoing = 1; |
7cca0d81 |
3597 | } |
3598 | |
3599 | /* |
67779be7 |
3600 | * Start a shell or a remote command. |
7cca0d81 |
3601 | */ |
3602 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
d211621f |
3603 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
4a8fc3c4 |
3604 | if (cfg.ssh_subsys) { |
3605 | ssh2_pkt_addstring("subsystem"); |
3606 | ssh2_pkt_addbool(1); /* want reply */ |
96621a84 |
3607 | ssh2_pkt_addstring(cfg.remote_cmd_ptr); |
3608 | } else if (*cfg.remote_cmd_ptr) { |
67779be7 |
3609 | ssh2_pkt_addstring("exec"); |
3610 | ssh2_pkt_addbool(1); /* want reply */ |
96621a84 |
3611 | ssh2_pkt_addstring(cfg.remote_cmd_ptr); |
67779be7 |
3612 | } else { |
3613 | ssh2_pkt_addstring("shell"); |
3614 | ssh2_pkt_addbool(1); /* want reply */ |
3615 | } |
7cca0d81 |
3616 | ssh2_pkt_send(); |
d211621f |
3617 | do { |
7cca0d81 |
3618 | crWaitUntilV(ispkt); |
d211621f |
3619 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3620 | unsigned i = ssh2_pkt_getuint32(); |
3621 | struct ssh_channel *c; |
3622 | c = find234(ssh_channels, &i, ssh_channelfind); |
3623 | if (!c) |
3624 | continue; /* nonexistent channel */ |
3625 | c->v2.remwindow += ssh2_pkt_getuint32(); |
d211621f |
3626 | } |
7cca0d81 |
3627 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3628 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3629 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
3630 | bombout(("Server got confused by shell/command request")); |
3631 | crReturnV; |
7cca0d81 |
3632 | } |
8d5de777 |
3633 | bombout(("Server refused to start a shell/command")); |
3634 | crReturnV; |
7cca0d81 |
3635 | } else { |
8d5de777 |
3636 | logevent("Started a shell/command"); |
7cca0d81 |
3637 | } |
3638 | |
6e48c3fe |
3639 | ssh_state = SSH_STATE_SESSION; |
3640 | if (size_needed) |
3641 | ssh_size(); |
3687d221 |
3642 | if (eof_needed) |
3643 | ssh_special(TS_EOF); |
6e48c3fe |
3644 | |
7cca0d81 |
3645 | /* |
3646 | * Transfer data! |
3647 | */ |
0965bee0 |
3648 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
8ccc75b0 |
3649 | ssh_send_ok = 1; |
7cca0d81 |
3650 | while (1) { |
d211621f |
3651 | static int try_send; |
e5574168 |
3652 | crReturnV; |
d211621f |
3653 | try_send = FALSE; |
7cca0d81 |
3654 | if (ispkt) { |
3655 | if (pktin.type == SSH2_MSG_CHANNEL_DATA || |
3656 | pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
3657 | char *data; |
3658 | int length; |
783415f8 |
3659 | unsigned i = ssh2_pkt_getuint32(); |
3660 | struct ssh_channel *c; |
3661 | c = find234(ssh_channels, &i, ssh_channelfind); |
3662 | if (!c) |
3663 | continue; /* nonexistent channel */ |
7cca0d81 |
3664 | if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
3665 | ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR) |
3666 | continue; /* extended but not stderr */ |
3667 | ssh2_pkt_getstring(&data, &length); |
d211621f |
3668 | if (data) { |
783415f8 |
3669 | switch (c->type) { |
3670 | case CHAN_MAINSESSION: |
3671 | from_backend(pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA, |
3672 | data, length); |
3673 | break; |
3674 | case CHAN_X11: |
3675 | x11_send(c->u.x11.s, data, length); |
3676 | break; |
36c2a3e9 |
3677 | case CHAN_AGENT: |
3678 | while (length > 0) { |
3679 | if (c->u.a.lensofar < 4) { |
3680 | int l = min(4 - c->u.a.lensofar, length); |
3681 | memcpy(c->u.a.msglen + c->u.a.lensofar, data, l); |
3682 | data += l; length -= l; c->u.a.lensofar += l; |
3683 | } |
3684 | if (c->u.a.lensofar == 4) { |
3685 | c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen); |
3686 | c->u.a.message = smalloc(c->u.a.totallen); |
3687 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
3688 | } |
3689 | if (c->u.a.lensofar >= 4 && length > 0) { |
3690 | int l = min(c->u.a.totallen - c->u.a.lensofar, |
3691 | length); |
3692 | memcpy(c->u.a.message + c->u.a.lensofar, data, l); |
3693 | data += l; length -= l; c->u.a.lensofar += l; |
3694 | } |
3695 | if (c->u.a.lensofar == c->u.a.totallen) { |
3696 | void *reply, *sentreply; |
3697 | int replylen; |
3698 | agent_query(c->u.a.message, c->u.a.totallen, |
3699 | &reply, &replylen); |
3700 | if (reply) |
3701 | sentreply = reply; |
3702 | else { |
3703 | /* Fake SSH_AGENT_FAILURE. */ |
3704 | sentreply = "\0\0\0\1\5"; |
3705 | replylen = 5; |
3706 | } |
3707 | ssh2_add_channel_data(c, sentreply, replylen); |
3708 | try_send = TRUE; |
3709 | if (reply) |
3710 | sfree(reply); |
3711 | sfree(c->u.a.message); |
3712 | c->u.a.lensofar = 0; |
3713 | } |
3714 | } |
3715 | break; |
783415f8 |
3716 | } |
d211621f |
3717 | /* |
783415f8 |
3718 | * Enlarge the window again at the remote |
3719 | * side, just in case it ever runs down and |
3720 | * they fail to send us any more data. |
d211621f |
3721 | */ |
3722 | ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
783415f8 |
3723 | ssh2_pkt_adduint32(c->remoteid); |
d211621f |
3724 | ssh2_pkt_adduint32(length); |
3725 | ssh2_pkt_send(); |
3726 | } |
7cca0d81 |
3727 | } else if (pktin.type == SSH2_MSG_DISCONNECT) { |
3728 | ssh_state = SSH_STATE_CLOSED; |
d211621f |
3729 | logevent("Received disconnect message"); |
3257deae |
3730 | crReturnV; |
7cca0d81 |
3731 | } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) { |
3732 | continue; /* exit status et al; ignore (FIXME?) */ |
d211621f |
3733 | } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) { |
783415f8 |
3734 | unsigned i = ssh2_pkt_getuint32(); |
3735 | struct ssh_channel *c; |
3736 | |
3737 | c = find234(ssh_channels, &i, ssh_channelfind); |
3738 | if (!c) |
3739 | continue; /* nonexistent channel */ |
3740 | |
3741 | if (c->type == CHAN_X11) { |
3742 | /* |
3743 | * Remote EOF on an X11 channel means we should |
3744 | * wrap up and close the channel ourselves. |
3745 | */ |
3746 | x11_close(c->u.x11.s); |
3747 | sshfwd_close(c); |
36c2a3e9 |
3748 | } else if (c->type == CHAN_AGENT) { |
3749 | sshfwd_close(c); |
3750 | } |
d211621f |
3751 | } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) { |
783415f8 |
3752 | unsigned i = ssh2_pkt_getuint32(); |
3753 | struct ssh_channel *c; |
3754 | enum234 e; |
3755 | |
3756 | c = find234(ssh_channels, &i, ssh_channelfind); |
3757 | if (!c) |
3758 | continue; /* nonexistent channel */ |
3759 | if (c->closes == 0) { |
3760 | ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
3761 | ssh2_pkt_adduint32(c->remoteid); |
3762 | ssh2_pkt_send(); |
3763 | } |
3764 | /* Do pre-close processing on the channel. */ |
3765 | switch (c->type) { |
3766 | case CHAN_MAINSESSION: |
3767 | break; /* nothing to see here, move along */ |
3768 | case CHAN_X11: |
3769 | break; |
36c2a3e9 |
3770 | case CHAN_AGENT: |
3771 | break; |
783415f8 |
3772 | } |
3773 | del234(ssh_channels, c); |
3774 | sfree(c->v2.outbuffer); |
3775 | sfree(c); |
3776 | |
3777 | /* |
3778 | * See if that was the last channel left open. |
3779 | */ |
3780 | c = first234(ssh_channels, &e); |
3781 | if (!c) { |
d211621f |
3782 | logevent("All channels closed. Disconnecting"); |
3783 | ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
9005f3ba |
3784 | ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION); |
3785 | ssh2_pkt_addstring("All open channels closed"); |
3786 | ssh2_pkt_addstring("en"); /* language tag */ |
d211621f |
3787 | ssh2_pkt_send(); |
cf6e59d6 |
3788 | ssh_state = SSH_STATE_CLOSED; |
3257deae |
3789 | crReturnV; |
d211621f |
3790 | } |
3791 | continue; /* remote sends close; ignore (FIXME) */ |
7cca0d81 |
3792 | } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3793 | unsigned i = ssh2_pkt_getuint32(); |
3794 | struct ssh_channel *c; |
3795 | c = find234(ssh_channels, &i, ssh_channelfind); |
3796 | if (!c) |
3797 | continue; /* nonexistent channel */ |
0993479d |
3798 | c->v2.remwindow += ssh2_pkt_getuint32(); |
d211621f |
3799 | try_send = TRUE; |
783415f8 |
3800 | } else if (pktin.type == SSH2_MSG_CHANNEL_OPEN) { |
3801 | char *type; |
3802 | int typelen; |
3803 | char *error = NULL; |
3804 | struct ssh_channel *c; |
3805 | ssh2_pkt_getstring(&type, &typelen); |
3806 | c = smalloc(sizeof(struct ssh_channel)); |
3807 | |
3808 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
783415f8 |
3809 | if (!ssh_X11_fwd_enabled) |
3810 | error = "X11 forwarding is not enabled"; |
0c6c9a70 |
3811 | else if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) { |
783415f8 |
3812 | error = "Unable to open an X11 connection"; |
3813 | } else { |
3814 | c->type = CHAN_X11; |
3815 | } |
36c2a3e9 |
3816 | } else if (typelen == 22 && |
3817 | !memcmp(type, "auth-agent@openssh.com", 3)) { |
3818 | if (!ssh_agentfwd_enabled) |
3819 | error = "Agent forwarding is not enabled"; |
3820 | else { |
3821 | c->type = CHAN_AGENT; /* identify channel type */ |
3822 | c->u.a.lensofar = 0; |
3823 | } |
783415f8 |
3824 | } else { |
3825 | error = "Unsupported channel type requested"; |
3826 | } |
3827 | |
3828 | c->remoteid = ssh2_pkt_getuint32(); |
3829 | if (error) { |
3830 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
3831 | ssh2_pkt_adduint32(c->remoteid); |
3832 | ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED); |
3833 | ssh2_pkt_addstring(error); |
3834 | ssh2_pkt_addstring("en"); /* language tag */ |
3835 | ssh2_pkt_send(); |
3836 | sfree(c); |
3837 | } else { |
3838 | struct ssh_channel *d; |
3839 | unsigned i; |
3840 | enum234 e; |
3841 | |
3842 | for (i=1, d = first234(ssh_channels, &e); d; |
3843 | d = next234(&e)) { |
3844 | if (d->localid > i) |
3845 | break; /* found a free number */ |
3846 | i = d->localid + 1; |
3847 | } |
3848 | c->localid = i; |
3849 | c->closes = 0; |
3850 | c->v2.remwindow = ssh2_pkt_getuint32(); |
3851 | c->v2.remmaxpkt = ssh2_pkt_getuint32(); |
3852 | c->v2.outbuffer = NULL; |
3853 | c->v2.outbuflen = c->v2.outbufsize = 0; |
3854 | add234(ssh_channels, c); |
3855 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
3856 | ssh2_pkt_adduint32(c->remoteid); |
3857 | ssh2_pkt_adduint32(c->localid); |
3858 | ssh2_pkt_adduint32(0x8000UL); /* our window size */ |
3859 | ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */ |
3860 | ssh2_pkt_send(); |
3861 | } |
7cca0d81 |
3862 | } else { |
8d5de777 |
3863 | bombout(("Strange packet received: type %d", pktin.type)); |
3864 | crReturnV; |
7cca0d81 |
3865 | } |
3866 | } else { |
d211621f |
3867 | /* |
3868 | * We have spare data. Add it to the channel buffer. |
3869 | */ |
783415f8 |
3870 | ssh2_add_channel_data(mainchan, in, inlen); |
d211621f |
3871 | try_send = TRUE; |
7cca0d81 |
3872 | } |
d211621f |
3873 | if (try_send) { |
783415f8 |
3874 | enum234 e; |
3875 | struct ssh_channel *c; |
d211621f |
3876 | /* |
783415f8 |
3877 | * Try to send data on all channels if we can. |
d211621f |
3878 | */ |
783415f8 |
3879 | for (c = first234(ssh_channels, &e); c; c = next234(&e)) |
3880 | ssh2_try_send(c); |
d211621f |
3881 | } |
e5574168 |
3882 | } |
3883 | |
3884 | crFinishV; |
3885 | } |
3886 | |
3887 | /* |
7cca0d81 |
3888 | * Handle the top-level SSH2 protocol. |
3889 | */ |
3890 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) |
3891 | { |
3892 | if (do_ssh2_transport(in, inlen, ispkt) == 0) |
3893 | return; |
3894 | do_ssh2_authconn(in, inlen, ispkt); |
3895 | } |
3896 | |
3897 | /* |
8df7a775 |
3898 | * Called to set up the connection. |
374330e2 |
3899 | * |
3900 | * Returns an error message, or NULL on success. |
374330e2 |
3901 | */ |
8df7a775 |
3902 | static char *ssh_init (char *host, int port, char **realhost) { |
fb09bf1c |
3903 | char *p; |
8f203108 |
3904 | |
3905 | #ifdef MSCRYPTOAPI |
3906 | if(crypto_startup() == 0) |
3907 | return "Microsoft high encryption pack not installed!"; |
3908 | #endif |
374330e2 |
3909 | |
8df7a775 |
3910 | ssh_send_ok = 0; |
0965bee0 |
3911 | ssh_editing = 0; |
3912 | ssh_echoing = 0; |
8df7a775 |
3913 | |
fb09bf1c |
3914 | p = connect_to_host(host, port, realhost); |
3915 | if (p != NULL) |
3916 | return p; |
374330e2 |
3917 | |
374330e2 |
3918 | return NULL; |
3919 | } |
3920 | |
3921 | /* |
374330e2 |
3922 | * Called to send data down the Telnet connection. |
3923 | */ |
3924 | static void ssh_send (char *buf, int len) { |
f78051a1 |
3925 | if (s == NULL || ssh_protocol == NULL) |
374330e2 |
3926 | return; |
3927 | |
3928 | ssh_protocol(buf, len, 0); |
3929 | } |
3930 | |
3931 | /* |
6e48c3fe |
3932 | * Called to set the size of the window from SSH's POV. |
374330e2 |
3933 | */ |
3934 | static void ssh_size(void) { |
3935 | switch (ssh_state) { |
3936 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
3937 | case SSH_STATE_PREPACKET: |
21248260 |
3938 | case SSH_STATE_CLOSED: |
374330e2 |
3939 | break; /* do nothing */ |
3940 | case SSH_STATE_INTERMED: |
3941 | size_needed = TRUE; /* buffer for later */ |
3942 | break; |
3943 | case SSH_STATE_SESSION: |
fef97f43 |
3944 | if (!cfg.nopty) { |
6e48c3fe |
3945 | if (ssh_version == 1) { |
3946 | send_packet(SSH1_CMSG_WINDOW_SIZE, |
3947 | PKT_INT, rows, PKT_INT, cols, |
3948 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
3949 | } else { |
3950 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3951 | ssh2_pkt_adduint32(mainchan->remoteid); |
3952 | ssh2_pkt_addstring("window-change"); |
3953 | ssh2_pkt_addbool(0); |
3954 | ssh2_pkt_adduint32(cols); |
3955 | ssh2_pkt_adduint32(rows); |
3956 | ssh2_pkt_adduint32(0); |
3957 | ssh2_pkt_adduint32(0); |
3958 | ssh2_pkt_send(); |
3959 | } |
fef97f43 |
3960 | } |
3687d221 |
3961 | break; |
374330e2 |
3962 | } |
3963 | } |
3964 | |
3965 | /* |
6abbf9e3 |
3966 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
3967 | * can send an EOF and collect resulting output (e.g. `plink |
3968 | * hostname sort'). |
374330e2 |
3969 | */ |
3970 | static void ssh_special (Telnet_Special code) { |
6abbf9e3 |
3971 | if (code == TS_EOF) { |
3687d221 |
3972 | if (ssh_state != SSH_STATE_SESSION) { |
3973 | /* |
3974 | * Buffer the EOF in case we are pre-SESSION, so we can |
3975 | * send it as soon as we reach SESSION. |
3976 | */ |
3977 | if (code == TS_EOF) |
3978 | eof_needed = TRUE; |
3979 | return; |
3980 | } |
67779be7 |
3981 | if (ssh_version == 1) { |
6abbf9e3 |
3982 | send_packet(SSH1_CMSG_EOF, PKT_END); |
3983 | } else { |
3984 | ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
d211621f |
3985 | ssh2_pkt_adduint32(mainchan->remoteid); |
6abbf9e3 |
3986 | ssh2_pkt_send(); |
3987 | } |
67779be7 |
3988 | logevent("Sent EOF message"); |
ec55b220 |
3989 | } else if (code == TS_PING) { |
3687d221 |
3990 | if (ssh_state == SSH_STATE_CLOSED || ssh_state == SSH_STATE_PREPACKET) |
3991 | return; |
ec55b220 |
3992 | if (ssh_version == 1) { |
3993 | send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
3994 | } else { |
3995 | ssh2_pkt_init(SSH2_MSG_IGNORE); |
3996 | ssh2_pkt_addstring_start(); |
3997 | ssh2_pkt_send(); |
3998 | } |
6abbf9e3 |
3999 | } else { |
4000 | /* do nothing */ |
4001 | } |
374330e2 |
4002 | } |
4003 | |
8df7a775 |
4004 | static Socket ssh_socket(void) { return s; } |
8ccc75b0 |
4005 | |
4006 | static int ssh_sendok(void) { return ssh_send_ok; } |
fb09bf1c |
4007 | |
0965bee0 |
4008 | static int ssh_ldisc(int option) { |
4009 | if (option == LD_ECHO) return ssh_echoing; |
4010 | if (option == LD_EDIT) return ssh_editing; |
4011 | return FALSE; |
4012 | } |
4013 | |
374330e2 |
4014 | Backend ssh_backend = { |
4015 | ssh_init, |
374330e2 |
4016 | ssh_send, |
4017 | ssh_size, |
4017be6d |
4018 | ssh_special, |
8ccc75b0 |
4019 | ssh_socket, |
97db3be4 |
4020 | ssh_sendok, |
0965bee0 |
4021 | ssh_ldisc, |
97db3be4 |
4022 | 22 |
374330e2 |
4023 | }; |