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