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