18 #define logevent(s) { logevent(s); \
19 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) \
20 fprintf(stderr, "%s\n", s); }
22 #define bombout(msg) ( ssh_state = SSH_STATE_CLOSED, sk_close(s), \
23 s = NULL, connection_fatal msg )
25 #define SSH1_MSG_DISCONNECT 1 /* 0x1 */
26 #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
27 #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
28 #define SSH1_CMSG_USER 4 /* 0x4 */
29 #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
30 #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
31 #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
32 #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
33 #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
34 #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
35 #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
36 #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
37 #define SSH1_SMSG_SUCCESS 14 /* 0xe */
38 #define SSH1_SMSG_FAILURE 15 /* 0xf */
39 #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
40 #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
41 #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
42 #define SSH1_CMSG_EOF 19 /* 0x13 */
43 #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
44 #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
45 #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
46 #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
47 #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
48 #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
49 #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
50 #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
51 #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
52 #define SSH1_MSG_IGNORE 32 /* 0x20 */
53 #define SSH1_MSG_DEBUG 36 /* 0x24 */
54 #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
55 #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
56 #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
57 #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
58 #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
59 #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
61 #define SSH1_AUTH_TIS 5 /* 0x5 */
62 #define SSH1_AUTH_CCARD 16 /* 0x10 */
64 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1 /* 0x1 */
65 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2 /* 0x2 */
66 #define SSH_AGENTC_RSA_CHALLENGE 3 /* 0x3 */
67 #define SSH_AGENT_RSA_RESPONSE 4 /* 0x4 */
68 #define SSH_AGENT_FAILURE 5 /* 0x5 */
69 #define SSH_AGENT_SUCCESS 6 /* 0x6 */
70 #define SSH_AGENTC_ADD_RSA_IDENTITY 7 /* 0x7 */
71 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8 /* 0x8 */
73 #define SSH2_MSG_DISCONNECT 1 /* 0x1 */
74 #define SSH2_MSG_IGNORE 2 /* 0x2 */
75 #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
76 #define SSH2_MSG_DEBUG 4 /* 0x4 */
77 #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
78 #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
79 #define SSH2_MSG_KEXINIT 20 /* 0x14 */
80 #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
81 #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
82 #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
83 #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
84 #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
85 #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
86 #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
87 #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
88 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
89 #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
90 #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
91 #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
92 #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
93 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
94 #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
95 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
96 #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
97 #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
98 #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
99 #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
100 #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
101 #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
102 #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
104 #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
105 #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
106 #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
107 #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
108 #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
109 #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
110 #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
111 #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
112 #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
113 #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
114 #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
116 #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
117 #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
118 #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
119 #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
121 #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
123 #define GET_32BIT(cp) \
124 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
125 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
126 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
127 ((unsigned long)(unsigned char)(cp)[3]))
129 #define PUT_32BIT(cp, value) { \
130 (cp)[0] = (unsigned char)((value) >> 24); \
131 (cp)[1] = (unsigned char)((value) >> 16); \
132 (cp)[2] = (unsigned char)((value) >> 8); \
133 (cp)[3] = (unsigned char)(value); }
135 enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM };
137 /* Coroutine mechanics for the sillier bits of the code */
138 #define crBegin1 static int crLine = 0;
139 #define crBegin2 switch(crLine) { case 0:;
140 #define crBegin crBegin1; crBegin2;
141 #define crFinish(z) } crLine = 0; return (z)
142 #define crFinishV } crLine = 0; return
143 #define crReturn(z) \
145 crLine=__LINE__; return (z); case __LINE__:;\
149 crLine=__LINE__; return; case __LINE__:;\
151 #define crStop(z) do{ crLine = 0; return (z); }while(0)
152 #define crStopV do{ crLine = 0; return; }while(0)
153 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
154 #define crWaitUntilV(c) do { crReturnV; } while (!(c))
156 extern const struct ssh_cipher ssh_3des;
157 extern const struct ssh_cipher ssh_3des_ssh2;
158 extern const struct ssh_cipher ssh_des;
159 extern const struct ssh_cipher ssh_blowfish_ssh1;
160 extern const struct ssh_cipher ssh_blowfish_ssh2;
163 * Ciphers for SSH2. We miss out single-DES because it isn't
164 * supported; also 3DES and Blowfish are both done differently from
165 * SSH1. (3DES uses outer chaining; Blowfish has the opposite
166 * endianness and different-sized keys.)
168 const static struct ssh_cipher *ciphers[] = { &ssh_blowfish_ssh2, &ssh_3des_ssh2 };
170 extern const struct ssh_kex ssh_diffiehellman;
171 const static struct ssh_kex *kex_algs[] = { &ssh_diffiehellman };
173 extern const struct ssh_hostkey ssh_dss;
174 const static struct ssh_hostkey *hostkey_algs[] = { &ssh_dss };
176 extern const struct ssh_mac ssh_md5, ssh_sha1, ssh_sha1_buggy;
178 static void nullmac_key(unsigned char *key) { }
179 static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { }
180 static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; }
181 const static struct ssh_mac ssh_mac_none = {
182 nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0
184 const static struct ssh_mac *macs[] = {
185 &ssh_sha1, &ssh_md5, &ssh_mac_none };
186 const static struct ssh_mac *buggymacs[] = {
187 &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none };
189 const static struct ssh_compress ssh_comp_none = {
192 const static struct ssh_compress *compressions[] = { &ssh_comp_none };
195 * 2-3-4 tree storing channels.
198 unsigned remoteid, localid;
202 struct ssh_agent_channel {
203 unsigned char *message;
204 unsigned char msglen[4];
205 int lensofar, totallen;
207 struct ssh2_data_channel {
208 unsigned char *outbuffer;
209 unsigned outbuflen, outbufsize;
210 unsigned remwindow, remmaxpkt;
224 static SHA_State exhash;
226 static Socket s = NULL;
228 static unsigned char session_key[32];
229 static const struct ssh_cipher *cipher = NULL;
230 static const struct ssh_cipher *cscipher = NULL;
231 static const struct ssh_cipher *sccipher = NULL;
232 static const struct ssh_mac *csmac = NULL;
233 static const struct ssh_mac *scmac = NULL;
234 static const struct ssh_compress *cscomp = NULL;
235 static const struct ssh_compress *sccomp = NULL;
236 static const struct ssh_kex *kex = NULL;
237 static const struct ssh_hostkey *hostkey = NULL;
238 int (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL;
240 static char *savedhost;
241 static int savedport;
242 static int ssh_send_ok;
244 static tree234 *ssh_channels; /* indexed by local id */
245 static struct ssh_channel *mainchan; /* primary session channel */
248 SSH_STATE_BEFORE_SIZE,
252 } ssh_state = SSH_STATE_BEFORE_SIZE;
254 static int size_needed = FALSE;
256 static struct Packet pktin = { 0, 0, NULL, NULL, 0 };
257 static struct Packet pktout = { 0, 0, NULL, NULL, 0 };
259 static int ssh_version;
260 static void (*ssh_protocol)(unsigned char *in, int inlen, int ispkt);
261 static void ssh1_protocol(unsigned char *in, int inlen, int ispkt);
262 static void ssh2_protocol(unsigned char *in, int inlen, int ispkt);
263 static void ssh_size(void);
265 static int (*s_rdpkt)(unsigned char **data, int *datalen);
267 static struct rdpkt1_state_tag {
268 long len, pad, biglen, to_read;
269 unsigned long realcrc, gotcrc;
275 static struct rdpkt2_state_tag {
276 long len, pad, payload, packetlen, maclen;
279 unsigned long incoming_sequence;
282 static int ssh_channelcmp(void *av, void *bv) {
283 struct ssh_channel *a = (struct ssh_channel *)av;
284 struct ssh_channel *b = (struct ssh_channel *)bv;
285 if (a->localid < b->localid) return -1;
286 if (a->localid > b->localid) return +1;
289 static int ssh_channelfind(void *av, void *bv) {
290 unsigned *a = (unsigned *)av;
291 struct ssh_channel *b = (struct ssh_channel *)bv;
292 if (*a < b->localid) return -1;
293 if (*a > b->localid) return +1;
297 static void c_write (char *buf, int len) {
298 if ((flags & FLAG_STDERR)) {
300 for (i = 0; i < len; i++)
302 fputc(buf[i], stderr);
305 from_backend(1, buf, len);
309 * Collect incoming data in the incoming packet buffer.
310 * Decipher and verify the packet when it is completely read.
311 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
312 * Update the *data and *datalen variables.
313 * Return the additional nr of bytes needed, or 0 when
314 * a complete packet is available.
316 static int ssh1_rdpkt(unsigned char **data, int *datalen)
318 struct rdpkt1_state_tag *st = &rdpkt1_state;
327 for (st->i = st->len = 0; st->i < 4; st->i++) {
328 while ((*datalen) == 0)
330 st->len = (st->len << 8) + **data;
331 (*data)++, (*datalen)--;
335 if (st->len == 0x52656d6f) { /* "Remo"te server has closed ... */
336 st->len = 0x300; /* big enough to carry to end */
340 st->pad = 8 - (st->len % 8);
341 st->biglen = st->len + st->pad;
342 pktin.length = st->len - 5;
344 if (pktin.maxlen < st->biglen) {
345 pktin.maxlen = st->biglen;
346 pktin.data = (pktin.data == NULL ? malloc(st->biglen+APIEXTRA) :
347 realloc(pktin.data, st->biglen+APIEXTRA));
349 fatalbox("Out of memory");
352 st->to_read = st->biglen;
354 while (st->to_read > 0) {
355 st->chunk = st->to_read;
356 while ((*datalen) == 0)
357 crReturn(st->to_read);
358 if (st->chunk > (*datalen))
359 st->chunk = (*datalen);
360 memcpy(st->p, *data, st->chunk);
362 *datalen -= st->chunk;
364 st->to_read -= st->chunk;
368 cipher->decrypt(pktin.data, st->biglen);
370 debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad));
371 for (st->i = 0; st->i < st->biglen; st->i++)
372 debug((" %02x", (unsigned char)pktin.data[st->i]));
376 pktin.type = pktin.data[st->pad];
377 pktin.body = pktin.data + st->pad + 1;
379 st->realcrc = crc32(pktin.data, st->biglen-4);
380 st->gotcrc = GET_32BIT(pktin.data+st->biglen-4);
381 if (st->gotcrc != st->realcrc) {
382 bombout(("Incorrect CRC received on packet"));
386 if (pktin.type == SSH1_SMSG_STDOUT_DATA ||
387 pktin.type == SSH1_SMSG_STDERR_DATA ||
388 pktin.type == SSH1_MSG_DEBUG ||
389 pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE ||
390 pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
391 long strlen = GET_32BIT(pktin.body);
392 if (strlen + 4 != pktin.length) {
393 bombout(("Received data packet with bogus string length"));
398 if (pktin.type == SSH1_MSG_DEBUG) {
399 /* log debug message */
401 int strlen = GET_32BIT(pktin.body);
402 strcpy(buf, "Remote: ");
403 if (strlen > 70) strlen = 70;
404 memcpy(buf+8, pktin.body+4, strlen);
405 buf[8+strlen] = '\0';
408 } else if (pktin.type == SSH1_MSG_IGNORE) {
416 static int ssh2_rdpkt(unsigned char **data, int *datalen)
418 struct rdpkt2_state_tag *st = &rdpkt2_state;
426 st->cipherblk = sccipher->blksize;
429 if (st->cipherblk < 8)
432 if (pktin.maxlen < st->cipherblk) {
433 pktin.maxlen = st->cipherblk;
434 pktin.data = (pktin.data == NULL ? malloc(st->cipherblk+APIEXTRA) :
435 realloc(pktin.data, st->cipherblk+APIEXTRA));
437 fatalbox("Out of memory");
441 * Acquire and decrypt the first block of the packet. This will
442 * contain the length and padding details.
444 for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {
445 while ((*datalen) == 0)
446 crReturn(st->cipherblk-st->i);
447 pktin.data[st->i] = *(*data)++;
451 if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */
456 sccipher->decrypt(pktin.data, st->cipherblk);
459 * Now get the length and padding figures.
461 st->len = GET_32BIT(pktin.data);
462 st->pad = pktin.data[4];
465 * This enables us to deduce the payload length.
467 st->payload = st->len - st->pad - 1;
469 pktin.length = st->payload + 5;
472 * So now we can work out the total packet length.
474 st->packetlen = st->len + 4;
475 st->maclen = scmac ? scmac->len : 0;
478 * Adjust memory allocation if packet is too big.
480 if (pktin.maxlen < st->packetlen+st->maclen) {
481 pktin.maxlen = st->packetlen+st->maclen;
482 pktin.data = (pktin.data == NULL ? malloc(pktin.maxlen+APIEXTRA) :
483 realloc(pktin.data, pktin.maxlen+APIEXTRA));
485 fatalbox("Out of memory");
489 * Read and decrypt the remainder of the packet.
491 for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) {
492 while ((*datalen) == 0)
493 crReturn(st->packetlen + st->maclen - st->i);
494 pktin.data[st->i] = *(*data)++;
497 /* Decrypt everything _except_ the MAC. */
499 sccipher->decrypt(pktin.data + st->cipherblk,
500 st->packetlen - st->cipherblk);
503 debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad));
504 for (st->i = 0; st->i < st->packetlen; st->i++)
505 debug((" %02x", (unsigned char)pktin.data[st->i]));
512 if (scmac && !scmac->verify(pktin.data, st->len+4, st->incoming_sequence)) {
513 bombout(("Incorrect MAC received on packet"));
516 st->incoming_sequence++; /* whether or not we MACed */
519 pktin.type = pktin.data[5];
521 if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG)
522 goto next_packet; /* FIXME: print DEBUG message */
527 static void s_wrpkt_start(int type, int len) {
530 len += 5; /* type and CRC */
534 pktout.length = len-5;
535 if (pktout.maxlen < biglen) {
536 pktout.maxlen = biglen;
538 /* Allocate enough buffer space for extra block
539 * for MS CryptEncrypt() */
540 pktout.data = (pktout.data == NULL ? malloc(biglen+12) :
541 realloc(pktout.data, biglen+12));
543 pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
544 realloc(pktout.data, biglen+4));
547 fatalbox("Out of memory");
551 pktout.body = pktout.data+4+pad+1;
554 static void s_wrpkt(void) {
555 int pad, len, biglen, i;
558 len = pktout.length + 5; /* type and CRC */
562 pktout.body[-1] = pktout.type;
563 for (i=0; i<pad; i++)
564 pktout.data[i+4] = random_byte();
565 crc = crc32(pktout.data+4, biglen-4);
566 PUT_32BIT(pktout.data+biglen, crc);
567 PUT_32BIT(pktout.data, len);
570 debug(("Sending packet len=%d\r\n", biglen+4));
571 for (i = 0; i < biglen+4; i++)
572 debug((" %02x", (unsigned char)pktout.data[i]));
576 cipher->encrypt(pktout.data+4, biglen);
578 sk_write(s, pktout.data, biglen+4);
582 * Construct a packet with the specified contents and
583 * send it to the server.
585 static void send_packet(int pkttype, ...)
588 unsigned char *p, *argp, argchar;
589 unsigned long argint;
590 int pktlen, argtype, arglen;
594 va_start(args, pkttype);
595 while ((argtype = va_arg(args, int)) != PKT_END) {
598 (void) va_arg(args, int);
602 (void) va_arg(args, char);
606 (void) va_arg(args, unsigned char *);
607 arglen = va_arg(args, int);
611 argp = va_arg(args, unsigned char *);
612 arglen = strlen(argp);
613 pktlen += 4 + arglen;
616 bn = va_arg(args, Bignum);
617 pktlen += ssh1_bignum_length(bn);
625 s_wrpkt_start(pkttype, pktlen);
628 va_start(args, pkttype);
629 while ((argtype = va_arg(args, int)) != PKT_END) {
632 argint = va_arg(args, int);
633 PUT_32BIT(p, argint);
637 argchar = va_arg(args, unsigned char);
642 argp = va_arg(args, unsigned char *);
643 arglen = va_arg(args, int);
644 memcpy(p, argp, arglen);
648 argp = va_arg(args, unsigned char *);
649 arglen = strlen(argp);
650 PUT_32BIT(p, arglen);
651 memcpy(p + 4, argp, arglen);
655 bn = va_arg(args, Bignum);
656 p += ssh1_write_bignum(p, bn);
665 static int ssh_versioncmp(char *a, char *b) {
667 unsigned long av, bv;
669 av = strtoul(a, &ae, 10);
670 bv = strtoul(b, &be, 10);
671 if (av != bv) return (av < bv ? -1 : +1);
672 if (*ae == '.') ae++;
673 if (*be == '.') be++;
674 av = strtoul(ae, &ae, 10);
675 bv = strtoul(be, &be, 10);
676 if (av != bv) return (av < bv ? -1 : +1);
682 * Utility routine for putting an SSH-protocol `string' into a SHA
686 static void sha_string(SHA_State *s, void *str, int len) {
687 unsigned char lenblk[4];
688 PUT_32BIT(lenblk, len);
689 SHA_Bytes(s, lenblk, 4);
690 SHA_Bytes(s, str, len);
694 * SSH2 packet construction functions.
696 static void ssh2_pkt_adddata(void *data, int len) {
697 pktout.length += len;
698 if (pktout.maxlen < pktout.length) {
699 pktout.maxlen = pktout.length + 256;
700 pktout.data = (pktout.data == NULL ? malloc(pktout.maxlen+APIEXTRA) :
701 realloc(pktout.data, pktout.maxlen+APIEXTRA));
703 fatalbox("Out of memory");
705 memcpy(pktout.data+pktout.length-len, data, len);
707 static void ssh2_pkt_addbyte(unsigned char byte) {
708 ssh2_pkt_adddata(&byte, 1);
710 static void ssh2_pkt_init(int pkt_type) {
712 ssh2_pkt_addbyte((unsigned char)pkt_type);
714 static void ssh2_pkt_addbool(unsigned char value) {
715 ssh2_pkt_adddata(&value, 1);
717 static void ssh2_pkt_adduint32(unsigned long value) {
720 ssh2_pkt_adddata(x, 4);
722 static void ssh2_pkt_addstring_start(void) {
723 ssh2_pkt_adduint32(0);
724 pktout.savedpos = pktout.length;
726 static void ssh2_pkt_addstring_str(char *data) {
727 ssh2_pkt_adddata(data, strlen(data));
728 PUT_32BIT(pktout.data + pktout.savedpos - 4,
729 pktout.length - pktout.savedpos);
731 static void ssh2_pkt_addstring_data(char *data, int len) {
732 ssh2_pkt_adddata(data, len);
733 PUT_32BIT(pktout.data + pktout.savedpos - 4,
734 pktout.length - pktout.savedpos);
736 static void ssh2_pkt_addstring(char *data) {
737 ssh2_pkt_addstring_start();
738 ssh2_pkt_addstring_str(data);
740 static char *ssh2_mpint_fmt(Bignum b, int *len) {
743 p = malloc(n * 2 + 1);
745 fatalbox("out of memory");
747 for (i = 0; i < n; i++) {
748 p[i*2+1] = (b[n-i] >> 8) & 0xFF;
749 p[i*2+2] = (b[n-i] ) & 0xFF;
752 while (p[i] == 0 && (p[i+1] & 0x80) == 0)
754 memmove(p, p+i, n*2+1-i);
758 static void ssh2_pkt_addmp(Bignum b) {
761 p = ssh2_mpint_fmt(b, &len);
762 ssh2_pkt_addstring_start();
763 ssh2_pkt_addstring_data(p, len);
766 static void ssh2_pkt_send(void) {
767 int cipherblk, maclen, padding, i;
768 static unsigned long outgoing_sequence = 0;
771 * Add padding. At least four bytes, and must also bring total
772 * length (minus MAC) up to a multiple of the block size.
774 cipherblk = cipher ? cipher->blksize : 8; /* block size */
775 cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
777 padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk;
778 pktout.data[4] = padding;
779 for (i = 0; i < padding; i++)
780 pktout.data[pktout.length + i] = random_byte();
781 PUT_32BIT(pktout.data, pktout.length + padding - 4);
783 csmac->generate(pktout.data, pktout.length + padding,
785 outgoing_sequence++; /* whether or not we MACed */
788 debug(("Sending packet len=%d\r\n", pktout.length+padding));
789 for (i = 0; i < pktout.length+padding; i++)
790 debug((" %02x", (unsigned char)pktout.data[i]));
795 cscipher->encrypt(pktout.data, pktout.length + padding);
796 maclen = csmac ? csmac->len : 0;
798 sk_write(s, pktout.data, pktout.length + padding + maclen);
802 void bndebug(char *string, Bignum b) {
805 p = ssh2_mpint_fmt(b, &len);
806 debug(("%s", string));
807 for (i = 0; i < len; i++)
808 debug((" %02x", p[i]));
814 static void sha_mpint(SHA_State *s, Bignum b) {
817 p = ssh2_mpint_fmt(b, &len);
818 sha_string(s, p, len);
823 * SSH2 packet decode functions.
825 static unsigned long ssh2_pkt_getuint32(void) {
827 if (pktin.length - pktin.savedpos < 4)
828 return 0; /* arrgh, no way to decline (FIXME?) */
829 value = GET_32BIT(pktin.data+pktin.savedpos);
833 static void ssh2_pkt_getstring(char **p, int *length) {
835 if (pktin.length - pktin.savedpos < 4)
837 *length = GET_32BIT(pktin.data+pktin.savedpos);
839 if (pktin.length - pktin.savedpos < *length)
841 *p = pktin.data+pktin.savedpos;
842 pktin.savedpos += *length;
844 static Bignum ssh2_pkt_getmp(void) {
849 ssh2_pkt_getstring(&p, &length);
853 bombout(("internal error: Can't handle negative mpints"));
856 b = newbn((length+1)/2);
857 for (i = 0; i < length; i++) {
860 b[j/2+1] |= ((unsigned char)p[i]) << 8;
862 b[j/2+1] |= ((unsigned char)p[i]);
864 while (b[0] > 1 && b[b[0]] == 0) b[0]--;
868 static int do_ssh_init(unsigned char c) {
870 static char version[10];
871 static char vstring[80];
872 static char vlog[sizeof(vstring)+20];
877 /* Search for the string "SSH-" in the input. */
880 static const int transS[] = { 1, 2, 2, 1 };
881 static const int transH[] = { 0, 0, 3, 0 };
882 static const int transminus[] = { 0, 0, 0, -1 };
883 if (c == 'S') i = transS[i];
884 else if (c == 'H') i = transH[i];
885 else if (c == '-') i = transminus[i];
889 crReturn(1); /* get another character */
892 strcpy(vstring, "SSH-");
896 crReturn(1); /* get another char */
897 if (vsp < vstring+sizeof(vstring)-1)
903 } else if (i < sizeof(version)-1)
910 rdpkt2_state.incoming_sequence = 0;
913 sprintf(vlog, "Server version: %s", vstring);
914 vlog[strcspn(vlog, "\r\n")] = '\0';
918 * Server version "1.99" means we can choose whether we use v1
919 * or v2 protocol. Choice is based on cfg.sshprot.
921 if (ssh_versioncmp(version, cfg.sshprot == 1 ? "2.0" : "1.99") >= 0) {
923 * This is a v2 server. Begin v2 protocol.
925 char *verstring = "SSH-2.0-PuTTY";
928 * Hash our version string and their version string.
930 sha_string(&exhash, verstring, strlen(verstring));
931 sha_string(&exhash, vstring, strcspn(vstring, "\r\n"));
932 sprintf(vstring, "%s\n", verstring);
933 sprintf(vlog, "We claim version: %s", verstring);
935 logevent("Using SSH protocol version 2");
936 sk_write(s, vstring, strlen(vstring));
937 ssh_protocol = ssh2_protocol;
939 s_rdpkt = ssh2_rdpkt;
942 * This is a v1 server. Begin v1 protocol.
944 sprintf(vstring, "SSH-%s-PuTTY\n",
945 (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
946 sprintf(vlog, "We claim version: %s", vstring);
947 vlog[strcspn(vlog, "\r\n")] = '\0';
949 logevent("Using SSH protocol version 1");
950 sk_write(s, vstring, strlen(vstring));
951 ssh_protocol = ssh1_protocol;
953 s_rdpkt = ssh1_rdpkt;
959 static void ssh_gotdata(unsigned char *data, int datalen)
964 * To begin with, feed the characters one by one to the
965 * protocol initialisation / selection function do_ssh_init().
966 * When that returns 0, we're done with the initial greeting
967 * exchange and can move on to packet discipline.
972 crReturnV; /* more data please */
973 ret = do_ssh_init(*data);
980 * We emerge from that loop when the initial negotiation is
981 * over and we have selected an s_rdpkt function. Now pass
982 * everything to s_rdpkt, and then pass the resulting packets
983 * to the proper protocol handler.
988 while (datalen > 0) {
989 if ( s_rdpkt(&data, &datalen) == 0 ) {
990 ssh_protocol(NULL, 0, 1);
991 if (ssh_state == SSH_STATE_CLOSED) {
1001 static int ssh_receive(Socket s, int urgent, char *data, int len) {
1003 /* Connection has closed. */
1008 ssh_gotdata (data, len);
1009 if (ssh_state == SSH_STATE_CLOSED) {
1020 * Connect to specified host and port.
1021 * Returns an error message, or NULL on success.
1022 * Also places the canonical host name into `realhost'.
1024 static char *connect_to_host(char *host, int port, char **realhost)
1033 savedhost = malloc(1+strlen(host));
1035 fatalbox("Out of memory");
1036 strcpy(savedhost, host);
1039 port = 22; /* default ssh port */
1052 addr = sk_namelookup(host, realhost);
1053 if ( (err = sk_addr_error(addr)) )
1063 s = sk_new(addr, port, ssh_receive);
1064 if ( (err = sk_socket_error(s)) )
1068 sk_write(s, "connect ", 8);
1069 sk_write(s, FWhost, strlen(FWhost));
1072 sprintf(buf, " %d\n", FWport);
1073 sk_write(s, buf, strlen(buf));
1081 * Handle the key exchange and user authentication phases.
1083 static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
1086 unsigned char *rsabuf, *keystr1, *keystr2;
1087 unsigned char cookie[8];
1088 struct RSAKey servkey, hostkey;
1089 struct MD5Context md5c;
1090 static unsigned long supported_ciphers_mask, supported_auths_mask;
1091 static int tried_publickey;
1092 static unsigned char session_id[16];
1094 static char username[100];
1098 if (!ispkt) crWaitUntil(ispkt);
1100 if (pktin.type != SSH1_SMSG_PUBLIC_KEY) {
1101 bombout(("Public key packet not received"));
1105 logevent("Received public keys");
1107 memcpy(cookie, pktin.body, 8);
1109 i = makekey(pktin.body+8, &servkey, &keystr1, 0);
1110 j = makekey(pktin.body+8+i, &hostkey, &keystr2, 0);
1113 * Log the host key fingerprint.
1117 logevent("Host key fingerprint is:");
1118 strcpy(logmsg, " ");
1119 hostkey.comment = NULL;
1120 rsa_fingerprint(logmsg+strlen(logmsg), sizeof(logmsg)-strlen(logmsg),
1125 supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j);
1126 supported_auths_mask = GET_32BIT(pktin.body+16+i+j);
1129 MD5Update(&md5c, keystr2, hostkey.bytes);
1130 MD5Update(&md5c, keystr1, servkey.bytes);
1131 MD5Update(&md5c, pktin.body, 8);
1132 MD5Final(session_id, &md5c);
1134 for (i=0; i<32; i++)
1135 session_key[i] = random_byte();
1137 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
1139 rsabuf = malloc(len);
1141 fatalbox("Out of memory");
1144 * Verify the host key.
1148 * First format the key into a string.
1150 int len = rsastr_len(&hostkey);
1151 char fingerprint[100];
1152 char *keystr = malloc(len);
1154 fatalbox("Out of memory");
1155 rsastr_fmt(keystr, &hostkey);
1156 rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey);
1157 verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint);
1161 for (i=0; i<32; i++) {
1162 rsabuf[i] = session_key[i];
1164 rsabuf[i] ^= session_id[i];
1167 if (hostkey.bytes > servkey.bytes) {
1168 rsaencrypt(rsabuf, 32, &servkey);
1169 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
1171 rsaencrypt(rsabuf, 32, &hostkey);
1172 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
1175 logevent("Encrypted session key");
1177 cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
1178 cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES :
1180 if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
1181 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
1182 cipher_type = SSH_CIPHER_3DES;
1184 switch (cipher_type) {
1185 case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break;
1186 case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break;
1187 case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break;
1190 send_packet(SSH1_CMSG_SESSION_KEY,
1191 PKT_CHAR, cipher_type,
1192 PKT_DATA, cookie, 8,
1193 PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF,
1194 PKT_DATA, rsabuf, len,
1198 logevent("Trying to enable encryption...");
1202 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :
1203 cipher_type == SSH_CIPHER_DES ? &ssh_des :
1205 cipher->sesskey(session_key);
1209 if (pktin.type != SSH1_SMSG_SUCCESS) {
1210 bombout(("Encryption not successfully enabled"));
1214 logevent("Successfully started encryption");
1220 if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
1221 c_write("login as: ", 10);
1224 crWaitUntil(!ispkt);
1225 while (inlen--) switch (c = *in++) {
1232 c_write("\b \b", 3);
1238 c_write("\b \b", 3);
1247 if (((c >= ' ' && c <= '~') ||
1248 ((unsigned char)c >= 160)) && pos < 40) {
1249 username[pos++] = c;
1256 username[strcspn(username, "\n\r")] = '\0';
1259 strncpy(username, cfg.username, 99);
1260 username[99] = '\0';
1261 if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
1262 sprintf(stuff, "Sent username \"%s\".\r\n", username);
1263 c_write(stuff, strlen(stuff));
1267 send_packet(SSH1_CMSG_USER, PKT_STR, username, PKT_END);
1269 char userlog[20+sizeof(username)];
1270 sprintf(userlog, "Sent username \"%s\"", username);
1277 tried_publickey = 0;
1279 while (pktin.type == SSH1_SMSG_FAILURE) {
1280 static char password[100];
1281 static char prompt[200];
1284 static int pwpkt_type;
1286 * Show password prompt, having first obtained it via a TIS
1287 * or CryptoCard exchange if we're doing TIS or CryptoCard
1290 pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;
1291 if (agent_exists()) {
1293 * Attempt RSA authentication using Pageant.
1295 static unsigned char request[5], *response, *p;
1296 static int responselen;
1297 static int i, nkeys;
1298 static int authed = FALSE;
1301 logevent("Pageant is running. Requesting keys.");
1303 /* Request the keys held by the agent. */
1304 PUT_32BIT(request, 1);
1305 request[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
1306 agent_query(request, 5, &r, &responselen);
1307 response = (unsigned char *)r;
1310 nkeys = GET_32BIT(p); p += 4;
1311 { char buf[64]; sprintf(buf, "Pageant has %d keys", nkeys);
1313 for (i = 0; i < nkeys; i++) {
1314 static struct RSAKey key;
1315 static Bignum challenge;
1316 static char *commentp;
1317 static int commentlen;
1319 { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i);
1322 p += ssh1_read_bignum(p, &key.exponent);
1323 p += ssh1_read_bignum(p, &key.modulus);
1324 commentlen = GET_32BIT(p); p += 4;
1325 commentp = p; p += commentlen;
1326 send_packet(SSH1_CMSG_AUTH_RSA,
1327 PKT_BIGNUM, key.modulus, PKT_END);
1329 if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
1330 logevent("Key refused");
1333 logevent("Received RSA challenge");
1334 ssh1_read_bignum(pktin.body, &challenge);
1336 char *agentreq, *q, *ret;
1338 len = 1 + 4; /* message type, bit count */
1339 len += ssh1_bignum_length(key.exponent);
1340 len += ssh1_bignum_length(key.modulus);
1341 len += ssh1_bignum_length(challenge);
1342 len += 16; /* session id */
1343 len += 4; /* response format */
1344 agentreq = malloc(4 + len);
1345 PUT_32BIT(agentreq, len);
1347 *q++ = SSH_AGENTC_RSA_CHALLENGE;
1348 PUT_32BIT(q, ssh1_bignum_bitcount(key.modulus));
1350 q += ssh1_write_bignum(q, key.exponent);
1351 q += ssh1_write_bignum(q, key.modulus);
1352 q += ssh1_write_bignum(q, challenge);
1353 memcpy(q, session_id, 16); q += 16;
1354 PUT_32BIT(q, 1); /* response format */
1355 agent_query(agentreq, len+4, &ret, &retlen);
1358 if (ret[4] == SSH_AGENT_RSA_RESPONSE) {
1359 logevent("Sending Pageant's response");
1360 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE,
1361 PKT_DATA, ret+5, 16, PKT_END);
1364 if (pktin.type == SSH1_SMSG_SUCCESS) {
1365 logevent("Pageant's response accepted");
1366 if (flags & FLAG_VERBOSE) {
1367 c_write("Authenticated using RSA key \"",
1369 c_write(commentp, commentlen);
1370 c_write("\" from agent\r\n", 14);
1374 logevent("Pageant's response not accepted");
1376 logevent("Pageant failed to answer challenge");
1380 logevent("No reply received from Pageant");
1383 freebn(key.exponent);
1384 freebn(key.modulus);
1393 if (*cfg.keyfile && !tried_publickey)
1394 pwpkt_type = SSH1_CMSG_AUTH_RSA;
1396 if (pktin.type == SSH1_SMSG_FAILURE &&
1398 (supported_auths_mask & (1<<SSH1_AUTH_TIS))) {
1399 pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;
1400 logevent("Requested TIS authentication");
1401 send_packet(SSH1_CMSG_AUTH_TIS, PKT_END);
1403 if (pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) {
1404 logevent("TIS authentication declined");
1405 if (flags & FLAG_INTERACTIVE)
1406 c_write("TIS authentication refused.\r\n", 29);
1408 int challengelen = ((pktin.body[0] << 24) |
1409 (pktin.body[1] << 16) |
1410 (pktin.body[2] << 8) |
1412 logevent("Received TIS challenge");
1413 if (challengelen > sizeof(prompt)-1)
1414 challengelen = sizeof(prompt)-1; /* prevent overrun */
1415 memcpy(prompt, pktin.body+4, challengelen);
1416 prompt[challengelen] = '\0';
1419 if (pktin.type == SSH1_SMSG_FAILURE &&
1421 (supported_auths_mask & (1<<SSH1_AUTH_CCARD))) {
1422 pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;
1423 logevent("Requested CryptoCard authentication");
1424 send_packet(SSH1_CMSG_AUTH_CCARD, PKT_END);
1426 if (pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
1427 logevent("CryptoCard authentication declined");
1428 c_write("CryptoCard authentication refused.\r\n", 29);
1430 int challengelen = ((pktin.body[0] << 24) |
1431 (pktin.body[1] << 16) |
1432 (pktin.body[2] << 8) |
1434 logevent("Received CryptoCard challenge");
1435 if (challengelen > sizeof(prompt)-1)
1436 challengelen = sizeof(prompt)-1; /* prevent overrun */
1437 memcpy(prompt, pktin.body+4, challengelen);
1438 strncpy(prompt + challengelen, "\r\nResponse : ",
1439 sizeof(prompt)-challengelen);
1440 prompt[sizeof(prompt)-1] = '\0';
1443 if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
1444 sprintf(prompt, "%.90s@%.90s's password: ",
1445 username, savedhost);
1447 if (pwpkt_type == SSH1_CMSG_AUTH_RSA) {
1448 char *comment = NULL;
1449 if (flags & FLAG_VERBOSE)
1450 c_write("Trying public key authentication.\r\n", 35);
1451 if (!rsakey_encrypted(cfg.keyfile, &comment)) {
1452 if (flags & FLAG_VERBOSE)
1453 c_write("No passphrase required.\r\n", 25);
1456 sprintf(prompt, "Passphrase for key \"%.100s\": ", comment);
1460 if (ssh_get_password) {
1461 if (!ssh_get_password(prompt, password, sizeof(password))) {
1463 * get_password failed to get a password (for
1464 * example because one was supplied on the command
1465 * line which has already failed to work).
1468 logevent("No more passwords to try");
1469 ssh_state = SSH_STATE_CLOSED;
1473 c_write(prompt, strlen(prompt));
1477 crWaitUntil(!ispkt);
1478 while (inlen--) switch (c = *in++) {
1495 if (((c >= ' ' && c <= '~') ||
1496 ((unsigned char)c >= 160)) && pos < sizeof(password))
1497 password[pos++] = c;
1505 if (pwpkt_type == SSH1_CMSG_AUTH_RSA) {
1507 * Try public key authentication with the specified
1510 static struct RSAKey pubkey;
1511 static Bignum challenge, response;
1513 static unsigned char buffer[32];
1515 tried_publickey = 1;
1516 i = loadrsakey(cfg.keyfile, &pubkey, NULL, password);
1518 c_write("Couldn't load public key from ", 30);
1519 c_write(cfg.keyfile, strlen(cfg.keyfile));
1520 c_write(".\r\n", 3);
1521 continue; /* go and try password */
1524 c_write("Wrong passphrase.\r\n", 19);
1525 tried_publickey = 0;
1526 continue; /* try again */
1530 * Send a public key attempt.
1532 send_packet(SSH1_CMSG_AUTH_RSA,
1533 PKT_BIGNUM, pubkey.modulus, PKT_END);
1536 if (pktin.type == SSH1_SMSG_FAILURE) {
1537 c_write("Server refused our public key.\r\n", 32);
1538 continue; /* go and try password */
1540 if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
1541 bombout(("Bizarre response to offer of public key"));
1544 ssh1_read_bignum(pktin.body, &challenge);
1545 response = rsadecrypt(challenge, &pubkey);
1546 freebn(pubkey.private_exponent); /* burn the evidence */
1548 for (i = 0; i < 32; i += 2) {
1549 buffer[i] = response[16-i/2] >> 8;
1550 buffer[i+1] = response[16-i/2] & 0xFF;
1554 MD5Update(&md5c, buffer, 32);
1555 MD5Update(&md5c, session_id, 16);
1556 MD5Final(buffer, &md5c);
1558 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE,
1559 PKT_DATA, buffer, 16, PKT_END);
1562 if (pktin.type == SSH1_SMSG_FAILURE) {
1563 if (flags & FLAG_VERBOSE)
1564 c_write("Failed to authenticate with our public key.\r\n",
1566 continue; /* go and try password */
1567 } else if (pktin.type != SSH1_SMSG_SUCCESS) {
1568 bombout(("Bizarre response to RSA authentication response"));
1572 break; /* we're through! */
1574 send_packet(pwpkt_type, PKT_STR, password, PKT_END);
1576 logevent("Sent password");
1577 memset(password, 0, strlen(password));
1579 if (pktin.type == SSH1_SMSG_FAILURE) {
1580 if (flags & FLAG_VERBOSE)
1581 c_write("Access denied\r\n", 15);
1582 logevent("Authentication refused");
1583 } else if (pktin.type == SSH1_MSG_DISCONNECT) {
1584 logevent("Received disconnect request");
1585 ssh_state = SSH_STATE_CLOSED;
1587 } else if (pktin.type != SSH1_SMSG_SUCCESS) {
1588 bombout(("Strange packet received, type %d", pktin.type));
1593 logevent("Authentication successful");
1598 static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
1603 while (!do_ssh1_login(in, inlen, ispkt)) {
1606 if (ssh_state == SSH_STATE_CLOSED)
1609 if (cfg.agentfwd && agent_exists()) {
1610 logevent("Requesting agent forwarding");
1611 send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);
1612 do { crReturnV; } while (!ispkt);
1613 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
1614 bombout(("Protocol confusion"));
1616 } else if (pktin.type == SSH1_SMSG_FAILURE) {
1617 logevent("Agent forwarding refused");
1619 logevent("Agent forwarding enabled");
1623 send_packet(SSH1_CMSG_REQUEST_PTY,
1624 PKT_STR, cfg.termtype,
1625 PKT_INT, rows, PKT_INT, cols,
1626 PKT_INT, 0, PKT_INT, 0,
1629 ssh_state = SSH_STATE_INTERMED;
1630 do { crReturnV; } while (!ispkt);
1631 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
1632 bombout(("Protocol confusion"));
1634 } else if (pktin.type == SSH1_SMSG_FAILURE) {
1635 c_write("Server refused to allocate pty\r\n", 32);
1637 logevent("Allocated pty");
1640 if (*cfg.remote_cmd)
1641 send_packet(SSH1_CMSG_EXEC_CMD, PKT_STR, cfg.remote_cmd, PKT_END);
1643 send_packet(SSH1_CMSG_EXEC_SHELL, PKT_END);
1644 logevent("Started session");
1646 ssh_state = SSH_STATE_SESSION;
1651 ssh_channels = newtree234(ssh_channelcmp);
1656 if (pktin.type == SSH1_SMSG_STDOUT_DATA ||
1657 pktin.type == SSH1_SMSG_STDERR_DATA) {
1658 long len = GET_32BIT(pktin.body);
1659 from_backend(pktin.type == SSH1_SMSG_STDERR_DATA,
1661 } else if (pktin.type == SSH1_MSG_DISCONNECT) {
1662 ssh_state = SSH_STATE_CLOSED;
1663 logevent("Received disconnect request");
1665 } else if (pktin.type == SSH1_SMSG_AGENT_OPEN) {
1666 /* Remote side is trying to open a channel to talk to our
1667 * agent. Give them back a local channel number. */
1669 struct ssh_channel *c;
1671 for (c = first234(ssh_channels, &e); c; c = next234(&e)) {
1673 break; /* found a free number */
1676 c = malloc(sizeof(struct ssh_channel));
1677 c->remoteid = GET_32BIT(pktin.body);
1680 c->type = SSH1_SMSG_AGENT_OPEN; /* identify channel type */
1681 c->u.a.lensofar = 0;
1682 add234(ssh_channels, c);
1683 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
1684 PKT_INT, c->remoteid, PKT_INT, c->localid,
1686 } else if (pktin.type == SSH1_MSG_CHANNEL_CLOSE ||
1687 pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) {
1688 /* Remote side closes a channel. */
1689 unsigned i = GET_32BIT(pktin.body);
1690 struct ssh_channel *c;
1691 c = find234(ssh_channels, &i, ssh_channelfind);
1694 closetype = (pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2);
1695 send_packet(pktin.type, PKT_INT, c->remoteid, PKT_END);
1696 c->closes |= closetype;
1697 if (c->closes == 3) {
1698 del234(ssh_channels, c);
1702 } else if (pktin.type == SSH1_MSG_CHANNEL_DATA) {
1703 /* Data sent down one of our channels. */
1704 int i = GET_32BIT(pktin.body);
1705 int len = GET_32BIT(pktin.body+4);
1706 unsigned char *p = pktin.body+8;
1707 struct ssh_channel *c;
1708 c = find234(ssh_channels, &i, ssh_channelfind);
1711 case SSH1_SMSG_AGENT_OPEN:
1712 /* Data for an agent message. Buffer it. */
1714 if (c->u.a.lensofar < 4) {
1715 int l = min(4 - c->u.a.lensofar, len);
1716 memcpy(c->u.a.msglen + c->u.a.lensofar, p, l);
1717 p += l; len -= l; c->u.a.lensofar += l;
1719 if (c->u.a.lensofar == 4) {
1720 c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
1721 c->u.a.message = malloc(c->u.a.totallen);
1722 memcpy(c->u.a.message, c->u.a.msglen, 4);
1724 if (c->u.a.lensofar >= 4 && len > 0) {
1725 int l = min(c->u.a.totallen - c->u.a.lensofar, len);
1726 memcpy(c->u.a.message + c->u.a.lensofar, p, l);
1727 p += l; len -= l; c->u.a.lensofar += l;
1729 if (c->u.a.lensofar == c->u.a.totallen) {
1730 void *reply, *sentreply;
1732 agent_query(c->u.a.message, c->u.a.totallen,
1737 /* Fake SSH_AGENT_FAILURE. */
1738 sentreply = "\0\0\0\1\5";
1741 send_packet(SSH1_MSG_CHANNEL_DATA,
1742 PKT_INT, c->remoteid,
1744 PKT_DATA, sentreply, replylen,
1748 free(c->u.a.message);
1749 c->u.a.lensofar = 0;
1755 } else if (pktin.type == SSH1_SMSG_SUCCESS) {
1756 /* may be from EXEC_SHELL on some servers */
1757 } else if (pktin.type == SSH1_SMSG_FAILURE) {
1758 /* may be from EXEC_SHELL on some servers
1759 * if no pty is available or in other odd cases. Ignore */
1760 } else if (pktin.type == SSH1_SMSG_EXIT_STATUS) {
1761 send_packet(SSH1_CMSG_EXIT_CONFIRMATION, PKT_END);
1763 bombout(("Strange packet received: type %d", pktin.type));
1768 int len = min(inlen, 512);
1769 send_packet(SSH1_CMSG_STDIN_DATA,
1770 PKT_INT, len, PKT_DATA, in, len, PKT_END);
1781 * Utility routine for decoding comma-separated strings in KEXINIT.
1783 static int in_commasep_string(char *needle, char *haystack, int haylen) {
1784 int needlen = strlen(needle);
1787 * Is it at the start of the string?
1789 if (haylen >= needlen && /* haystack is long enough */
1790 !memcmp(needle, haystack, needlen) && /* initial match */
1791 (haylen == needlen || haystack[needlen] == ',')
1792 /* either , or EOS follows */
1796 * If not, search for the next comma and resume after that.
1797 * If no comma found, terminate.
1799 while (haylen > 0 && *haystack != ',')
1800 haylen--, haystack++;
1803 haylen--, haystack++; /* skip over comma itself */
1808 * SSH2 key creation method.
1810 static void ssh2_mkkey(Bignum K, char *H, char chr, char *keyspace) {
1812 /* First 20 bytes. */
1815 SHA_Bytes(&s, H, 20);
1816 SHA_Bytes(&s, &chr, 1);
1817 SHA_Bytes(&s, H, 20);
1818 SHA_Final(&s, keyspace);
1819 /* Next 20 bytes. */
1822 SHA_Bytes(&s, H, 20);
1823 SHA_Bytes(&s, keyspace, 20);
1824 SHA_Final(&s, keyspace+20);
1828 * Handle the SSH2 transport layer.
1830 static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt)
1834 static Bignum e, f, K;
1835 static const struct ssh_mac **maclist;
1837 static const struct ssh_cipher *cscipher_tobe = NULL;
1838 static const struct ssh_cipher *sccipher_tobe = NULL;
1839 static const struct ssh_mac *csmac_tobe = NULL;
1840 static const struct ssh_mac *scmac_tobe = NULL;
1841 static const struct ssh_compress *cscomp_tobe = NULL;
1842 static const struct ssh_compress *sccomp_tobe = NULL;
1843 static char *hostkeydata, *sigdata, *keystr, *fingerprint;
1844 static int hostkeylen, siglen;
1845 static unsigned char exchange_hash[20];
1846 static unsigned char keyspace[40];
1847 static const struct ssh_cipher *preferred_cipher;
1853 * Set up the preferred cipher.
1855 if (cfg.cipher == CIPHER_BLOWFISH) {
1856 preferred_cipher = &ssh_blowfish_ssh2;
1857 } else if (cfg.cipher == CIPHER_DES) {
1858 logevent("Single DES not supported in SSH2; using 3DES");
1859 preferred_cipher = &ssh_3des_ssh2;
1860 } else if (cfg.cipher == CIPHER_3DES) {
1861 preferred_cipher = &ssh_3des_ssh2;
1863 /* Shouldn't happen, but we do want to initialise to _something_. */
1864 preferred_cipher = &ssh_3des_ssh2;
1868 * Be prepared to work around the buggy MAC problem.
1871 maclist = buggymacs, nmacs = lenof(buggymacs);
1873 maclist = macs, nmacs = lenof(macs);
1877 * Construct and send our key exchange packet.
1879 ssh2_pkt_init(SSH2_MSG_KEXINIT);
1880 for (i = 0; i < 16; i++)
1881 ssh2_pkt_addbyte((unsigned char)random_byte());
1882 /* List key exchange algorithms. */
1883 ssh2_pkt_addstring_start();
1884 for (i = 0; i < lenof(kex_algs); i++) {
1885 ssh2_pkt_addstring_str(kex_algs[i]->name);
1886 if (i < lenof(kex_algs)-1)
1887 ssh2_pkt_addstring_str(",");
1889 /* List server host key algorithms. */
1890 ssh2_pkt_addstring_start();
1891 for (i = 0; i < lenof(hostkey_algs); i++) {
1892 ssh2_pkt_addstring_str(hostkey_algs[i]->name);
1893 if (i < lenof(hostkey_algs)-1)
1894 ssh2_pkt_addstring_str(",");
1896 /* List client->server encryption algorithms. */
1897 ssh2_pkt_addstring_start();
1898 for (i = 0; i < lenof(ciphers)+1; i++) {
1899 const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1];
1900 ssh2_pkt_addstring_str(c->name);
1901 if (i < lenof(ciphers))
1902 ssh2_pkt_addstring_str(",");
1904 /* List server->client encryption algorithms. */
1905 ssh2_pkt_addstring_start();
1906 for (i = 0; i < lenof(ciphers)+1; i++) {
1907 const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1];
1908 ssh2_pkt_addstring_str(c->name);
1909 if (i < lenof(ciphers))
1910 ssh2_pkt_addstring_str(",");
1912 /* List client->server MAC algorithms. */
1913 ssh2_pkt_addstring_start();
1914 for (i = 0; i < nmacs; i++) {
1915 ssh2_pkt_addstring_str(maclist[i]->name);
1917 ssh2_pkt_addstring_str(",");
1919 /* List server->client MAC algorithms. */
1920 ssh2_pkt_addstring_start();
1921 for (i = 0; i < nmacs; i++) {
1922 ssh2_pkt_addstring_str(maclist[i]->name);
1924 ssh2_pkt_addstring_str(",");
1926 /* List client->server compression algorithms. */
1927 ssh2_pkt_addstring_start();
1928 for (i = 0; i < lenof(compressions); i++) {
1929 ssh2_pkt_addstring_str(compressions[i]->name);
1930 if (i < lenof(compressions)-1)
1931 ssh2_pkt_addstring_str(",");
1933 /* List server->client compression algorithms. */
1934 ssh2_pkt_addstring_start();
1935 for (i = 0; i < lenof(compressions); i++) {
1936 ssh2_pkt_addstring_str(compressions[i]->name);
1937 if (i < lenof(compressions)-1)
1938 ssh2_pkt_addstring_str(",");
1940 /* List client->server languages. Empty list. */
1941 ssh2_pkt_addstring_start();
1942 /* List server->client languages. Empty list. */
1943 ssh2_pkt_addstring_start();
1944 /* First KEX packet does _not_ follow, because we're not that brave. */
1945 ssh2_pkt_addbool(FALSE);
1947 ssh2_pkt_adduint32(0);
1948 sha_string(&exhash, pktout.data+5, pktout.length-5);
1951 if (!ispkt) crWaitUntil(ispkt);
1952 sha_string(&exhash, pktin.data+5, pktin.length-5);
1955 * Now examine the other side's KEXINIT to see what we're up
1958 if (pktin.type != SSH2_MSG_KEXINIT) {
1959 bombout(("expected key exchange packet from server"));
1962 kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL;
1963 csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL;
1964 pktin.savedpos += 16; /* skip garbage cookie */
1965 ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */
1966 for (i = 0; i < lenof(kex_algs); i++) {
1967 if (in_commasep_string(kex_algs[i]->name, str, len)) {
1972 ssh2_pkt_getstring(&str, &len); /* host key algorithms */
1973 for (i = 0; i < lenof(hostkey_algs); i++) {
1974 if (in_commasep_string(hostkey_algs[i]->name, str, len)) {
1975 hostkey = hostkey_algs[i];
1979 ssh2_pkt_getstring(&str, &len); /* client->server cipher */
1980 for (i = 0; i < lenof(ciphers)+1; i++) {
1981 const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1];
1982 if (in_commasep_string(c->name, str, len)) {
1987 ssh2_pkt_getstring(&str, &len); /* server->client cipher */
1988 for (i = 0; i < lenof(ciphers)+1; i++) {
1989 const struct ssh_cipher *c = i==0 ? preferred_cipher : ciphers[i-1];
1990 if (in_commasep_string(c->name, str, len)) {
1995 ssh2_pkt_getstring(&str, &len); /* client->server mac */
1996 for (i = 0; i < nmacs; i++) {
1997 if (in_commasep_string(maclist[i]->name, str, len)) {
1998 csmac_tobe = maclist[i];
2002 ssh2_pkt_getstring(&str, &len); /* server->client mac */
2003 for (i = 0; i < nmacs; i++) {
2004 if (in_commasep_string(maclist[i]->name, str, len)) {
2005 scmac_tobe = maclist[i];
2009 ssh2_pkt_getstring(&str, &len); /* client->server compression */
2010 for (i = 0; i < lenof(compressions); i++) {
2011 if (in_commasep_string(compressions[i]->name, str, len)) {
2012 cscomp_tobe = compressions[i];
2016 ssh2_pkt_getstring(&str, &len); /* server->client compression */
2017 for (i = 0; i < lenof(compressions); i++) {
2018 if (in_commasep_string(compressions[i]->name, str, len)) {
2019 sccomp_tobe = compressions[i];
2025 * Currently we only support Diffie-Hellman and DSS, so let's
2026 * bomb out if those aren't selected.
2028 if (kex != &ssh_diffiehellman || hostkey != &ssh_dss) {
2029 bombout(("internal fault: chaos in SSH 2 transport layer"));
2034 * Now we begin the fun. Generate and send e for Diffie-Hellman.
2037 ssh2_pkt_init(SSH2_MSG_KEXDH_INIT);
2042 if (pktin.type != SSH2_MSG_KEXDH_REPLY) {
2043 bombout(("expected key exchange packet from server"));
2046 ssh2_pkt_getstring(&hostkeydata, &hostkeylen);
2047 f = ssh2_pkt_getmp();
2048 ssh2_pkt_getstring(&sigdata, &siglen);
2052 sha_string(&exhash, hostkeydata, hostkeylen);
2053 sha_mpint(&exhash, e);
2054 sha_mpint(&exhash, f);
2055 sha_mpint(&exhash, K);
2056 SHA_Final(&exhash, exchange_hash);
2059 debug(("Exchange hash is:\r\n"));
2060 for (i = 0; i < 20; i++)
2061 debug((" %02x", exchange_hash[i]));
2065 hostkey->setkey(hostkeydata, hostkeylen);
2066 if (!hostkey->verifysig(sigdata, siglen, exchange_hash, 20)) {
2067 bombout(("Server failed host key check"));
2072 * Expect SSH2_MSG_NEWKEYS from server.
2075 if (pktin.type != SSH2_MSG_NEWKEYS) {
2076 bombout(("expected new-keys packet from server"));
2081 * Authenticate remote host: verify host key. (We've already
2082 * checked the signature of the exchange hash.)
2084 keystr = hostkey->fmtkey();
2085 fingerprint = hostkey->fingerprint();
2086 verify_ssh_host_key(savedhost, savedport, hostkey->keytype,
2087 keystr, fingerprint);
2088 logevent("Host key fingerprint is:");
2089 logevent(fingerprint);
2094 * Send SSH2_MSG_NEWKEYS.
2096 ssh2_pkt_init(SSH2_MSG_NEWKEYS);
2100 * Create and initialise session keys.
2102 cscipher = cscipher_tobe;
2103 sccipher = sccipher_tobe;
2106 cscomp = cscomp_tobe;
2107 sccomp = sccomp_tobe;
2109 * Set IVs after keys.
2111 ssh2_mkkey(K, exchange_hash, 'C', keyspace); cscipher->setcskey(keyspace);
2112 ssh2_mkkey(K, exchange_hash, 'D', keyspace); cscipher->setsckey(keyspace);
2113 ssh2_mkkey(K, exchange_hash, 'A', keyspace); cscipher->setcsiv(keyspace);
2114 ssh2_mkkey(K, exchange_hash, 'B', keyspace); sccipher->setsciv(keyspace);
2115 ssh2_mkkey(K, exchange_hash, 'E', keyspace); csmac->setcskey(keyspace);
2116 ssh2_mkkey(K, exchange_hash, 'F', keyspace); scmac->setsckey(keyspace);
2119 * Now we're encrypting. Begin returning 1 to the protocol main
2120 * function so that other things can run on top of the
2121 * transport. If we ever see a KEXINIT, we must go back to the
2126 } while (!(ispkt && pktin.type == SSH2_MSG_KEXINIT));
2127 goto begin_key_exchange;
2133 * Handle the SSH2 userauth and connection layers.
2135 static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
2137 static unsigned long remote_winsize;
2138 static unsigned long remote_maxpkt;
2143 * Request userauth protocol, and await a response to it.
2145 ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);
2146 ssh2_pkt_addstring("ssh-userauth");
2148 crWaitUntilV(ispkt);
2149 if (pktin.type != SSH2_MSG_SERVICE_ACCEPT) {
2150 bombout(("Server refused user authentication protocol"));
2155 * FIXME: currently we support only password authentication.
2156 * (This places us technically in violation of the SSH2 spec.
2157 * We must fix this.)
2161 * Get a username and a password.
2163 static char username[100];
2164 static char password[100];
2168 if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
2169 c_write("login as: ", 10);
2172 crWaitUntilV(!ispkt);
2173 while (inlen--) switch (c = *in++) {
2180 c_write("\b \b", 3);
2186 c_write("\b \b", 3);
2195 if (((c >= ' ' && c <= '~') ||
2196 ((unsigned char)c >= 160)) && pos < 40) {
2197 username[pos++] = c;
2204 username[strcspn(username, "\n\r")] = '\0';
2207 strncpy(username, cfg.username, 99);
2208 username[99] = '\0';
2209 if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
2210 sprintf(stuff, "Using username \"%s\".\r\n", username);
2211 c_write(stuff, strlen(stuff));
2215 if (ssh_get_password) {
2217 sprintf(prompt, "%.90s@%.90s's password: ", username, savedhost);
2218 if (!ssh_get_password(prompt, password, sizeof(password))) {
2220 * get_password failed to get a password (for
2221 * example because one was supplied on the command
2222 * line which has already failed to work).
2225 logevent("No more passwords to try");
2226 ssh_state = SSH_STATE_CLOSED;
2230 c_write("password: ", 10);
2235 crWaitUntilV(!ispkt);
2236 while (inlen--) switch (c = *in++) {
2253 if (((c >= ' ' && c <= '~') ||
2254 ((unsigned char)c >= 160)) && pos < 40)
2255 password[pos++] = c;
2262 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
2263 ssh2_pkt_addstring(username);
2264 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2265 ssh2_pkt_addstring("password");
2266 ssh2_pkt_addbool(FALSE);
2267 ssh2_pkt_addstring(password);
2270 crWaitUntilV(ispkt);
2271 if (pktin.type != SSH2_MSG_USERAUTH_SUCCESS) {
2272 c_write("Access denied\r\n", 15);
2273 logevent("Authentication refused");
2279 * Now we're authenticated for the connection protocol. The
2280 * connection protocol will automatically have started at this
2281 * point; there's no need to send SERVICE_REQUEST.
2285 * So now create a channel with a session in it.
2287 mainchan = malloc(sizeof(struct ssh_channel));
2288 mainchan->localid = 100; /* as good as any */
2289 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
2290 ssh2_pkt_addstring("session");
2291 ssh2_pkt_adduint32(mainchan->localid);
2292 ssh2_pkt_adduint32(0x8000UL); /* our window size */
2293 ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
2295 crWaitUntilV(ispkt);
2296 if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {
2297 bombout(("Server refused to open a session"));
2299 /* FIXME: error data comes back in FAILURE packet */
2301 if (ssh2_pkt_getuint32() != mainchan->localid) {
2302 bombout(("Server's channel confirmation cited wrong channel"));
2305 mainchan->remoteid = ssh2_pkt_getuint32();
2306 mainchan->u.v2.remwindow = ssh2_pkt_getuint32();
2307 mainchan->u.v2.remmaxpkt = ssh2_pkt_getuint32();
2308 mainchan->u.v2.outbuffer = NULL;
2309 mainchan->u.v2.outbuflen = mainchan->u.v2.outbufsize = 0;
2310 logevent("Opened channel for session");
2313 * Now allocate a pty for the session.
2316 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
2317 ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
2318 ssh2_pkt_addstring("pty-req");
2319 ssh2_pkt_addbool(1); /* want reply */
2320 ssh2_pkt_addstring(cfg.termtype);
2321 ssh2_pkt_adduint32(cols);
2322 ssh2_pkt_adduint32(rows);
2323 ssh2_pkt_adduint32(0); /* pixel width */
2324 ssh2_pkt_adduint32(0); /* pixel height */
2325 ssh2_pkt_addstring_start();
2326 ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */
2328 ssh_state = SSH_STATE_INTERMED;
2331 crWaitUntilV(ispkt);
2332 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
2333 /* FIXME: be able to handle other channels here */
2334 if (ssh2_pkt_getuint32() != mainchan->localid)
2335 continue; /* wrong channel */
2336 mainchan->u.v2.remwindow += ssh2_pkt_getuint32();
2338 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2340 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
2341 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
2342 bombout(("Server got confused by pty request"));
2345 c_write("Server refused to allocate pty\r\n", 32);
2347 logevent("Allocated pty");
2352 * Start a shell or a remote command.
2354 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
2355 ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
2356 if (*cfg.remote_cmd) {
2357 ssh2_pkt_addstring("exec");
2358 ssh2_pkt_addbool(1); /* want reply */
2359 ssh2_pkt_addstring(cfg.remote_cmd);
2361 ssh2_pkt_addstring("shell");
2362 ssh2_pkt_addbool(1); /* want reply */
2366 crWaitUntilV(ispkt);
2367 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
2368 /* FIXME: be able to handle other channels here */
2369 if (ssh2_pkt_getuint32() != mainchan->localid)
2370 continue; /* wrong channel */
2371 mainchan->u.v2.remwindow += ssh2_pkt_getuint32();
2373 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2374 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
2375 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
2376 bombout(("Server got confused by shell/command request"));
2379 bombout(("Server refused to start a shell/command"));
2382 logevent("Started a shell/command");
2385 ssh_state = SSH_STATE_SESSION;
2395 static int try_send;
2399 if (pktin.type == SSH2_MSG_CHANNEL_DATA ||
2400 pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) {
2403 /* FIXME: be able to handle other channels here */
2404 if (ssh2_pkt_getuint32() != mainchan->localid)
2405 continue; /* wrong channel */
2406 if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA &&
2407 ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR)
2408 continue; /* extended but not stderr */
2409 ssh2_pkt_getstring(&data, &length);
2411 from_backend(pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA,
2414 * Enlarge the window again at the remote side,
2415 * just in case it ever runs down and they fail
2416 * to send us any more data.
2418 ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2419 ssh2_pkt_adduint32(mainchan->remoteid);
2420 ssh2_pkt_adduint32(length);
2423 } else if (pktin.type == SSH2_MSG_DISCONNECT) {
2424 ssh_state = SSH_STATE_CLOSED;
2425 logevent("Received disconnect message");
2427 } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) {
2428 continue; /* exit status et al; ignore (FIXME?) */
2429 } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) {
2430 continue; /* remote sends EOF; ignore */
2431 } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) {
2432 /* FIXME: be able to handle other channels here */
2433 if (ssh2_pkt_getuint32() != mainchan->localid)
2434 continue; /* wrong channel */
2435 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
2436 ssh2_pkt_adduint32(mainchan->remoteid);
2438 /* FIXME: mark the channel as closed */
2439 if (1 /* FIXME: "all channels are closed" */) {
2440 logevent("All channels closed. Disconnecting");
2441 ssh2_pkt_init(SSH2_MSG_DISCONNECT);
2442 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION);
2443 ssh2_pkt_addstring("All open channels closed");
2444 ssh2_pkt_addstring("en"); /* language tag */
2446 ssh_state = SSH_STATE_CLOSED;
2449 continue; /* remote sends close; ignore (FIXME) */
2450 } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
2451 /* FIXME: be able to handle other channels here */
2452 if (ssh2_pkt_getuint32() != mainchan->localid)
2453 continue; /* wrong channel */
2454 mainchan->u.v2.remwindow += ssh2_pkt_getuint32();
2457 bombout(("Strange packet received: type %d", pktin.type));
2462 * We have spare data. Add it to the channel buffer.
2464 if (mainchan->u.v2.outbufsize <
2465 mainchan->u.v2.outbuflen + inlen) {
2466 mainchan->u.v2.outbufsize =
2467 mainchan->u.v2.outbuflen + inlen + 1024;
2468 mainchan->u.v2.outbuffer = srealloc(mainchan->u.v2.outbuffer,
2469 mainchan->u.v2.outbufsize);
2471 memcpy(mainchan->u.v2.outbuffer + mainchan->u.v2.outbuflen,
2473 mainchan->u.v2.outbuflen += inlen;
2478 * Try to send data on the channel if we can. (FIXME:
2479 * on _all_ channels.)
2481 while (mainchan->u.v2.remwindow > 0 &&
2482 mainchan->u.v2.outbuflen > 0) {
2483 unsigned len = mainchan->u.v2.remwindow;
2484 if (len > mainchan->u.v2.outbuflen)
2485 len = mainchan->u.v2.outbuflen;
2486 if (len > mainchan->u.v2.remmaxpkt)
2487 len = mainchan->u.v2.remmaxpkt;
2488 ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA);
2489 ssh2_pkt_adduint32(mainchan->remoteid);
2490 ssh2_pkt_addstring_start();
2491 ssh2_pkt_addstring_data(mainchan->u.v2.outbuffer, len);
2493 mainchan->u.v2.outbuflen -= len;
2494 memmove(mainchan->u.v2.outbuffer, mainchan->u.v2.outbuffer+len,
2495 mainchan->u.v2.outbuflen);
2496 mainchan->u.v2.remwindow -= len;
2505 * Handle the top-level SSH2 protocol.
2507 static void ssh2_protocol(unsigned char *in, int inlen, int ispkt)
2509 if (do_ssh2_transport(in, inlen, ispkt) == 0)
2511 do_ssh2_authconn(in, inlen, ispkt);
2515 * Called to set up the connection.
2517 * Returns an error message, or NULL on success.
2519 static char *ssh_init (char *host, int port, char **realhost) {
2523 if(crypto_startup() == 0)
2524 return "Microsoft high encryption pack not installed!";
2529 p = connect_to_host(host, port, realhost);
2537 * Called to send data down the Telnet connection.
2539 static void ssh_send (char *buf, int len) {
2543 ssh_protocol(buf, len, 0);
2547 * Called to set the size of the window from SSH's POV.
2549 static void ssh_size(void) {
2550 switch (ssh_state) {
2551 case SSH_STATE_BEFORE_SIZE:
2552 case SSH_STATE_CLOSED:
2553 break; /* do nothing */
2554 case SSH_STATE_INTERMED:
2555 size_needed = TRUE; /* buffer for later */
2557 case SSH_STATE_SESSION:
2559 if (ssh_version == 1) {
2560 send_packet(SSH1_CMSG_WINDOW_SIZE,
2561 PKT_INT, rows, PKT_INT, cols,
2562 PKT_INT, 0, PKT_INT, 0, PKT_END);
2564 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
2565 ssh2_pkt_adduint32(mainchan->remoteid);
2566 ssh2_pkt_addstring("window-change");
2567 ssh2_pkt_addbool(0);
2568 ssh2_pkt_adduint32(cols);
2569 ssh2_pkt_adduint32(rows);
2570 ssh2_pkt_adduint32(0);
2571 ssh2_pkt_adduint32(0);
2579 * Send Telnet special codes. TS_EOF is useful for `plink', so you
2580 * can send an EOF and collect resulting output (e.g. `plink
2583 static void ssh_special (Telnet_Special code) {
2584 if (code == TS_EOF) {
2585 if (ssh_version == 1) {
2586 send_packet(SSH1_CMSG_EOF, PKT_END);
2588 ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF);
2589 ssh2_pkt_adduint32(mainchan->remoteid);
2592 logevent("Sent EOF message");
2593 } else if (code == TS_PING) {
2594 if (ssh_version == 1) {
2595 send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);
2597 ssh2_pkt_init(SSH2_MSG_IGNORE);
2598 ssh2_pkt_addstring_start();
2606 static Socket ssh_socket(void) { return s; }
2608 static int ssh_sendok(void) { return ssh_send_ok; }
2610 Backend ssh_backend = {