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