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