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, \
23 (s ? sk_close(s), s = NULL : (void)0), \
24 connection_fatal msg )
26 #define SSH1_MSG_DISCONNECT 1 /* 0x1 */
27 #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
28 #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
29 #define SSH1_CMSG_USER 4 /* 0x4 */
30 #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
31 #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
32 #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
33 #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
34 #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
35 #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
36 #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
37 #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
38 #define SSH1_SMSG_SUCCESS 14 /* 0xe */
39 #define SSH1_SMSG_FAILURE 15 /* 0xf */
40 #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
41 #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
42 #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
43 #define SSH1_CMSG_EOF 19 /* 0x13 */
44 #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
45 #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
46 #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
47 #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
48 #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
49 #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
50 #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
51 #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
52 #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
53 #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
54 #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
55 #define SSH1_MSG_IGNORE 32 /* 0x20 */
56 #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
57 #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
58 #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
59 #define SSH1_MSG_DEBUG 36 /* 0x24 */
60 #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
61 #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
62 #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
63 #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
64 #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
65 #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
66 #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
68 #define SSH1_AUTH_TIS 5 /* 0x5 */
69 #define SSH1_AUTH_CCARD 16 /* 0x10 */
71 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1 /* 0x1 */
72 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2 /* 0x2 */
73 #define SSH_AGENTC_RSA_CHALLENGE 3 /* 0x3 */
74 #define SSH_AGENT_RSA_RESPONSE 4 /* 0x4 */
75 #define SSH_AGENT_FAILURE 5 /* 0x5 */
76 #define SSH_AGENT_SUCCESS 6 /* 0x6 */
77 #define SSH_AGENTC_ADD_RSA_IDENTITY 7 /* 0x7 */
78 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8 /* 0x8 */
80 #define SSH2_MSG_DISCONNECT 1 /* 0x1 */
81 #define SSH2_MSG_IGNORE 2 /* 0x2 */
82 #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
83 #define SSH2_MSG_DEBUG 4 /* 0x4 */
84 #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
85 #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
86 #define SSH2_MSG_KEXINIT 20 /* 0x14 */
87 #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
88 #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
89 #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
90 #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
91 #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
92 #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
93 #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
94 #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
95 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
96 #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
97 #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
98 #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
99 #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
100 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
101 #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
102 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
103 #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
104 #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
105 #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
106 #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
107 #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
108 #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
109 #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
111 #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
112 #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
113 #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
114 #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
115 #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
116 #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
117 #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
118 #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
119 #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
120 #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
121 #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
123 #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
124 #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
125 #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
126 #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
128 #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
130 #define GET_32BIT(cp) \
131 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
132 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
133 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
134 ((unsigned long)(unsigned char)(cp)[3]))
136 #define PUT_32BIT(cp, value) { \
137 (cp)[0] = (unsigned char)((value) >> 24); \
138 (cp)[1] = (unsigned char)((value) >> 16); \
139 (cp)[2] = (unsigned char)((value) >> 8); \
140 (cp)[3] = (unsigned char)(value); }
142 enum { PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
, PKT_BIGNUM
};
144 /* Coroutine mechanics for the sillier bits of the code */
145 #define crBegin1 static int crLine = 0;
146 #define crBegin2 switch(crLine) { case 0:;
147 #define crBegin crBegin1; crBegin2;
148 #define crFinish(z) } crLine = 0; return (z)
149 #define crFinishV } crLine = 0; return
150 #define crReturn(z) \
152 crLine=__LINE__; return (z); case __LINE__:;\
156 crLine=__LINE__; return; case __LINE__:;\
158 #define crStop(z) do{ crLine = 0; return (z); }while(0)
159 #define crStopV do{ crLine = 0; return; }while(0)
160 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
161 #define crWaitUntilV(c) do { crReturnV; } while (!(c))
163 extern const struct ssh_cipher ssh_3des
;
164 extern const struct ssh_cipher ssh_3des_ssh2
;
165 extern const struct ssh_cipher ssh_des
;
166 extern const struct ssh_cipher ssh_blowfish_ssh1
;
167 extern const struct ssh_cipher ssh_blowfish_ssh2
;
169 extern char *x11_init (Socket
*, char *, void *);
170 extern void x11_close (Socket
);
171 extern void x11_send (Socket
, char *, int);
172 extern void x11_invent_auth(char *, int, char *, int);
175 * Ciphers for SSH2. We miss out single-DES because it isn't
176 * supported; also 3DES and Blowfish are both done differently from
177 * SSH1. (3DES uses outer chaining; Blowfish has the opposite
178 * endianness and different-sized keys.)
180 const static struct ssh_cipher
*ciphers
[] = { &ssh_blowfish_ssh2
, &ssh_3des_ssh2
};
182 extern const struct ssh_kex ssh_diffiehellman
;
183 const static struct ssh_kex
*kex_algs
[] = { &ssh_diffiehellman
};
185 extern const struct ssh_signkey ssh_dss
;
186 const static struct ssh_signkey
*hostkey_algs
[] = { &ssh_dss
};
188 extern const struct ssh_mac ssh_md5
, ssh_sha1
, ssh_sha1_buggy
;
190 static void nullmac_key(unsigned char *key
) { }
191 static void nullmac_generate(unsigned char *blk
, int len
, unsigned long seq
) { }
192 static int nullmac_verify(unsigned char *blk
, int len
, unsigned long seq
) { return 1; }
193 const static struct ssh_mac ssh_mac_none
= {
194 nullmac_key
, nullmac_key
, nullmac_generate
, nullmac_verify
, "none", 0
196 const static struct ssh_mac
*macs
[] = {
197 &ssh_sha1
, &ssh_md5
, &ssh_mac_none
};
198 const static struct ssh_mac
*buggymacs
[] = {
199 &ssh_sha1_buggy
, &ssh_md5
, &ssh_mac_none
};
201 static void ssh_comp_none_init(void) { }
202 static int ssh_comp_none_block(unsigned char *block
, int len
,
203 unsigned char **outblock
, int *outlen
) {
206 const static struct ssh_compress ssh_comp_none
= {
208 ssh_comp_none_init
, ssh_comp_none_block
,
209 ssh_comp_none_init
, ssh_comp_none_block
211 extern const struct ssh_compress ssh_zlib
;
212 const static struct ssh_compress
*compressions
[] = {
213 &ssh_zlib
, &ssh_comp_none
};
215 enum { /* channel types */
222 * 2-3-4 tree storing channels.
225 unsigned remoteid
, localid
;
228 struct ssh2_data_channel
{
229 unsigned char *outbuffer
;
230 unsigned outbuflen
, outbufsize
;
231 unsigned remwindow
, remmaxpkt
;
234 struct ssh_agent_channel
{
235 unsigned char *message
;
236 unsigned char msglen
[4];
237 int lensofar
, totallen
;
239 struct ssh_x11_channel
{
254 static SHA_State exhash
, exhashbase
;
256 static Socket s
= NULL
;
258 static unsigned char session_key
[32];
259 static int ssh1_compressing
;
260 static int ssh_agentfwd_enabled
;
261 static int ssh_X11_fwd_enabled
;
262 static const struct ssh_cipher
*cipher
= NULL
;
263 static const struct ssh_cipher
*cscipher
= NULL
;
264 static const struct ssh_cipher
*sccipher
= NULL
;
265 static const struct ssh_mac
*csmac
= NULL
;
266 static const struct ssh_mac
*scmac
= NULL
;
267 static const struct ssh_compress
*cscomp
= NULL
;
268 static const struct ssh_compress
*sccomp
= NULL
;
269 static const struct ssh_kex
*kex
= NULL
;
270 static const struct ssh_signkey
*hostkey
= NULL
;
271 int (*ssh_get_password
)(const char *prompt
, char *str
, int maxlen
) = NULL
;
273 static char *savedhost
;
274 static int savedport
;
275 static int ssh_send_ok
;
276 static int ssh_echoing
, ssh_editing
;
278 static tree234
*ssh_channels
; /* indexed by local id */
279 static struct ssh_channel
*mainchan
; /* primary session channel */
283 SSH_STATE_BEFORE_SIZE
,
287 } ssh_state
= SSH_STATE_PREPACKET
;
289 static int size_needed
= FALSE
, eof_needed
= FALSE
;
291 static struct Packet pktin
= { 0, 0, NULL
, NULL
, 0 };
292 static struct Packet pktout
= { 0, 0, NULL
, NULL
, 0 };
294 static int ssh_version
;
295 static void (*ssh_protocol
)(unsigned char *in
, int inlen
, int ispkt
);
296 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
);
297 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
);
298 static void ssh_size(void);
299 static void ssh_special (Telnet_Special
);
300 static void ssh2_try_send(struct ssh_channel
*c
);
301 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
, int len
);
303 static int (*s_rdpkt
)(unsigned char **data
, int *datalen
);
305 static struct rdpkt1_state_tag
{
306 long len
, pad
, biglen
, to_read
;
307 unsigned long realcrc
, gotcrc
;
313 static struct rdpkt2_state_tag
{
314 long len
, pad
, payload
, packetlen
, maclen
;
317 unsigned long incoming_sequence
;
320 static int ssh_channelcmp(void *av
, void *bv
) {
321 struct ssh_channel
*a
= (struct ssh_channel
*)av
;
322 struct ssh_channel
*b
= (struct ssh_channel
*)bv
;
323 if (a
->localid
< b
->localid
) return -1;
324 if (a
->localid
> b
->localid
) return +1;
327 static int ssh_channelfind(void *av
, void *bv
) {
328 unsigned *a
= (unsigned *)av
;
329 struct ssh_channel
*b
= (struct ssh_channel
*)bv
;
330 if (*a
< b
->localid
) return -1;
331 if (*a
> b
->localid
) return +1;
335 static void c_write (char *buf
, int len
) {
336 if ((flags
& FLAG_STDERR
)) {
338 for (i
= 0; i
< len
; i
++)
340 fputc(buf
[i
], stderr
);
343 from_backend(1, buf
, len
);
347 * Collect incoming data in the incoming packet buffer.
348 * Decipher and verify the packet when it is completely read.
349 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
350 * Update the *data and *datalen variables.
351 * Return the additional nr of bytes needed, or 0 when
352 * a complete packet is available.
354 static int ssh1_rdpkt(unsigned char **data
, int *datalen
)
356 struct rdpkt1_state_tag
*st
= &rdpkt1_state
;
365 for (st
->i
= st
->len
= 0; st
->i
< 4; st
->i
++) {
366 while ((*datalen
) == 0)
368 st
->len
= (st
->len
<< 8) + **data
;
369 (*data
)++, (*datalen
)--;
373 if (st
->len
== 0x52656d6f) { /* "Remo"te server has closed ... */
374 st
->len
= 0x300; /* big enough to carry to end */
378 st
->pad
= 8 - (st
->len
% 8);
379 st
->biglen
= st
->len
+ st
->pad
;
380 pktin
.length
= st
->len
- 5;
382 if (pktin
.maxlen
< st
->biglen
) {
383 pktin
.maxlen
= st
->biglen
;
384 pktin
.data
= (pktin
.data
== NULL ?
smalloc(st
->biglen
+APIEXTRA
) :
385 srealloc(pktin
.data
, st
->biglen
+APIEXTRA
));
387 fatalbox("Out of memory");
390 st
->to_read
= st
->biglen
;
392 while (st
->to_read
> 0) {
393 st
->chunk
= st
->to_read
;
394 while ((*datalen
) == 0)
395 crReturn(st
->to_read
);
396 if (st
->chunk
> (*datalen
))
397 st
->chunk
= (*datalen
);
398 memcpy(st
->p
, *data
, st
->chunk
);
400 *datalen
-= st
->chunk
;
402 st
->to_read
-= st
->chunk
;
406 cipher
->decrypt(pktin
.data
, st
->biglen
);
408 debug(("Got packet len=%d pad=%d\r\n", st
->len
, st
->pad
));
409 for (st
->i
= 0; st
->i
< st
->biglen
; st
->i
++)
410 debug((" %02x", (unsigned char)pktin
.data
[st
->i
]));
414 st
->realcrc
= crc32(pktin
.data
, st
->biglen
-4);
415 st
->gotcrc
= GET_32BIT(pktin
.data
+st
->biglen
-4);
416 if (st
->gotcrc
!= st
->realcrc
) {
417 bombout(("Incorrect CRC received on packet"));
421 pktin
.body
= pktin
.data
+ st
->pad
+ 1;
423 if (ssh1_compressing
) {
424 unsigned char *decompblk
;
428 debug(("Packet payload pre-decompression:\n"));
429 for (i
= -1; i
< pktin
.length
; i
++)
430 debug((" %02x", (unsigned char)pktin
.body
[i
]));
433 zlib_decompress_block(pktin
.body
-1, pktin
.length
+1,
434 &decompblk
, &decomplen
);
436 if (pktin
.maxlen
< st
->pad
+ decomplen
) {
437 pktin
.maxlen
= st
->pad
+ decomplen
;
438 pktin
.data
= srealloc(pktin
.data
, pktin
.maxlen
+APIEXTRA
);
439 pktin
.body
= pktin
.data
+ st
->pad
+ 1;
441 fatalbox("Out of memory");
444 memcpy(pktin
.body
-1, decompblk
, decomplen
);
446 pktin
.length
= decomplen
-1;
448 debug(("Packet payload post-decompression:\n"));
449 for (i
= -1; i
< pktin
.length
; i
++)
450 debug((" %02x", (unsigned char)pktin
.body
[i
]));
455 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
456 pktin
.type
== SSH1_SMSG_STDERR_DATA
||
457 pktin
.type
== SSH1_MSG_DEBUG
||
458 pktin
.type
== SSH1_SMSG_AUTH_TIS_CHALLENGE
||
459 pktin
.type
== SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
460 long strlen
= GET_32BIT(pktin
.body
);
461 if (strlen
+ 4 != pktin
.length
) {
462 bombout(("Received data packet with bogus string length"));
467 pktin
.type
= pktin
.body
[-1];
469 if (pktin
.type
== SSH1_MSG_DEBUG
) {
470 /* log debug message */
472 int strlen
= GET_32BIT(pktin
.body
);
473 strcpy(buf
, "Remote: ");
474 if (strlen
> 70) strlen
= 70;
475 memcpy(buf
+8, pktin
.body
+4, strlen
);
476 buf
[8+strlen
] = '\0';
479 } else if (pktin
.type
== SSH1_MSG_IGNORE
) {
487 static int ssh2_rdpkt(unsigned char **data
, int *datalen
)
489 struct rdpkt2_state_tag
*st
= &rdpkt2_state
;
497 st
->cipherblk
= sccipher
->blksize
;
500 if (st
->cipherblk
< 8)
503 if (pktin
.maxlen
< st
->cipherblk
) {
504 pktin
.maxlen
= st
->cipherblk
;
505 pktin
.data
= (pktin
.data
== NULL ?
smalloc(st
->cipherblk
+APIEXTRA
) :
506 srealloc(pktin
.data
, st
->cipherblk
+APIEXTRA
));
508 fatalbox("Out of memory");
512 * Acquire and decrypt the first block of the packet. This will
513 * contain the length and padding details.
515 for (st
->i
= st
->len
= 0; st
->i
< st
->cipherblk
; st
->i
++) {
516 while ((*datalen
) == 0)
517 crReturn(st
->cipherblk
-st
->i
);
518 pktin
.data
[st
->i
] = *(*data
)++;
522 if (!memcmp(pktin
.data
, "Remo", 4)) {/* "Remo"te server has closed ... */
527 sccipher
->decrypt(pktin
.data
, st
->cipherblk
);
530 * Now get the length and padding figures.
532 st
->len
= GET_32BIT(pktin
.data
);
533 st
->pad
= pktin
.data
[4];
536 * This enables us to deduce the payload length.
538 st
->payload
= st
->len
- st
->pad
- 1;
540 pktin
.length
= st
->payload
+ 5;
543 * So now we can work out the total packet length.
545 st
->packetlen
= st
->len
+ 4;
546 st
->maclen
= scmac ? scmac
->len
: 0;
549 * Adjust memory allocation if packet is too big.
551 if (pktin
.maxlen
< st
->packetlen
+st
->maclen
) {
552 pktin
.maxlen
= st
->packetlen
+st
->maclen
;
553 pktin
.data
= (pktin
.data
== NULL ?
smalloc(pktin
.maxlen
+APIEXTRA
) :
554 srealloc(pktin
.data
, pktin
.maxlen
+APIEXTRA
));
556 fatalbox("Out of memory");
560 * Read and decrypt the remainder of the packet.
562 for (st
->i
= st
->cipherblk
; st
->i
< st
->packetlen
+ st
->maclen
; st
->i
++) {
563 while ((*datalen
) == 0)
564 crReturn(st
->packetlen
+ st
->maclen
- st
->i
);
565 pktin
.data
[st
->i
] = *(*data
)++;
568 /* Decrypt everything _except_ the MAC. */
570 sccipher
->decrypt(pktin
.data
+ st
->cipherblk
,
571 st
->packetlen
- st
->cipherblk
);
574 debug(("Got packet len=%d pad=%d\r\n", st
->len
, st
->pad
));
575 for (st
->i
= 0; st
->i
< st
->packetlen
; st
->i
++)
576 debug((" %02x", (unsigned char)pktin
.data
[st
->i
]));
583 if (scmac
&& !scmac
->verify(pktin
.data
, st
->len
+4, st
->incoming_sequence
)) {
584 bombout(("Incorrect MAC received on packet"));
587 st
->incoming_sequence
++; /* whether or not we MACed */
590 * Decompress packet payload.
593 unsigned char *newpayload
;
595 if (sccomp
&& sccomp
->decompress(pktin
.data
+5, pktin
.length
-5,
596 &newpayload
, &newlen
)) {
597 if (pktin
.maxlen
< newlen
+5) {
598 pktin
.maxlen
= newlen
+5;
599 pktin
.data
= (pktin
.data
== NULL ?
smalloc(pktin
.maxlen
+APIEXTRA
) :
600 srealloc(pktin
.data
, pktin
.maxlen
+APIEXTRA
));
602 fatalbox("Out of memory");
604 pktin
.length
= 5 + newlen
;
605 memcpy(pktin
.data
+5, newpayload
, newlen
);
607 debug(("Post-decompression payload:\r\n"));
608 for (st
->i
= 0; st
->i
< newlen
; st
->i
++)
609 debug((" %02x", (unsigned char)pktin
.data
[5+st
->i
]));
618 pktin
.type
= pktin
.data
[5];
620 if (pktin
.type
== SSH2_MSG_IGNORE
|| pktin
.type
== SSH2_MSG_DEBUG
)
621 goto next_packet
; /* FIXME: print DEBUG message */
626 static void ssh1_pktout_size(int len
) {
629 len
+= 5; /* type and CRC */
633 pktout
.length
= len
-5;
634 if (pktout
.maxlen
< biglen
) {
635 pktout
.maxlen
= biglen
;
637 /* Allocate enough buffer space for extra block
638 * for MS CryptEncrypt() */
639 pktout
.data
= (pktout
.data
== NULL ?
smalloc(biglen
+12) :
640 srealloc(pktout
.data
, biglen
+12));
642 pktout
.data
= (pktout
.data
== NULL ?
smalloc(biglen
+4) :
643 srealloc(pktout
.data
, biglen
+4));
646 fatalbox("Out of memory");
648 pktout
.body
= pktout
.data
+4+pad
+1;
651 static void s_wrpkt_start(int type
, int len
) {
652 ssh1_pktout_size(len
);
656 static void s_wrpkt(void) {
657 int pad
, len
, biglen
, i
;
660 pktout
.body
[-1] = pktout
.type
;
662 if (ssh1_compressing
) {
663 unsigned char *compblk
;
666 debug(("Packet payload pre-compression:\n"));
667 for (i
= -1; i
< pktout
.length
; i
++)
668 debug((" %02x", (unsigned char)pktout
.body
[i
]));
671 zlib_compress_block(pktout
.body
-1, pktout
.length
+1,
673 ssh1_pktout_size(complen
-1);
674 memcpy(pktout
.body
-1, compblk
, complen
);
677 debug(("Packet payload post-compression:\n"));
678 for (i
= -1; i
< pktout
.length
; i
++)
679 debug((" %02x", (unsigned char)pktout
.body
[i
]));
684 len
= pktout
.length
+ 5; /* type and CRC */
688 for (i
=0; i
<pad
; i
++)
689 pktout
.data
[i
+4] = random_byte();
690 crc
= crc32(pktout
.data
+4, biglen
-4);
691 PUT_32BIT(pktout
.data
+biglen
, crc
);
692 PUT_32BIT(pktout
.data
, len
);
695 debug(("Sending packet len=%d\r\n", biglen
+4));
696 for (i
= 0; i
< biglen
+4; i
++)
697 debug((" %02x", (unsigned char)pktout
.data
[i
]));
701 cipher
->encrypt(pktout
.data
+4, biglen
);
703 sk_write(s
, pktout
.data
, biglen
+4);
707 * Construct a packet with the specified contents and
708 * send it to the server.
710 static void send_packet(int pkttype
, ...)
713 unsigned char *p
, *argp
, argchar
;
714 unsigned long argint
;
715 int pktlen
, argtype
, arglen
;
719 va_start(args
, pkttype
);
720 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
723 (void) va_arg(args
, int);
727 (void) va_arg(args
, char);
731 (void) va_arg(args
, unsigned char *);
732 arglen
= va_arg(args
, int);
736 argp
= va_arg(args
, unsigned char *);
737 arglen
= strlen(argp
);
738 pktlen
+= 4 + arglen
;
741 bn
= va_arg(args
, Bignum
);
742 pktlen
+= ssh1_bignum_length(bn
);
750 s_wrpkt_start(pkttype
, pktlen
);
753 va_start(args
, pkttype
);
754 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
757 argint
= va_arg(args
, int);
758 PUT_32BIT(p
, argint
);
762 argchar
= va_arg(args
, unsigned char);
767 argp
= va_arg(args
, unsigned char *);
768 arglen
= va_arg(args
, int);
769 memcpy(p
, argp
, arglen
);
773 argp
= va_arg(args
, unsigned char *);
774 arglen
= strlen(argp
);
775 PUT_32BIT(p
, arglen
);
776 memcpy(p
+ 4, argp
, arglen
);
780 bn
= va_arg(args
, Bignum
);
781 p
+= ssh1_write_bignum(p
, bn
);
790 static int ssh_versioncmp(char *a
, char *b
) {
792 unsigned long av
, bv
;
794 av
= strtoul(a
, &ae
, 10);
795 bv
= strtoul(b
, &be
, 10);
796 if (av
!= bv
) return (av
< bv ?
-1 : +1);
797 if (*ae
== '.') ae
++;
798 if (*be
== '.') be
++;
799 av
= strtoul(ae
, &ae
, 10);
800 bv
= strtoul(be
, &be
, 10);
801 if (av
!= bv
) return (av
< bv ?
-1 : +1);
807 * Utility routine for putting an SSH-protocol `string' into a SHA
811 static void sha_string(SHA_State
*s
, void *str
, int len
) {
812 unsigned char lenblk
[4];
813 PUT_32BIT(lenblk
, len
);
814 SHA_Bytes(s
, lenblk
, 4);
815 SHA_Bytes(s
, str
, len
);
819 * SSH2 packet construction functions.
821 static void ssh2_pkt_ensure(int length
) {
822 if (pktout
.maxlen
< length
) {
823 pktout
.maxlen
= length
+ 256;
824 pktout
.data
= (pktout
.data
== NULL ?
smalloc(pktout
.maxlen
+APIEXTRA
) :
825 srealloc(pktout
.data
, pktout
.maxlen
+APIEXTRA
));
827 fatalbox("Out of memory");
830 static void ssh2_pkt_adddata(void *data
, int len
) {
831 pktout
.length
+= len
;
832 ssh2_pkt_ensure(pktout
.length
);
833 memcpy(pktout
.data
+pktout
.length
-len
, data
, len
);
835 static void ssh2_pkt_addbyte(unsigned char byte
) {
836 ssh2_pkt_adddata(&byte
, 1);
838 static void ssh2_pkt_init(int pkt_type
) {
840 ssh2_pkt_addbyte((unsigned char)pkt_type
);
842 static void ssh2_pkt_addbool(unsigned char value
) {
843 ssh2_pkt_adddata(&value
, 1);
845 static void ssh2_pkt_adduint32(unsigned long value
) {
848 ssh2_pkt_adddata(x
, 4);
850 static void ssh2_pkt_addstring_start(void) {
851 ssh2_pkt_adduint32(0);
852 pktout
.savedpos
= pktout
.length
;
854 static void ssh2_pkt_addstring_str(char *data
) {
855 ssh2_pkt_adddata(data
, strlen(data
));
856 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
857 pktout
.length
- pktout
.savedpos
);
859 static void ssh2_pkt_addstring_data(char *data
, int len
) {
860 ssh2_pkt_adddata(data
, len
);
861 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
862 pktout
.length
- pktout
.savedpos
);
864 static void ssh2_pkt_addstring(char *data
) {
865 ssh2_pkt_addstring_start();
866 ssh2_pkt_addstring_str(data
);
868 static char *ssh2_mpint_fmt(Bignum b
, int *len
) {
871 p
= smalloc(n
* 2 + 1);
873 fatalbox("out of memory");
875 for (i
= 0; i
< n
; i
++) {
876 p
[i
*2+1] = (b
[n
-i
] >> 8) & 0xFF;
877 p
[i
*2+2] = (b
[n
-i
] ) & 0xFF;
880 while (p
[i
] == 0 && (p
[i
+1] & 0x80) == 0)
882 memmove(p
, p
+i
, n
*2+1-i
);
886 static void ssh2_pkt_addmp(Bignum b
) {
889 p
= ssh2_mpint_fmt(b
, &len
);
890 ssh2_pkt_addstring_start();
891 ssh2_pkt_addstring_data(p
, len
);
894 static void ssh2_pkt_send(void) {
895 int cipherblk
, maclen
, padding
, i
;
896 static unsigned long outgoing_sequence
= 0;
899 * Compress packet payload.
902 debug(("Pre-compression payload:\r\n"));
903 for (i
= 5; i
< pktout
.length
; i
++)
904 debug((" %02x", (unsigned char)pktout
.data
[i
]));
908 unsigned char *newpayload
;
910 if (cscomp
&& cscomp
->compress(pktout
.data
+5, pktout
.length
-5,
911 &newpayload
, &newlen
)) {
913 ssh2_pkt_adddata(newpayload
, newlen
);
919 * Add padding. At least four bytes, and must also bring total
920 * length (minus MAC) up to a multiple of the block size.
922 cipherblk
= cipher ? cipher
->blksize
: 8; /* block size */
923 cipherblk
= cipherblk
< 8 ?
8 : cipherblk
; /* or 8 if blksize < 8 */
925 padding
+= (cipherblk
- (pktout
.length
+ padding
) % cipherblk
) % cipherblk
;
926 maclen
= csmac ? csmac
->len
: 0;
927 ssh2_pkt_ensure(pktout
.length
+ padding
+ maclen
);
928 pktout
.data
[4] = padding
;
929 for (i
= 0; i
< padding
; i
++)
930 pktout
.data
[pktout
.length
+ i
] = random_byte();
931 PUT_32BIT(pktout
.data
, pktout
.length
+ padding
- 4);
933 csmac
->generate(pktout
.data
, pktout
.length
+ padding
,
935 outgoing_sequence
++; /* whether or not we MACed */
938 debug(("Sending packet len=%d\r\n", pktout
.length
+padding
));
939 for (i
= 0; i
< pktout
.length
+padding
; i
++)
940 debug((" %02x", (unsigned char)pktout
.data
[i
]));
945 cscipher
->encrypt(pktout
.data
, pktout
.length
+ padding
);
947 sk_write(s
, pktout
.data
, pktout
.length
+ padding
+ maclen
);
951 void bndebug(char *string
, Bignum b
) {
954 p
= ssh2_mpint_fmt(b
, &len
);
955 debug(("%s", string
));
956 for (i
= 0; i
< len
; i
++)
957 debug((" %02x", p
[i
]));
963 static void sha_mpint(SHA_State
*s
, Bignum b
) {
966 p
= ssh2_mpint_fmt(b
, &len
);
967 sha_string(s
, p
, len
);
972 * SSH2 packet decode functions.
974 static unsigned long ssh2_pkt_getuint32(void) {
976 if (pktin
.length
- pktin
.savedpos
< 4)
977 return 0; /* arrgh, no way to decline (FIXME?) */
978 value
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
982 static void ssh2_pkt_getstring(char **p
, int *length
) {
984 if (pktin
.length
- pktin
.savedpos
< 4)
986 *length
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
988 if (pktin
.length
- pktin
.savedpos
< *length
)
990 *p
= pktin
.data
+pktin
.savedpos
;
991 pktin
.savedpos
+= *length
;
993 static Bignum
ssh2_pkt_getmp(void) {
998 ssh2_pkt_getstring(&p
, &length
);
1002 bombout(("internal error: Can't handle negative mpints"));
1005 b
= newbn((length
+1)/2);
1006 for (i
= 0; i
< length
; i
++) {
1009 b
[j
/2+1] |= ((unsigned char)p
[i
]) << 8;
1011 b
[j
/2+1] |= ((unsigned char)p
[i
]);
1013 while (b
[0] > 1 && b
[b
[0]] == 0) b
[0]--;
1017 static int do_ssh_init(unsigned char c
) {
1019 static char version
[10];
1020 static char vstring
[80];
1021 static char vlog
[sizeof(vstring
)+20];
1026 /* Search for the string "SSH-" in the input. */
1029 static const int transS
[] = { 1, 2, 2, 1 };
1030 static const int transH
[] = { 0, 0, 3, 0 };
1031 static const int transminus
[] = { 0, 0, 0, -1 };
1032 if (c
== 'S') i
= transS
[i
];
1033 else if (c
== 'H') i
= transH
[i
];
1034 else if (c
== '-') i
= transminus
[i
];
1038 crReturn(1); /* get another character */
1041 strcpy(vstring
, "SSH-");
1045 crReturn(1); /* get another char */
1046 if (vsp
< vstring
+sizeof(vstring
)-1)
1052 } else if (i
< sizeof(version
)-1)
1059 ssh_agentfwd_enabled
= FALSE
;
1060 rdpkt2_state
.incoming_sequence
= 0;
1063 sprintf(vlog
, "Server version: %s", vstring
);
1064 vlog
[strcspn(vlog
, "\r\n")] = '\0';
1068 * Server version "1.99" means we can choose whether we use v1
1069 * or v2 protocol. Choice is based on cfg.sshprot.
1071 if (ssh_versioncmp(version
, cfg
.sshprot
== 1 ?
"2.0" : "1.99") >= 0) {
1073 * This is a v2 server. Begin v2 protocol.
1075 char *verstring
= "SSH-2.0-PuTTY";
1076 SHA_Init(&exhashbase
);
1078 * Hash our version string and their version string.
1080 sha_string(&exhashbase
, verstring
, strlen(verstring
));
1081 sha_string(&exhashbase
, vstring
, strcspn(vstring
, "\r\n"));
1082 sprintf(vstring
, "%s\n", verstring
);
1083 sprintf(vlog
, "We claim version: %s", verstring
);
1085 logevent("Using SSH protocol version 2");
1086 sk_write(s
, vstring
, strlen(vstring
));
1087 ssh_protocol
= ssh2_protocol
;
1089 s_rdpkt
= ssh2_rdpkt
;
1092 * This is a v1 server. Begin v1 protocol.
1094 sprintf(vstring
, "SSH-%s-PuTTY\n",
1095 (ssh_versioncmp(version
, "1.5") <= 0 ? version
: "1.5"));
1096 sprintf(vlog
, "We claim version: %s", vstring
);
1097 vlog
[strcspn(vlog
, "\r\n")] = '\0';
1099 logevent("Using SSH protocol version 1");
1100 sk_write(s
, vstring
, strlen(vstring
));
1101 ssh_protocol
= ssh1_protocol
;
1103 s_rdpkt
= ssh1_rdpkt
;
1105 ssh_state
= SSH_STATE_BEFORE_SIZE
;
1110 static void ssh_gotdata(unsigned char *data
, int datalen
)
1115 * To begin with, feed the characters one by one to the
1116 * protocol initialisation / selection function do_ssh_init().
1117 * When that returns 0, we're done with the initial greeting
1118 * exchange and can move on to packet discipline.
1123 crReturnV
; /* more data please */
1124 ret
= do_ssh_init(*data
);
1131 * We emerge from that loop when the initial negotiation is
1132 * over and we have selected an s_rdpkt function. Now pass
1133 * everything to s_rdpkt, and then pass the resulting packets
1134 * to the proper protocol handler.
1139 while (datalen
> 0) {
1140 if ( s_rdpkt(&data
, &datalen
) == 0 ) {
1141 ssh_protocol(NULL
, 0, 1);
1142 if (ssh_state
== SSH_STATE_CLOSED
) {
1152 static int ssh_receive(Socket skt
, int urgent
, char *data
, int len
) {
1154 /* A socket error has occurred. */
1155 ssh_state
= SSH_STATE_CLOSED
;
1158 connection_fatal(data
);
1161 /* Connection has closed. */
1162 ssh_state
= SSH_STATE_CLOSED
;
1167 ssh_gotdata (data
, len
);
1168 if (ssh_state
== SSH_STATE_CLOSED
) {
1179 * Connect to specified host and port.
1180 * Returns an error message, or NULL on success.
1181 * Also places the canonical host name into `realhost'.
1183 static char *connect_to_host(char *host
, int port
, char **realhost
)
1192 savedhost
= smalloc(1+strlen(host
));
1194 fatalbox("Out of memory");
1195 strcpy(savedhost
, host
);
1198 port
= 22; /* default ssh port */
1211 addr
= sk_namelookup(host
, realhost
);
1212 if ( (err
= sk_addr_error(addr
)) )
1222 s
= sk_new(addr
, port
, 0, 1, ssh_receive
);
1223 if ( (err
= sk_socket_error(s
)) )
1227 sk_write(s
, "connect ", 8);
1228 sk_write(s
, FWhost
, strlen(FWhost
));
1231 sprintf(buf
, " %d\n", FWport
);
1232 sk_write(s
, buf
, strlen(buf
));
1240 * Handle the key exchange and user authentication phases.
1242 static int do_ssh1_login(unsigned char *in
, int inlen
, int ispkt
)
1245 unsigned char *rsabuf
, *keystr1
, *keystr2
;
1246 unsigned char cookie
[8];
1247 struct RSAKey servkey
, hostkey
;
1248 struct MD5Context md5c
;
1249 static unsigned long supported_ciphers_mask
, supported_auths_mask
;
1250 static int tried_publickey
;
1251 static unsigned char session_id
[16];
1253 static char username
[100];
1257 if (!ispkt
) crWaitUntil(ispkt
);
1259 if (pktin
.type
!= SSH1_SMSG_PUBLIC_KEY
) {
1260 bombout(("Public key packet not received"));
1264 logevent("Received public keys");
1266 memcpy(cookie
, pktin
.body
, 8);
1268 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
, 0);
1269 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
, 0);
1272 * Log the host key fingerprint.
1276 logevent("Host key fingerprint is:");
1277 strcpy(logmsg
, " ");
1278 hostkey
.comment
= NULL
;
1279 rsa_fingerprint(logmsg
+strlen(logmsg
), sizeof(logmsg
)-strlen(logmsg
),
1284 supported_ciphers_mask
= GET_32BIT(pktin
.body
+12+i
+j
);
1285 supported_auths_mask
= GET_32BIT(pktin
.body
+16+i
+j
);
1288 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
1289 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
1290 MD5Update(&md5c
, pktin
.body
, 8);
1291 MD5Final(session_id
, &md5c
);
1293 for (i
=0; i
<32; i
++)
1294 session_key
[i
] = random_byte();
1296 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
1298 rsabuf
= smalloc(len
);
1300 fatalbox("Out of memory");
1303 * Verify the host key.
1307 * First format the key into a string.
1309 int len
= rsastr_len(&hostkey
);
1310 char fingerprint
[100];
1311 char *keystr
= smalloc(len
);
1313 fatalbox("Out of memory");
1314 rsastr_fmt(keystr
, &hostkey
);
1315 rsa_fingerprint(fingerprint
, sizeof(fingerprint
), &hostkey
);
1316 verify_ssh_host_key(savedhost
, savedport
, "rsa", keystr
, fingerprint
);
1320 for (i
=0; i
<32; i
++) {
1321 rsabuf
[i
] = session_key
[i
];
1323 rsabuf
[i
] ^= session_id
[i
];
1326 if (hostkey
.bytes
> servkey
.bytes
) {
1327 rsaencrypt(rsabuf
, 32, &servkey
);
1328 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
1330 rsaencrypt(rsabuf
, 32, &hostkey
);
1331 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
1334 logevent("Encrypted session key");
1336 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
1337 cfg
.cipher
== CIPHER_DES ? SSH_CIPHER_DES
:
1339 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
1340 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
1341 cipher_type
= SSH_CIPHER_3DES
;
1343 switch (cipher_type
) {
1344 case SSH_CIPHER_3DES
: logevent("Using 3DES encryption"); break;
1345 case SSH_CIPHER_DES
: logevent("Using single-DES encryption"); break;
1346 case SSH_CIPHER_BLOWFISH
: logevent("Using Blowfish encryption"); break;
1349 send_packet(SSH1_CMSG_SESSION_KEY
,
1350 PKT_CHAR
, cipher_type
,
1351 PKT_DATA
, cookie
, 8,
1352 PKT_CHAR
, (len
*8) >> 8, PKT_CHAR
, (len
*8) & 0xFF,
1353 PKT_DATA
, rsabuf
, len
,
1357 logevent("Trying to enable encryption...");
1361 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish_ssh1
:
1362 cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
1364 cipher
->sesskey(session_key
);
1368 if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1369 bombout(("Encryption not successfully enabled"));
1373 logevent("Successfully started encryption");
1379 if ((flags
& FLAG_INTERACTIVE
) && !*cfg
.username
) {
1380 c_write("login as: ", 10);
1383 crWaitUntil(!ispkt
);
1384 while (inlen
--) switch (c
= *in
++) {
1391 c_write("\b \b", 3);
1397 c_write("\b \b", 3);
1406 if (((c
>= ' ' && c
<= '~') ||
1407 ((unsigned char)c
>= 160)) && pos
< 40) {
1408 username
[pos
++] = c
;
1415 username
[strcspn(username
, "\n\r")] = '\0';
1417 strncpy(username
, cfg
.username
, 99);
1418 username
[99] = '\0';
1421 send_packet(SSH1_CMSG_USER
, PKT_STR
, username
, PKT_END
);
1423 char userlog
[22+sizeof(username
)];
1424 sprintf(userlog
, "Sent username \"%s\"", username
);
1426 if (flags
& FLAG_INTERACTIVE
&&
1427 (!((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)))) {
1428 strcat(userlog
, "\r\n");
1429 c_write(userlog
, strlen(userlog
));
1436 tried_publickey
= 0;
1438 while (pktin
.type
== SSH1_SMSG_FAILURE
) {
1439 static char password
[100];
1440 static char prompt
[200];
1443 static int pwpkt_type
;
1445 * Show password prompt, having first obtained it via a TIS
1446 * or CryptoCard exchange if we're doing TIS or CryptoCard
1449 pwpkt_type
= SSH1_CMSG_AUTH_PASSWORD
;
1450 if (agent_exists()) {
1452 * Attempt RSA authentication using Pageant.
1454 static unsigned char request
[5], *response
, *p
;
1455 static int responselen
;
1456 static int i
, nkeys
;
1457 static int authed
= FALSE
;
1460 logevent("Pageant is running. Requesting keys.");
1462 /* Request the keys held by the agent. */
1463 PUT_32BIT(request
, 1);
1464 request
[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES
;
1465 agent_query(request
, 5, &r
, &responselen
);
1466 response
= (unsigned char *)r
;
1469 nkeys
= GET_32BIT(p
); p
+= 4;
1470 { char buf
[64]; sprintf(buf
, "Pageant has %d keys", nkeys
);
1472 for (i
= 0; i
< nkeys
; i
++) {
1473 static struct RSAKey key
;
1474 static Bignum challenge
;
1475 static char *commentp
;
1476 static int commentlen
;
1478 { char buf
[64]; sprintf(buf
, "Trying Pageant key #%d", i
);
1481 p
+= ssh1_read_bignum(p
, &key
.exponent
);
1482 p
+= ssh1_read_bignum(p
, &key
.modulus
);
1483 commentlen
= GET_32BIT(p
); p
+= 4;
1484 commentp
= p
; p
+= commentlen
;
1485 send_packet(SSH1_CMSG_AUTH_RSA
,
1486 PKT_BIGNUM
, key
.modulus
, PKT_END
);
1488 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
1489 logevent("Key refused");
1492 logevent("Received RSA challenge");
1493 ssh1_read_bignum(pktin
.body
, &challenge
);
1495 char *agentreq
, *q
, *ret
;
1497 len
= 1 + 4; /* message type, bit count */
1498 len
+= ssh1_bignum_length(key
.exponent
);
1499 len
+= ssh1_bignum_length(key
.modulus
);
1500 len
+= ssh1_bignum_length(challenge
);
1501 len
+= 16; /* session id */
1502 len
+= 4; /* response format */
1503 agentreq
= smalloc(4 + len
);
1504 PUT_32BIT(agentreq
, len
);
1506 *q
++ = SSH_AGENTC_RSA_CHALLENGE
;
1507 PUT_32BIT(q
, ssh1_bignum_bitcount(key
.modulus
));
1509 q
+= ssh1_write_bignum(q
, key
.exponent
);
1510 q
+= ssh1_write_bignum(q
, key
.modulus
);
1511 q
+= ssh1_write_bignum(q
, challenge
);
1512 memcpy(q
, session_id
, 16); q
+= 16;
1513 PUT_32BIT(q
, 1); /* response format */
1514 agent_query(agentreq
, len
+4, &ret
, &retlen
);
1517 if (ret
[4] == SSH_AGENT_RSA_RESPONSE
) {
1518 logevent("Sending Pageant's response");
1519 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1520 PKT_DATA
, ret
+5, 16, PKT_END
);
1523 if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
1524 logevent("Pageant's response accepted");
1525 if (flags
& FLAG_VERBOSE
) {
1526 c_write("Authenticated using RSA key \"",
1528 c_write(commentp
, commentlen
);
1529 c_write("\" from agent\r\n", 14);
1533 logevent("Pageant's response not accepted");
1535 logevent("Pageant failed to answer challenge");
1539 logevent("No reply received from Pageant");
1542 freebn(key
.exponent
);
1543 freebn(key
.modulus
);
1552 if (*cfg
.keyfile
&& !tried_publickey
)
1553 pwpkt_type
= SSH1_CMSG_AUTH_RSA
;
1555 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1557 (supported_auths_mask
& (1<<SSH1_AUTH_TIS
))) {
1558 pwpkt_type
= SSH1_CMSG_AUTH_TIS_RESPONSE
;
1559 logevent("Requested TIS authentication");
1560 send_packet(SSH1_CMSG_AUTH_TIS
, PKT_END
);
1562 if (pktin
.type
!= SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
1563 logevent("TIS authentication declined");
1564 if (flags
& FLAG_INTERACTIVE
)
1565 c_write("TIS authentication refused.\r\n", 29);
1567 int challengelen
= ((pktin
.body
[0] << 24) |
1568 (pktin
.body
[1] << 16) |
1569 (pktin
.body
[2] << 8) |
1571 logevent("Received TIS challenge");
1572 if (challengelen
> sizeof(prompt
)-1)
1573 challengelen
= sizeof(prompt
)-1; /* prevent overrun */
1574 memcpy(prompt
, pktin
.body
+4, challengelen
);
1575 prompt
[challengelen
] = '\0';
1578 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1580 (supported_auths_mask
& (1<<SSH1_AUTH_CCARD
))) {
1581 pwpkt_type
= SSH1_CMSG_AUTH_CCARD_RESPONSE
;
1582 logevent("Requested CryptoCard authentication");
1583 send_packet(SSH1_CMSG_AUTH_CCARD
, PKT_END
);
1585 if (pktin
.type
!= SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
1586 logevent("CryptoCard authentication declined");
1587 c_write("CryptoCard authentication refused.\r\n", 29);
1589 int challengelen
= ((pktin
.body
[0] << 24) |
1590 (pktin
.body
[1] << 16) |
1591 (pktin
.body
[2] << 8) |
1593 logevent("Received CryptoCard challenge");
1594 if (challengelen
> sizeof(prompt
)-1)
1595 challengelen
= sizeof(prompt
)-1; /* prevent overrun */
1596 memcpy(prompt
, pktin
.body
+4, challengelen
);
1597 strncpy(prompt
+ challengelen
, "\r\nResponse : ",
1598 sizeof(prompt
)-challengelen
);
1599 prompt
[sizeof(prompt
)-1] = '\0';
1602 if (pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
) {
1603 sprintf(prompt
, "%.90s@%.90s's password: ",
1604 username
, savedhost
);
1606 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1607 char *comment
= NULL
;
1608 if (flags
& FLAG_VERBOSE
)
1609 c_write("Trying public key authentication.\r\n", 35);
1610 if (!rsakey_encrypted(cfg
.keyfile
, &comment
)) {
1611 if (flags
& FLAG_VERBOSE
)
1612 c_write("No passphrase required.\r\n", 25);
1615 sprintf(prompt
, "Passphrase for key \"%.100s\": ", comment
);
1619 if (ssh_get_password
) {
1620 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
1622 * get_password failed to get a password (for
1623 * example because one was supplied on the command
1624 * line which has already failed to work).
1627 logevent("No more passwords to try");
1628 ssh_state
= SSH_STATE_CLOSED
;
1632 c_write(prompt
, strlen(prompt
));
1636 crWaitUntil(!ispkt
);
1637 while (inlen
--) switch (c
= *in
++) {
1654 if (((c
>= ' ' && c
<= '~') ||
1655 ((unsigned char)c
>= 160)) && pos
< sizeof(password
))
1656 password
[pos
++] = c
;
1664 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1666 * Try public key authentication with the specified
1669 static struct RSAKey pubkey
;
1670 static Bignum challenge
, response
;
1672 static unsigned char buffer
[32];
1674 tried_publickey
= 1;
1675 i
= loadrsakey(cfg
.keyfile
, &pubkey
, NULL
, password
);
1677 c_write("Couldn't load public key from ", 30);
1678 c_write(cfg
.keyfile
, strlen(cfg
.keyfile
));
1679 c_write(".\r\n", 3);
1680 continue; /* go and try password */
1683 c_write("Wrong passphrase.\r\n", 19);
1684 tried_publickey
= 0;
1685 continue; /* try again */
1689 * Send a public key attempt.
1691 send_packet(SSH1_CMSG_AUTH_RSA
,
1692 PKT_BIGNUM
, pubkey
.modulus
, PKT_END
);
1695 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1696 c_write("Server refused our public key.\r\n", 32);
1697 continue; /* go and try password */
1699 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
1700 bombout(("Bizarre response to offer of public key"));
1703 ssh1_read_bignum(pktin
.body
, &challenge
);
1704 response
= rsadecrypt(challenge
, &pubkey
);
1705 freebn(pubkey
.private_exponent
); /* burn the evidence */
1707 for (i
= 0; i
< 32; i
+= 2) {
1708 buffer
[i
] = response
[16-i
/2] >> 8;
1709 buffer
[i
+1] = response
[16-i
/2] & 0xFF;
1713 MD5Update(&md5c
, buffer
, 32);
1714 MD5Update(&md5c
, session_id
, 16);
1715 MD5Final(buffer
, &md5c
);
1717 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1718 PKT_DATA
, buffer
, 16, PKT_END
);
1721 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1722 if (flags
& FLAG_VERBOSE
)
1723 c_write("Failed to authenticate with our public key.\r\n",
1725 continue; /* go and try password */
1726 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1727 bombout(("Bizarre response to RSA authentication response"));
1731 break; /* we're through! */
1733 send_packet(pwpkt_type
, PKT_STR
, password
, PKT_END
);
1735 logevent("Sent password");
1736 memset(password
, 0, strlen(password
));
1738 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1739 if (flags
& FLAG_VERBOSE
)
1740 c_write("Access denied\r\n", 15);
1741 logevent("Authentication refused");
1742 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1743 logevent("Received disconnect request");
1744 ssh_state
= SSH_STATE_CLOSED
;
1746 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1747 bombout(("Strange packet received, type %d", pktin
.type
));
1752 logevent("Authentication successful");
1757 void sshfwd_close(struct ssh_channel
*c
) {
1759 if (ssh_version
== 1) {
1760 send_packet(SSH1_MSG_CHANNEL_CLOSE
, PKT_INT
, c
->remoteid
, PKT_END
);
1762 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
1763 ssh2_pkt_adduint32(c
->remoteid
);
1767 if (c
->type
== CHAN_X11
) {
1769 logevent("X11 connection terminated");
1774 void sshfwd_write(struct ssh_channel
*c
, char *buf
, int len
) {
1775 if (ssh_version
== 1) {
1776 send_packet(SSH1_MSG_CHANNEL_DATA
,
1777 PKT_INT
, c
->remoteid
,
1782 ssh2_add_channel_data(c
, buf
, len
);
1787 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
) {
1792 while (!do_ssh1_login(in
, inlen
, ispkt
)) {
1795 if (ssh_state
== SSH_STATE_CLOSED
)
1798 if (cfg
.agentfwd
&& agent_exists()) {
1799 logevent("Requesting agent forwarding");
1800 send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING
, PKT_END
);
1801 do { crReturnV
; } while (!ispkt
);
1802 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1803 bombout(("Protocol confusion"));
1805 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1806 logevent("Agent forwarding refused");
1808 logevent("Agent forwarding enabled");
1809 ssh_agentfwd_enabled
= TRUE
;
1813 if (cfg
.x11_forward
) {
1814 char proto
[20], data
[64];
1815 logevent("Requesting X11 forwarding");
1816 x11_invent_auth(proto
, sizeof(proto
), data
, sizeof(data
));
1817 send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING
,
1818 PKT_STR
, proto
, PKT_STR
, data
,
1821 do { crReturnV
; } while (!ispkt
);
1822 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1823 bombout(("Protocol confusion"));
1825 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1826 logevent("X11 forwarding refused");
1828 logevent("X11 forwarding enabled");
1829 ssh_X11_fwd_enabled
= TRUE
;
1834 send_packet(SSH1_CMSG_REQUEST_PTY
,
1835 PKT_STR
, cfg
.termtype
,
1836 PKT_INT
, rows
, PKT_INT
, cols
,
1837 PKT_INT
, 0, PKT_INT
, 0,
1840 ssh_state
= SSH_STATE_INTERMED
;
1841 do { crReturnV
; } while (!ispkt
);
1842 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1843 bombout(("Protocol confusion"));
1845 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1846 c_write("Server refused to allocate pty\r\n", 32);
1847 ssh_editing
= ssh_echoing
= 1;
1849 logevent("Allocated pty");
1851 ssh_editing
= ssh_echoing
= 1;
1854 if (cfg
.compression
) {
1855 send_packet(SSH1_CMSG_REQUEST_COMPRESSION
, PKT_INT
, 6, PKT_END
);
1856 do { crReturnV
; } while (!ispkt
);
1857 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1858 bombout(("Protocol confusion"));
1860 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1861 c_write("Server refused to compress\r\n", 32);
1863 logevent("Started compression");
1864 ssh1_compressing
= TRUE
;
1865 zlib_compress_init();
1866 zlib_decompress_init();
1869 if (*cfg
.remote_cmd
)
1870 send_packet(SSH1_CMSG_EXEC_CMD
, PKT_STR
, cfg
.remote_cmd
, PKT_END
);
1872 send_packet(SSH1_CMSG_EXEC_SHELL
, PKT_END
);
1873 logevent("Started session");
1875 ssh_state
= SSH_STATE_SESSION
;
1879 ssh_special(TS_EOF
);
1881 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
1883 ssh_channels
= newtree234(ssh_channelcmp
);
1887 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
1888 pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
1889 long len
= GET_32BIT(pktin
.body
);
1890 from_backend(pktin
.type
== SSH1_SMSG_STDERR_DATA
,
1892 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1893 ssh_state
= SSH_STATE_CLOSED
;
1894 logevent("Received disconnect request");
1896 } else if (pktin
.type
== SSH1_SMSG_X11_OPEN
) {
1897 /* Remote side is trying to open a channel to talk to our
1898 * X-Server. Give them back a local channel number. */
1900 struct ssh_channel
*c
, *d
;
1903 logevent("Received X11 connect request");
1904 /* Refuse if X11 forwarding is disabled. */
1905 if (!ssh_X11_fwd_enabled
) {
1906 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
1907 PKT_INT
, GET_32BIT(pktin
.body
),
1909 logevent("Rejected X11 connect request");
1911 c
= smalloc(sizeof(struct ssh_channel
));
1913 if ( x11_init(&c
->u
.x11
.s
, cfg
.x11_display
, c
) != NULL
) {
1914 logevent("opening X11 forward connection failed");
1916 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
1917 PKT_INT
, GET_32BIT(pktin
.body
),
1920 logevent("opening X11 forward connection succeeded");
1921 for (i
=1, d
= first234(ssh_channels
, &e
); d
; d
= next234(&e
)) {
1923 break; /* found a free number */
1926 c
->remoteid
= GET_32BIT(pktin
.body
);
1929 c
->type
= CHAN_X11
; /* identify channel type */
1930 add234(ssh_channels
, c
);
1931 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
1932 PKT_INT
, c
->remoteid
, PKT_INT
, c
->localid
,
1934 logevent("Opened X11 forward channel");
1937 } else if (pktin
.type
== SSH1_SMSG_AGENT_OPEN
) {
1938 /* Remote side is trying to open a channel to talk to our
1939 * agent. Give them back a local channel number. */
1941 struct ssh_channel
*c
;
1944 /* Refuse if agent forwarding is disabled. */
1945 if (!ssh_agentfwd_enabled
) {
1946 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
1947 PKT_INT
, GET_32BIT(pktin
.body
),
1951 for (c
= first234(ssh_channels
, &e
); c
; c
= next234(&e
)) {
1953 break; /* found a free number */
1956 c
= smalloc(sizeof(struct ssh_channel
));
1957 c
->remoteid
= GET_32BIT(pktin
.body
);
1960 c
->type
= CHAN_AGENT
; /* identify channel type */
1961 c
->u
.a
.lensofar
= 0;
1962 add234(ssh_channels
, c
);
1963 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
1964 PKT_INT
, c
->remoteid
, PKT_INT
, c
->localid
,
1967 } else if (pktin
.type
== SSH1_MSG_CHANNEL_CLOSE
||
1968 pktin
.type
== SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION
) {
1969 /* Remote side closes a channel. */
1970 unsigned i
= GET_32BIT(pktin
.body
);
1971 struct ssh_channel
*c
;
1972 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
1975 closetype
= (pktin
.type
== SSH1_MSG_CHANNEL_CLOSE ?
1 : 2);
1976 send_packet(pktin
.type
, PKT_INT
, c
->remoteid
, PKT_END
);
1977 if ((c
->closes
== 0) && (c
->type
== CHAN_X11
)) {
1978 logevent("X11 connection closed");
1979 assert(c
->u
.x11
.s
!= NULL
);
1980 x11_close(c
->u
.x11
.s
);
1983 c
->closes
|= closetype
;
1984 if (c
->closes
== 3) {
1985 del234(ssh_channels
, c
);
1989 } else if (pktin
.type
== SSH1_MSG_CHANNEL_DATA
) {
1990 /* Data sent down one of our channels. */
1991 int i
= GET_32BIT(pktin
.body
);
1992 int len
= GET_32BIT(pktin
.body
+4);
1993 unsigned char *p
= pktin
.body
+8;
1994 struct ssh_channel
*c
;
1995 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
1999 x11_send(c
->u
.x11
.s
, p
, len
);
2002 /* Data for an agent message. Buffer it. */
2004 if (c
->u
.a
.lensofar
< 4) {
2005 int l
= min(4 - c
->u
.a
.lensofar
, len
);
2006 memcpy(c
->u
.a
.msglen
+ c
->u
.a
.lensofar
, p
, l
);
2007 p
+= l
; len
-= l
; c
->u
.a
.lensofar
+= l
;
2009 if (c
->u
.a
.lensofar
== 4) {
2010 c
->u
.a
.totallen
= 4 + GET_32BIT(c
->u
.a
.msglen
);
2011 c
->u
.a
.message
= smalloc(c
->u
.a
.totallen
);
2012 memcpy(c
->u
.a
.message
, c
->u
.a
.msglen
, 4);
2014 if (c
->u
.a
.lensofar
>= 4 && len
> 0) {
2015 int l
= min(c
->u
.a
.totallen
- c
->u
.a
.lensofar
, len
);
2016 memcpy(c
->u
.a
.message
+ c
->u
.a
.lensofar
, p
, l
);
2017 p
+= l
; len
-= l
; c
->u
.a
.lensofar
+= l
;
2019 if (c
->u
.a
.lensofar
== c
->u
.a
.totallen
) {
2020 void *reply
, *sentreply
;
2022 agent_query(c
->u
.a
.message
, c
->u
.a
.totallen
,
2027 /* Fake SSH_AGENT_FAILURE. */
2028 sentreply
= "\0\0\0\1\5";
2031 send_packet(SSH1_MSG_CHANNEL_DATA
,
2032 PKT_INT
, c
->remoteid
,
2034 PKT_DATA
, sentreply
, replylen
,
2038 sfree(c
->u
.a
.message
);
2039 c
->u
.a
.lensofar
= 0;
2045 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
2046 /* may be from EXEC_SHELL on some servers */
2047 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
2048 /* may be from EXEC_SHELL on some servers
2049 * if no pty is available or in other odd cases. Ignore */
2050 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
2051 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
2053 bombout(("Strange packet received: type %d", pktin
.type
));
2058 int len
= min(inlen
, 512);
2059 send_packet(SSH1_CMSG_STDIN_DATA
,
2060 PKT_INT
, len
, PKT_DATA
, in
, len
, PKT_END
);
2071 * Utility routine for decoding comma-separated strings in KEXINIT.
2073 static int in_commasep_string(char *needle
, char *haystack
, int haylen
) {
2074 int needlen
= strlen(needle
);
2077 * Is it at the start of the string?
2079 if (haylen
>= needlen
&& /* haystack is long enough */
2080 !memcmp(needle
, haystack
, needlen
) && /* initial match */
2081 (haylen
== needlen
|| haystack
[needlen
] == ',')
2082 /* either , or EOS follows */
2086 * If not, search for the next comma and resume after that.
2087 * If no comma found, terminate.
2089 while (haylen
> 0 && *haystack
!= ',')
2090 haylen
--, haystack
++;
2093 haylen
--, haystack
++; /* skip over comma itself */
2098 * SSH2 key creation method.
2100 static void ssh2_mkkey(Bignum K
, char *H
, char *sessid
, char chr
, char *keyspace
) {
2102 /* First 20 bytes. */
2105 SHA_Bytes(&s
, H
, 20);
2106 SHA_Bytes(&s
, &chr
, 1);
2107 SHA_Bytes(&s
, sessid
, 20);
2108 SHA_Final(&s
, keyspace
);
2109 /* Next 20 bytes. */
2112 SHA_Bytes(&s
, H
, 20);
2113 SHA_Bytes(&s
, keyspace
, 20);
2114 SHA_Final(&s
, keyspace
+20);
2118 * Handle the SSH2 transport layer.
2120 static int do_ssh2_transport(unsigned char *in
, int inlen
, int ispkt
)
2124 static Bignum e
, f
, K
;
2125 static const struct ssh_mac
**maclist
;
2127 static const struct ssh_cipher
*cscipher_tobe
= NULL
;
2128 static const struct ssh_cipher
*sccipher_tobe
= NULL
;
2129 static const struct ssh_mac
*csmac_tobe
= NULL
;
2130 static const struct ssh_mac
*scmac_tobe
= NULL
;
2131 static const struct ssh_compress
*cscomp_tobe
= NULL
;
2132 static const struct ssh_compress
*sccomp_tobe
= NULL
;
2133 static char *hostkeydata
, *sigdata
, *keystr
, *fingerprint
;
2134 static int hostkeylen
, siglen
;
2135 static void *hkey
; /* actual host key */
2136 static unsigned char exchange_hash
[20];
2137 static unsigned char first_exchange_hash
[20];
2138 static unsigned char keyspace
[40];
2139 static const struct ssh_cipher
*preferred_cipher
;
2140 static const struct ssh_compress
*preferred_comp
;
2141 static int first_kex
;
2148 * Set up the preferred cipher and compression.
2150 if (cfg
.cipher
== CIPHER_BLOWFISH
) {
2151 preferred_cipher
= &ssh_blowfish_ssh2
;
2152 } else if (cfg
.cipher
== CIPHER_DES
) {
2153 logevent("Single DES not supported in SSH2; using 3DES");
2154 preferred_cipher
= &ssh_3des_ssh2
;
2155 } else if (cfg
.cipher
== CIPHER_3DES
) {
2156 preferred_cipher
= &ssh_3des_ssh2
;
2158 /* Shouldn't happen, but we do want to initialise to _something_. */
2159 preferred_cipher
= &ssh_3des_ssh2
;
2161 if (cfg
.compression
)
2162 preferred_comp
= &ssh_zlib
;
2164 preferred_comp
= &ssh_comp_none
;
2167 * Be prepared to work around the buggy MAC problem.
2170 maclist
= buggymacs
, nmacs
= lenof(buggymacs
);
2172 maclist
= macs
, nmacs
= lenof(macs
);
2176 * Construct and send our key exchange packet.
2178 ssh2_pkt_init(SSH2_MSG_KEXINIT
);
2179 for (i
= 0; i
< 16; i
++)
2180 ssh2_pkt_addbyte((unsigned char)random_byte());
2181 /* List key exchange algorithms. */
2182 ssh2_pkt_addstring_start();
2183 for (i
= 0; i
< lenof(kex_algs
); i
++) {
2184 ssh2_pkt_addstring_str(kex_algs
[i
]->name
);
2185 if (i
< lenof(kex_algs
)-1)
2186 ssh2_pkt_addstring_str(",");
2188 /* List server host key algorithms. */
2189 ssh2_pkt_addstring_start();
2190 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
2191 ssh2_pkt_addstring_str(hostkey_algs
[i
]->name
);
2192 if (i
< lenof(hostkey_algs
)-1)
2193 ssh2_pkt_addstring_str(",");
2195 /* List client->server encryption algorithms. */
2196 ssh2_pkt_addstring_start();
2197 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2198 const struct ssh_cipher
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2199 ssh2_pkt_addstring_str(c
->name
);
2200 if (i
< lenof(ciphers
))
2201 ssh2_pkt_addstring_str(",");
2203 /* List server->client encryption algorithms. */
2204 ssh2_pkt_addstring_start();
2205 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2206 const struct ssh_cipher
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2207 ssh2_pkt_addstring_str(c
->name
);
2208 if (i
< lenof(ciphers
))
2209 ssh2_pkt_addstring_str(",");
2211 /* List client->server MAC algorithms. */
2212 ssh2_pkt_addstring_start();
2213 for (i
= 0; i
< nmacs
; i
++) {
2214 ssh2_pkt_addstring_str(maclist
[i
]->name
);
2216 ssh2_pkt_addstring_str(",");
2218 /* List server->client MAC algorithms. */
2219 ssh2_pkt_addstring_start();
2220 for (i
= 0; i
< nmacs
; i
++) {
2221 ssh2_pkt_addstring_str(maclist
[i
]->name
);
2223 ssh2_pkt_addstring_str(",");
2225 /* List client->server compression algorithms. */
2226 ssh2_pkt_addstring_start();
2227 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2228 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2229 ssh2_pkt_addstring_str(c
->name
);
2230 if (i
< lenof(compressions
))
2231 ssh2_pkt_addstring_str(",");
2233 /* List server->client compression algorithms. */
2234 ssh2_pkt_addstring_start();
2235 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2236 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2237 ssh2_pkt_addstring_str(c
->name
);
2238 if (i
< lenof(compressions
))
2239 ssh2_pkt_addstring_str(",");
2241 /* List client->server languages. Empty list. */
2242 ssh2_pkt_addstring_start();
2243 /* List server->client languages. Empty list. */
2244 ssh2_pkt_addstring_start();
2245 /* First KEX packet does _not_ follow, because we're not that brave. */
2246 ssh2_pkt_addbool(FALSE
);
2248 ssh2_pkt_adduint32(0);
2250 exhash
= exhashbase
;
2251 sha_string(&exhash
, pktout
.data
+5, pktout
.length
-5);
2255 if (!ispkt
) crWaitUntil(ispkt
);
2256 sha_string(&exhash
, pktin
.data
+5, pktin
.length
-5);
2259 * Now examine the other side's KEXINIT to see what we're up
2262 if (pktin
.type
!= SSH2_MSG_KEXINIT
) {
2263 bombout(("expected key exchange packet from server"));
2266 kex
= NULL
; hostkey
= NULL
; cscipher_tobe
= NULL
; sccipher_tobe
= NULL
;
2267 csmac_tobe
= NULL
; scmac_tobe
= NULL
; cscomp_tobe
= NULL
; sccomp_tobe
= NULL
;
2268 pktin
.savedpos
+= 16; /* skip garbage cookie */
2269 ssh2_pkt_getstring(&str
, &len
); /* key exchange algorithms */
2270 for (i
= 0; i
< lenof(kex_algs
); i
++) {
2271 if (in_commasep_string(kex_algs
[i
]->name
, str
, len
)) {
2276 ssh2_pkt_getstring(&str
, &len
); /* host key algorithms */
2277 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
2278 if (in_commasep_string(hostkey_algs
[i
]->name
, str
, len
)) {
2279 hostkey
= hostkey_algs
[i
];
2283 ssh2_pkt_getstring(&str
, &len
); /* client->server cipher */
2284 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2285 const struct ssh_cipher
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2286 if (in_commasep_string(c
->name
, str
, len
)) {
2291 ssh2_pkt_getstring(&str
, &len
); /* server->client cipher */
2292 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2293 const struct ssh_cipher
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2294 if (in_commasep_string(c
->name
, str
, len
)) {
2299 ssh2_pkt_getstring(&str
, &len
); /* client->server mac */
2300 for (i
= 0; i
< nmacs
; i
++) {
2301 if (in_commasep_string(maclist
[i
]->name
, str
, len
)) {
2302 csmac_tobe
= maclist
[i
];
2306 ssh2_pkt_getstring(&str
, &len
); /* server->client mac */
2307 for (i
= 0; i
< nmacs
; i
++) {
2308 if (in_commasep_string(maclist
[i
]->name
, str
, len
)) {
2309 scmac_tobe
= maclist
[i
];
2313 ssh2_pkt_getstring(&str
, &len
); /* client->server compression */
2314 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2315 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2316 if (in_commasep_string(c
->name
, str
, len
)) {
2321 ssh2_pkt_getstring(&str
, &len
); /* server->client compression */
2322 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2323 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2324 if (in_commasep_string(c
->name
, str
, len
)) {
2331 * Currently we only support Diffie-Hellman and DSS, so let's
2332 * bomb out if those aren't selected.
2334 if (kex
!= &ssh_diffiehellman
|| hostkey
!= &ssh_dss
) {
2335 bombout(("internal fault: chaos in SSH 2 transport layer"));
2340 * Now we begin the fun. Generate and send e for Diffie-Hellman.
2343 ssh2_pkt_init(SSH2_MSG_KEXDH_INIT
);
2348 if (pktin
.type
!= SSH2_MSG_KEXDH_REPLY
) {
2349 bombout(("expected key exchange packet from server"));
2352 ssh2_pkt_getstring(&hostkeydata
, &hostkeylen
);
2353 f
= ssh2_pkt_getmp();
2354 ssh2_pkt_getstring(&sigdata
, &siglen
);
2358 sha_string(&exhash
, hostkeydata
, hostkeylen
);
2359 sha_mpint(&exhash
, e
);
2360 sha_mpint(&exhash
, f
);
2361 sha_mpint(&exhash
, K
);
2362 SHA_Final(&exhash
, exchange_hash
);
2365 debug(("Exchange hash is:\r\n"));
2366 for (i
= 0; i
< 20; i
++)
2367 debug((" %02x", exchange_hash
[i
]));
2371 hkey
= hostkey
->newkey(hostkeydata
, hostkeylen
);
2372 if (!hostkey
->verifysig(hkey
, sigdata
, siglen
, exchange_hash
, 20)) {
2373 bombout(("Server failed host key check"));
2378 * Expect SSH2_MSG_NEWKEYS from server.
2381 if (pktin
.type
!= SSH2_MSG_NEWKEYS
) {
2382 bombout(("expected new-keys packet from server"));
2387 * Authenticate remote host: verify host key. (We've already
2388 * checked the signature of the exchange hash.)
2390 keystr
= hostkey
->fmtkey(hkey
);
2391 fingerprint
= hostkey
->fingerprint(hkey
);
2392 verify_ssh_host_key(savedhost
, savedport
, hostkey
->keytype
,
2393 keystr
, fingerprint
);
2394 if (first_kex
) { /* don't bother logging this in rekeys */
2395 logevent("Host key fingerprint is:");
2396 logevent(fingerprint
);
2400 hostkey
->freekey(hkey
);
2403 * Send SSH2_MSG_NEWKEYS.
2405 ssh2_pkt_init(SSH2_MSG_NEWKEYS
);
2409 * Create and initialise session keys.
2411 cscipher
= cscipher_tobe
;
2412 sccipher
= sccipher_tobe
;
2415 cscomp
= cscomp_tobe
;
2416 sccomp
= sccomp_tobe
;
2417 cscomp
->compress_init();
2418 sccomp
->decompress_init();
2420 * Set IVs after keys. Here we use the exchange hash from the
2421 * _first_ key exchange.
2424 memcpy(first_exchange_hash
, exchange_hash
, sizeof(exchange_hash
));
2425 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'C', keyspace
);
2426 cscipher
->setcskey(keyspace
);
2427 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'D', keyspace
);
2428 sccipher
->setsckey(keyspace
);
2429 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'A', keyspace
);
2430 cscipher
->setcsiv(keyspace
);
2431 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'B', keyspace
);
2432 sccipher
->setsciv(keyspace
);
2433 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'E', keyspace
);
2434 csmac
->setcskey(keyspace
);
2435 ssh2_mkkey(K
, exchange_hash
, first_exchange_hash
, 'F', keyspace
);
2436 scmac
->setsckey(keyspace
);
2439 * If this is the first key exchange phase, we must pass the
2440 * SSH2_MSG_NEWKEYS packet to the next layer, not because it
2441 * wants to see it but because it will need time to initialise
2442 * itself before it sees an actual packet. In subsequent key
2443 * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
2444 * it would only confuse the layer above.
2452 * Now we're encrypting. Begin returning 1 to the protocol main
2453 * function so that other things can run on top of the
2454 * transport. If we ever see a KEXINIT, we must go back to the
2459 } while (!(ispkt
&& pktin
.type
== SSH2_MSG_KEXINIT
));
2460 logevent("Server initiated key re-exchange");
2461 goto begin_key_exchange
;
2467 * Add data to an SSH2 channel output buffer.
2469 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
, int len
) {
2470 if (c
->v2
.outbufsize
<
2471 c
->v2
.outbuflen
+ len
) {
2473 c
->v2
.outbuflen
+ len
+ 1024;
2474 c
->v2
.outbuffer
= srealloc(c
->v2
.outbuffer
,
2477 memcpy(c
->v2
.outbuffer
+ c
->v2
.outbuflen
,
2479 c
->v2
.outbuflen
+= len
;
2483 * Attempt to send data on an SSH2 channel.
2485 static void ssh2_try_send(struct ssh_channel
*c
) {
2486 while (c
->v2
.remwindow
> 0 &&
2487 c
->v2
.outbuflen
> 0) {
2488 unsigned len
= c
->v2
.remwindow
;
2489 if (len
> c
->v2
.outbuflen
)
2490 len
= c
->v2
.outbuflen
;
2491 if (len
> c
->v2
.remmaxpkt
)
2492 len
= c
->v2
.remmaxpkt
;
2493 ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA
);
2494 ssh2_pkt_adduint32(c
->remoteid
);
2495 ssh2_pkt_addstring_start();
2496 ssh2_pkt_addstring_data(c
->v2
.outbuffer
, len
);
2498 c
->v2
.outbuflen
-= len
;
2499 memmove(c
->v2
.outbuffer
, c
->v2
.outbuffer
+len
,
2501 c
->v2
.remwindow
-= len
;
2506 * Handle the SSH2 userauth and connection layers.
2508 static void do_ssh2_authconn(unsigned char *in
, int inlen
, int ispkt
)
2510 static unsigned long remote_winsize
;
2511 static unsigned long remote_maxpkt
;
2516 * Request userauth protocol, and await a response to it.
2518 ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST
);
2519 ssh2_pkt_addstring("ssh-userauth");
2521 crWaitUntilV(ispkt
);
2522 if (pktin
.type
!= SSH2_MSG_SERVICE_ACCEPT
) {
2523 bombout(("Server refused user authentication protocol"));
2528 * FIXME: currently we support only password authentication.
2529 * (This places us technically in violation of the SSH2 spec.
2530 * We must fix this.)
2534 * Get a username and a password.
2536 static char username
[100];
2537 static char password
[100];
2541 if ((flags
& FLAG_INTERACTIVE
) && !*cfg
.username
) {
2542 c_write("login as: ", 10);
2545 crWaitUntilV(!ispkt
);
2546 while (inlen
--) switch (c
= *in
++) {
2553 c_write("\b \b", 3);
2559 c_write("\b \b", 3);
2568 if (((c
>= ' ' && c
<= '~') ||
2569 ((unsigned char)c
>= 160)) && pos
< 40) {
2570 username
[pos
++] = c
;
2577 username
[strcspn(username
, "\n\r")] = '\0';
2580 strncpy(username
, cfg
.username
, 99);
2581 username
[99] = '\0';
2582 if ((flags
& FLAG_VERBOSE
) || (flags
& FLAG_INTERACTIVE
)) {
2583 sprintf(stuff
, "Using username \"%s\".\r\n", username
);
2584 c_write(stuff
, strlen(stuff
));
2588 if (ssh_get_password
) {
2590 sprintf(prompt
, "%.90s@%.90s's password: ", username
, savedhost
);
2591 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
2593 * get_password failed to get a password (for
2594 * example because one was supplied on the command
2595 * line which has already failed to work).
2598 logevent("No more passwords to try");
2599 ssh_state
= SSH_STATE_CLOSED
;
2603 c_write("password: ", 10);
2608 crWaitUntilV(!ispkt
);
2609 while (inlen
--) switch (c
= *in
++) {
2626 if (((c
>= ' ' && c
<= '~') ||
2627 ((unsigned char)c
>= 160)) && pos
< 40)
2628 password
[pos
++] = c
;
2635 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
2636 ssh2_pkt_addstring(username
);
2637 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2638 ssh2_pkt_addstring("password");
2639 ssh2_pkt_addbool(FALSE
);
2640 ssh2_pkt_addstring(password
);
2643 crWaitUntilV(ispkt
);
2644 if (pktin
.type
!= SSH2_MSG_USERAUTH_SUCCESS
) {
2645 c_write("Access denied\r\n", 15);
2646 logevent("Authentication refused");
2652 * Now we're authenticated for the connection protocol. The
2653 * connection protocol will automatically have started at this
2654 * point; there's no need to send SERVICE_REQUEST.
2658 * So now create a channel with a session in it.
2660 mainchan
= smalloc(sizeof(struct ssh_channel
));
2661 mainchan
->localid
= 100; /* as good as any */
2662 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN
);
2663 ssh2_pkt_addstring("session");
2664 ssh2_pkt_adduint32(mainchan
->localid
);
2665 ssh2_pkt_adduint32(0x8000UL
); /* our window size */
2666 ssh2_pkt_adduint32(0x4000UL
); /* our max pkt size */
2668 crWaitUntilV(ispkt
);
2669 if (pktin
.type
!= SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
) {
2670 bombout(("Server refused to open a session"));
2672 /* FIXME: error data comes back in FAILURE packet */
2674 if (ssh2_pkt_getuint32() != mainchan
->localid
) {
2675 bombout(("Server's channel confirmation cited wrong channel"));
2678 mainchan
->remoteid
= ssh2_pkt_getuint32();
2679 mainchan
->type
= CHAN_MAINSESSION
;
2680 mainchan
->closes
= 0;
2681 mainchan
->v2
.remwindow
= ssh2_pkt_getuint32();
2682 mainchan
->v2
.remmaxpkt
= ssh2_pkt_getuint32();
2683 mainchan
->v2
.outbuffer
= NULL
;
2684 mainchan
->v2
.outbuflen
= mainchan
->v2
.outbufsize
= 0;
2685 ssh_channels
= newtree234(ssh_channelcmp
);
2686 add234(ssh_channels
, mainchan
);
2687 logevent("Opened channel for session");
2690 * Potentially enable X11 forwarding.
2692 if (cfg
.x11_forward
) {
2693 char proto
[20], data
[64];
2694 logevent("Requesting X11 forwarding");
2695 x11_invent_auth(proto
, sizeof(proto
), data
, sizeof(data
));
2696 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
2697 ssh2_pkt_adduint32(mainchan
->remoteid
);
2698 ssh2_pkt_addstring("x11-req");
2699 ssh2_pkt_addbool(1); /* want reply */
2700 ssh2_pkt_addbool(0); /* many connections */
2701 ssh2_pkt_addstring(proto
);
2702 ssh2_pkt_addstring(data
);
2703 ssh2_pkt_adduint32(0); /* screen number */
2707 crWaitUntilV(ispkt
);
2708 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
2709 unsigned i
= ssh2_pkt_getuint32();
2710 struct ssh_channel
*c
;
2711 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2713 continue; /* nonexistent channel */
2714 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
2716 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2718 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
2719 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
2720 bombout(("Server got confused by X11 forwarding request"));
2723 logevent("X11 forwarding refused");
2725 logevent("X11 forwarding enabled");
2726 ssh_X11_fwd_enabled
= TRUE
;
2731 * Now allocate a pty for the session.
2734 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
2735 ssh2_pkt_adduint32(mainchan
->remoteid
); /* recipient channel */
2736 ssh2_pkt_addstring("pty-req");
2737 ssh2_pkt_addbool(1); /* want reply */
2738 ssh2_pkt_addstring(cfg
.termtype
);
2739 ssh2_pkt_adduint32(cols
);
2740 ssh2_pkt_adduint32(rows
);
2741 ssh2_pkt_adduint32(0); /* pixel width */
2742 ssh2_pkt_adduint32(0); /* pixel height */
2743 ssh2_pkt_addstring_start();
2744 ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */
2746 ssh_state
= SSH_STATE_INTERMED
;
2749 crWaitUntilV(ispkt
);
2750 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
2751 unsigned i
= ssh2_pkt_getuint32();
2752 struct ssh_channel
*c
;
2753 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2755 continue; /* nonexistent channel */
2756 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
2758 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2760 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
2761 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
2762 bombout(("Server got confused by pty request"));
2765 c_write("Server refused to allocate pty\r\n", 32);
2766 ssh_editing
= ssh_echoing
= 1;
2768 logevent("Allocated pty");
2771 ssh_editing
= ssh_echoing
= 1;
2775 * Start a shell or a remote command.
2777 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
2778 ssh2_pkt_adduint32(mainchan
->remoteid
); /* recipient channel */
2779 if (*cfg
.remote_cmd
) {
2780 ssh2_pkt_addstring("exec");
2781 ssh2_pkt_addbool(1); /* want reply */
2782 ssh2_pkt_addstring(cfg
.remote_cmd
);
2784 ssh2_pkt_addstring("shell");
2785 ssh2_pkt_addbool(1); /* want reply */
2789 crWaitUntilV(ispkt
);
2790 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
2791 unsigned i
= ssh2_pkt_getuint32();
2792 struct ssh_channel
*c
;
2793 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2795 continue; /* nonexistent channel */
2796 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
2798 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2799 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
2800 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
2801 bombout(("Server got confused by shell/command request"));
2804 bombout(("Server refused to start a shell/command"));
2807 logevent("Started a shell/command");
2810 ssh_state
= SSH_STATE_SESSION
;
2814 ssh_special(TS_EOF
);
2819 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
2822 static int try_send
;
2826 if (pktin
.type
== SSH2_MSG_CHANNEL_DATA
||
2827 pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
) {
2830 unsigned i
= ssh2_pkt_getuint32();
2831 struct ssh_channel
*c
;
2832 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2834 continue; /* nonexistent channel */
2835 if (pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
&&
2836 ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR
)
2837 continue; /* extended but not stderr */
2838 ssh2_pkt_getstring(&data
, &length
);
2841 case CHAN_MAINSESSION
:
2842 from_backend(pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
,
2846 x11_send(c
->u
.x11
.s
, data
, length
);
2850 * Enlarge the window again at the remote
2851 * side, just in case it ever runs down and
2852 * they fail to send us any more data.
2854 ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2855 ssh2_pkt_adduint32(c
->remoteid
);
2856 ssh2_pkt_adduint32(length
);
2859 } else if (pktin
.type
== SSH2_MSG_DISCONNECT
) {
2860 ssh_state
= SSH_STATE_CLOSED
;
2861 logevent("Received disconnect message");
2863 } else if (pktin
.type
== SSH2_MSG_CHANNEL_REQUEST
) {
2864 continue; /* exit status et al; ignore (FIXME?) */
2865 } else if (pktin
.type
== SSH2_MSG_CHANNEL_EOF
) {
2866 unsigned i
= ssh2_pkt_getuint32();
2867 struct ssh_channel
*c
;
2869 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2871 continue; /* nonexistent channel */
2873 if (c
->type
== CHAN_X11
) {
2875 * Remote EOF on an X11 channel means we should
2876 * wrap up and close the channel ourselves.
2878 x11_close(c
->u
.x11
.s
);
2881 } else if (pktin
.type
== SSH2_MSG_CHANNEL_CLOSE
) {
2882 unsigned i
= ssh2_pkt_getuint32();
2883 struct ssh_channel
*c
;
2886 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2888 continue; /* nonexistent channel */
2889 if (c
->closes
== 0) {
2890 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
2891 ssh2_pkt_adduint32(c
->remoteid
);
2894 /* Do pre-close processing on the channel. */
2896 case CHAN_MAINSESSION
:
2897 break; /* nothing to see here, move along */
2901 del234(ssh_channels
, c
);
2902 sfree(c
->v2
.outbuffer
);
2906 * See if that was the last channel left open.
2908 c
= first234(ssh_channels
, &e
);
2910 logevent("All channels closed. Disconnecting");
2911 ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
2912 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION
);
2913 ssh2_pkt_addstring("All open channels closed");
2914 ssh2_pkt_addstring("en"); /* language tag */
2916 ssh_state
= SSH_STATE_CLOSED
;
2919 continue; /* remote sends close; ignore (FIXME) */
2920 } else if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
2921 unsigned i
= ssh2_pkt_getuint32();
2922 struct ssh_channel
*c
;
2923 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2925 continue; /* nonexistent channel */
2926 mainchan
->v2
.remwindow
+= ssh2_pkt_getuint32();
2928 } else if (pktin
.type
== SSH2_MSG_CHANNEL_OPEN
) {
2932 struct ssh_channel
*c
;
2933 ssh2_pkt_getstring(&type
, &typelen
);
2934 c
= smalloc(sizeof(struct ssh_channel
));
2936 if (typelen
== 3 && !memcmp(type
, "x11", 3)) {
2937 if (!ssh_X11_fwd_enabled
)
2938 error
= "X11 forwarding is not enabled";
2939 else if ( x11_init(&c
->u
.x11
.s
, cfg
.x11_display
, c
) != NULL
) {
2940 error
= "Unable to open an X11 connection";
2945 error
= "Unsupported channel type requested";
2948 c
->remoteid
= ssh2_pkt_getuint32();
2950 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE
);
2951 ssh2_pkt_adduint32(c
->remoteid
);
2952 ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED
);
2953 ssh2_pkt_addstring(error
);
2954 ssh2_pkt_addstring("en"); /* language tag */
2958 struct ssh_channel
*d
;
2962 for (i
=1, d
= first234(ssh_channels
, &e
); d
;
2965 break; /* found a free number */
2970 c
->v2
.remwindow
= ssh2_pkt_getuint32();
2971 c
->v2
.remmaxpkt
= ssh2_pkt_getuint32();
2972 c
->v2
.outbuffer
= NULL
;
2973 c
->v2
.outbuflen
= c
->v2
.outbufsize
= 0;
2974 add234(ssh_channels
, c
);
2975 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
2976 ssh2_pkt_adduint32(c
->remoteid
);
2977 ssh2_pkt_adduint32(c
->localid
);
2978 ssh2_pkt_adduint32(0x8000UL
); /* our window size */
2979 ssh2_pkt_adduint32(0x4000UL
); /* our max pkt size */
2983 bombout(("Strange packet received: type %d", pktin
.type
));
2988 * We have spare data. Add it to the channel buffer.
2990 ssh2_add_channel_data(mainchan
, in
, inlen
);
2995 struct ssh_channel
*c
;
2997 * Try to send data on all channels if we can.
2999 for (c
= first234(ssh_channels
, &e
); c
; c
= next234(&e
))
3008 * Handle the top-level SSH2 protocol.
3010 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
)
3012 if (do_ssh2_transport(in
, inlen
, ispkt
) == 0)
3014 do_ssh2_authconn(in
, inlen
, ispkt
);
3018 * Called to set up the connection.
3020 * Returns an error message, or NULL on success.
3022 static char *ssh_init (char *host
, int port
, char **realhost
) {
3026 if(crypto_startup() == 0)
3027 return "Microsoft high encryption pack not installed!";
3034 p
= connect_to_host(host
, port
, realhost
);
3042 * Called to send data down the Telnet connection.
3044 static void ssh_send (char *buf
, int len
) {
3045 if (s
== NULL
|| ssh_protocol
== NULL
)
3048 ssh_protocol(buf
, len
, 0);
3052 * Called to set the size of the window from SSH's POV.
3054 static void ssh_size(void) {
3055 switch (ssh_state
) {
3056 case SSH_STATE_BEFORE_SIZE
:
3057 case SSH_STATE_PREPACKET
:
3058 case SSH_STATE_CLOSED
:
3059 break; /* do nothing */
3060 case SSH_STATE_INTERMED
:
3061 size_needed
= TRUE
; /* buffer for later */
3063 case SSH_STATE_SESSION
:
3065 if (ssh_version
== 1) {
3066 send_packet(SSH1_CMSG_WINDOW_SIZE
,
3067 PKT_INT
, rows
, PKT_INT
, cols
,
3068 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
3070 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
3071 ssh2_pkt_adduint32(mainchan
->remoteid
);
3072 ssh2_pkt_addstring("window-change");
3073 ssh2_pkt_addbool(0);
3074 ssh2_pkt_adduint32(cols
);
3075 ssh2_pkt_adduint32(rows
);
3076 ssh2_pkt_adduint32(0);
3077 ssh2_pkt_adduint32(0);
3086 * Send Telnet special codes. TS_EOF is useful for `plink', so you
3087 * can send an EOF and collect resulting output (e.g. `plink
3090 static void ssh_special (Telnet_Special code
) {
3091 if (code
== TS_EOF
) {
3092 if (ssh_state
!= SSH_STATE_SESSION
) {
3094 * Buffer the EOF in case we are pre-SESSION, so we can
3095 * send it as soon as we reach SESSION.
3101 if (ssh_version
== 1) {
3102 send_packet(SSH1_CMSG_EOF
, PKT_END
);
3104 ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF
);
3105 ssh2_pkt_adduint32(mainchan
->remoteid
);
3108 logevent("Sent EOF message");
3109 } else if (code
== TS_PING
) {
3110 if (ssh_state
== SSH_STATE_CLOSED
|| ssh_state
== SSH_STATE_PREPACKET
)
3112 if (ssh_version
== 1) {
3113 send_packet(SSH1_MSG_IGNORE
, PKT_STR
, "", PKT_END
);
3115 ssh2_pkt_init(SSH2_MSG_IGNORE
);
3116 ssh2_pkt_addstring_start();
3124 static Socket
ssh_socket(void) { return s
; }
3126 static int ssh_sendok(void) { return ssh_send_ok
; }
3128 static int ssh_ldisc(int option
) {
3129 if (option
== LD_ECHO
) return ssh_echoing
;
3130 if (option
== LD_EDIT
) return ssh_editing
;
3134 Backend ssh_backend
= {