374330e2 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
fb09bf1c |
3 | #include <stdarg.h> |
4 | #include <assert.h> |
4d331a77 |
5 | #ifndef AUTO_WINSOCK |
6 | #ifdef WINSOCK_TWO |
7 | #include <winsock2.h> |
8 | #else |
374330e2 |
9 | #include <winsock.h> |
4d331a77 |
10 | #endif |
11 | #endif |
374330e2 |
12 | |
13 | #include "putty.h" |
dacbd0e8 |
14 | #include "tree234.h" |
fb09bf1c |
15 | #include "ssh.h" |
374330e2 |
16 | |
17 | #ifndef FALSE |
18 | #define FALSE 0 |
19 | #endif |
20 | #ifndef TRUE |
21 | #define TRUE 1 |
22 | #endif |
23 | |
fb09bf1c |
24 | #define logevent(s) { logevent(s); \ |
67779be7 |
25 | if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) \ |
fb09bf1c |
26 | fprintf(stderr, "%s\n", s); } |
27 | |
cf6e59d6 |
28 | #define bombout(msg) ( ssh_state = SSH_STATE_CLOSED, closesocket(s), \ |
8d5de777 |
29 | s = INVALID_SOCKET, connection_fatal msg ) |
30 | |
d211621f |
31 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
32 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
33 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
34 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
35 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
36 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
37 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
38 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
39 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
40 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
41 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
42 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
43 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
44 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
45 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
46 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
47 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
48 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
49 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
50 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
51 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
52 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
53 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
54 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
55 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
56 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
57 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
58 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
59 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
60 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
61 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
62 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
63 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
64 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
65 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
66 | |
67 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
68 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
69 | |
70 | #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1 /* 0x1 */ |
71 | #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2 /* 0x2 */ |
72 | #define SSH_AGENTC_RSA_CHALLENGE 3 /* 0x3 */ |
73 | #define SSH_AGENT_RSA_RESPONSE 4 /* 0x4 */ |
74 | #define SSH_AGENT_FAILURE 5 /* 0x5 */ |
75 | #define SSH_AGENT_SUCCESS 6 /* 0x6 */ |
76 | #define SSH_AGENTC_ADD_RSA_IDENTITY 7 /* 0x7 */ |
77 | #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8 /* 0x8 */ |
78 | |
79 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
80 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
81 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
82 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
83 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
84 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
85 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
86 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
87 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
88 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
89 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
90 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
91 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
92 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
93 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
94 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
95 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
96 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
97 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
98 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
99 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
100 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
101 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
102 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
103 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
104 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
105 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
106 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
107 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
108 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
109 | |
110 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
111 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
112 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
113 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
114 | |
115 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
116 | |
117 | #define GET_32BIT(cp) \ |
118 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
119 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
120 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
121 | ((unsigned long)(unsigned char)(cp)[3])) |
122 | |
123 | #define PUT_32BIT(cp, value) { \ |
124 | (cp)[0] = (unsigned char)((value) >> 24); \ |
125 | (cp)[1] = (unsigned char)((value) >> 16); \ |
126 | (cp)[2] = (unsigned char)((value) >> 8); \ |
127 | (cp)[3] = (unsigned char)(value); } |
128 | |
7cca0d81 |
129 | enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM }; |
972a41c8 |
130 | |
374330e2 |
131 | /* Coroutine mechanics for the sillier bits of the code */ |
132 | #define crBegin1 static int crLine = 0; |
133 | #define crBegin2 switch(crLine) { case 0:; |
134 | #define crBegin crBegin1; crBegin2; |
135 | #define crFinish(z) } crLine = 0; return (z) |
136 | #define crFinishV } crLine = 0; return |
137 | #define crReturn(z) \ |
138 | do {\ |
139 | crLine=__LINE__; return (z); case __LINE__:;\ |
140 | } while (0) |
141 | #define crReturnV \ |
142 | do {\ |
143 | crLine=__LINE__; return; case __LINE__:;\ |
144 | } while (0) |
145 | #define crStop(z) do{ crLine = 0; return (z); }while(0) |
146 | #define crStopV do{ crLine = 0; return; }while(0) |
fb09bf1c |
147 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
148 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
149 | |
57476f6b |
150 | extern const struct ssh_cipher ssh_3des; |
151 | extern const struct ssh_cipher ssh_3des_ssh2; |
152 | extern const struct ssh_cipher ssh_des; |
153 | extern const struct ssh_cipher ssh_blowfish_ssh1; |
154 | extern const struct ssh_cipher ssh_blowfish_ssh2; |
e5574168 |
155 | |
5e8358ad |
156 | /* |
157 | * Ciphers for SSH2. We miss out single-DES because it isn't |
158 | * supported; also 3DES and Blowfish are both done differently from |
159 | * SSH1. (3DES uses outer chaining; Blowfish has the opposite |
160 | * endianness and different-sized keys.) |
5e8358ad |
161 | */ |
57476f6b |
162 | const static struct ssh_cipher *ciphers[] = { &ssh_blowfish_ssh2, &ssh_3des_ssh2 }; |
e5574168 |
163 | |
57476f6b |
164 | extern const struct ssh_kex ssh_diffiehellman; |
165 | const static struct ssh_kex *kex_algs[] = { &ssh_diffiehellman }; |
e5574168 |
166 | |
57476f6b |
167 | extern const struct ssh_hostkey ssh_dss; |
168 | const static struct ssh_hostkey *hostkey_algs[] = { &ssh_dss }; |
e5574168 |
169 | |
8b2715b2 |
170 | extern const struct ssh_mac ssh_md5, ssh_sha1, ssh_sha1_buggy; |
e5574168 |
171 | |
d39f364a |
172 | static void nullmac_key(unsigned char *key) { } |
e5574168 |
173 | static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { } |
174 | static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; } |
57476f6b |
175 | const static struct ssh_mac ssh_mac_none = { |
d39f364a |
176 | nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0 |
e5574168 |
177 | }; |
8b2715b2 |
178 | const static struct ssh_mac *macs[] = { |
179 | &ssh_sha1, &ssh_md5, &ssh_mac_none }; |
180 | const static struct ssh_mac *buggymacs[] = { |
181 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none }; |
e5574168 |
182 | |
57476f6b |
183 | const static struct ssh_compress ssh_comp_none = { |
e5574168 |
184 | "none" |
185 | }; |
57476f6b |
186 | const static struct ssh_compress *compressions[] = { &ssh_comp_none }; |
374330e2 |
187 | |
dacbd0e8 |
188 | /* |
189 | * 2-3-4 tree storing channels. |
190 | */ |
191 | struct ssh_channel { |
d211621f |
192 | unsigned remoteid, localid; |
dacbd0e8 |
193 | int type; |
194 | int closes; |
195 | union { |
196 | struct ssh_agent_channel { |
197 | unsigned char *message; |
198 | unsigned char msglen[4]; |
199 | int lensofar, totallen; |
200 | } a; |
d211621f |
201 | struct ssh2_data_channel { |
202 | unsigned char *outbuffer; |
203 | unsigned outbuflen, outbufsize; |
204 | unsigned remwindow, remmaxpkt; |
205 | } v2; |
dacbd0e8 |
206 | } u; |
207 | }; |
57476f6b |
208 | |
209 | struct Packet { |
210 | long length; |
211 | int type; |
212 | unsigned char *data; |
213 | unsigned char *body; |
214 | long savedpos; |
215 | long maxlen; |
216 | }; |
217 | |
218 | static SHA_State exhash; |
219 | |
220 | static SOCKET s = INVALID_SOCKET; |
221 | |
222 | static unsigned char session_key[32]; |
223 | static const struct ssh_cipher *cipher = NULL; |
224 | static const struct ssh_cipher *cscipher = NULL; |
225 | static const struct ssh_cipher *sccipher = NULL; |
226 | static const struct ssh_mac *csmac = NULL; |
227 | static const struct ssh_mac *scmac = NULL; |
228 | static const struct ssh_compress *cscomp = NULL; |
229 | static const struct ssh_compress *sccomp = NULL; |
230 | static const struct ssh_kex *kex = NULL; |
231 | static const struct ssh_hostkey *hostkey = NULL; |
232 | int (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL; |
233 | |
234 | static char *savedhost; |
235 | static int savedport; |
236 | static int ssh_send_ok; |
237 | |
dacbd0e8 |
238 | static tree234 *ssh_channels; /* indexed by local id */ |
57476f6b |
239 | static struct ssh_channel *mainchan; /* primary session channel */ |
240 | |
241 | static enum { |
242 | SSH_STATE_BEFORE_SIZE, |
243 | SSH_STATE_INTERMED, |
244 | SSH_STATE_SESSION, |
245 | SSH_STATE_CLOSED |
246 | } ssh_state = SSH_STATE_BEFORE_SIZE; |
247 | |
248 | static int size_needed = FALSE; |
249 | |
250 | static struct Packet pktin = { 0, 0, NULL, NULL, 0 }; |
251 | static struct Packet pktout = { 0, 0, NULL, NULL, 0 }; |
252 | |
253 | static int ssh_version; |
254 | static void (*ssh_protocol)(unsigned char *in, int inlen, int ispkt); |
255 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt); |
256 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt); |
257 | static void ssh_size(void); |
258 | |
259 | static int (*s_rdpkt)(unsigned char **data, int *datalen); |
260 | |
261 | static struct rdpkt1_state_tag { |
262 | long len, pad, biglen, to_read; |
263 | unsigned long realcrc, gotcrc; |
264 | unsigned char *p; |
265 | int i; |
266 | int chunk; |
267 | } rdpkt1_state; |
268 | |
960e736a |
269 | static struct rdpkt2_state_tag { |
270 | long len, pad, payload, packetlen, maclen; |
271 | int i; |
272 | int cipherblk; |
273 | unsigned long incoming_sequence; |
274 | } rdpkt2_state; |
275 | |
dacbd0e8 |
276 | static int ssh_channelcmp(void *av, void *bv) { |
277 | struct ssh_channel *a = (struct ssh_channel *)av; |
278 | struct ssh_channel *b = (struct ssh_channel *)bv; |
279 | if (a->localid < b->localid) return -1; |
280 | if (a->localid > b->localid) return +1; |
281 | return 0; |
282 | } |
283 | static int ssh_channelfind(void *av, void *bv) { |
d211621f |
284 | unsigned *a = (unsigned *)av; |
dacbd0e8 |
285 | struct ssh_channel *b = (struct ssh_channel *)bv; |
286 | if (*a < b->localid) return -1; |
287 | if (*a > b->localid) return +1; |
288 | return 0; |
289 | } |
290 | |
374330e2 |
291 | static void s_write (char *buf, int len) { |
292 | while (len > 0) { |
293 | int i = send (s, buf, len, 0); |
4017be6d |
294 | noise_ultralight(i); |
8d5de777 |
295 | if (i <= 0) { |
296 | bombout(("Lost connection while sending")); |
297 | return; |
298 | } |
374330e2 |
299 | if (i > 0) |
300 | len -= i, buf += i; |
301 | } |
302 | } |
303 | |
304 | static int s_read (char *buf, int len) { |
305 | int ret = 0; |
306 | while (len > 0) { |
307 | int i = recv (s, buf, len, 0); |
4017be6d |
308 | noise_ultralight(i); |
374330e2 |
309 | if (i > 0) |
310 | len -= i, buf += i, ret += i; |
311 | else |
312 | return i; |
313 | } |
314 | return ret; |
315 | } |
316 | |
317 | static void c_write (char *buf, int len) { |
67779be7 |
318 | if ((flags & FLAG_STDERR)) { |
4017be6d |
319 | int i; |
320 | for (i = 0; i < len; i++) |
321 | if (buf[i] != '\r') |
322 | fputc(buf[i], stderr); |
fb09bf1c |
323 | return; |
324 | } |
c9def1b8 |
325 | while (len--) |
326 | c_write1(*buf++); |
374330e2 |
327 | } |
328 | |
3bdaf79d |
329 | static void c_writedata (char *buf, int len) { |
330 | while (len--) |
331 | c_write1(*buf++); |
332 | } |
333 | |
fb09bf1c |
334 | /* |
335 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
336 | * Decipher and verify the packet when it is completely read. |
337 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
338 | * Update the *data and *datalen variables. |
339 | * Return the additional nr of bytes needed, or 0 when |
340 | * a complete packet is available. |
341 | */ |
e5574168 |
342 | static int ssh1_rdpkt(unsigned char **data, int *datalen) |
fb09bf1c |
343 | { |
57476f6b |
344 | struct rdpkt1_state_tag *st = &rdpkt1_state; |
374330e2 |
345 | |
346 | crBegin; |
374330e2 |
347 | |
fb09bf1c |
348 | next_packet: |
37508af4 |
349 | |
fb09bf1c |
350 | pktin.type = 0; |
351 | pktin.length = 0; |
374330e2 |
352 | |
57476f6b |
353 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
354 | while ((*datalen) == 0) |
57476f6b |
355 | crReturn(4-st->i); |
356 | st->len = (st->len << 8) + **data; |
fb09bf1c |
357 | (*data)++, (*datalen)--; |
358 | } |
374330e2 |
359 | |
fb09bf1c |
360 | #ifdef FWHACK |
57476f6b |
361 | if (st->len == 0x52656d6f) { /* "Remo"te server has closed ... */ |
362 | st->len = 0x300; /* big enough to carry to end */ |
fb09bf1c |
363 | } |
364 | #endif |
374330e2 |
365 | |
57476f6b |
366 | st->pad = 8 - (st->len % 8); |
367 | st->biglen = st->len + st->pad; |
368 | pktin.length = st->len - 5; |
fb09bf1c |
369 | |
57476f6b |
370 | if (pktin.maxlen < st->biglen) { |
371 | pktin.maxlen = st->biglen; |
372 | pktin.data = (pktin.data == NULL ? malloc(st->biglen+APIEXTRA) : |
373 | realloc(pktin.data, st->biglen+APIEXTRA)); |
fb09bf1c |
374 | if (!pktin.data) |
375 | fatalbox("Out of memory"); |
376 | } |
374330e2 |
377 | |
57476f6b |
378 | st->to_read = st->biglen; |
379 | st->p = pktin.data; |
380 | while (st->to_read > 0) { |
381 | st->chunk = st->to_read; |
fb09bf1c |
382 | while ((*datalen) == 0) |
57476f6b |
383 | crReturn(st->to_read); |
384 | if (st->chunk > (*datalen)) |
385 | st->chunk = (*datalen); |
386 | memcpy(st->p, *data, st->chunk); |
387 | *data += st->chunk; |
388 | *datalen -= st->chunk; |
389 | st->p += st->chunk; |
390 | st->to_read -= st->chunk; |
fb09bf1c |
391 | } |
374330e2 |
392 | |
fb09bf1c |
393 | if (cipher) |
57476f6b |
394 | cipher->decrypt(pktin.data, st->biglen); |
698a5108 |
395 | #if 0 |
396 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
397 | for (st->i = 0; st->i < st->biglen; st->i++) |
398 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
399 | debug(("\r\n")); |
400 | #endif |
374330e2 |
401 | |
57476f6b |
402 | pktin.type = pktin.data[st->pad]; |
403 | pktin.body = pktin.data + st->pad + 1; |
374330e2 |
404 | |
57476f6b |
405 | st->realcrc = crc32(pktin.data, st->biglen-4); |
406 | st->gotcrc = GET_32BIT(pktin.data+st->biglen-4); |
407 | if (st->gotcrc != st->realcrc) { |
8d5de777 |
408 | bombout(("Incorrect CRC received on packet")); |
409 | crReturn(0); |
fb09bf1c |
410 | } |
572f871e |
411 | |
e5574168 |
412 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
413 | pktin.type == SSH1_SMSG_STDERR_DATA || |
414 | pktin.type == SSH1_MSG_DEBUG || |
18515c51 |
415 | pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE || |
416 | pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
fb09bf1c |
417 | long strlen = GET_32BIT(pktin.body); |
8d5de777 |
418 | if (strlen + 4 != pktin.length) { |
419 | bombout(("Received data packet with bogus string length")); |
420 | crReturn(0); |
421 | } |
fb09bf1c |
422 | } |
423 | |
e5574168 |
424 | if (pktin.type == SSH1_MSG_DEBUG) { |
fb09bf1c |
425 | /* log debug message */ |
426 | char buf[80]; |
427 | int strlen = GET_32BIT(pktin.body); |
428 | strcpy(buf, "Remote: "); |
429 | if (strlen > 70) strlen = 70; |
430 | memcpy(buf+8, pktin.body+4, strlen); |
431 | buf[8+strlen] = '\0'; |
432 | logevent(buf); |
433 | goto next_packet; |
e5574168 |
434 | } else if (pktin.type == SSH1_MSG_IGNORE) { |
fb09bf1c |
435 | /* do nothing */ |
436 | goto next_packet; |
437 | } |
438 | |
439 | crFinish(0); |
440 | } |
441 | |
e5574168 |
442 | static int ssh2_rdpkt(unsigned char **data, int *datalen) |
443 | { |
960e736a |
444 | struct rdpkt2_state_tag *st = &rdpkt2_state; |
e5574168 |
445 | |
446 | crBegin; |
447 | |
448 | next_packet: |
e5574168 |
449 | pktin.type = 0; |
450 | pktin.length = 0; |
960e736a |
451 | if (sccipher) |
452 | st->cipherblk = sccipher->blksize; |
e5574168 |
453 | else |
960e736a |
454 | st->cipherblk = 8; |
455 | if (st->cipherblk < 8) |
456 | st->cipherblk = 8; |
457 | |
458 | if (pktin.maxlen < st->cipherblk) { |
459 | pktin.maxlen = st->cipherblk; |
460 | pktin.data = (pktin.data == NULL ? malloc(st->cipherblk+APIEXTRA) : |
461 | realloc(pktin.data, st->cipherblk+APIEXTRA)); |
e5574168 |
462 | if (!pktin.data) |
463 | fatalbox("Out of memory"); |
464 | } |
465 | |
466 | /* |
467 | * Acquire and decrypt the first block of the packet. This will |
468 | * contain the length and padding details. |
469 | */ |
960e736a |
470 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
471 | while ((*datalen) == 0) |
960e736a |
472 | crReturn(st->cipherblk-st->i); |
473 | pktin.data[st->i] = *(*data)++; |
e5574168 |
474 | (*datalen)--; |
475 | } |
476 | #ifdef FWHACK |
477 | if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */ |
478 | /* FIXME */ |
479 | } |
480 | #endif |
481 | if (sccipher) |
960e736a |
482 | sccipher->decrypt(pktin.data, st->cipherblk); |
e5574168 |
483 | |
484 | /* |
485 | * Now get the length and padding figures. |
486 | */ |
960e736a |
487 | st->len = GET_32BIT(pktin.data); |
488 | st->pad = pktin.data[4]; |
e5574168 |
489 | |
490 | /* |
491 | * This enables us to deduce the payload length. |
492 | */ |
960e736a |
493 | st->payload = st->len - st->pad - 1; |
e5574168 |
494 | |
960e736a |
495 | pktin.length = st->payload + 5; |
e5574168 |
496 | |
497 | /* |
498 | * So now we can work out the total packet length. |
499 | */ |
960e736a |
500 | st->packetlen = st->len + 4; |
501 | st->maclen = scmac ? scmac->len : 0; |
e5574168 |
502 | |
503 | /* |
504 | * Adjust memory allocation if packet is too big. |
505 | */ |
960e736a |
506 | if (pktin.maxlen < st->packetlen+st->maclen) { |
507 | pktin.maxlen = st->packetlen+st->maclen; |
59dcd2a8 |
508 | pktin.data = (pktin.data == NULL ? malloc(pktin.maxlen+APIEXTRA) : |
509 | realloc(pktin.data, pktin.maxlen+APIEXTRA)); |
e5574168 |
510 | if (!pktin.data) |
511 | fatalbox("Out of memory"); |
512 | } |
513 | |
514 | /* |
515 | * Read and decrypt the remainder of the packet. |
516 | */ |
960e736a |
517 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) { |
e5574168 |
518 | while ((*datalen) == 0) |
960e736a |
519 | crReturn(st->packetlen + st->maclen - st->i); |
520 | pktin.data[st->i] = *(*data)++; |
e5574168 |
521 | (*datalen)--; |
522 | } |
523 | /* Decrypt everything _except_ the MAC. */ |
524 | if (sccipher) |
960e736a |
525 | sccipher->decrypt(pktin.data + st->cipherblk, |
526 | st->packetlen - st->cipherblk); |
e5574168 |
527 | |
7cca0d81 |
528 | #if 0 |
960e736a |
529 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
530 | for (st->i = 0; st->i < st->packetlen; st->i++) |
531 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
d39f364a |
532 | debug(("\r\n")); |
7cca0d81 |
533 | #endif |
d39f364a |
534 | |
e5574168 |
535 | /* |
536 | * Check the MAC. |
537 | */ |
960e736a |
538 | if (scmac && !scmac->verify(pktin.data, st->len+4, st->incoming_sequence)) { |
8d5de777 |
539 | bombout(("Incorrect MAC received on packet")); |
540 | crReturn(0); |
541 | } |
960e736a |
542 | st->incoming_sequence++; /* whether or not we MACed */ |
e5574168 |
543 | |
544 | pktin.savedpos = 6; |
545 | pktin.type = pktin.data[5]; |
e5574168 |
546 | |
7cca0d81 |
547 | if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG) |
548 | goto next_packet; /* FIXME: print DEBUG message */ |
e5574168 |
549 | |
550 | crFinish(0); |
551 | } |
552 | |
fb09bf1c |
553 | static void ssh_gotdata(unsigned char *data, int datalen) |
554 | { |
555 | while (datalen > 0) { |
85ee8208 |
556 | if ( s_rdpkt(&data, &datalen) == 0 ) { |
374330e2 |
557 | ssh_protocol(NULL, 0, 1); |
85ee8208 |
558 | if (ssh_state == SSH_STATE_CLOSED) { |
559 | return; |
560 | } |
561 | } |
374330e2 |
562 | } |
374330e2 |
563 | } |
564 | |
fb09bf1c |
565 | |
374330e2 |
566 | static void s_wrpkt_start(int type, int len) { |
567 | int pad, biglen; |
568 | |
569 | len += 5; /* type and CRC */ |
570 | pad = 8 - (len%8); |
571 | biglen = len + pad; |
572 | |
573 | pktout.length = len-5; |
574 | if (pktout.maxlen < biglen) { |
575 | pktout.maxlen = biglen; |
8f203108 |
576 | #ifdef MSCRYPTOAPI |
fb09bf1c |
577 | /* Allocate enough buffer space for extra block |
8f203108 |
578 | * for MS CryptEncrypt() */ |
fb09bf1c |
579 | pktout.data = (pktout.data == NULL ? malloc(biglen+12) : |
580 | realloc(pktout.data, biglen+12)); |
8f203108 |
581 | #else |
c1f5f956 |
582 | pktout.data = (pktout.data == NULL ? malloc(biglen+4) : |
583 | realloc(pktout.data, biglen+4)); |
8f203108 |
584 | #endif |
374330e2 |
585 | if (!pktout.data) |
586 | fatalbox("Out of memory"); |
587 | } |
588 | |
589 | pktout.type = type; |
590 | pktout.body = pktout.data+4+pad+1; |
591 | } |
592 | |
593 | static void s_wrpkt(void) { |
594 | int pad, len, biglen, i; |
595 | unsigned long crc; |
596 | |
597 | len = pktout.length + 5; /* type and CRC */ |
598 | pad = 8 - (len%8); |
599 | biglen = len + pad; |
600 | |
601 | pktout.body[-1] = pktout.type; |
602 | for (i=0; i<pad; i++) |
603 | pktout.data[i+4] = random_byte(); |
604 | crc = crc32(pktout.data+4, biglen-4); |
fb09bf1c |
605 | PUT_32BIT(pktout.data+biglen, crc); |
606 | PUT_32BIT(pktout.data, len); |
374330e2 |
607 | |
698a5108 |
608 | #if 0 |
609 | debug(("Sending packet len=%d\r\n", biglen+4)); |
610 | for (i = 0; i < biglen+4; i++) |
611 | debug((" %02x", (unsigned char)pktout.data[i])); |
612 | debug(("\r\n")); |
613 | #endif |
374330e2 |
614 | if (cipher) |
615 | cipher->encrypt(pktout.data+4, biglen); |
616 | |
617 | s_write(pktout.data, biglen+4); |
618 | } |
619 | |
fb09bf1c |
620 | /* |
621 | * Construct a packet with the specified contents and |
622 | * send it to the server. |
623 | */ |
624 | static void send_packet(int pkttype, ...) |
625 | { |
626 | va_list args; |
627 | unsigned char *p, *argp, argchar; |
628 | unsigned long argint; |
629 | int pktlen, argtype, arglen; |
7cca0d81 |
630 | Bignum bn; |
fb09bf1c |
631 | |
632 | pktlen = 0; |
633 | va_start(args, pkttype); |
634 | while ((argtype = va_arg(args, int)) != PKT_END) { |
635 | switch (argtype) { |
636 | case PKT_INT: |
637 | (void) va_arg(args, int); |
638 | pktlen += 4; |
639 | break; |
640 | case PKT_CHAR: |
641 | (void) va_arg(args, char); |
642 | pktlen++; |
643 | break; |
644 | case PKT_DATA: |
645 | (void) va_arg(args, unsigned char *); |
646 | arglen = va_arg(args, int); |
647 | pktlen += arglen; |
648 | break; |
649 | case PKT_STR: |
650 | argp = va_arg(args, unsigned char *); |
651 | arglen = strlen(argp); |
652 | pktlen += 4 + arglen; |
653 | break; |
7cca0d81 |
654 | case PKT_BIGNUM: |
655 | bn = va_arg(args, Bignum); |
5c58ad2d |
656 | pktlen += ssh1_bignum_length(bn); |
7cca0d81 |
657 | break; |
fb09bf1c |
658 | default: |
659 | assert(0); |
660 | } |
661 | } |
662 | va_end(args); |
663 | |
664 | s_wrpkt_start(pkttype, pktlen); |
665 | p = pktout.body; |
666 | |
667 | va_start(args, pkttype); |
668 | while ((argtype = va_arg(args, int)) != PKT_END) { |
669 | switch (argtype) { |
670 | case PKT_INT: |
671 | argint = va_arg(args, int); |
672 | PUT_32BIT(p, argint); |
673 | p += 4; |
674 | break; |
675 | case PKT_CHAR: |
676 | argchar = va_arg(args, unsigned char); |
677 | *p = argchar; |
678 | p++; |
679 | break; |
680 | case PKT_DATA: |
681 | argp = va_arg(args, unsigned char *); |
682 | arglen = va_arg(args, int); |
683 | memcpy(p, argp, arglen); |
684 | p += arglen; |
685 | break; |
686 | case PKT_STR: |
687 | argp = va_arg(args, unsigned char *); |
688 | arglen = strlen(argp); |
689 | PUT_32BIT(p, arglen); |
690 | memcpy(p + 4, argp, arglen); |
691 | p += 4 + arglen; |
692 | break; |
7cca0d81 |
693 | case PKT_BIGNUM: |
694 | bn = va_arg(args, Bignum); |
5c58ad2d |
695 | p += ssh1_write_bignum(p, bn); |
7cca0d81 |
696 | break; |
fb09bf1c |
697 | } |
698 | } |
699 | va_end(args); |
700 | |
701 | s_wrpkt(); |
702 | } |
703 | |
704 | |
705 | /* |
706 | * Connect to specified host and port. |
707 | * Returns an error message, or NULL on success. |
708 | * Also places the canonical host name into `realhost'. |
709 | */ |
710 | static char *connect_to_host(char *host, int port, char **realhost) |
711 | { |
712 | SOCKADDR_IN addr; |
713 | struct hostent *h; |
714 | unsigned long a; |
715 | #ifdef FWHACK |
716 | char *FWhost; |
717 | int FWport; |
718 | #endif |
719 | |
720 | savedhost = malloc(1+strlen(host)); |
721 | if (!savedhost) |
722 | fatalbox("Out of memory"); |
723 | strcpy(savedhost, host); |
724 | |
725 | if (port < 0) |
726 | port = 22; /* default ssh port */ |
d4857987 |
727 | savedport = port; |
fb09bf1c |
728 | |
729 | #ifdef FWHACK |
730 | FWhost = host; |
731 | FWport = port; |
732 | host = FWSTR; |
733 | port = 23; |
734 | #endif |
735 | |
736 | /* |
737 | * Try to find host. |
738 | */ |
739 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
740 | if ( (h = gethostbyname(host)) == NULL) |
741 | switch (WSAGetLastError()) { |
742 | case WSAENETDOWN: return "Network is down"; |
743 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
744 | return "Host does not exist"; |
745 | case WSATRY_AGAIN: return "Host not found"; |
746 | default: return "gethostbyname: unknown error"; |
747 | } |
748 | memcpy (&a, h->h_addr, sizeof(a)); |
749 | *realhost = h->h_name; |
750 | } else |
751 | *realhost = host; |
752 | #ifdef FWHACK |
753 | *realhost = FWhost; |
754 | #endif |
755 | a = ntohl(a); |
756 | |
757 | /* |
758 | * Open socket. |
759 | */ |
760 | s = socket(AF_INET, SOCK_STREAM, 0); |
761 | if (s == INVALID_SOCKET) |
762 | switch (WSAGetLastError()) { |
763 | case WSAENETDOWN: return "Network is down"; |
764 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
765 | default: return "socket(): unknown error"; |
766 | } |
767 | |
768 | /* |
769 | * Bind to local address. |
770 | */ |
771 | addr.sin_family = AF_INET; |
772 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
773 | addr.sin_port = htons(0); |
774 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
775 | switch (WSAGetLastError()) { |
776 | case WSAENETDOWN: return "Network is down"; |
777 | default: return "bind(): unknown error"; |
778 | } |
779 | |
780 | /* |
781 | * Connect to remote address. |
782 | */ |
783 | addr.sin_addr.s_addr = htonl(a); |
784 | addr.sin_port = htons((short)port); |
785 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
786 | switch (WSAGetLastError()) { |
787 | case WSAENETDOWN: return "Network is down"; |
788 | case WSAECONNREFUSED: return "Connection refused"; |
789 | case WSAENETUNREACH: return "Network is unreachable"; |
790 | case WSAEHOSTUNREACH: return "No route to host"; |
791 | default: return "connect(): unknown error"; |
792 | } |
793 | |
794 | #ifdef FWHACK |
795 | send(s, "connect ", 8, 0); |
796 | send(s, FWhost, strlen(FWhost), 0); |
797 | { |
798 | char buf[20]; |
799 | sprintf(buf, " %d\n", FWport); |
800 | send (s, buf, strlen(buf), 0); |
801 | } |
802 | #endif |
803 | |
804 | return NULL; |
805 | } |
806 | |
9697bfd2 |
807 | static int ssh_versioncmp(char *a, char *b) { |
808 | char *ae, *be; |
809 | unsigned long av, bv; |
810 | |
43aa02a7 |
811 | av = strtoul(a, &ae, 10); |
812 | bv = strtoul(b, &be, 10); |
9697bfd2 |
813 | if (av != bv) return (av < bv ? -1 : +1); |
814 | if (*ae == '.') ae++; |
815 | if (*be == '.') be++; |
43aa02a7 |
816 | av = strtoul(ae, &ae, 10); |
817 | bv = strtoul(be, &be, 10); |
9697bfd2 |
818 | if (av != bv) return (av < bv ? -1 : +1); |
819 | return 0; |
820 | } |
821 | |
e5574168 |
822 | |
823 | /* |
824 | * Utility routine for putting an SSH-protocol `string' into a SHA |
825 | * state. |
826 | */ |
827 | #include <stdio.h> |
75cab814 |
828 | static void sha_string(SHA_State *s, void *str, int len) { |
e5574168 |
829 | unsigned char lenblk[4]; |
e5574168 |
830 | PUT_32BIT(lenblk, len); |
e5574168 |
831 | SHA_Bytes(s, lenblk, 4); |
e5574168 |
832 | SHA_Bytes(s, str, len); |
833 | } |
834 | |
7cca0d81 |
835 | /* |
836 | * SSH2 packet construction functions. |
837 | */ |
75cab814 |
838 | static void ssh2_pkt_adddata(void *data, int len) { |
7cca0d81 |
839 | pktout.length += len; |
840 | if (pktout.maxlen < pktout.length) { |
841 | pktout.maxlen = pktout.length + 256; |
842 | pktout.data = (pktout.data == NULL ? malloc(pktout.maxlen+APIEXTRA) : |
843 | realloc(pktout.data, pktout.maxlen+APIEXTRA)); |
844 | if (!pktout.data) |
845 | fatalbox("Out of memory"); |
846 | } |
847 | memcpy(pktout.data+pktout.length-len, data, len); |
848 | } |
75cab814 |
849 | static void ssh2_pkt_addbyte(unsigned char byte) { |
7cca0d81 |
850 | ssh2_pkt_adddata(&byte, 1); |
851 | } |
75cab814 |
852 | static void ssh2_pkt_init(int pkt_type) { |
7cca0d81 |
853 | pktout.length = 5; |
854 | ssh2_pkt_addbyte((unsigned char)pkt_type); |
855 | } |
75cab814 |
856 | static void ssh2_pkt_addbool(unsigned char value) { |
7cca0d81 |
857 | ssh2_pkt_adddata(&value, 1); |
858 | } |
75cab814 |
859 | static void ssh2_pkt_adduint32(unsigned long value) { |
7cca0d81 |
860 | unsigned char x[4]; |
861 | PUT_32BIT(x, value); |
862 | ssh2_pkt_adddata(x, 4); |
863 | } |
75cab814 |
864 | static void ssh2_pkt_addstring_start(void) { |
7cca0d81 |
865 | ssh2_pkt_adduint32(0); |
866 | pktout.savedpos = pktout.length; |
867 | } |
75cab814 |
868 | static void ssh2_pkt_addstring_str(char *data) { |
7cca0d81 |
869 | ssh2_pkt_adddata(data, strlen(data)); |
870 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
871 | pktout.length - pktout.savedpos); |
872 | } |
75cab814 |
873 | static void ssh2_pkt_addstring_data(char *data, int len) { |
7cca0d81 |
874 | ssh2_pkt_adddata(data, len); |
875 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
876 | pktout.length - pktout.savedpos); |
877 | } |
75cab814 |
878 | static void ssh2_pkt_addstring(char *data) { |
7cca0d81 |
879 | ssh2_pkt_addstring_start(); |
880 | ssh2_pkt_addstring_str(data); |
881 | } |
75cab814 |
882 | static char *ssh2_mpint_fmt(Bignum b, int *len) { |
7cca0d81 |
883 | unsigned char *p; |
884 | int i, n = b[0]; |
885 | p = malloc(n * 2 + 1); |
886 | if (!p) |
887 | fatalbox("out of memory"); |
888 | p[0] = 0; |
889 | for (i = 0; i < n; i++) { |
890 | p[i*2+1] = (b[n-i] >> 8) & 0xFF; |
891 | p[i*2+2] = (b[n-i] ) & 0xFF; |
892 | } |
893 | i = 0; |
894 | while (p[i] == 0 && (p[i+1] & 0x80) == 0) |
895 | i++; |
896 | memmove(p, p+i, n*2+1-i); |
897 | *len = n*2+1-i; |
898 | return p; |
899 | } |
75cab814 |
900 | static void ssh2_pkt_addmp(Bignum b) { |
7cca0d81 |
901 | unsigned char *p; |
902 | int len; |
903 | p = ssh2_mpint_fmt(b, &len); |
904 | ssh2_pkt_addstring_start(); |
905 | ssh2_pkt_addstring_data(p, len); |
906 | free(p); |
907 | } |
75cab814 |
908 | static void ssh2_pkt_send(void) { |
7cca0d81 |
909 | int cipherblk, maclen, padding, i; |
910 | static unsigned long outgoing_sequence = 0; |
911 | |
912 | /* |
913 | * Add padding. At least four bytes, and must also bring total |
914 | * length (minus MAC) up to a multiple of the block size. |
915 | */ |
916 | cipherblk = cipher ? cipher->blksize : 8; /* block size */ |
917 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
918 | padding = 4; |
919 | padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk; |
920 | pktout.data[4] = padding; |
921 | for (i = 0; i < padding; i++) |
922 | pktout.data[pktout.length + i] = random_byte(); |
923 | PUT_32BIT(pktout.data, pktout.length + padding - 4); |
924 | if (csmac) |
925 | csmac->generate(pktout.data, pktout.length + padding, |
926 | outgoing_sequence); |
927 | outgoing_sequence++; /* whether or not we MACed */ |
928 | |
929 | #if 0 |
930 | debug(("Sending packet len=%d\r\n", pktout.length+padding)); |
931 | for (i = 0; i < pktout.length+padding; i++) |
932 | debug((" %02x", (unsigned char)pktout.data[i])); |
933 | debug(("\r\n")); |
934 | #endif |
935 | |
936 | if (cscipher) |
937 | cscipher->encrypt(pktout.data, pktout.length + padding); |
938 | maclen = csmac ? csmac->len : 0; |
939 | |
940 | s_write(pktout.data, pktout.length + padding + maclen); |
941 | } |
942 | |
943 | #if 0 |
944 | void bndebug(char *string, Bignum b) { |
945 | unsigned char *p; |
946 | int i, len; |
947 | p = ssh2_mpint_fmt(b, &len); |
948 | debug(("%s", string)); |
949 | for (i = 0; i < len; i++) |
950 | debug((" %02x", p[i])); |
951 | debug(("\r\n")); |
952 | free(p); |
953 | } |
954 | #endif |
955 | |
75cab814 |
956 | static void sha_mpint(SHA_State *s, Bignum b) { |
7cca0d81 |
957 | unsigned char *p; |
958 | int len; |
959 | p = ssh2_mpint_fmt(b, &len); |
960 | sha_string(s, p, len); |
961 | free(p); |
962 | } |
963 | |
964 | /* |
965 | * SSH2 packet decode functions. |
966 | */ |
75cab814 |
967 | static unsigned long ssh2_pkt_getuint32(void) { |
7cca0d81 |
968 | unsigned long value; |
969 | if (pktin.length - pktin.savedpos < 4) |
970 | return 0; /* arrgh, no way to decline (FIXME?) */ |
971 | value = GET_32BIT(pktin.data+pktin.savedpos); |
972 | pktin.savedpos += 4; |
973 | return value; |
974 | } |
75cab814 |
975 | static void ssh2_pkt_getstring(char **p, int *length) { |
7cca0d81 |
976 | *p = NULL; |
977 | if (pktin.length - pktin.savedpos < 4) |
978 | return; |
979 | *length = GET_32BIT(pktin.data+pktin.savedpos); |
980 | pktin.savedpos += 4; |
981 | if (pktin.length - pktin.savedpos < *length) |
982 | return; |
983 | *p = pktin.data+pktin.savedpos; |
984 | pktin.savedpos += *length; |
985 | } |
75cab814 |
986 | static Bignum ssh2_pkt_getmp(void) { |
7cca0d81 |
987 | char *p; |
988 | int i, j, length; |
989 | Bignum b; |
990 | |
991 | ssh2_pkt_getstring(&p, &length); |
992 | if (!p) |
993 | return NULL; |
8d5de777 |
994 | if (p[0] & 0x80) { |
995 | bombout(("internal error: Can't handle negative mpints")); |
996 | return NULL; |
997 | } |
7cca0d81 |
998 | b = newbn((length+1)/2); |
999 | for (i = 0; i < length; i++) { |
1000 | j = length - 1 - i; |
1001 | if (j & 1) |
1002 | b[j/2+1] |= ((unsigned char)p[i]) << 8; |
1003 | else |
1004 | b[j/2+1] |= ((unsigned char)p[i]); |
1005 | } |
1006 | return b; |
1007 | } |
1008 | |
374330e2 |
1009 | static int do_ssh_init(void) { |
c5e9c988 |
1010 | char c, *vsp; |
374330e2 |
1011 | char version[10]; |
c5e9c988 |
1012 | char vstring[80]; |
1013 | char vlog[sizeof(vstring)+20]; |
374330e2 |
1014 | int i; |
1015 | |
1016 | #ifdef FWHACK |
1017 | i = 0; |
1018 | while (s_read(&c, 1) == 1) { |
1019 | if (c == 'S' && i < 2) i++; |
1020 | else if (c == 'S' && i == 2) i = 2; |
1021 | else if (c == 'H' && i == 2) break; |
1022 | else i = 0; |
1023 | } |
1024 | #else |
1025 | if (s_read(&c,1) != 1 || c != 'S') return 0; |
1026 | if (s_read(&c,1) != 1 || c != 'S') return 0; |
1027 | if (s_read(&c,1) != 1 || c != 'H') return 0; |
1028 | #endif |
c5e9c988 |
1029 | strcpy(vstring, "SSH-"); |
1030 | vsp = vstring+4; |
374330e2 |
1031 | if (s_read(&c,1) != 1 || c != '-') return 0; |
1032 | i = 0; |
1033 | while (1) { |
1034 | if (s_read(&c,1) != 1) |
1035 | return 0; |
c5e9c988 |
1036 | if (vsp < vstring+sizeof(vstring)-1) |
1037 | *vsp++ = c; |
374330e2 |
1038 | if (i >= 0) { |
1039 | if (c == '-') { |
1040 | version[i] = '\0'; |
1041 | i = -1; |
1042 | } else if (i < sizeof(version)-1) |
1043 | version[i++] = c; |
1044 | } |
1045 | else if (c == '\n') |
1046 | break; |
1047 | } |
1048 | |
960e736a |
1049 | rdpkt2_state.incoming_sequence = 0; |
1050 | |
c5e9c988 |
1051 | *vsp = 0; |
1052 | sprintf(vlog, "Server version: %s", vstring); |
1053 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
1054 | logevent(vlog); |
1055 | |
adf799dd |
1056 | /* |
1057 | * Server version "1.99" means we can choose whether we use v1 |
1058 | * or v2 protocol. Choice is based on cfg.sshprot. |
1059 | */ |
1060 | if (ssh_versioncmp(version, cfg.sshprot == 1 ? "2.0" : "1.99") >= 0) { |
e5574168 |
1061 | /* |
1062 | * This is a v2 server. Begin v2 protocol. |
1063 | */ |
1064 | char *verstring = "SSH-2.0-PuTTY"; |
1065 | SHA_Init(&exhash); |
1066 | /* |
1067 | * Hash our version string and their version string. |
1068 | */ |
1069 | sha_string(&exhash, verstring, strlen(verstring)); |
1070 | sha_string(&exhash, vstring, strcspn(vstring, "\r\n")); |
1071 | sprintf(vstring, "%s\n", verstring); |
1072 | sprintf(vlog, "We claim version: %s", verstring); |
1073 | logevent(vlog); |
1074 | logevent("Using SSH protocol version 2"); |
1075 | s_write(vstring, strlen(vstring)); |
1076 | ssh_protocol = ssh2_protocol; |
6abbf9e3 |
1077 | ssh_version = 2; |
e5574168 |
1078 | s_rdpkt = ssh2_rdpkt; |
1079 | } else { |
1080 | /* |
1081 | * This is a v1 server. Begin v1 protocol. |
1082 | */ |
1083 | sprintf(vstring, "SSH-%s-PuTTY\n", |
1084 | (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5")); |
1085 | sprintf(vlog, "We claim version: %s", vstring); |
1086 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
1087 | logevent(vlog); |
1088 | logevent("Using SSH protocol version 1"); |
1089 | s_write(vstring, strlen(vstring)); |
1090 | ssh_protocol = ssh1_protocol; |
6abbf9e3 |
1091 | ssh_version = 1; |
e5574168 |
1092 | s_rdpkt = ssh1_rdpkt; |
1093 | } |
8ccc75b0 |
1094 | ssh_send_ok = 0; |
fef97f43 |
1095 | return 1; |
374330e2 |
1096 | } |
1097 | |
fb09bf1c |
1098 | /* |
1099 | * Handle the key exchange and user authentication phases. |
1100 | */ |
e5574168 |
1101 | static int do_ssh1_login(unsigned char *in, int inlen, int ispkt) |
fb09bf1c |
1102 | { |
374330e2 |
1103 | int i, j, len; |
374330e2 |
1104 | unsigned char *rsabuf, *keystr1, *keystr2; |
1105 | unsigned char cookie[8]; |
1106 | struct RSAKey servkey, hostkey; |
1107 | struct MD5Context md5c; |
ccbfb941 |
1108 | static unsigned long supported_ciphers_mask, supported_auths_mask; |
7cca0d81 |
1109 | static int tried_publickey; |
1110 | static unsigned char session_id[16]; |
bea1ef5f |
1111 | int cipher_type; |
b585ec46 |
1112 | static char username[100]; |
374330e2 |
1113 | |
374330e2 |
1114 | crBegin; |
1115 | |
fb09bf1c |
1116 | if (!ispkt) crWaitUntil(ispkt); |
374330e2 |
1117 | |
8d5de777 |
1118 | if (pktin.type != SSH1_SMSG_PUBLIC_KEY) { |
1119 | bombout(("Public key packet not received")); |
1120 | crReturn(0); |
1121 | } |
374330e2 |
1122 | |
c5e9c988 |
1123 | logevent("Received public keys"); |
374330e2 |
1124 | |
c5e9c988 |
1125 | memcpy(cookie, pktin.body, 8); |
374330e2 |
1126 | |
7cca0d81 |
1127 | i = makekey(pktin.body+8, &servkey, &keystr1, 0); |
1128 | j = makekey(pktin.body+8+i, &hostkey, &keystr2, 0); |
374330e2 |
1129 | |
c5e9c988 |
1130 | /* |
1c2a93c4 |
1131 | * Log the host key fingerprint. |
c5e9c988 |
1132 | */ |
c5e9c988 |
1133 | { |
1134 | char logmsg[80]; |
1c2a93c4 |
1135 | logevent("Host key fingerprint is:"); |
c5e9c988 |
1136 | strcpy(logmsg, " "); |
1c2a93c4 |
1137 | hostkey.comment = NULL; |
1138 | rsa_fingerprint(logmsg+strlen(logmsg), sizeof(logmsg)-strlen(logmsg), |
1139 | &hostkey); |
c5e9c988 |
1140 | logevent(logmsg); |
1141 | } |
1142 | |
fb09bf1c |
1143 | supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j); |
1144 | supported_auths_mask = GET_32BIT(pktin.body+16+i+j); |
bea1ef5f |
1145 | |
c5e9c988 |
1146 | MD5Init(&md5c); |
374330e2 |
1147 | MD5Update(&md5c, keystr2, hostkey.bytes); |
1148 | MD5Update(&md5c, keystr1, servkey.bytes); |
1149 | MD5Update(&md5c, pktin.body, 8); |
374330e2 |
1150 | MD5Final(session_id, &md5c); |
1151 | |
1152 | for (i=0; i<32; i++) |
1153 | session_key[i] = random_byte(); |
1154 | |
1155 | len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
1156 | |
1157 | rsabuf = malloc(len); |
1158 | if (!rsabuf) |
1159 | fatalbox("Out of memory"); |
1160 | |
89ee5268 |
1161 | /* |
1162 | * Verify the host key. |
1163 | */ |
1164 | { |
1165 | /* |
1166 | * First format the key into a string. |
1167 | */ |
1168 | int len = rsastr_len(&hostkey); |
d5859615 |
1169 | char fingerprint[100]; |
89ee5268 |
1170 | char *keystr = malloc(len); |
1171 | if (!keystr) |
1172 | fatalbox("Out of memory"); |
1173 | rsastr_fmt(keystr, &hostkey); |
d5859615 |
1174 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
d4857987 |
1175 | verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint); |
89ee5268 |
1176 | free(keystr); |
1177 | } |
374330e2 |
1178 | |
1179 | for (i=0; i<32; i++) { |
1180 | rsabuf[i] = session_key[i]; |
1181 | if (i < 16) |
1182 | rsabuf[i] ^= session_id[i]; |
1183 | } |
1184 | |
1185 | if (hostkey.bytes > servkey.bytes) { |
1186 | rsaencrypt(rsabuf, 32, &servkey); |
1187 | rsaencrypt(rsabuf, servkey.bytes, &hostkey); |
1188 | } else { |
1189 | rsaencrypt(rsabuf, 32, &hostkey); |
1190 | rsaencrypt(rsabuf, hostkey.bytes, &servkey); |
1191 | } |
1192 | |
c5e9c988 |
1193 | logevent("Encrypted session key"); |
1194 | |
bea1ef5f |
1195 | cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH : |
9697bfd2 |
1196 | cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES : |
bea1ef5f |
1197 | SSH_CIPHER_3DES; |
1198 | if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { |
1199 | c_write("Selected cipher not supported, falling back to 3DES\r\n", 53); |
1200 | cipher_type = SSH_CIPHER_3DES; |
1201 | } |
c5e9c988 |
1202 | switch (cipher_type) { |
1203 | case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break; |
1204 | case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break; |
1205 | case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break; |
1206 | } |
bea1ef5f |
1207 | |
e5574168 |
1208 | send_packet(SSH1_CMSG_SESSION_KEY, |
fb09bf1c |
1209 | PKT_CHAR, cipher_type, |
1210 | PKT_DATA, cookie, 8, |
1211 | PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF, |
1212 | PKT_DATA, rsabuf, len, |
1213 | PKT_INT, 0, |
1214 | PKT_END); |
1215 | |
c5e9c988 |
1216 | logevent("Trying to enable encryption..."); |
374330e2 |
1217 | |
1218 | free(rsabuf); |
1219 | |
5e8358ad |
1220 | cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
9697bfd2 |
1221 | cipher_type == SSH_CIPHER_DES ? &ssh_des : |
bea1ef5f |
1222 | &ssh_3des; |
374330e2 |
1223 | cipher->sesskey(session_key); |
1224 | |
fb09bf1c |
1225 | crWaitUntil(ispkt); |
374330e2 |
1226 | |
8d5de777 |
1227 | if (pktin.type != SSH1_SMSG_SUCCESS) { |
1228 | bombout(("Encryption not successfully enabled")); |
1229 | crReturn(0); |
1230 | } |
374330e2 |
1231 | |
c5e9c988 |
1232 | logevent("Successfully started encryption"); |
1233 | |
374330e2 |
1234 | fflush(stdout); |
1235 | { |
374330e2 |
1236 | static int pos = 0; |
1237 | static char c; |
67779be7 |
1238 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
374330e2 |
1239 | c_write("login as: ", 10); |
67779be7 |
1240 | ssh_send_ok = 1; |
374330e2 |
1241 | while (pos >= 0) { |
fb09bf1c |
1242 | crWaitUntil(!ispkt); |
374330e2 |
1243 | while (inlen--) switch (c = *in++) { |
1244 | case 10: case 13: |
1245 | username[pos] = 0; |
1246 | pos = -1; |
1247 | break; |
1248 | case 8: case 127: |
1249 | if (pos > 0) { |
1250 | c_write("\b \b", 3); |
1251 | pos--; |
1252 | } |
1253 | break; |
1254 | case 21: case 27: |
1255 | while (pos > 0) { |
1256 | c_write("\b \b", 3); |
1257 | pos--; |
1258 | } |
1259 | break; |
1260 | case 3: case 4: |
1261 | random_save_seed(); |
1262 | exit(0); |
1263 | break; |
1264 | default: |
40d24aa6 |
1265 | if (((c >= ' ' && c <= '~') || |
1266 | ((unsigned char)c >= 160)) && pos < 40) { |
374330e2 |
1267 | username[pos++] = c; |
1268 | c_write(&c, 1); |
1269 | } |
1270 | break; |
1271 | } |
1272 | } |
1273 | c_write("\r\n", 2); |
1274 | username[strcspn(username, "\n\r")] = '\0'; |
1275 | } else { |
1276 | char stuff[200]; |
1277 | strncpy(username, cfg.username, 99); |
1278 | username[99] = '\0'; |
67779be7 |
1279 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
fb09bf1c |
1280 | sprintf(stuff, "Sent username \"%s\".\r\n", username); |
4017be6d |
1281 | c_write(stuff, strlen(stuff)); |
fb09bf1c |
1282 | } |
374330e2 |
1283 | } |
fb09bf1c |
1284 | |
e5574168 |
1285 | send_packet(SSH1_CMSG_USER, PKT_STR, username, PKT_END); |
c5e9c988 |
1286 | { |
1287 | char userlog[20+sizeof(username)]; |
1288 | sprintf(userlog, "Sent username \"%s\"", username); |
1289 | logevent(userlog); |
1290 | } |
374330e2 |
1291 | } |
1292 | |
fb09bf1c |
1293 | crWaitUntil(ispkt); |
374330e2 |
1294 | |
7cca0d81 |
1295 | tried_publickey = 0; |
1296 | |
e5574168 |
1297 | while (pktin.type == SSH1_SMSG_FAILURE) { |
374330e2 |
1298 | static char password[100]; |
a52f067e |
1299 | static char prompt[200]; |
374330e2 |
1300 | static int pos; |
1301 | static char c; |
ccbfb941 |
1302 | static int pwpkt_type; |
ccbfb941 |
1303 | /* |
1304 | * Show password prompt, having first obtained it via a TIS |
18515c51 |
1305 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
1306 | * authentication. |
ccbfb941 |
1307 | */ |
e5574168 |
1308 | pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
5c58ad2d |
1309 | if (agent_exists()) { |
1310 | /* |
1311 | * Attempt RSA authentication using Pageant. |
1312 | */ |
1313 | static unsigned char request[5], *response, *p; |
1314 | static int responselen; |
1315 | static int i, nkeys; |
1316 | static int authed = FALSE; |
1317 | void *r; |
1318 | |
1319 | logevent("Pageant is running. Requesting keys."); |
1320 | |
1321 | /* Request the keys held by the agent. */ |
1322 | PUT_32BIT(request, 1); |
1323 | request[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES; |
1324 | agent_query(request, 5, &r, &responselen); |
1325 | response = (unsigned char *)r; |
1326 | if (response) { |
1327 | p = response + 5; |
1328 | nkeys = GET_32BIT(p); p += 4; |
1329 | { char buf[64]; sprintf(buf, "Pageant has %d keys", nkeys); |
1330 | logevent(buf); } |
1331 | for (i = 0; i < nkeys; i++) { |
1332 | static struct RSAKey key; |
1333 | static Bignum challenge; |
b8bb811a |
1334 | static char *commentp; |
1335 | static int commentlen; |
5c58ad2d |
1336 | |
1337 | { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i); |
1338 | logevent(buf); } |
1339 | p += 4; |
1340 | p += ssh1_read_bignum(p, &key.exponent); |
1341 | p += ssh1_read_bignum(p, &key.modulus); |
b8bb811a |
1342 | commentlen = GET_32BIT(p); p += 4; |
1343 | commentp = p; p += commentlen; |
5c58ad2d |
1344 | send_packet(SSH1_CMSG_AUTH_RSA, |
1345 | PKT_BIGNUM, key.modulus, PKT_END); |
1346 | crWaitUntil(ispkt); |
1347 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1348 | logevent("Key refused"); |
1349 | continue; |
1350 | } |
1351 | logevent("Received RSA challenge"); |
1352 | ssh1_read_bignum(pktin.body, &challenge); |
1353 | { |
1354 | char *agentreq, *q, *ret; |
1355 | int len, retlen; |
1356 | len = 1 + 4; /* message type, bit count */ |
1357 | len += ssh1_bignum_length(key.exponent); |
1358 | len += ssh1_bignum_length(key.modulus); |
1359 | len += ssh1_bignum_length(challenge); |
1360 | len += 16; /* session id */ |
1361 | len += 4; /* response format */ |
1362 | agentreq = malloc(4 + len); |
1363 | PUT_32BIT(agentreq, len); |
1364 | q = agentreq + 4; |
1365 | *q++ = SSH_AGENTC_RSA_CHALLENGE; |
1366 | PUT_32BIT(q, ssh1_bignum_bitcount(key.modulus)); |
1367 | q += 4; |
1368 | q += ssh1_write_bignum(q, key.exponent); |
1369 | q += ssh1_write_bignum(q, key.modulus); |
1370 | q += ssh1_write_bignum(q, challenge); |
1371 | memcpy(q, session_id, 16); q += 16; |
1372 | PUT_32BIT(q, 1); /* response format */ |
1373 | agent_query(agentreq, len+4, &ret, &retlen); |
1374 | free(agentreq); |
1375 | if (ret) { |
1376 | if (ret[4] == SSH_AGENT_RSA_RESPONSE) { |
1377 | logevent("Sending Pageant's response"); |
1378 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
1379 | PKT_DATA, ret+5, 16, PKT_END); |
1380 | free(ret); |
1381 | crWaitUntil(ispkt); |
1382 | if (pktin.type == SSH1_SMSG_SUCCESS) { |
1383 | logevent("Pageant's response accepted"); |
c4fa2fce |
1384 | if (flags & FLAG_VERBOSE) { |
1385 | c_write("Authenticated using RSA key \"", |
1386 | 29); |
1387 | c_write(commentp, commentlen); |
1388 | c_write("\" from agent\r\n", 14); |
1389 | } |
5c58ad2d |
1390 | authed = TRUE; |
1391 | } else |
1392 | logevent("Pageant's response not accepted"); |
1393 | } else { |
1394 | logevent("Pageant failed to answer challenge"); |
1395 | free(ret); |
1396 | } |
1397 | } else { |
1398 | logevent("No reply received from Pageant"); |
1399 | } |
1400 | } |
1401 | freebn(key.exponent); |
1402 | freebn(key.modulus); |
1403 | freebn(challenge); |
1404 | if (authed) |
1405 | break; |
1406 | } |
1407 | } |
1408 | if (authed) |
1409 | break; |
1410 | } |
7cca0d81 |
1411 | if (*cfg.keyfile && !tried_publickey) |
1412 | pwpkt_type = SSH1_CMSG_AUTH_RSA; |
fb09bf1c |
1413 | |
a52f067e |
1414 | if (pktin.type == SSH1_SMSG_FAILURE && |
1415 | cfg.try_tis_auth && |
1416 | (supported_auths_mask & (1<<SSH1_AUTH_TIS))) { |
1417 | pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
1418 | logevent("Requested TIS authentication"); |
1419 | send_packet(SSH1_CMSG_AUTH_TIS, PKT_END); |
1420 | crWaitUntil(ispkt); |
1421 | if (pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
1422 | logevent("TIS authentication declined"); |
1423 | if (flags & FLAG_INTERACTIVE) |
1424 | c_write("TIS authentication refused.\r\n", 29); |
1425 | } else { |
1426 | int challengelen = ((pktin.body[0] << 24) | |
1427 | (pktin.body[1] << 16) | |
1428 | (pktin.body[2] << 8) | |
1429 | (pktin.body[3])); |
1430 | logevent("Received TIS challenge"); |
1431 | if (challengelen > sizeof(prompt)-1) |
1432 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1433 | memcpy(prompt, pktin.body+4, challengelen); |
1434 | prompt[challengelen] = '\0'; |
1435 | } |
1436 | } |
1437 | if (pktin.type == SSH1_SMSG_FAILURE && |
1438 | cfg.try_tis_auth && |
1439 | (supported_auths_mask & (1<<SSH1_AUTH_CCARD))) { |
1440 | pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
1441 | logevent("Requested CryptoCard authentication"); |
1442 | send_packet(SSH1_CMSG_AUTH_CCARD, PKT_END); |
1443 | crWaitUntil(ispkt); |
1444 | if (pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
1445 | logevent("CryptoCard authentication declined"); |
1446 | c_write("CryptoCard authentication refused.\r\n", 29); |
1447 | } else { |
1448 | int challengelen = ((pktin.body[0] << 24) | |
1449 | (pktin.body[1] << 16) | |
1450 | (pktin.body[2] << 8) | |
1451 | (pktin.body[3])); |
1452 | logevent("Received CryptoCard challenge"); |
1453 | if (challengelen > sizeof(prompt)-1) |
1454 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1455 | memcpy(prompt, pktin.body+4, challengelen); |
1456 | strncpy(prompt + challengelen, "\r\nResponse : ", |
1457 | sizeof(prompt)-challengelen); |
1458 | prompt[sizeof(prompt)-1] = '\0'; |
1459 | } |
1460 | } |
1461 | if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
1462 | sprintf(prompt, "%.90s@%.90s's password: ", |
b585ec46 |
1463 | username, savedhost); |
a52f067e |
1464 | } |
1465 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1466 | char *comment = NULL; |
1467 | if (flags & FLAG_VERBOSE) |
1468 | c_write("Trying public key authentication.\r\n", 35); |
1469 | if (!rsakey_encrypted(cfg.keyfile, &comment)) { |
1470 | if (flags & FLAG_VERBOSE) |
1471 | c_write("No passphrase required.\r\n", 25); |
1472 | goto tryauth; |
1473 | } |
1474 | sprintf(prompt, "Passphrase for key \"%.100s\": ", comment); |
1475 | free(comment); |
1476 | } |
1477 | |
d8426c54 |
1478 | if (ssh_get_password) { |
85ee8208 |
1479 | if (!ssh_get_password(prompt, password, sizeof(password))) { |
1480 | /* |
1481 | * get_password failed to get a password (for |
1482 | * example because one was supplied on the command |
1483 | * line which has already failed to work). |
1484 | * Terminate. |
1485 | */ |
1486 | logevent("No more passwords to try"); |
1487 | ssh_state = SSH_STATE_CLOSED; |
1488 | crReturn(1); |
1489 | } |
fb09bf1c |
1490 | } else { |
a52f067e |
1491 | c_write(prompt, strlen(prompt)); |
7cca0d81 |
1492 | pos = 0; |
67779be7 |
1493 | ssh_send_ok = 1; |
7cca0d81 |
1494 | while (pos >= 0) { |
1495 | crWaitUntil(!ispkt); |
1496 | while (inlen--) switch (c = *in++) { |
1497 | case 10: case 13: |
1498 | password[pos] = 0; |
1499 | pos = -1; |
1500 | break; |
1501 | case 8: case 127: |
1502 | if (pos > 0) |
1503 | pos--; |
1504 | break; |
1505 | case 21: case 27: |
1506 | pos = 0; |
1507 | break; |
1508 | case 3: case 4: |
1509 | random_save_seed(); |
1510 | exit(0); |
1511 | break; |
1512 | default: |
1513 | if (((c >= ' ' && c <= '~') || |
1514 | ((unsigned char)c >= 160)) && pos < sizeof(password)) |
1515 | password[pos++] = c; |
1516 | break; |
1517 | } |
1518 | } |
1519 | c_write("\r\n", 2); |
a52f067e |
1520 | } |
fb09bf1c |
1521 | |
7cca0d81 |
1522 | tryauth: |
1523 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1524 | /* |
1525 | * Try public key authentication with the specified |
1526 | * key file. |
1527 | */ |
1528 | static struct RSAKey pubkey; |
1529 | static Bignum challenge, response; |
1530 | static int i; |
1531 | static unsigned char buffer[32]; |
1532 | |
1533 | tried_publickey = 1; |
6e522441 |
1534 | i = loadrsakey(cfg.keyfile, &pubkey, NULL, password); |
7cca0d81 |
1535 | if (i == 0) { |
1536 | c_write("Couldn't load public key from ", 30); |
1537 | c_write(cfg.keyfile, strlen(cfg.keyfile)); |
1538 | c_write(".\r\n", 3); |
1539 | continue; /* go and try password */ |
1540 | } |
1541 | if (i == -1) { |
1542 | c_write("Wrong passphrase.\r\n", 19); |
1543 | tried_publickey = 0; |
1544 | continue; /* try again */ |
1545 | } |
1546 | |
1547 | /* |
1548 | * Send a public key attempt. |
1549 | */ |
1550 | send_packet(SSH1_CMSG_AUTH_RSA, |
1551 | PKT_BIGNUM, pubkey.modulus, PKT_END); |
1552 | |
1553 | crWaitUntil(ispkt); |
1554 | if (pktin.type == SSH1_SMSG_FAILURE) { |
a52f067e |
1555 | c_write("Server refused our public key.\r\n", 32); |
7cca0d81 |
1556 | continue; /* go and try password */ |
1557 | } |
8d5de777 |
1558 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1559 | bombout(("Bizarre response to offer of public key")); |
1560 | crReturn(0); |
1561 | } |
7cca0d81 |
1562 | ssh1_read_bignum(pktin.body, &challenge); |
1563 | response = rsadecrypt(challenge, &pubkey); |
1564 | freebn(pubkey.private_exponent); /* burn the evidence */ |
1565 | |
1566 | for (i = 0; i < 32; i += 2) { |
1567 | buffer[i] = response[16-i/2] >> 8; |
1568 | buffer[i+1] = response[16-i/2] & 0xFF; |
1569 | } |
1570 | |
1571 | MD5Init(&md5c); |
1572 | MD5Update(&md5c, buffer, 32); |
1573 | MD5Update(&md5c, session_id, 16); |
1574 | MD5Final(buffer, &md5c); |
1575 | |
1576 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
1577 | PKT_DATA, buffer, 16, PKT_END); |
1578 | |
1579 | crWaitUntil(ispkt); |
1580 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
1581 | if (flags & FLAG_VERBOSE) |
1582 | c_write("Failed to authenticate with our public key.\r\n", |
1583 | 45); |
7cca0d81 |
1584 | continue; /* go and try password */ |
1585 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
1586 | bombout(("Bizarre response to RSA authentication response")); |
1587 | crReturn(0); |
7cca0d81 |
1588 | } |
1589 | |
1590 | break; /* we're through! */ |
1591 | } else { |
1592 | send_packet(pwpkt_type, PKT_STR, password, PKT_END); |
1593 | } |
c5e9c988 |
1594 | logevent("Sent password"); |
374330e2 |
1595 | memset(password, 0, strlen(password)); |
fb09bf1c |
1596 | crWaitUntil(ispkt); |
e5574168 |
1597 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
1598 | if (flags & FLAG_VERBOSE) |
1599 | c_write("Access denied\r\n", 15); |
c5e9c988 |
1600 | logevent("Authentication refused"); |
e5574168 |
1601 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
fb09bf1c |
1602 | logevent("Received disconnect request"); |
85ee8208 |
1603 | ssh_state = SSH_STATE_CLOSED; |
1604 | crReturn(1); |
e5574168 |
1605 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
1606 | bombout(("Strange packet received, type %d", pktin.type)); |
1607 | crReturn(0); |
374330e2 |
1608 | } |
1609 | } |
1610 | |
c5e9c988 |
1611 | logevent("Authentication successful"); |
1612 | |
fb09bf1c |
1613 | crFinish(1); |
1614 | } |
1615 | |
e5574168 |
1616 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) { |
fb09bf1c |
1617 | crBegin; |
1618 | |
1619 | random_init(); |
1620 | |
e5574168 |
1621 | while (!do_ssh1_login(in, inlen, ispkt)) { |
fb09bf1c |
1622 | crReturnV; |
85ee8208 |
1623 | } |
1624 | if (ssh_state == SSH_STATE_CLOSED) |
1625 | crReturnV; |
fb09bf1c |
1626 | |
979310f1 |
1627 | if (cfg.agentfwd && agent_exists()) { |
dacbd0e8 |
1628 | logevent("Requesting agent forwarding"); |
1629 | send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
1630 | do { crReturnV; } while (!ispkt); |
1631 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
1632 | bombout(("Protocol confusion")); |
1633 | crReturnV; |
dacbd0e8 |
1634 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
1635 | logevent("Agent forwarding refused"); |
1636 | } else |
1637 | logevent("Agent forwarding enabled"); |
1638 | } |
1639 | |
fef97f43 |
1640 | if (!cfg.nopty) { |
e5574168 |
1641 | send_packet(SSH1_CMSG_REQUEST_PTY, |
fb09bf1c |
1642 | PKT_STR, cfg.termtype, |
1643 | PKT_INT, rows, PKT_INT, cols, |
1644 | PKT_INT, 0, PKT_INT, 0, |
1645 | PKT_CHAR, 0, |
1646 | PKT_END); |
fef97f43 |
1647 | ssh_state = SSH_STATE_INTERMED; |
1648 | do { crReturnV; } while (!ispkt); |
e5574168 |
1649 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
1650 | bombout(("Protocol confusion")); |
1651 | crReturnV; |
e5574168 |
1652 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
fef97f43 |
1653 | c_write("Server refused to allocate pty\r\n", 32); |
1654 | } |
c5e9c988 |
1655 | logevent("Allocated pty"); |
374330e2 |
1656 | } |
1657 | |
6abbf9e3 |
1658 | if (*cfg.remote_cmd) |
1659 | send_packet(SSH1_CMSG_EXEC_CMD, PKT_STR, cfg.remote_cmd, PKT_END); |
1660 | else |
1661 | send_packet(SSH1_CMSG_EXEC_SHELL, PKT_END); |
c5e9c988 |
1662 | logevent("Started session"); |
374330e2 |
1663 | |
1664 | ssh_state = SSH_STATE_SESSION; |
1665 | if (size_needed) |
1666 | ssh_size(); |
1667 | |
8ccc75b0 |
1668 | ssh_send_ok = 1; |
dacbd0e8 |
1669 | ssh_channels = newtree234(ssh_channelcmp); |
6f34e365 |
1670 | begin_session(); |
374330e2 |
1671 | while (1) { |
1672 | crReturnV; |
1673 | if (ispkt) { |
e5574168 |
1674 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
1675 | pktin.type == SSH1_SMSG_STDERR_DATA) { |
fb09bf1c |
1676 | long len = GET_32BIT(pktin.body); |
3bdaf79d |
1677 | c_writedata(pktin.body+4, len); |
e5574168 |
1678 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
21248260 |
1679 | ssh_state = SSH_STATE_CLOSED; |
c5e9c988 |
1680 | logevent("Received disconnect request"); |
dacbd0e8 |
1681 | } else if (pktin.type == SSH1_SMSG_AGENT_OPEN) { |
1682 | /* Remote side is trying to open a channel to talk to our |
1683 | * agent. Give them back a local channel number. */ |
d211621f |
1684 | unsigned i = 1; |
dacbd0e8 |
1685 | struct ssh_channel *c; |
1686 | enum234 e; |
1687 | for (c = first234(ssh_channels, &e); c; c = next234(&e)) { |
1688 | if (c->localid > i) |
1689 | break; /* found a free number */ |
1690 | i = c->localid + 1; |
1691 | } |
1692 | c = malloc(sizeof(struct ssh_channel)); |
1693 | c->remoteid = GET_32BIT(pktin.body); |
1694 | c->localid = i; |
b8bb811a |
1695 | c->closes = 0; |
dacbd0e8 |
1696 | c->type = SSH1_SMSG_AGENT_OPEN; /* identify channel type */ |
bb487ce2 |
1697 | c->u.a.lensofar = 0; |
dacbd0e8 |
1698 | add234(ssh_channels, c); |
1699 | send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
1700 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
1701 | PKT_END); |
1702 | } else if (pktin.type == SSH1_MSG_CHANNEL_CLOSE || |
1703 | pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) { |
1704 | /* Remote side closes a channel. */ |
d211621f |
1705 | unsigned i = GET_32BIT(pktin.body); |
dacbd0e8 |
1706 | struct ssh_channel *c; |
1707 | c = find234(ssh_channels, &i, ssh_channelfind); |
1708 | if (c) { |
1709 | int closetype; |
1710 | closetype = (pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
1711 | send_packet(pktin.type, PKT_INT, c->remoteid, PKT_END); |
1712 | c->closes |= closetype; |
1713 | if (c->closes == 3) { |
1714 | del234(ssh_channels, c); |
1715 | free(c); |
1716 | } |
1717 | } |
1718 | } else if (pktin.type == SSH1_MSG_CHANNEL_DATA) { |
1719 | /* Data sent down one of our channels. */ |
1720 | int i = GET_32BIT(pktin.body); |
1721 | int len = GET_32BIT(pktin.body+4); |
1722 | unsigned char *p = pktin.body+8; |
1723 | struct ssh_channel *c; |
1724 | c = find234(ssh_channels, &i, ssh_channelfind); |
1725 | if (c) { |
1726 | switch(c->type) { |
1727 | case SSH1_SMSG_AGENT_OPEN: |
1728 | /* Data for an agent message. Buffer it. */ |
1729 | while (len > 0) { |
1730 | if (c->u.a.lensofar < 4) { |
1731 | int l = min(4 - c->u.a.lensofar, len); |
1732 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, l); |
1733 | p += l; len -= l; c->u.a.lensofar += l; |
1734 | } |
1735 | if (c->u.a.lensofar == 4) { |
1736 | c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen); |
1737 | c->u.a.message = malloc(c->u.a.totallen); |
1738 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
1739 | } |
1740 | if (c->u.a.lensofar >= 4 && len > 0) { |
1741 | int l = min(c->u.a.totallen - c->u.a.lensofar, len); |
1742 | memcpy(c->u.a.message + c->u.a.lensofar, p, l); |
1743 | p += l; len -= l; c->u.a.lensofar += l; |
1744 | } |
1745 | if (c->u.a.lensofar == c->u.a.totallen) { |
1746 | void *reply, *sentreply; |
1747 | int replylen; |
1748 | agent_query(c->u.a.message, c->u.a.totallen, |
1749 | &reply, &replylen); |
1750 | if (reply) |
1751 | sentreply = reply; |
1752 | else { |
1753 | /* Fake SSH_AGENT_FAILURE. */ |
1754 | sentreply = "\0\0\0\1\5"; |
1755 | replylen = 5; |
1756 | } |
1757 | send_packet(SSH1_MSG_CHANNEL_DATA, |
1758 | PKT_INT, c->remoteid, |
1759 | PKT_INT, replylen, |
1760 | PKT_DATA, sentreply, replylen, |
1761 | PKT_END); |
1762 | if (reply) |
1763 | free(reply); |
1764 | free(c->u.a.message); |
1765 | c->u.a.lensofar = 0; |
1766 | } |
1767 | } |
1768 | break; |
1769 | } |
1770 | } |
e5574168 |
1771 | } else if (pktin.type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
1772 | /* may be from EXEC_SHELL on some servers */ |
e5574168 |
1773 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
972a41c8 |
1774 | /* may be from EXEC_SHELL on some servers |
374330e2 |
1775 | * if no pty is available or in other odd cases. Ignore */ |
e5574168 |
1776 | } else if (pktin.type == SSH1_SMSG_EXIT_STATUS) { |
1777 | send_packet(SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
374330e2 |
1778 | } else { |
8d5de777 |
1779 | bombout(("Strange packet received: type %d", pktin.type)); |
1780 | crReturnV; |
374330e2 |
1781 | } |
1782 | } else { |
e5574168 |
1783 | send_packet(SSH1_CMSG_STDIN_DATA, |
fb09bf1c |
1784 | PKT_INT, inlen, PKT_DATA, in, inlen, PKT_END); |
374330e2 |
1785 | } |
1786 | } |
1787 | |
1788 | crFinishV; |
1789 | } |
1790 | |
1791 | /* |
e5574168 |
1792 | * Utility routine for decoding comma-separated strings in KEXINIT. |
1793 | */ |
75cab814 |
1794 | static int in_commasep_string(char *needle, char *haystack, int haylen) { |
e5574168 |
1795 | int needlen = strlen(needle); |
1796 | while (1) { |
1797 | /* |
1798 | * Is it at the start of the string? |
1799 | */ |
1800 | if (haylen >= needlen && /* haystack is long enough */ |
1801 | !memcmp(needle, haystack, needlen) && /* initial match */ |
1802 | (haylen == needlen || haystack[needlen] == ',') |
1803 | /* either , or EOS follows */ |
1804 | ) |
1805 | return 1; |
1806 | /* |
1807 | * If not, search for the next comma and resume after that. |
1808 | * If no comma found, terminate. |
1809 | */ |
1810 | while (haylen > 0 && *haystack != ',') |
1811 | haylen--, haystack++; |
1812 | if (haylen == 0) |
1813 | return 0; |
1814 | haylen--, haystack++; /* skip over comma itself */ |
1815 | } |
1816 | } |
1817 | |
1818 | /* |
d39f364a |
1819 | * SSH2 key creation method. |
1820 | */ |
75cab814 |
1821 | static void ssh2_mkkey(Bignum K, char *H, char chr, char *keyspace) { |
d39f364a |
1822 | SHA_State s; |
1823 | /* First 20 bytes. */ |
1824 | SHA_Init(&s); |
1825 | sha_mpint(&s, K); |
1826 | SHA_Bytes(&s, H, 20); |
1827 | SHA_Bytes(&s, &chr, 1); |
1828 | SHA_Bytes(&s, H, 20); |
1829 | SHA_Final(&s, keyspace); |
1830 | /* Next 20 bytes. */ |
1831 | SHA_Init(&s); |
1832 | sha_mpint(&s, K); |
1833 | SHA_Bytes(&s, H, 20); |
1834 | SHA_Bytes(&s, keyspace, 20); |
1835 | SHA_Final(&s, keyspace+20); |
1836 | } |
1837 | |
1838 | /* |
7cca0d81 |
1839 | * Handle the SSH2 transport layer. |
e5574168 |
1840 | */ |
7cca0d81 |
1841 | static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt) |
e5574168 |
1842 | { |
1843 | static int i, len; |
1844 | static char *str; |
1845 | static Bignum e, f, K; |
7591b9ff |
1846 | static const struct ssh_mac **maclist; |
8b2715b2 |
1847 | static int nmacs; |
57476f6b |
1848 | static const struct ssh_cipher *cscipher_tobe = NULL; |
1849 | static const struct ssh_cipher *sccipher_tobe = NULL; |
1850 | static const struct ssh_mac *csmac_tobe = NULL; |
1851 | static const struct ssh_mac *scmac_tobe = NULL; |
1852 | static const struct ssh_compress *cscomp_tobe = NULL; |
1853 | static const struct ssh_compress *sccomp_tobe = NULL; |
d5859615 |
1854 | static char *hostkeydata, *sigdata, *keystr, *fingerprint; |
e5574168 |
1855 | static int hostkeylen, siglen; |
1856 | static unsigned char exchange_hash[20]; |
d39f364a |
1857 | static unsigned char keyspace[40]; |
57476f6b |
1858 | static const struct ssh_cipher *preferred_cipher; |
e5574168 |
1859 | |
1860 | crBegin; |
7cca0d81 |
1861 | random_init(); |
e5574168 |
1862 | |
5e8358ad |
1863 | /* |
1864 | * Set up the preferred cipher. |
1865 | */ |
1866 | if (cfg.cipher == CIPHER_BLOWFISH) { |
57476f6b |
1867 | preferred_cipher = &ssh_blowfish_ssh2; |
5e8358ad |
1868 | } else if (cfg.cipher == CIPHER_DES) { |
1869 | logevent("Single DES not supported in SSH2; using 3DES"); |
57476f6b |
1870 | preferred_cipher = &ssh_3des_ssh2; |
5e8358ad |
1871 | } else if (cfg.cipher == CIPHER_3DES) { |
57476f6b |
1872 | preferred_cipher = &ssh_3des_ssh2; |
5e8358ad |
1873 | } else { |
1874 | /* Shouldn't happen, but we do want to initialise to _something_. */ |
57476f6b |
1875 | preferred_cipher = &ssh_3des_ssh2; |
5e8358ad |
1876 | } |
1877 | |
7591b9ff |
1878 | /* |
1879 | * Be prepared to work around the buggy MAC problem. |
1880 | */ |
1881 | if (cfg.buggymac) |
8b2715b2 |
1882 | maclist = buggymacs, nmacs = lenof(buggymacs); |
7591b9ff |
1883 | else |
8b2715b2 |
1884 | maclist = macs, nmacs = lenof(macs); |
7591b9ff |
1885 | |
7cca0d81 |
1886 | begin_key_exchange: |
e5574168 |
1887 | /* |
1888 | * Construct and send our key exchange packet. |
1889 | */ |
1890 | ssh2_pkt_init(SSH2_MSG_KEXINIT); |
1891 | for (i = 0; i < 16; i++) |
1892 | ssh2_pkt_addbyte((unsigned char)random_byte()); |
1893 | /* List key exchange algorithms. */ |
1894 | ssh2_pkt_addstring_start(); |
1895 | for (i = 0; i < lenof(kex_algs); i++) { |
1896 | ssh2_pkt_addstring_str(kex_algs[i]->name); |
1897 | if (i < lenof(kex_algs)-1) |
1898 | ssh2_pkt_addstring_str(","); |
1899 | } |
1900 | /* List server host key algorithms. */ |
1901 | ssh2_pkt_addstring_start(); |
1902 | for (i = 0; i < lenof(hostkey_algs); i++) { |
1903 | ssh2_pkt_addstring_str(hostkey_algs[i]->name); |
1904 | if (i < lenof(hostkey_algs)-1) |
1905 | ssh2_pkt_addstring_str(","); |
1906 | } |
1907 | /* List client->server encryption algorithms. */ |
1908 | ssh2_pkt_addstring_start(); |
729e54b4 |
1909 | for (i = 0; i < lenof(ciphers)+1; i++) { |
1910 | const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1]; |
57476f6b |
1911 | ssh2_pkt_addstring_str(c->name); |
729e54b4 |
1912 | if (i < lenof(ciphers)) |
e5574168 |
1913 | ssh2_pkt_addstring_str(","); |
1914 | } |
1915 | /* List server->client encryption algorithms. */ |
1916 | ssh2_pkt_addstring_start(); |
729e54b4 |
1917 | for (i = 0; i < lenof(ciphers)+1; i++) { |
1918 | const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1]; |
57476f6b |
1919 | ssh2_pkt_addstring_str(c->name); |
729e54b4 |
1920 | if (i < lenof(ciphers)) |
e5574168 |
1921 | ssh2_pkt_addstring_str(","); |
1922 | } |
1923 | /* List client->server MAC algorithms. */ |
1924 | ssh2_pkt_addstring_start(); |
8b2715b2 |
1925 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
1926 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
1927 | if (i < nmacs-1) |
e5574168 |
1928 | ssh2_pkt_addstring_str(","); |
1929 | } |
1930 | /* List server->client MAC algorithms. */ |
1931 | ssh2_pkt_addstring_start(); |
8b2715b2 |
1932 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
1933 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
1934 | if (i < nmacs-1) |
e5574168 |
1935 | ssh2_pkt_addstring_str(","); |
1936 | } |
1937 | /* List client->server compression algorithms. */ |
1938 | ssh2_pkt_addstring_start(); |
1939 | for (i = 0; i < lenof(compressions); i++) { |
1940 | ssh2_pkt_addstring_str(compressions[i]->name); |
1941 | if (i < lenof(compressions)-1) |
1942 | ssh2_pkt_addstring_str(","); |
1943 | } |
1944 | /* List server->client compression algorithms. */ |
1945 | ssh2_pkt_addstring_start(); |
1946 | for (i = 0; i < lenof(compressions); i++) { |
1947 | ssh2_pkt_addstring_str(compressions[i]->name); |
1948 | if (i < lenof(compressions)-1) |
1949 | ssh2_pkt_addstring_str(","); |
1950 | } |
1951 | /* List client->server languages. Empty list. */ |
1952 | ssh2_pkt_addstring_start(); |
1953 | /* List server->client languages. Empty list. */ |
1954 | ssh2_pkt_addstring_start(); |
1955 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
1956 | ssh2_pkt_addbool(FALSE); |
1957 | /* Reserved. */ |
1958 | ssh2_pkt_adduint32(0); |
1959 | sha_string(&exhash, pktout.data+5, pktout.length-5); |
1960 | ssh2_pkt_send(); |
1961 | |
1962 | if (!ispkt) crWaitUntil(ispkt); |
1963 | sha_string(&exhash, pktin.data+5, pktin.length-5); |
1964 | |
1965 | /* |
1966 | * Now examine the other side's KEXINIT to see what we're up |
1967 | * to. |
1968 | */ |
7cca0d81 |
1969 | if (pktin.type != SSH2_MSG_KEXINIT) { |
8d5de777 |
1970 | bombout(("expected key exchange packet from server")); |
1971 | crReturn(0); |
7cca0d81 |
1972 | } |
e5574168 |
1973 | kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL; |
1974 | csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL; |
1975 | pktin.savedpos += 16; /* skip garbage cookie */ |
1976 | ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */ |
1977 | for (i = 0; i < lenof(kex_algs); i++) { |
1978 | if (in_commasep_string(kex_algs[i]->name, str, len)) { |
1979 | kex = kex_algs[i]; |
1980 | break; |
1981 | } |
1982 | } |
1983 | ssh2_pkt_getstring(&str, &len); /* host key algorithms */ |
1984 | for (i = 0; i < lenof(hostkey_algs); i++) { |
1985 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
1986 | hostkey = hostkey_algs[i]; |
1987 | break; |
1988 | } |
1989 | } |
1990 | ssh2_pkt_getstring(&str, &len); /* client->server cipher */ |
729e54b4 |
1991 | for (i = 0; i < lenof(ciphers)+1; i++) { |
1992 | const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1]; |
57476f6b |
1993 | if (in_commasep_string(c->name, str, len)) { |
729e54b4 |
1994 | cscipher_tobe = c; |
e5574168 |
1995 | break; |
1996 | } |
1997 | } |
1998 | ssh2_pkt_getstring(&str, &len); /* server->client cipher */ |
729e54b4 |
1999 | for (i = 0; i < lenof(ciphers)+1; i++) { |
2000 | const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1]; |
57476f6b |
2001 | if (in_commasep_string(c->name, str, len)) { |
729e54b4 |
2002 | sccipher_tobe = c; |
e5574168 |
2003 | break; |
2004 | } |
2005 | } |
2006 | ssh2_pkt_getstring(&str, &len); /* client->server mac */ |
8b2715b2 |
2007 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2008 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2009 | csmac_tobe = maclist[i]; |
e5574168 |
2010 | break; |
2011 | } |
2012 | } |
2013 | ssh2_pkt_getstring(&str, &len); /* server->client mac */ |
8b2715b2 |
2014 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2015 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2016 | scmac_tobe = maclist[i]; |
e5574168 |
2017 | break; |
2018 | } |
2019 | } |
2020 | ssh2_pkt_getstring(&str, &len); /* client->server compression */ |
2021 | for (i = 0; i < lenof(compressions); i++) { |
2022 | if (in_commasep_string(compressions[i]->name, str, len)) { |
2023 | cscomp_tobe = compressions[i]; |
2024 | break; |
2025 | } |
2026 | } |
2027 | ssh2_pkt_getstring(&str, &len); /* server->client compression */ |
2028 | for (i = 0; i < lenof(compressions); i++) { |
2029 | if (in_commasep_string(compressions[i]->name, str, len)) { |
2030 | sccomp_tobe = compressions[i]; |
2031 | break; |
2032 | } |
2033 | } |
e5574168 |
2034 | |
2035 | /* |
2036 | * Currently we only support Diffie-Hellman and DSS, so let's |
2037 | * bomb out if those aren't selected. |
2038 | */ |
8d5de777 |
2039 | if (kex != &ssh_diffiehellman || hostkey != &ssh_dss) { |
2040 | bombout(("internal fault: chaos in SSH 2 transport layer")); |
2041 | crReturn(0); |
2042 | } |
e5574168 |
2043 | |
2044 | /* |
2045 | * Now we begin the fun. Generate and send e for Diffie-Hellman. |
2046 | */ |
2047 | e = dh_create_e(); |
e5574168 |
2048 | ssh2_pkt_init(SSH2_MSG_KEXDH_INIT); |
2049 | ssh2_pkt_addmp(e); |
2050 | ssh2_pkt_send(); |
2051 | |
2052 | crWaitUntil(ispkt); |
7cca0d81 |
2053 | if (pktin.type != SSH2_MSG_KEXDH_REPLY) { |
8d5de777 |
2054 | bombout(("expected key exchange packet from server")); |
2055 | crReturn(0); |
7cca0d81 |
2056 | } |
e5574168 |
2057 | ssh2_pkt_getstring(&hostkeydata, &hostkeylen); |
2058 | f = ssh2_pkt_getmp(); |
e5574168 |
2059 | ssh2_pkt_getstring(&sigdata, &siglen); |
2060 | |
2061 | K = dh_find_K(f); |
e5574168 |
2062 | |
2063 | sha_string(&exhash, hostkeydata, hostkeylen); |
2064 | sha_mpint(&exhash, e); |
2065 | sha_mpint(&exhash, f); |
2066 | sha_mpint(&exhash, K); |
2067 | SHA_Final(&exhash, exchange_hash); |
2068 | |
7cca0d81 |
2069 | #if 0 |
e5574168 |
2070 | debug(("Exchange hash is:\r\n")); |
2071 | for (i = 0; i < 20; i++) |
2072 | debug((" %02x", exchange_hash[i])); |
2073 | debug(("\r\n")); |
7cca0d81 |
2074 | #endif |
2075 | |
2076 | hostkey->setkey(hostkeydata, hostkeylen); |
8d5de777 |
2077 | if (!hostkey->verifysig(sigdata, siglen, exchange_hash, 20)) { |
2078 | bombout(("Server failed host key check")); |
2079 | crReturn(0); |
2080 | } |
e5574168 |
2081 | |
2082 | /* |
7cca0d81 |
2083 | * Expect SSH2_MSG_NEWKEYS from server. |
d39f364a |
2084 | */ |
7cca0d81 |
2085 | crWaitUntil(ispkt); |
8d5de777 |
2086 | if (pktin.type != SSH2_MSG_NEWKEYS) { |
2087 | bombout(("expected new-keys packet from server")); |
2088 | crReturn(0); |
2089 | } |
d39f364a |
2090 | |
2091 | /* |
7cca0d81 |
2092 | * Authenticate remote host: verify host key. (We've already |
2093 | * checked the signature of the exchange hash.) |
e5574168 |
2094 | */ |
7cca0d81 |
2095 | keystr = hostkey->fmtkey(); |
d5859615 |
2096 | fingerprint = hostkey->fingerprint(); |
d4857987 |
2097 | verify_ssh_host_key(savedhost, savedport, hostkey->keytype, |
2098 | keystr, fingerprint); |
d5859615 |
2099 | logevent("Host key fingerprint is:"); |
2100 | logevent(fingerprint); |
2101 | free(fingerprint); |
7cca0d81 |
2102 | free(keystr); |
d39f364a |
2103 | |
2104 | /* |
7cca0d81 |
2105 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
2106 | */ |
2107 | ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
2108 | ssh2_pkt_send(); |
d39f364a |
2109 | |
2110 | /* |
2111 | * Create and initialise session keys. |
2112 | */ |
2113 | cscipher = cscipher_tobe; |
2114 | sccipher = sccipher_tobe; |
2115 | csmac = csmac_tobe; |
2116 | scmac = scmac_tobe; |
2117 | cscomp = cscomp_tobe; |
2118 | sccomp = sccomp_tobe; |
2119 | /* |
2120 | * Set IVs after keys. |
2121 | */ |
2122 | ssh2_mkkey(K, exchange_hash, 'C', keyspace); cscipher->setcskey(keyspace); |
2123 | ssh2_mkkey(K, exchange_hash, 'D', keyspace); cscipher->setsckey(keyspace); |
2124 | ssh2_mkkey(K, exchange_hash, 'A', keyspace); cscipher->setcsiv(keyspace); |
2125 | ssh2_mkkey(K, exchange_hash, 'B', keyspace); sccipher->setsciv(keyspace); |
2126 | ssh2_mkkey(K, exchange_hash, 'E', keyspace); csmac->setcskey(keyspace); |
2127 | ssh2_mkkey(K, exchange_hash, 'F', keyspace); scmac->setsckey(keyspace); |
2128 | |
033b4cef |
2129 | /* |
7cca0d81 |
2130 | * Now we're encrypting. Begin returning 1 to the protocol main |
2131 | * function so that other things can run on top of the |
2132 | * transport. If we ever see a KEXINIT, we must go back to the |
2133 | * start. |
033b4cef |
2134 | */ |
7cca0d81 |
2135 | do { |
2136 | crReturn(1); |
2137 | } while (!(ispkt && pktin.type == SSH2_MSG_KEXINIT)); |
2138 | goto begin_key_exchange; |
e5574168 |
2139 | |
2140 | crFinish(1); |
2141 | } |
2142 | |
7cca0d81 |
2143 | /* |
2144 | * Handle the SSH2 userauth and connection layers. |
2145 | */ |
2146 | static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt) |
2147 | { |
7cca0d81 |
2148 | static unsigned long remote_winsize; |
2149 | static unsigned long remote_maxpkt; |
2150 | |
e5574168 |
2151 | crBegin; |
2152 | |
7cca0d81 |
2153 | /* |
2154 | * Request userauth protocol, and await a response to it. |
2155 | */ |
2156 | ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
2157 | ssh2_pkt_addstring("ssh-userauth"); |
2158 | ssh2_pkt_send(); |
2159 | crWaitUntilV(ispkt); |
8d5de777 |
2160 | if (pktin.type != SSH2_MSG_SERVICE_ACCEPT) { |
2161 | bombout(("Server refused user authentication protocol")); |
2162 | crReturnV; |
2163 | } |
7cca0d81 |
2164 | |
2165 | /* |
2166 | * FIXME: currently we support only password authentication. |
2167 | * (This places us technically in violation of the SSH2 spec. |
2168 | * We must fix this.) |
2169 | */ |
2170 | while (1) { |
2171 | /* |
2172 | * Get a username and a password. |
2173 | */ |
2174 | static char username[100]; |
2175 | static char password[100]; |
2176 | static int pos = 0; |
2177 | static char c; |
e5574168 |
2178 | |
67779be7 |
2179 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
7cca0d81 |
2180 | c_write("login as: ", 10); |
67779be7 |
2181 | ssh_send_ok = 1; |
7cca0d81 |
2182 | while (pos >= 0) { |
2183 | crWaitUntilV(!ispkt); |
2184 | while (inlen--) switch (c = *in++) { |
2185 | case 10: case 13: |
2186 | username[pos] = 0; |
2187 | pos = -1; |
2188 | break; |
2189 | case 8: case 127: |
2190 | if (pos > 0) { |
2191 | c_write("\b \b", 3); |
2192 | pos--; |
2193 | } |
2194 | break; |
2195 | case 21: case 27: |
2196 | while (pos > 0) { |
2197 | c_write("\b \b", 3); |
2198 | pos--; |
2199 | } |
2200 | break; |
2201 | case 3: case 4: |
2202 | random_save_seed(); |
2203 | exit(0); |
2204 | break; |
2205 | default: |
2206 | if (((c >= ' ' && c <= '~') || |
2207 | ((unsigned char)c >= 160)) && pos < 40) { |
2208 | username[pos++] = c; |
2209 | c_write(&c, 1); |
2210 | } |
2211 | break; |
2212 | } |
2213 | } |
2214 | c_write("\r\n", 2); |
2215 | username[strcspn(username, "\n\r")] = '\0'; |
2216 | } else { |
2217 | char stuff[200]; |
2218 | strncpy(username, cfg.username, 99); |
2219 | username[99] = '\0'; |
67779be7 |
2220 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
7cca0d81 |
2221 | sprintf(stuff, "Using username \"%s\".\r\n", username); |
2222 | c_write(stuff, strlen(stuff)); |
2223 | } |
2224 | } |
2225 | |
d8426c54 |
2226 | if (ssh_get_password) { |
7cca0d81 |
2227 | char prompt[200]; |
b585ec46 |
2228 | sprintf(prompt, "%.90s@%.90s's password: ", username, savedhost); |
7cca0d81 |
2229 | if (!ssh_get_password(prompt, password, sizeof(password))) { |
2230 | /* |
2231 | * get_password failed to get a password (for |
2232 | * example because one was supplied on the command |
2233 | * line which has already failed to work). |
2234 | * Terminate. |
2235 | */ |
2236 | logevent("No more passwords to try"); |
2237 | ssh_state = SSH_STATE_CLOSED; |
2238 | crReturnV; |
2239 | } |
2240 | } else { |
2241 | c_write("password: ", 10); |
67779be7 |
2242 | ssh_send_ok = 1; |
7cca0d81 |
2243 | |
2244 | pos = 0; |
2245 | while (pos >= 0) { |
2246 | crWaitUntilV(!ispkt); |
2247 | while (inlen--) switch (c = *in++) { |
2248 | case 10: case 13: |
2249 | password[pos] = 0; |
2250 | pos = -1; |
2251 | break; |
2252 | case 8: case 127: |
2253 | if (pos > 0) |
2254 | pos--; |
2255 | break; |
2256 | case 21: case 27: |
2257 | pos = 0; |
2258 | break; |
2259 | case 3: case 4: |
2260 | random_save_seed(); |
2261 | exit(0); |
2262 | break; |
2263 | default: |
2264 | if (((c >= ' ' && c <= '~') || |
2265 | ((unsigned char)c >= 160)) && pos < 40) |
2266 | password[pos++] = c; |
2267 | break; |
2268 | } |
2269 | } |
2270 | c_write("\r\n", 2); |
2271 | } |
2272 | |
2273 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
2274 | ssh2_pkt_addstring(username); |
2275 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
2276 | ssh2_pkt_addstring("password"); |
2277 | ssh2_pkt_addbool(FALSE); |
2278 | ssh2_pkt_addstring(password); |
2279 | ssh2_pkt_send(); |
2280 | |
2281 | crWaitUntilV(ispkt); |
2282 | if (pktin.type != SSH2_MSG_USERAUTH_SUCCESS) { |
2283 | c_write("Access denied\r\n", 15); |
2284 | logevent("Authentication refused"); |
2285 | } else |
2286 | break; |
2287 | } |
2288 | |
2289 | /* |
2290 | * Now we're authenticated for the connection protocol. The |
2291 | * connection protocol will automatically have started at this |
2292 | * point; there's no need to send SERVICE_REQUEST. |
2293 | */ |
2294 | |
2295 | /* |
2296 | * So now create a channel with a session in it. |
2297 | */ |
d211621f |
2298 | mainchan = malloc(sizeof(struct ssh_channel)); |
2299 | mainchan->localid = 100; /* as good as any */ |
7cca0d81 |
2300 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
2301 | ssh2_pkt_addstring("session"); |
d211621f |
2302 | ssh2_pkt_adduint32(mainchan->localid); |
cf6e59d6 |
2303 | ssh2_pkt_adduint32(0x8000UL); /* our window size */ |
d211621f |
2304 | ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */ |
7cca0d81 |
2305 | ssh2_pkt_send(); |
2306 | crWaitUntilV(ispkt); |
2307 | if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
8d5de777 |
2308 | bombout(("Server refused to open a session")); |
2309 | crReturnV; |
7cca0d81 |
2310 | /* FIXME: error data comes back in FAILURE packet */ |
2311 | } |
d211621f |
2312 | if (ssh2_pkt_getuint32() != mainchan->localid) { |
8d5de777 |
2313 | bombout(("Server's channel confirmation cited wrong channel")); |
2314 | crReturnV; |
7cca0d81 |
2315 | } |
d211621f |
2316 | mainchan->remoteid = ssh2_pkt_getuint32(); |
2317 | mainchan->u.v2.remwindow = ssh2_pkt_getuint32(); |
2318 | mainchan->u.v2.remmaxpkt = ssh2_pkt_getuint32(); |
2319 | mainchan->u.v2.outbuffer = NULL; |
2320 | mainchan->u.v2.outbuflen = mainchan->u.v2.outbufsize = 0; |
7cca0d81 |
2321 | logevent("Opened channel for session"); |
2322 | |
2323 | /* |
2324 | * Now allocate a pty for the session. |
2325 | */ |
67779be7 |
2326 | if (!cfg.nopty) { |
2327 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
2328 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
2329 | ssh2_pkt_addstring("pty-req"); |
2330 | ssh2_pkt_addbool(1); /* want reply */ |
2331 | ssh2_pkt_addstring(cfg.termtype); |
2332 | ssh2_pkt_adduint32(cols); |
2333 | ssh2_pkt_adduint32(rows); |
2334 | ssh2_pkt_adduint32(0); /* pixel width */ |
2335 | ssh2_pkt_adduint32(0); /* pixel height */ |
2336 | ssh2_pkt_addstring_start(); |
2337 | ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */ |
2338 | ssh2_pkt_send(); |
7cca0d81 |
2339 | |
67779be7 |
2340 | do { |
2341 | crWaitUntilV(ispkt); |
2342 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
2343 | /* FIXME: be able to handle other channels here */ |
2344 | if (ssh2_pkt_getuint32() != mainchan->localid) |
2345 | continue; /* wrong channel */ |
2346 | mainchan->u.v2.remwindow += ssh2_pkt_getuint32(); |
2347 | } |
2348 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
7cca0d81 |
2349 | |
67779be7 |
2350 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
2351 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
2352 | bombout(("Server got confused by pty request")); |
2353 | crReturnV; |
67779be7 |
2354 | } |
2355 | c_write("Server refused to allocate pty\r\n", 32); |
2356 | } else { |
2357 | logevent("Allocated pty"); |
7cca0d81 |
2358 | } |
7cca0d81 |
2359 | } |
2360 | |
2361 | /* |
67779be7 |
2362 | * Start a shell or a remote command. |
7cca0d81 |
2363 | */ |
2364 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
d211621f |
2365 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
67779be7 |
2366 | if (*cfg.remote_cmd) { |
2367 | ssh2_pkt_addstring("exec"); |
2368 | ssh2_pkt_addbool(1); /* want reply */ |
2369 | ssh2_pkt_addstring(cfg.remote_cmd); |
2370 | } else { |
2371 | ssh2_pkt_addstring("shell"); |
2372 | ssh2_pkt_addbool(1); /* want reply */ |
2373 | } |
7cca0d81 |
2374 | ssh2_pkt_send(); |
d211621f |
2375 | do { |
7cca0d81 |
2376 | crWaitUntilV(ispkt); |
d211621f |
2377 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
2378 | /* FIXME: be able to handle other channels here */ |
2379 | if (ssh2_pkt_getuint32() != mainchan->localid) |
2380 | continue; /* wrong channel */ |
2381 | mainchan->u.v2.remwindow += ssh2_pkt_getuint32(); |
2382 | } |
7cca0d81 |
2383 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
2384 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
2385 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
2386 | bombout(("Server got confused by shell/command request")); |
2387 | crReturnV; |
7cca0d81 |
2388 | } |
8d5de777 |
2389 | bombout(("Server refused to start a shell/command")); |
2390 | crReturnV; |
7cca0d81 |
2391 | } else { |
8d5de777 |
2392 | logevent("Started a shell/command"); |
7cca0d81 |
2393 | } |
2394 | |
2395 | /* |
2396 | * Transfer data! |
2397 | */ |
8ccc75b0 |
2398 | ssh_send_ok = 1; |
6f34e365 |
2399 | begin_session(); |
7cca0d81 |
2400 | while (1) { |
d211621f |
2401 | static int try_send; |
e5574168 |
2402 | crReturnV; |
d211621f |
2403 | try_send = FALSE; |
7cca0d81 |
2404 | if (ispkt) { |
2405 | if (pktin.type == SSH2_MSG_CHANNEL_DATA || |
2406 | pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
2407 | char *data; |
2408 | int length; |
d211621f |
2409 | /* FIXME: be able to handle other channels here */ |
2410 | if (ssh2_pkt_getuint32() != mainchan->localid) |
7cca0d81 |
2411 | continue; /* wrong channel */ |
2412 | if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
2413 | ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR) |
2414 | continue; /* extended but not stderr */ |
2415 | ssh2_pkt_getstring(&data, &length); |
d211621f |
2416 | if (data) { |
3bdaf79d |
2417 | c_writedata(data, length); |
d211621f |
2418 | /* |
2419 | * Enlarge the window again at the remote side, |
2420 | * just in case it ever runs down and they fail |
2421 | * to send us any more data. |
2422 | */ |
2423 | ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
2424 | ssh2_pkt_adduint32(mainchan->remoteid); |
2425 | ssh2_pkt_adduint32(length); |
2426 | ssh2_pkt_send(); |
2427 | } |
7cca0d81 |
2428 | } else if (pktin.type == SSH2_MSG_DISCONNECT) { |
2429 | ssh_state = SSH_STATE_CLOSED; |
d211621f |
2430 | logevent("Received disconnect message"); |
7cca0d81 |
2431 | } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) { |
2432 | continue; /* exit status et al; ignore (FIXME?) */ |
d211621f |
2433 | } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) { |
2434 | continue; /* remote sends EOF; ignore */ |
2435 | } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) { |
2436 | /* FIXME: be able to handle other channels here */ |
2437 | if (ssh2_pkt_getuint32() != mainchan->localid) |
2438 | continue; /* wrong channel */ |
2439 | ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
2440 | ssh2_pkt_adduint32(mainchan->remoteid); |
2441 | ssh2_pkt_send(); |
2442 | /* FIXME: mark the channel as closed */ |
2443 | if (1 /* FIXME: "all channels are closed" */) { |
2444 | logevent("All channels closed. Disconnecting"); |
2445 | ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
2446 | ssh2_pkt_send(); |
cf6e59d6 |
2447 | ssh_state = SSH_STATE_CLOSED; |
2448 | closesocket(s); |
2449 | s = INVALID_SOCKET; |
d211621f |
2450 | } |
2451 | continue; /* remote sends close; ignore (FIXME) */ |
7cca0d81 |
2452 | } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
d211621f |
2453 | /* FIXME: be able to handle other channels here */ |
2454 | if (ssh2_pkt_getuint32() != mainchan->localid) |
2455 | continue; /* wrong channel */ |
2456 | mainchan->u.v2.remwindow += ssh2_pkt_getuint32(); |
2457 | try_send = TRUE; |
7cca0d81 |
2458 | } else { |
8d5de777 |
2459 | bombout(("Strange packet received: type %d", pktin.type)); |
2460 | crReturnV; |
7cca0d81 |
2461 | } |
2462 | } else { |
d211621f |
2463 | /* |
2464 | * We have spare data. Add it to the channel buffer. |
2465 | */ |
2466 | if (mainchan->u.v2.outbufsize < |
2467 | mainchan->u.v2.outbuflen + inlen) { |
2468 | mainchan->u.v2.outbufsize = |
2469 | mainchan->u.v2.outbuflen + inlen + 1024; |
2470 | mainchan->u.v2.outbuffer = srealloc(mainchan->u.v2.outbuffer, |
2471 | mainchan->u.v2.outbufsize); |
2472 | } |
2473 | memcpy(mainchan->u.v2.outbuffer + mainchan->u.v2.outbuflen, |
2474 | in, inlen); |
2475 | mainchan->u.v2.outbuflen += inlen; |
2476 | try_send = TRUE; |
7cca0d81 |
2477 | } |
d211621f |
2478 | if (try_send) { |
2479 | /* |
2480 | * Try to send data on the channel if we can. (FIXME: |
2481 | * on _all_ channels.) |
2482 | */ |
2483 | while (mainchan->u.v2.remwindow > 0 && |
2484 | mainchan->u.v2.outbuflen > 0) { |
2485 | unsigned len = mainchan->u.v2.remwindow; |
2486 | if (len > mainchan->u.v2.outbuflen) |
2487 | len = mainchan->u.v2.outbuflen; |
2488 | if (len > mainchan->u.v2.remmaxpkt) |
2489 | len = mainchan->u.v2.remmaxpkt; |
2490 | ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
2491 | ssh2_pkt_adduint32(mainchan->remoteid); |
2492 | ssh2_pkt_addstring_start(); |
2493 | ssh2_pkt_addstring_data(mainchan->u.v2.outbuffer, len); |
2494 | ssh2_pkt_send(); |
2495 | mainchan->u.v2.outbuflen -= len; |
2496 | memmove(mainchan->u.v2.outbuffer, mainchan->u.v2.outbuffer+len, |
2497 | mainchan->u.v2.outbuflen); |
2498 | mainchan->u.v2.remwindow -= len; |
2499 | } |
2500 | } |
e5574168 |
2501 | } |
2502 | |
2503 | crFinishV; |
2504 | } |
2505 | |
2506 | /* |
7cca0d81 |
2507 | * Handle the top-level SSH2 protocol. |
2508 | */ |
2509 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) |
2510 | { |
2511 | if (do_ssh2_transport(in, inlen, ispkt) == 0) |
2512 | return; |
2513 | do_ssh2_authconn(in, inlen, ispkt); |
2514 | } |
2515 | |
2516 | /* |
374330e2 |
2517 | * Called to set up the connection. Will arrange for WM_NETEVENT |
2518 | * messages to be passed to the specified window, whose window |
2519 | * procedure should then call telnet_msg(). |
2520 | * |
2521 | * Returns an error message, or NULL on success. |
374330e2 |
2522 | */ |
2523 | static char *ssh_init (HWND hwnd, char *host, int port, char **realhost) { |
fb09bf1c |
2524 | char *p; |
8f203108 |
2525 | |
2526 | #ifdef MSCRYPTOAPI |
2527 | if(crypto_startup() == 0) |
2528 | return "Microsoft high encryption pack not installed!"; |
2529 | #endif |
374330e2 |
2530 | |
fb09bf1c |
2531 | p = connect_to_host(host, port, realhost); |
2532 | if (p != NULL) |
2533 | return p; |
374330e2 |
2534 | |
2535 | if (!do_ssh_init()) |
2536 | return "Protocol initialisation error"; |
2537 | |
4017be6d |
2538 | if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | FD_CLOSE) == SOCKET_ERROR) |
374330e2 |
2539 | switch (WSAGetLastError()) { |
2540 | case WSAENETDOWN: return "Network is down"; |
2541 | default: return "WSAAsyncSelect(): unknown error"; |
2542 | } |
2543 | |
2544 | return NULL; |
2545 | } |
2546 | |
2547 | /* |
2548 | * Process a WM_NETEVENT message. Will return 0 if the connection |
2549 | * has closed, or <0 for a socket error. |
2550 | */ |
2551 | static int ssh_msg (WPARAM wParam, LPARAM lParam) { |
2552 | int ret; |
2553 | char buf[256]; |
2554 | |
8ce72d2c |
2555 | /* |
2556 | * Because reading less than the whole of the available pending |
2557 | * data can generate an FD_READ event, we need to allow for the |
2558 | * possibility that FD_READ may arrive with FD_CLOSE already in |
2559 | * the queue; so it's possible that we can get here even with s |
2560 | * invalid. If so, we return 1 and don't worry about it. |
2561 | */ |
2562 | if (s == INVALID_SOCKET) |
2563 | return 1; |
374330e2 |
2564 | |
8d5de777 |
2565 | if (WSAGETSELECTERROR(lParam) != 0) { |
2566 | closesocket(s); |
2567 | s = INVALID_SOCKET; |
374330e2 |
2568 | return -WSAGETSELECTERROR(lParam); |
8d5de777 |
2569 | } |
374330e2 |
2570 | |
2571 | switch (WSAGETSELECTEVENT(lParam)) { |
2572 | case FD_READ: |
8ce72d2c |
2573 | case FD_CLOSE: |
374330e2 |
2574 | ret = recv(s, buf, sizeof(buf), 0); |
2575 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
2576 | return 1; |
8d5de777 |
2577 | if (ret < 0) { /* any _other_ error */ |
2578 | closesocket(s); |
2579 | s = INVALID_SOCKET; |
374330e2 |
2580 | return -10000-WSAGetLastError(); |
8d5de777 |
2581 | } |
374330e2 |
2582 | if (ret == 0) { |
2583 | s = INVALID_SOCKET; |
8ce72d2c |
2584 | return 0; |
374330e2 |
2585 | } |
2586 | ssh_gotdata (buf, ret); |
85ee8208 |
2587 | if (ssh_state == SSH_STATE_CLOSED) { |
2588 | closesocket(s); |
2589 | s = INVALID_SOCKET; |
2590 | return 0; |
2591 | } |
374330e2 |
2592 | return 1; |
374330e2 |
2593 | } |
2594 | return 1; /* shouldn't happen, but WTF */ |
2595 | } |
2596 | |
2597 | /* |
2598 | * Called to send data down the Telnet connection. |
2599 | */ |
2600 | static void ssh_send (char *buf, int len) { |
2601 | if (s == INVALID_SOCKET) |
2602 | return; |
2603 | |
2604 | ssh_protocol(buf, len, 0); |
2605 | } |
2606 | |
2607 | /* |
2608 | * Called to set the size of the window from Telnet's POV. |
2609 | */ |
2610 | static void ssh_size(void) { |
2611 | switch (ssh_state) { |
2612 | case SSH_STATE_BEFORE_SIZE: |
21248260 |
2613 | case SSH_STATE_CLOSED: |
374330e2 |
2614 | break; /* do nothing */ |
2615 | case SSH_STATE_INTERMED: |
2616 | size_needed = TRUE; /* buffer for later */ |
2617 | break; |
2618 | case SSH_STATE_SESSION: |
fef97f43 |
2619 | if (!cfg.nopty) { |
e5574168 |
2620 | send_packet(SSH1_CMSG_WINDOW_SIZE, |
fb09bf1c |
2621 | PKT_INT, rows, PKT_INT, cols, |
2622 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
fef97f43 |
2623 | } |
374330e2 |
2624 | } |
2625 | } |
2626 | |
2627 | /* |
6abbf9e3 |
2628 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
2629 | * can send an EOF and collect resulting output (e.g. `plink |
2630 | * hostname sort'). |
374330e2 |
2631 | */ |
2632 | static void ssh_special (Telnet_Special code) { |
6abbf9e3 |
2633 | if (code == TS_EOF) { |
67779be7 |
2634 | if (ssh_version == 1) { |
6abbf9e3 |
2635 | send_packet(SSH1_CMSG_EOF, PKT_END); |
2636 | } else { |
2637 | ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
d211621f |
2638 | ssh2_pkt_adduint32(mainchan->remoteid); |
6abbf9e3 |
2639 | ssh2_pkt_send(); |
2640 | } |
67779be7 |
2641 | logevent("Sent EOF message"); |
ec55b220 |
2642 | } else if (code == TS_PING) { |
2643 | if (ssh_version == 1) { |
2644 | send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
2645 | } else { |
2646 | ssh2_pkt_init(SSH2_MSG_IGNORE); |
2647 | ssh2_pkt_addstring_start(); |
2648 | ssh2_pkt_send(); |
2649 | } |
6abbf9e3 |
2650 | } else { |
2651 | /* do nothing */ |
2652 | } |
374330e2 |
2653 | } |
2654 | |
8ccc75b0 |
2655 | static SOCKET ssh_socket(void) { return s; } |
2656 | |
2657 | static int ssh_sendok(void) { return ssh_send_ok; } |
fb09bf1c |
2658 | |
374330e2 |
2659 | Backend ssh_backend = { |
2660 | ssh_init, |
2661 | ssh_msg, |
2662 | ssh_send, |
2663 | ssh_size, |
4017be6d |
2664 | ssh_special, |
8ccc75b0 |
2665 | ssh_socket, |
97db3be4 |
2666 | ssh_sendok, |
2667 | 22 |
374330e2 |
2668 | }; |