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