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 : 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 SSH2_MSG_DISCONNECT 1 /* 0x1 */
72 #define SSH2_MSG_IGNORE 2 /* 0x2 */
73 #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
74 #define SSH2_MSG_DEBUG 4 /* 0x4 */
75 #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
76 #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
77 #define SSH2_MSG_KEXINIT 20 /* 0x14 */
78 #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
79 #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
80 #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
81 #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */
82 #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
83 #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
84 #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
85 #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
86 #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
87 #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
88 #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
89 #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
90 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
91 #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
92 #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
93 #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
94 #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
95 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
96 #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
97 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
98 #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
99 #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
100 #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
101 #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
102 #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
103 #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
104 #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
106 #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
107 #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
108 #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
109 #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
110 #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
111 #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
112 #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
113 #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
114 #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
115 #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
116 #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
118 #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
119 #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
120 #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
121 #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
123 #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
125 #define GET_32BIT(cp) \
126 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
127 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
128 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
129 ((unsigned long)(unsigned char)(cp)[3]))
131 #define PUT_32BIT(cp, value) { \
132 (cp)[0] = (unsigned char)((value) >> 24); \
133 (cp)[1] = (unsigned char)((value) >> 16); \
134 (cp)[2] = (unsigned char)((value) >> 8); \
135 (cp)[3] = (unsigned char)(value); }
137 enum { PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
, PKT_BIGNUM
};
139 /* Coroutine mechanics for the sillier bits of the code */
140 #define crBegin1 static int crLine = 0;
141 #define crBegin2 switch(crLine) { case 0:;
142 #define crBegin crBegin1; crBegin2;
143 #define crFinish(z) } crLine = 0; return (z)
144 #define crFinishV } crLine = 0; return
145 #define crReturn(z) \
147 crLine=__LINE__; return (z); case __LINE__:;\
151 crLine=__LINE__; return; case __LINE__:;\
153 #define crStop(z) do{ crLine = 0; return (z); }while(0)
154 #define crStopV do{ crLine = 0; return; }while(0)
155 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
156 #define crWaitUntilV(c) do { crReturnV; } while (!(c))
158 extern char *x11_init (Socket
*, char *, void *);
159 extern void x11_close (Socket
);
160 extern void x11_send (Socket
, char *, int);
161 extern void x11_invent_auth(char *, int, char *, int);
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.)
169 const static struct ssh2_ciphers
*ciphers
[] = {
175 const static struct ssh_kex
*kex_algs
[] = {
176 #ifdef DO_DIFFIE_HELLMAN_GEX
177 &ssh_diffiehellman_gex
,
179 &ssh_diffiehellman
};
181 const static struct ssh_signkey
*hostkey_algs
[] = { &ssh_rsa
, &ssh_dss
};
183 static void nullmac_key(unsigned char *key
) { }
184 static void nullmac_generate(unsigned char *blk
, int len
, unsigned long seq
) { }
185 static int nullmac_verify(unsigned char *blk
, int len
, unsigned long seq
) { return 1; }
186 const static struct ssh_mac ssh_mac_none
= {
187 nullmac_key
, nullmac_key
, nullmac_generate
, nullmac_verify
, "none", 0
189 const static struct ssh_mac
*macs
[] = {
190 &ssh_sha1
, &ssh_md5
, &ssh_mac_none
};
191 const static struct ssh_mac
*buggymacs
[] = {
192 &ssh_sha1_buggy
, &ssh_md5
, &ssh_mac_none
};
194 static void ssh_comp_none_init(void) { }
195 static int ssh_comp_none_block(unsigned char *block
, int len
,
196 unsigned char **outblock
, int *outlen
) {
199 const static struct ssh_compress ssh_comp_none
= {
201 ssh_comp_none_init
, ssh_comp_none_block
,
202 ssh_comp_none_init
, ssh_comp_none_block
204 extern const struct ssh_compress ssh_zlib
;
205 const static struct ssh_compress
*compressions
[] = {
206 &ssh_zlib
, &ssh_comp_none
};
208 enum { /* channel types */
215 * 2-3-4 tree storing channels.
218 unsigned remoteid
, localid
;
221 struct ssh2_data_channel
{
222 unsigned char *outbuffer
;
223 unsigned outbuflen
, outbufsize
;
224 unsigned remwindow
, remmaxpkt
;
227 struct ssh_agent_channel
{
228 unsigned char *message
;
229 unsigned char msglen
[4];
230 int lensofar
, totallen
;
232 struct ssh_x11_channel
{
247 static SHA_State exhash
, exhashbase
;
249 static Socket s
= NULL
;
251 static unsigned char session_key
[32];
252 static int ssh1_compressing
;
253 static int ssh_agentfwd_enabled
;
254 static int ssh_X11_fwd_enabled
;
255 static const struct ssh_cipher
*cipher
= NULL
;
256 static const struct ssh2_cipher
*cscipher
= NULL
;
257 static const struct ssh2_cipher
*sccipher
= NULL
;
258 static const struct ssh_mac
*csmac
= NULL
;
259 static const struct ssh_mac
*scmac
= NULL
;
260 static const struct ssh_compress
*cscomp
= NULL
;
261 static const struct ssh_compress
*sccomp
= NULL
;
262 static const struct ssh_kex
*kex
= NULL
;
263 static const struct ssh_signkey
*hostkey
= NULL
;
264 static unsigned char ssh2_session_id
[20];
265 int (*ssh_get_password
)(const char *prompt
, char *str
, int maxlen
) = NULL
;
267 static char *savedhost
;
268 static int savedport
;
269 static int ssh_send_ok
;
270 static int ssh_echoing
, ssh_editing
;
272 static tree234
*ssh_channels
; /* indexed by local id */
273 static struct ssh_channel
*mainchan
; /* primary session channel */
277 SSH_STATE_BEFORE_SIZE
,
281 } ssh_state
= SSH_STATE_PREPACKET
;
283 static int size_needed
= FALSE
, eof_needed
= FALSE
;
285 static struct Packet pktin
= { 0, 0, NULL
, NULL
, 0 };
286 static struct Packet pktout
= { 0, 0, NULL
, NULL
, 0 };
287 static unsigned char *deferred_send_data
= NULL
;
288 static int deferred_len
= 0, deferred_size
= 0;
290 static int ssh_version
;
291 static void (*ssh_protocol
)(unsigned char *in
, int inlen
, int ispkt
);
292 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
);
293 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
);
294 static void ssh_size(void);
295 static void ssh_special (Telnet_Special
);
296 static void ssh2_try_send(struct ssh_channel
*c
);
297 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
, int len
);
299 static int (*s_rdpkt
)(unsigned char **data
, int *datalen
);
301 static struct rdpkt1_state_tag
{
302 long len
, pad
, biglen
, to_read
;
303 unsigned long realcrc
, gotcrc
;
309 static struct rdpkt2_state_tag
{
310 long len
, pad
, payload
, packetlen
, maclen
;
313 unsigned long incoming_sequence
;
316 static int ssh_channelcmp(void *av
, void *bv
) {
317 struct ssh_channel
*a
= (struct ssh_channel
*)av
;
318 struct ssh_channel
*b
= (struct ssh_channel
*)bv
;
319 if (a
->localid
< b
->localid
) return -1;
320 if (a
->localid
> b
->localid
) return +1;
323 static int ssh_channelfind(void *av
, void *bv
) {
324 unsigned *a
= (unsigned *)av
;
325 struct ssh_channel
*b
= (struct ssh_channel
*)bv
;
326 if (*a
< b
->localid
) return -1;
327 if (*a
> b
->localid
) return +1;
331 static void c_write (char *buf
, int len
) {
332 if ((flags
& FLAG_STDERR
)) {
334 for (i
= 0; i
< len
; i
++)
336 fputc(buf
[i
], stderr
);
339 from_backend(1, buf
, len
);
342 static void c_write_str (char *buf
) {
343 c_write(buf
, strlen(buf
));
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 routines for putting an SSH-protocol `string' and
808 * `uint32' into a SHA state.
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
);
818 static void sha_uint32(SHA_State
*s
, unsigned i
) {
819 unsigned char intblk
[4];
820 PUT_32BIT(intblk
, i
);
821 SHA_Bytes(s
, intblk
, 4);
825 * SSH2 packet construction functions.
827 static void ssh2_pkt_ensure(int length
) {
828 if (pktout
.maxlen
< length
) {
829 pktout
.maxlen
= length
+ 256;
830 pktout
.data
= (pktout
.data
== NULL ?
smalloc(pktout
.maxlen
+APIEXTRA
) :
831 srealloc(pktout
.data
, pktout
.maxlen
+APIEXTRA
));
833 fatalbox("Out of memory");
836 static void ssh2_pkt_adddata(void *data
, int len
) {
837 pktout
.length
+= len
;
838 ssh2_pkt_ensure(pktout
.length
);
839 memcpy(pktout
.data
+pktout
.length
-len
, data
, len
);
841 static void ssh2_pkt_addbyte(unsigned char byte
) {
842 ssh2_pkt_adddata(&byte
, 1);
844 static void ssh2_pkt_init(int pkt_type
) {
846 ssh2_pkt_addbyte((unsigned char)pkt_type
);
848 static void ssh2_pkt_addbool(unsigned char value
) {
849 ssh2_pkt_adddata(&value
, 1);
851 static void ssh2_pkt_adduint32(unsigned long value
) {
854 ssh2_pkt_adddata(x
, 4);
856 static void ssh2_pkt_addstring_start(void) {
857 ssh2_pkt_adduint32(0);
858 pktout
.savedpos
= pktout
.length
;
860 static void ssh2_pkt_addstring_str(char *data
) {
861 ssh2_pkt_adddata(data
, strlen(data
));
862 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
863 pktout
.length
- pktout
.savedpos
);
865 static void ssh2_pkt_addstring_data(char *data
, int len
) {
866 ssh2_pkt_adddata(data
, len
);
867 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
868 pktout
.length
- pktout
.savedpos
);
870 static void ssh2_pkt_addstring(char *data
) {
871 ssh2_pkt_addstring_start();
872 ssh2_pkt_addstring_str(data
);
874 static char *ssh2_mpint_fmt(Bignum b
, int *len
) {
876 int i
, n
= (ssh1_bignum_bitcount(b
)+7)/8;
879 fatalbox("out of memory");
881 for (i
= 1; i
<= n
; i
++)
882 p
[i
] = bignum_byte(b
, n
-i
);
884 while (i
<= n
&& p
[i
] == 0 && (p
[i
+1] & 0x80) == 0)
886 memmove(p
, p
+i
, n
+1-i
);
890 static void ssh2_pkt_addmp(Bignum b
) {
893 p
= ssh2_mpint_fmt(b
, &len
);
894 ssh2_pkt_addstring_start();
895 ssh2_pkt_addstring_data(p
, len
);
900 * Construct an SSH2 final-form packet: compress it, encrypt it,
901 * put the MAC on it. Final packet, ready to be sent, is stored in
902 * pktout.data. Total length is returned.
904 static int ssh2_pkt_construct(void) {
905 int cipherblk
, maclen
, padding
, i
;
906 static unsigned long outgoing_sequence
= 0;
909 * Compress packet payload.
912 debug(("Pre-compression payload:\r\n"));
913 for (i
= 5; i
< pktout
.length
; i
++)
914 debug((" %02x", (unsigned char)pktout
.data
[i
]));
918 unsigned char *newpayload
;
920 if (cscomp
&& cscomp
->compress(pktout
.data
+5, pktout
.length
-5,
921 &newpayload
, &newlen
)) {
923 ssh2_pkt_adddata(newpayload
, newlen
);
929 * Add padding. At least four bytes, and must also bring total
930 * length (minus MAC) up to a multiple of the block size.
932 cipherblk
= cscipher ? cscipher
->blksize
: 8; /* block size */
933 cipherblk
= cipherblk
< 8 ?
8 : cipherblk
; /* or 8 if blksize < 8 */
935 padding
+= (cipherblk
- (pktout
.length
+ padding
) % cipherblk
) % cipherblk
;
936 maclen
= csmac ? csmac
->len
: 0;
937 ssh2_pkt_ensure(pktout
.length
+ padding
+ maclen
);
938 pktout
.data
[4] = padding
;
939 for (i
= 0; i
< padding
; i
++)
940 pktout
.data
[pktout
.length
+ i
] = random_byte();
941 PUT_32BIT(pktout
.data
, pktout
.length
+ padding
- 4);
943 csmac
->generate(pktout
.data
, pktout
.length
+ padding
,
945 outgoing_sequence
++; /* whether or not we MACed */
948 debug(("Sending packet len=%d\r\n", pktout
.length
+padding
));
949 for (i
= 0; i
< pktout
.length
+padding
; i
++)
950 debug((" %02x", (unsigned char)pktout
.data
[i
]));
955 cscipher
->encrypt(pktout
.data
, pktout
.length
+ padding
);
957 /* Ready-to-send packet starts at pktout.data. We return length. */
958 return pktout
.length
+ padding
+ maclen
;
962 * Construct and send an SSH2 packet immediately.
964 static void ssh2_pkt_send(void) {
965 int len
= ssh2_pkt_construct();
966 sk_write(s
, pktout
.data
, len
);
970 * Construct an SSH2 packet and add it to a deferred data block.
971 * Useful for sending multiple packets in a single sk_write() call,
972 * to prevent a traffic-analysing listener from being able to work
973 * out the length of any particular packet (such as the password
976 * Note that because SSH2 sequence-numbers its packets, this can
977 * NOT be used as an m4-style `defer' allowing packets to be
978 * constructed in one order and sent in another.
980 static void ssh2_pkt_defer(void) {
981 int len
= ssh2_pkt_construct();
982 if (deferred_len
+ len
> deferred_size
) {
983 deferred_size
= deferred_len
+ len
+ 128;
984 deferred_send_data
= srealloc(deferred_send_data
, deferred_size
);
986 memcpy(deferred_send_data
+deferred_len
, pktout
.data
, len
);
991 * Send the whole deferred data block constructed by
994 static void ssh2_pkt_defersend(void) {
995 sk_write(s
, deferred_send_data
, deferred_len
);
996 deferred_len
= deferred_size
= 0;
997 sfree(deferred_send_data
);
998 deferred_send_data
= NULL
;
1002 void bndebug(char *string
, Bignum b
) {
1005 p
= ssh2_mpint_fmt(b
, &len
);
1006 debug(("%s", string
));
1007 for (i
= 0; i
< len
; i
++)
1008 debug((" %02x", p
[i
]));
1014 static void sha_mpint(SHA_State
*s
, Bignum b
) {
1017 p
= ssh2_mpint_fmt(b
, &len
);
1018 sha_string(s
, p
, len
);
1023 * SSH2 packet decode functions.
1025 static unsigned long ssh2_pkt_getuint32(void) {
1026 unsigned long value
;
1027 if (pktin
.length
- pktin
.savedpos
< 4)
1028 return 0; /* arrgh, no way to decline (FIXME?) */
1029 value
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
1030 pktin
.savedpos
+= 4;
1033 static int ssh2_pkt_getbool(void) {
1034 unsigned long value
;
1035 if (pktin
.length
- pktin
.savedpos
< 1)
1036 return 0; /* arrgh, no way to decline (FIXME?) */
1037 value
= pktin
.data
[pktin
.savedpos
] != 0;
1041 static void ssh2_pkt_getstring(char **p
, int *length
) {
1043 if (pktin
.length
- pktin
.savedpos
< 4)
1045 *length
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
1046 pktin
.savedpos
+= 4;
1047 if (pktin
.length
- pktin
.savedpos
< *length
)
1049 *p
= pktin
.data
+pktin
.savedpos
;
1050 pktin
.savedpos
+= *length
;
1052 static Bignum
ssh2_pkt_getmp(void) {
1057 ssh2_pkt_getstring(&p
, &length
);
1061 bombout(("internal error: Can't handle negative mpints"));
1064 b
= bignum_from_bytes(p
, length
);
1068 static int do_ssh_init(unsigned char c
) {
1070 static char version
[10];
1071 static char vstring
[80];
1072 static char vlog
[sizeof(vstring
)+20];
1077 /* Search for the string "SSH-" in the input. */
1080 static const int transS
[] = { 1, 2, 2, 1 };
1081 static const int transH
[] = { 0, 0, 3, 0 };
1082 static const int transminus
[] = { 0, 0, 0, -1 };
1083 if (c
== 'S') i
= transS
[i
];
1084 else if (c
== 'H') i
= transH
[i
];
1085 else if (c
== '-') i
= transminus
[i
];
1089 crReturn(1); /* get another character */
1092 strcpy(vstring
, "SSH-");
1096 crReturn(1); /* get another char */
1097 if (vsp
< vstring
+sizeof(vstring
)-1)
1103 } else if (i
< sizeof(version
)-1)
1110 ssh_agentfwd_enabled
= FALSE
;
1111 rdpkt2_state
.incoming_sequence
= 0;
1114 sprintf(vlog
, "Server version: %s", vstring
);
1115 vlog
[strcspn(vlog
, "\r\n")] = '\0';
1119 * Server version "1.99" means we can choose whether we use v1
1120 * or v2 protocol. Choice is based on cfg.sshprot.
1122 if (ssh_versioncmp(version
, cfg
.sshprot
== 1 ?
"2.0" : "1.99") >= 0) {
1124 * This is a v2 server. Begin v2 protocol.
1126 char *verstring
= "SSH-2.0-PuTTY";
1127 SHA_Init(&exhashbase
);
1129 * Hash our version string and their version string.
1131 sha_string(&exhashbase
, verstring
, strlen(verstring
));
1132 sha_string(&exhashbase
, vstring
, strcspn(vstring
, "\r\n"));
1133 sprintf(vstring
, "%s\n", verstring
);
1134 sprintf(vlog
, "We claim version: %s", verstring
);
1136 logevent("Using SSH protocol version 2");
1137 sk_write(s
, vstring
, strlen(vstring
));
1138 ssh_protocol
= ssh2_protocol
;
1140 s_rdpkt
= ssh2_rdpkt
;
1143 * This is a v1 server. Begin v1 protocol.
1145 sprintf(vstring
, "SSH-%s-PuTTY\n",
1146 (ssh_versioncmp(version
, "1.5") <= 0 ? version
: "1.5"));
1147 sprintf(vlog
, "We claim version: %s", vstring
);
1148 vlog
[strcspn(vlog
, "\r\n")] = '\0';
1150 logevent("Using SSH protocol version 1");
1151 sk_write(s
, vstring
, strlen(vstring
));
1152 ssh_protocol
= ssh1_protocol
;
1154 s_rdpkt
= ssh1_rdpkt
;
1156 ssh_state
= SSH_STATE_BEFORE_SIZE
;
1161 static void ssh_gotdata(unsigned char *data
, int datalen
)
1166 * To begin with, feed the characters one by one to the
1167 * protocol initialisation / selection function do_ssh_init().
1168 * When that returns 0, we're done with the initial greeting
1169 * exchange and can move on to packet discipline.
1174 crReturnV
; /* more data please */
1175 ret
= do_ssh_init(*data
);
1182 * We emerge from that loop when the initial negotiation is
1183 * over and we have selected an s_rdpkt function. Now pass
1184 * everything to s_rdpkt, and then pass the resulting packets
1185 * to the proper protocol handler.
1190 while (datalen
> 0) {
1191 if ( s_rdpkt(&data
, &datalen
) == 0 ) {
1192 ssh_protocol(NULL
, 0, 1);
1193 if (ssh_state
== SSH_STATE_CLOSED
) {
1203 static int ssh_receive(Socket skt
, int urgent
, char *data
, int len
) {
1205 /* A socket error has occurred. */
1206 ssh_state
= SSH_STATE_CLOSED
;
1209 connection_fatal(data
);
1212 /* Connection has closed. */
1213 ssh_state
= SSH_STATE_CLOSED
;
1218 ssh_gotdata (data
, len
);
1219 if (ssh_state
== SSH_STATE_CLOSED
) {
1230 * Connect to specified host and port.
1231 * Returns an error message, or NULL on success.
1232 * Also places the canonical host name into `realhost'.
1234 static char *connect_to_host(char *host
, int port
, char **realhost
)
1243 savedhost
= smalloc(1+strlen(host
));
1245 fatalbox("Out of memory");
1246 strcpy(savedhost
, host
);
1249 port
= 22; /* default ssh port */
1262 addr
= sk_namelookup(host
, realhost
);
1263 if ( (err
= sk_addr_error(addr
)) )
1273 s
= sk_new(addr
, port
, 0, 1, ssh_receive
);
1274 if ( (err
= sk_socket_error(s
)) )
1278 sk_write(s
, "connect ", 8);
1279 sk_write(s
, FWhost
, strlen(FWhost
));
1282 sprintf(buf
, " %d\n", FWport
);
1283 sk_write(s
, buf
, strlen(buf
));
1291 * Handle the key exchange and user authentication phases.
1293 static int do_ssh1_login(unsigned char *in
, int inlen
, int ispkt
)
1296 unsigned char *rsabuf
, *keystr1
, *keystr2
;
1297 unsigned char cookie
[8];
1298 struct RSAKey servkey
, hostkey
;
1299 struct MD5Context md5c
;
1300 static unsigned long supported_ciphers_mask
, supported_auths_mask
;
1301 static int tried_publickey
;
1302 static unsigned char session_id
[16];
1304 static char username
[100];
1308 if (!ispkt
) crWaitUntil(ispkt
);
1310 if (pktin
.type
!= SSH1_SMSG_PUBLIC_KEY
) {
1311 bombout(("Public key packet not received"));
1315 logevent("Received public keys");
1317 memcpy(cookie
, pktin
.body
, 8);
1319 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
, 0);
1320 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
, 0);
1323 * Log the host key fingerprint.
1327 logevent("Host key fingerprint is:");
1328 strcpy(logmsg
, " ");
1329 hostkey
.comment
= NULL
;
1330 rsa_fingerprint(logmsg
+strlen(logmsg
), sizeof(logmsg
)-strlen(logmsg
),
1335 supported_ciphers_mask
= GET_32BIT(pktin
.body
+12+i
+j
);
1336 supported_auths_mask
= GET_32BIT(pktin
.body
+16+i
+j
);
1339 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
1340 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
1341 MD5Update(&md5c
, pktin
.body
, 8);
1342 MD5Final(session_id
, &md5c
);
1344 for (i
=0; i
<32; i
++)
1345 session_key
[i
] = random_byte();
1347 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
1349 rsabuf
= smalloc(len
);
1351 fatalbox("Out of memory");
1354 * Verify the host key.
1358 * First format the key into a string.
1360 int len
= rsastr_len(&hostkey
);
1361 char fingerprint
[100];
1362 char *keystr
= smalloc(len
);
1364 fatalbox("Out of memory");
1365 rsastr_fmt(keystr
, &hostkey
);
1366 rsa_fingerprint(fingerprint
, sizeof(fingerprint
), &hostkey
);
1367 verify_ssh_host_key(savedhost
, savedport
, "rsa", keystr
, fingerprint
);
1371 for (i
=0; i
<32; i
++) {
1372 rsabuf
[i
] = session_key
[i
];
1374 rsabuf
[i
] ^= session_id
[i
];
1377 if (hostkey
.bytes
> servkey
.bytes
) {
1378 rsaencrypt(rsabuf
, 32, &servkey
);
1379 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
1381 rsaencrypt(rsabuf
, 32, &hostkey
);
1382 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
1385 logevent("Encrypted session key");
1387 switch (cfg
.cipher
) {
1388 case CIPHER_BLOWFISH
: cipher_type
= SSH_CIPHER_BLOWFISH
; break;
1389 case CIPHER_DES
: cipher_type
= SSH_CIPHER_DES
; break;
1390 case CIPHER_3DES
: cipher_type
= SSH_CIPHER_3DES
; break;
1392 c_write_str("AES not supported in SSH1, falling back to 3DES\r\n");
1393 cipher_type
= SSH_CIPHER_3DES
;
1396 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
1397 c_write_str("Selected cipher not supported, falling back to 3DES\r\n");
1398 cipher_type
= SSH_CIPHER_3DES
;
1400 switch (cipher_type
) {
1401 case SSH_CIPHER_3DES
: logevent("Using 3DES encryption"); break;
1402 case SSH_CIPHER_DES
: logevent("Using single-DES encryption"); break;
1403 case SSH_CIPHER_BLOWFISH
: logevent("Using Blowfish encryption"); break;
1406 send_packet(SSH1_CMSG_SESSION_KEY
,
1407 PKT_CHAR
, cipher_type
,
1408 PKT_DATA
, cookie
, 8,
1409 PKT_CHAR
, (len
*8) >> 8, PKT_CHAR
, (len
*8) & 0xFF,
1410 PKT_DATA
, rsabuf
, len
,
1414 logevent("Trying to enable encryption...");
1418 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish_ssh1
:
1419 cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
1421 cipher
->sesskey(session_key
);
1425 if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1426 bombout(("Encryption not successfully enabled"));
1430 logevent("Successfully started encryption");
1436 if ((flags
& FLAG_INTERACTIVE
) && !*cfg
.username
) {
1437 c_write_str("login as: ");
1440 crWaitUntil(!ispkt
);
1441 while (inlen
--) switch (c
= *in
++) {
1448 c_write_str("\b \b");
1454 c_write_str("\b \b");
1463 if (((c
>= ' ' && c
<= '~') ||
1464 ((unsigned char)c
>= 160)) && pos
< 40) {
1465 username
[pos
++] = c
;
1471 c_write_str("\r\n");
1472 username
[strcspn(username
, "\n\r")] = '\0';
1474 strncpy(username
, cfg
.username
, 99);
1475 username
[99] = '\0';
1478 send_packet(SSH1_CMSG_USER
, PKT_STR
, username
, PKT_END
);
1480 char userlog
[22+sizeof(username
)];
1481 sprintf(userlog
, "Sent username \"%s\"", username
);
1483 if (flags
& FLAG_INTERACTIVE
&&
1484 (!((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)))) {
1485 strcat(userlog
, "\r\n");
1486 c_write_str(userlog
);
1493 tried_publickey
= 0;
1495 while (pktin
.type
== SSH1_SMSG_FAILURE
) {
1496 static char password
[100];
1497 static char prompt
[200];
1500 static int pwpkt_type
;
1502 * Show password prompt, having first obtained it via a TIS
1503 * or CryptoCard exchange if we're doing TIS or CryptoCard
1506 pwpkt_type
= SSH1_CMSG_AUTH_PASSWORD
;
1507 if (agent_exists()) {
1509 * Attempt RSA authentication using Pageant.
1511 static unsigned char request
[5], *response
, *p
;
1512 static int responselen
;
1513 static int i
, nkeys
;
1514 static int authed
= FALSE
;
1517 logevent("Pageant is running. Requesting keys.");
1519 /* Request the keys held by the agent. */
1520 PUT_32BIT(request
, 1);
1521 request
[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES
;
1522 agent_query(request
, 5, &r
, &responselen
);
1523 response
= (unsigned char *)r
;
1526 nkeys
= GET_32BIT(p
); p
+= 4;
1527 { char buf
[64]; sprintf(buf
, "Pageant has %d keys", nkeys
);
1529 for (i
= 0; i
< nkeys
; i
++) {
1530 static struct RSAKey key
;
1531 static Bignum challenge
;
1532 static char *commentp
;
1533 static int commentlen
;
1535 { char buf
[64]; sprintf(buf
, "Trying Pageant key #%d", i
);
1538 p
+= ssh1_read_bignum(p
, &key
.exponent
);
1539 p
+= ssh1_read_bignum(p
, &key
.modulus
);
1540 commentlen
= GET_32BIT(p
); p
+= 4;
1541 commentp
= p
; p
+= commentlen
;
1542 send_packet(SSH1_CMSG_AUTH_RSA
,
1543 PKT_BIGNUM
, key
.modulus
, PKT_END
);
1545 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
1546 logevent("Key refused");
1549 logevent("Received RSA challenge");
1550 ssh1_read_bignum(pktin
.body
, &challenge
);
1552 char *agentreq
, *q
, *ret
;
1554 len
= 1 + 4; /* message type, bit count */
1555 len
+= ssh1_bignum_length(key
.exponent
);
1556 len
+= ssh1_bignum_length(key
.modulus
);
1557 len
+= ssh1_bignum_length(challenge
);
1558 len
+= 16; /* session id */
1559 len
+= 4; /* response format */
1560 agentreq
= smalloc(4 + len
);
1561 PUT_32BIT(agentreq
, len
);
1563 *q
++ = SSH1_AGENTC_RSA_CHALLENGE
;
1564 PUT_32BIT(q
, ssh1_bignum_bitcount(key
.modulus
));
1566 q
+= ssh1_write_bignum(q
, key
.exponent
);
1567 q
+= ssh1_write_bignum(q
, key
.modulus
);
1568 q
+= ssh1_write_bignum(q
, challenge
);
1569 memcpy(q
, session_id
, 16); q
+= 16;
1570 PUT_32BIT(q
, 1); /* response format */
1571 agent_query(agentreq
, len
+4, &ret
, &retlen
);
1574 if (ret
[4] == SSH1_AGENT_RSA_RESPONSE
) {
1575 logevent("Sending Pageant's response");
1576 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1577 PKT_DATA
, ret
+5, 16, PKT_END
);
1580 if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
1581 logevent("Pageant's response accepted");
1582 if (flags
& FLAG_VERBOSE
) {
1583 c_write_str("Authenticated using RSA key \"");
1584 c_write(commentp
, commentlen
);
1585 c_write_str("\" from agent\r\n");
1589 logevent("Pageant's response not accepted");
1591 logevent("Pageant failed to answer challenge");
1595 logevent("No reply received from Pageant");
1598 freebn(key
.exponent
);
1599 freebn(key
.modulus
);
1608 if (*cfg
.keyfile
&& !tried_publickey
)
1609 pwpkt_type
= SSH1_CMSG_AUTH_RSA
;
1611 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1613 (supported_auths_mask
& (1<<SSH1_AUTH_TIS
))) {
1614 pwpkt_type
= SSH1_CMSG_AUTH_TIS_RESPONSE
;
1615 logevent("Requested TIS authentication");
1616 send_packet(SSH1_CMSG_AUTH_TIS
, PKT_END
);
1618 if (pktin
.type
!= SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
1619 logevent("TIS authentication declined");
1620 if (flags
& FLAG_INTERACTIVE
)
1621 c_write_str("TIS authentication refused.\r\n");
1623 int challengelen
= ((pktin
.body
[0] << 24) |
1624 (pktin
.body
[1] << 16) |
1625 (pktin
.body
[2] << 8) |
1627 logevent("Received TIS challenge");
1628 if (challengelen
> sizeof(prompt
)-1)
1629 challengelen
= sizeof(prompt
)-1; /* prevent overrun */
1630 memcpy(prompt
, pktin
.body
+4, challengelen
);
1631 prompt
[challengelen
] = '\0';
1634 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1636 (supported_auths_mask
& (1<<SSH1_AUTH_CCARD
))) {
1637 pwpkt_type
= SSH1_CMSG_AUTH_CCARD_RESPONSE
;
1638 logevent("Requested CryptoCard authentication");
1639 send_packet(SSH1_CMSG_AUTH_CCARD
, PKT_END
);
1641 if (pktin
.type
!= SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
1642 logevent("CryptoCard authentication declined");
1643 c_write_str("CryptoCard authentication refused.\r\n");
1645 int challengelen
= ((pktin
.body
[0] << 24) |
1646 (pktin
.body
[1] << 16) |
1647 (pktin
.body
[2] << 8) |
1649 logevent("Received CryptoCard challenge");
1650 if (challengelen
> sizeof(prompt
)-1)
1651 challengelen
= sizeof(prompt
)-1; /* prevent overrun */
1652 memcpy(prompt
, pktin
.body
+4, challengelen
);
1653 strncpy(prompt
+ challengelen
, "\r\nResponse : ",
1654 sizeof(prompt
)-challengelen
);
1655 prompt
[sizeof(prompt
)-1] = '\0';
1658 if (pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
) {
1659 sprintf(prompt
, "%.90s@%.90s's password: ",
1660 username
, savedhost
);
1662 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1663 char *comment
= NULL
;
1664 if (flags
& FLAG_VERBOSE
)
1665 c_write_str("Trying public key authentication.\r\n");
1666 if (!rsakey_encrypted(cfg
.keyfile
, &comment
)) {
1667 if (flags
& FLAG_VERBOSE
)
1668 c_write_str("No passphrase required.\r\n");
1671 sprintf(prompt
, "Passphrase for key \"%.100s\": ", comment
);
1675 if (ssh_get_password
) {
1676 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
1678 * get_password failed to get a password (for
1679 * example because one was supplied on the command
1680 * line which has already failed to work).
1683 logevent("No more passwords to try");
1684 ssh_state
= SSH_STATE_CLOSED
;
1688 c_write_str(prompt
);
1692 crWaitUntil(!ispkt
);
1693 while (inlen
--) switch (c
= *in
++) {
1710 if (((c
>= ' ' && c
<= '~') ||
1711 ((unsigned char)c
>= 160)) && pos
< sizeof(password
))
1712 password
[pos
++] = c
;
1716 c_write_str("\r\n");
1720 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1722 * Try public key authentication with the specified
1725 static struct RSAKey pubkey
;
1726 static Bignum challenge
, response
;
1728 static unsigned char buffer
[32];
1730 tried_publickey
= 1;
1731 i
= loadrsakey(cfg
.keyfile
, &pubkey
, password
);
1733 c_write_str("Couldn't load public key from ");
1734 c_write_str(cfg
.keyfile
);
1735 c_write_str(".\r\n");
1736 continue; /* go and try password */
1739 c_write_str("Wrong passphrase.\r\n");
1740 tried_publickey
= 0;
1741 continue; /* try again */
1745 * Send a public key attempt.
1747 send_packet(SSH1_CMSG_AUTH_RSA
,
1748 PKT_BIGNUM
, pubkey
.modulus
, PKT_END
);
1751 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1752 c_write_str("Server refused our public key.\r\n");
1753 continue; /* go and try password */
1755 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
1756 bombout(("Bizarre response to offer of public key"));
1759 ssh1_read_bignum(pktin
.body
, &challenge
);
1760 response
= rsadecrypt(challenge
, &pubkey
);
1761 freebn(pubkey
.private_exponent
); /* burn the evidence */
1763 for (i
= 0; i
< 32; i
++) {
1764 buffer
[i
] = bignum_byte(response
, 31-i
);
1768 MD5Update(&md5c
, buffer
, 32);
1769 MD5Update(&md5c
, session_id
, 16);
1770 MD5Final(buffer
, &md5c
);
1772 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1773 PKT_DATA
, buffer
, 16, PKT_END
);
1776 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1777 if (flags
& FLAG_VERBOSE
)
1778 c_write_str("Failed to authenticate with our public key.\r\n");
1779 continue; /* go and try password */
1780 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1781 bombout(("Bizarre response to RSA authentication response"));
1785 break; /* we're through! */
1787 send_packet(pwpkt_type
, PKT_STR
, password
, PKT_END
);
1789 logevent("Sent password");
1790 memset(password
, 0, strlen(password
));
1792 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1793 if (flags
& FLAG_VERBOSE
)
1794 c_write_str("Access denied\r\n");
1795 logevent("Authentication refused");
1796 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1797 logevent("Received disconnect request");
1798 ssh_state
= SSH_STATE_CLOSED
;
1800 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1801 bombout(("Strange packet received, type %d", pktin
.type
));
1806 logevent("Authentication successful");
1811 void sshfwd_close(struct ssh_channel
*c
) {
1813 if (ssh_version
== 1) {
1814 send_packet(SSH1_MSG_CHANNEL_CLOSE
, PKT_INT
, c
->remoteid
, PKT_END
);
1816 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
1817 ssh2_pkt_adduint32(c
->remoteid
);
1821 if (c
->type
== CHAN_X11
) {
1823 logevent("X11 connection terminated");
1828 void sshfwd_write(struct ssh_channel
*c
, char *buf
, int len
) {
1829 if (ssh_version
== 1) {
1830 send_packet(SSH1_MSG_CHANNEL_DATA
,
1831 PKT_INT
, c
->remoteid
,
1836 ssh2_add_channel_data(c
, buf
, len
);
1841 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
) {
1846 while (!do_ssh1_login(in
, inlen
, ispkt
)) {
1849 if (ssh_state
== SSH_STATE_CLOSED
)
1852 if (cfg
.agentfwd
&& agent_exists()) {
1853 logevent("Requesting agent forwarding");
1854 send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING
, PKT_END
);
1855 do { crReturnV
; } while (!ispkt
);
1856 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1857 bombout(("Protocol confusion"));
1859 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1860 logevent("Agent forwarding refused");
1862 logevent("Agent forwarding enabled");
1863 ssh_agentfwd_enabled
= TRUE
;
1867 if (cfg
.x11_forward
) {
1868 char proto
[20], data
[64];
1869 logevent("Requesting X11 forwarding");
1870 x11_invent_auth(proto
, sizeof(proto
), data
, sizeof(data
));
1871 send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING
,
1872 PKT_STR
, proto
, PKT_STR
, data
,
1875 do { crReturnV
; } while (!ispkt
);
1876 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1877 bombout(("Protocol confusion"));
1879 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1880 logevent("X11 forwarding refused");
1882 logevent("X11 forwarding enabled");
1883 ssh_X11_fwd_enabled
= TRUE
;
1888 send_packet(SSH1_CMSG_REQUEST_PTY
,
1889 PKT_STR
, cfg
.termtype
,
1890 PKT_INT
, rows
, PKT_INT
, cols
,
1891 PKT_INT
, 0, PKT_INT
, 0,
1894 ssh_state
= SSH_STATE_INTERMED
;
1895 do { crReturnV
; } while (!ispkt
);
1896 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1897 bombout(("Protocol confusion"));
1899 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1900 c_write_str("Server refused to allocate pty\r\n");
1901 ssh_editing
= ssh_echoing
= 1;
1903 logevent("Allocated pty");
1905 ssh_editing
= ssh_echoing
= 1;
1908 if (cfg
.compression
) {
1909 send_packet(SSH1_CMSG_REQUEST_COMPRESSION
, PKT_INT
, 6, PKT_END
);
1910 do { crReturnV
; } while (!ispkt
);
1911 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1912 bombout(("Protocol confusion"));
1914 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1915 c_write_str("Server refused to compress\r\n");
1917 logevent("Started compression");
1918 ssh1_compressing
= TRUE
;
1919 zlib_compress_init();
1920 zlib_decompress_init();
1923 if (*cfg
.remote_cmd
)
1924 send_packet(SSH1_CMSG_EXEC_CMD
, PKT_STR
, cfg
.remote_cmd
, PKT_END
);
1926 send_packet(SSH1_CMSG_EXEC_SHELL
, PKT_END
);
1927 logevent("Started session");
1929 ssh_state
= SSH_STATE_SESSION
;
1933 ssh_special(TS_EOF
);
1935 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
1937 ssh_channels
= newtree234(ssh_channelcmp
);
1941 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
1942 pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
1943 long len
= GET_32BIT(pktin
.body
);
1944 from_backend(pktin
.type
== SSH1_SMSG_STDERR_DATA
,
1946 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1947 ssh_state
= SSH_STATE_CLOSED
;
1948 logevent("Received disconnect request");
1950 } else if (pktin
.type
== SSH1_SMSG_X11_OPEN
) {
1951 /* Remote side is trying to open a channel to talk to our
1952 * X-Server. Give them back a local channel number. */
1954 struct ssh_channel
*c
, *d
;
1957 logevent("Received X11 connect request");
1958 /* Refuse if X11 forwarding is disabled. */
1959 if (!ssh_X11_fwd_enabled
) {
1960 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
1961 PKT_INT
, GET_32BIT(pktin
.body
),
1963 logevent("Rejected X11 connect request");
1965 c
= smalloc(sizeof(struct ssh_channel
));
1967 if ( x11_init(&c
->u
.x11
.s
, cfg
.x11_display
, c
) != NULL
) {
1968 logevent("opening X11 forward connection failed");
1970 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
1971 PKT_INT
, GET_32BIT(pktin
.body
),
1974 logevent("opening X11 forward connection succeeded");
1975 for (i
=1, d
= first234(ssh_channels
, &e
); d
; d
= next234(&e
)) {
1977 break; /* found a free number */
1980 c
->remoteid
= GET_32BIT(pktin
.body
);
1983 c
->type
= CHAN_X11
; /* identify channel type */
1984 add234(ssh_channels
, c
);
1985 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
1986 PKT_INT
, c
->remoteid
, PKT_INT
, c
->localid
,
1988 logevent("Opened X11 forward channel");
1991 } else if (pktin
.type
== SSH1_SMSG_AGENT_OPEN
) {
1992 /* Remote side is trying to open a channel to talk to our
1993 * agent. Give them back a local channel number. */
1995 struct ssh_channel
*c
;
1998 /* Refuse if agent forwarding is disabled. */
1999 if (!ssh_agentfwd_enabled
) {
2000 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE
,
2001 PKT_INT
, GET_32BIT(pktin
.body
),
2005 for (c
= first234(ssh_channels
, &e
); c
; c
= next234(&e
)) {
2007 break; /* found a free number */
2010 c
= smalloc(sizeof(struct ssh_channel
));
2011 c
->remoteid
= GET_32BIT(pktin
.body
);
2014 c
->type
= CHAN_AGENT
; /* identify channel type */
2015 c
->u
.a
.lensofar
= 0;
2016 add234(ssh_channels
, c
);
2017 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
2018 PKT_INT
, c
->remoteid
, PKT_INT
, c
->localid
,
2021 } else if (pktin
.type
== SSH1_MSG_CHANNEL_CLOSE
||
2022 pktin
.type
== SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION
) {
2023 /* Remote side closes a channel. */
2024 unsigned i
= GET_32BIT(pktin
.body
);
2025 struct ssh_channel
*c
;
2026 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2029 closetype
= (pktin
.type
== SSH1_MSG_CHANNEL_CLOSE ?
1 : 2);
2030 send_packet(pktin
.type
, PKT_INT
, c
->remoteid
, PKT_END
);
2031 if ((c
->closes
== 0) && (c
->type
== CHAN_X11
)) {
2032 logevent("X11 connection closed");
2033 assert(c
->u
.x11
.s
!= NULL
);
2034 x11_close(c
->u
.x11
.s
);
2037 c
->closes
|= closetype
;
2038 if (c
->closes
== 3) {
2039 del234(ssh_channels
, c
);
2043 } else if (pktin
.type
== SSH1_MSG_CHANNEL_DATA
) {
2044 /* Data sent down one of our channels. */
2045 int i
= GET_32BIT(pktin
.body
);
2046 int len
= GET_32BIT(pktin
.body
+4);
2047 unsigned char *p
= pktin
.body
+8;
2048 struct ssh_channel
*c
;
2049 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
2053 x11_send(c
->u
.x11
.s
, p
, len
);
2056 /* Data for an agent message. Buffer it. */
2058 if (c
->u
.a
.lensofar
< 4) {
2059 int l
= min(4 - c
->u
.a
.lensofar
, len
);
2060 memcpy(c
->u
.a
.msglen
+ c
->u
.a
.lensofar
, p
, l
);
2061 p
+= l
; len
-= l
; c
->u
.a
.lensofar
+= l
;
2063 if (c
->u
.a
.lensofar
== 4) {
2064 c
->u
.a
.totallen
= 4 + GET_32BIT(c
->u
.a
.msglen
);
2065 c
->u
.a
.message
= smalloc(c
->u
.a
.totallen
);
2066 memcpy(c
->u
.a
.message
, c
->u
.a
.msglen
, 4);
2068 if (c
->u
.a
.lensofar
>= 4 && len
> 0) {
2069 int l
= min(c
->u
.a
.totallen
- c
->u
.a
.lensofar
, len
);
2070 memcpy(c
->u
.a
.message
+ c
->u
.a
.lensofar
, p
, l
);
2071 p
+= l
; len
-= l
; c
->u
.a
.lensofar
+= l
;
2073 if (c
->u
.a
.lensofar
== c
->u
.a
.totallen
) {
2074 void *reply
, *sentreply
;
2076 agent_query(c
->u
.a
.message
, c
->u
.a
.totallen
,
2081 /* Fake SSH_AGENT_FAILURE. */
2082 sentreply
= "\0\0\0\1\5";
2085 send_packet(SSH1_MSG_CHANNEL_DATA
,
2086 PKT_INT
, c
->remoteid
,
2088 PKT_DATA
, sentreply
, replylen
,
2092 sfree(c
->u
.a
.message
);
2093 c
->u
.a
.lensofar
= 0;
2099 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
2100 /* may be from EXEC_SHELL on some servers */
2101 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
2102 /* may be from EXEC_SHELL on some servers
2103 * if no pty is available or in other odd cases. Ignore */
2104 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
2105 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
2107 bombout(("Strange packet received: type %d", pktin
.type
));
2112 int len
= min(inlen
, 512);
2113 send_packet(SSH1_CMSG_STDIN_DATA
,
2114 PKT_INT
, len
, PKT_DATA
, in
, len
, PKT_END
);
2125 * Utility routine for decoding comma-separated strings in KEXINIT.
2127 static int in_commasep_string(char *needle
, char *haystack
, int haylen
) {
2128 int needlen
= strlen(needle
);
2131 * Is it at the start of the string?
2133 if (haylen
>= needlen
&& /* haystack is long enough */
2134 !memcmp(needle
, haystack
, needlen
) && /* initial match */
2135 (haylen
== needlen
|| haystack
[needlen
] == ',')
2136 /* either , or EOS follows */
2140 * If not, search for the next comma and resume after that.
2141 * If no comma found, terminate.
2143 while (haylen
> 0 && *haystack
!= ',')
2144 haylen
--, haystack
++;
2147 haylen
--, haystack
++; /* skip over comma itself */
2152 * SSH2 key creation method.
2154 static void ssh2_mkkey(Bignum K
, char *H
, char *sessid
, char chr
, char *keyspace
) {
2156 /* First 20 bytes. */
2159 SHA_Bytes(&s
, H
, 20);
2160 SHA_Bytes(&s
, &chr
, 1);
2161 SHA_Bytes(&s
, sessid
, 20);
2162 SHA_Final(&s
, keyspace
);
2163 /* Next 20 bytes. */
2166 SHA_Bytes(&s
, H
, 20);
2167 SHA_Bytes(&s
, keyspace
, 20);
2168 SHA_Final(&s
, keyspace
+20);
2172 * Handle the SSH2 transport layer.
2174 static int do_ssh2_transport(unsigned char *in
, int inlen
, int ispkt
)
2176 static int i
, j
, len
, nbits
;
2178 static Bignum p
, g
, e
, f
, K
;
2179 static int kex_init_value
, kex_reply_value
;
2180 static const struct ssh_mac
**maclist
;
2182 static const struct ssh2_cipher
*cscipher_tobe
= NULL
;
2183 static const struct ssh2_cipher
*sccipher_tobe
= NULL
;
2184 static const struct ssh_mac
*csmac_tobe
= NULL
;
2185 static const struct ssh_mac
*scmac_tobe
= NULL
;
2186 static const struct ssh_compress
*cscomp_tobe
= NULL
;
2187 static const struct ssh_compress
*sccomp_tobe
= NULL
;
2188 static char *hostkeydata
, *sigdata
, *keystr
, *fingerprint
;
2189 static int hostkeylen
, siglen
;
2190 static void *hkey
; /* actual host key */
2191 static unsigned char exchange_hash
[20];
2192 static unsigned char keyspace
[40];
2193 static const struct ssh2_ciphers
*preferred_cipher
;
2194 static const struct ssh_compress
*preferred_comp
;
2195 static int first_kex
;
2202 * Set up the preferred cipher and compression.
2204 if (cfg
.cipher
== CIPHER_BLOWFISH
) {
2205 preferred_cipher
= &ssh2_blowfish
;
2206 } else if (cfg
.cipher
== CIPHER_DES
) {
2207 logevent("Single DES not supported in SSH2; using 3DES");
2208 preferred_cipher
= &ssh2_3des
;
2209 } else if (cfg
.cipher
== CIPHER_3DES
) {
2210 preferred_cipher
= &ssh2_3des
;
2211 } else if (cfg
.cipher
== CIPHER_AES
) {
2212 preferred_cipher
= &ssh2_aes
;
2214 /* Shouldn't happen, but we do want to initialise to _something_. */
2215 preferred_cipher
= &ssh2_3des
;
2217 if (cfg
.compression
)
2218 preferred_comp
= &ssh_zlib
;
2220 preferred_comp
= &ssh_comp_none
;
2223 * Be prepared to work around the buggy MAC problem.
2226 maclist
= buggymacs
, nmacs
= lenof(buggymacs
);
2228 maclist
= macs
, nmacs
= lenof(macs
);
2232 * Construct and send our key exchange packet.
2234 ssh2_pkt_init(SSH2_MSG_KEXINIT
);
2235 for (i
= 0; i
< 16; i
++)
2236 ssh2_pkt_addbyte((unsigned char)random_byte());
2237 /* List key exchange algorithms. */
2238 ssh2_pkt_addstring_start();
2239 for (i
= 0; i
< lenof(kex_algs
); i
++) {
2240 ssh2_pkt_addstring_str(kex_algs
[i
]->name
);
2241 if (i
< lenof(kex_algs
)-1)
2242 ssh2_pkt_addstring_str(",");
2244 /* List server host key algorithms. */
2245 ssh2_pkt_addstring_start();
2246 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
2247 ssh2_pkt_addstring_str(hostkey_algs
[i
]->name
);
2248 if (i
< lenof(hostkey_algs
)-1)
2249 ssh2_pkt_addstring_str(",");
2251 /* List client->server encryption algorithms. */
2252 ssh2_pkt_addstring_start();
2253 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2254 const struct ssh2_ciphers
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2255 for (j
= 0; j
< c
->nciphers
; j
++) {
2256 ssh2_pkt_addstring_str(c
->list
[j
]->name
);
2257 if (i
< lenof(ciphers
) || j
< c
->nciphers
-1)
2258 ssh2_pkt_addstring_str(",");
2261 /* List server->client encryption algorithms. */
2262 ssh2_pkt_addstring_start();
2263 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2264 const struct ssh2_ciphers
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2265 for (j
= 0; j
< c
->nciphers
; j
++) {
2266 ssh2_pkt_addstring_str(c
->list
[j
]->name
);
2267 if (i
< lenof(ciphers
) || j
< c
->nciphers
-1)
2268 ssh2_pkt_addstring_str(",");
2271 /* List client->server MAC algorithms. */
2272 ssh2_pkt_addstring_start();
2273 for (i
= 0; i
< nmacs
; i
++) {
2274 ssh2_pkt_addstring_str(maclist
[i
]->name
);
2276 ssh2_pkt_addstring_str(",");
2278 /* List server->client MAC algorithms. */
2279 ssh2_pkt_addstring_start();
2280 for (i
= 0; i
< nmacs
; i
++) {
2281 ssh2_pkt_addstring_str(maclist
[i
]->name
);
2283 ssh2_pkt_addstring_str(",");
2285 /* List client->server compression algorithms. */
2286 ssh2_pkt_addstring_start();
2287 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2288 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2289 ssh2_pkt_addstring_str(c
->name
);
2290 if (i
< lenof(compressions
))
2291 ssh2_pkt_addstring_str(",");
2293 /* List server->client compression algorithms. */
2294 ssh2_pkt_addstring_start();
2295 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2296 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2297 ssh2_pkt_addstring_str(c
->name
);
2298 if (i
< lenof(compressions
))
2299 ssh2_pkt_addstring_str(",");
2301 /* List client->server languages. Empty list. */
2302 ssh2_pkt_addstring_start();
2303 /* List server->client languages. Empty list. */
2304 ssh2_pkt_addstring_start();
2305 /* First KEX packet does _not_ follow, because we're not that brave. */
2306 ssh2_pkt_addbool(FALSE
);
2308 ssh2_pkt_adduint32(0);
2310 exhash
= exhashbase
;
2311 sha_string(&exhash
, pktout
.data
+5, pktout
.length
-5);
2315 if (!ispkt
) crWaitUntil(ispkt
);
2316 sha_string(&exhash
, pktin
.data
+5, pktin
.length
-5);
2319 * Now examine the other side's KEXINIT to see what we're up
2322 if (pktin
.type
!= SSH2_MSG_KEXINIT
) {
2323 bombout(("expected key exchange packet from server"));
2326 kex
= NULL
; hostkey
= NULL
; cscipher_tobe
= NULL
; sccipher_tobe
= NULL
;
2327 csmac_tobe
= NULL
; scmac_tobe
= NULL
; cscomp_tobe
= NULL
; sccomp_tobe
= NULL
;
2328 pktin
.savedpos
+= 16; /* skip garbage cookie */
2329 ssh2_pkt_getstring(&str
, &len
); /* key exchange algorithms */
2330 for (i
= 0; i
< lenof(kex_algs
); i
++) {
2331 if (in_commasep_string(kex_algs
[i
]->name
, str
, len
)) {
2336 ssh2_pkt_getstring(&str
, &len
); /* host key algorithms */
2337 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
2338 if (in_commasep_string(hostkey_algs
[i
]->name
, str
, len
)) {
2339 hostkey
= hostkey_algs
[i
];
2343 ssh2_pkt_getstring(&str
, &len
); /* client->server cipher */
2344 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2345 const struct ssh2_ciphers
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2346 for (j
= 0; j
< c
->nciphers
; j
++) {
2347 if (in_commasep_string(c
->list
[j
]->name
, str
, len
)) {
2348 cscipher_tobe
= c
->list
[j
];
2355 ssh2_pkt_getstring(&str
, &len
); /* server->client cipher */
2356 for (i
= 0; i
< lenof(ciphers
)+1; i
++) {
2357 const struct ssh2_ciphers
*c
= i
==0 ? preferred_cipher
: ciphers
[i
-1];
2358 for (j
= 0; j
< c
->nciphers
; j
++) {
2359 if (in_commasep_string(c
->list
[j
]->name
, str
, len
)) {
2360 sccipher_tobe
= c
->list
[j
];
2367 ssh2_pkt_getstring(&str
, &len
); /* client->server mac */
2368 for (i
= 0; i
< nmacs
; i
++) {
2369 if (in_commasep_string(maclist
[i
]->name
, str
, len
)) {
2370 csmac_tobe
= maclist
[i
];
2374 ssh2_pkt_getstring(&str
, &len
); /* server->client mac */
2375 for (i
= 0; i
< nmacs
; i
++) {
2376 if (in_commasep_string(maclist
[i
]->name
, str
, len
)) {
2377 scmac_tobe
= maclist
[i
];
2381 ssh2_pkt_getstring(&str
, &len
); /* client->server compression */
2382 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2383 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2384 if (in_commasep_string(c
->name
, str
, len
)) {
2389 ssh2_pkt_getstring(&str
, &len
); /* server->client compression */
2390 for (i
= 0; i
< lenof(compressions
)+1; i
++) {
2391 const struct ssh_compress
*c
= i
==0 ? preferred_comp
: compressions
[i
-1];
2392 if (in_commasep_string(c
->name
, str
, len
)) {
2399 * If we're doing Diffie-Hellman group exchange, start by
2400 * requesting a group.
2402 if (kex
== &ssh_diffiehellman_gex
) {
2405 logevent("Doing Diffie-Hellman group exchange");
2407 * Work out number of bits. We start with the maximum key
2408 * length of either cipher...
2410 csbits
= cscipher_tobe
->keylen
;
2411 scbits
= sccipher_tobe
->keylen
;
2412 nbits
= (csbits
> scbits ? csbits
: scbits
);
2413 /* The keys only have 160-bit entropy, since they're based on
2414 * a SHA-1 hash. So cap the key size at 160 bits. */
2415 if (nbits
> 160) nbits
= 160;
2417 * ... and then work out how big a DH group we will need to
2418 * allow that much data.
2420 nbits
= 512 << ((nbits
-1) / 64);
2421 ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST
);
2422 ssh2_pkt_adduint32(nbits
);
2426 if (pktin
.type
!= SSH2_MSG_KEX_DH_GEX_GROUP
) {
2427 bombout(("expected key exchange group packet from server"));
2430 p
= ssh2_pkt_getmp();
2431 g
= ssh2_pkt_getmp();
2432 dh_setup_group(p
, g
);
2433 kex_init_value
= SSH2_MSG_KEX_DH_GEX_INIT
;
2434 kex_reply_value
= SSH2_MSG_KEX_DH_GEX_REPLY
;
2437 kex_init_value
= SSH2_MSG_KEXDH_INIT
;
2438 kex_reply_value
= SSH2_MSG_KEXDH_REPLY
;
2441 logevent("Doing Diffie-Hellman key exchange");
2443 * Now generate and send e for Diffie-Hellman.
2446 ssh2_pkt_init(kex_init_value
);
2451 if (pktin
.type
!= kex_reply_value
) {
2452 bombout(("expected key exchange reply packet from server"));
2455 ssh2_pkt_getstring(&hostkeydata
, &hostkeylen
);
2456 f
= ssh2_pkt_getmp();
2457 ssh2_pkt_getstring(&sigdata
, &siglen
);
2461 sha_string(&exhash
, hostkeydata
, hostkeylen
);
2462 if (kex
== &ssh_diffiehellman_gex
) {
2463 sha_uint32(&exhash
, nbits
);
2464 sha_mpint(&exhash
, p
);
2465 sha_mpint(&exhash
, g
);
2467 sha_mpint(&exhash
, e
);
2468 sha_mpint(&exhash
, f
);
2469 sha_mpint(&exhash
, K
);
2470 SHA_Final(&exhash
, exchange_hash
);
2475 debug(("Exchange hash is:\r\n"));
2476 for (i
= 0; i
< 20; i
++)
2477 debug((" %02x", exchange_hash
[i
]));
2481 hkey
= hostkey
->newkey(hostkeydata
, hostkeylen
);
2482 if (!hostkey
->verifysig(hkey
, sigdata
, siglen
, exchange_hash
, 20)) {
2483 bombout(("Server failed host key check"));
2488 * Expect SSH2_MSG_NEWKEYS from server.
2491 if (pktin
.type
!= SSH2_MSG_NEWKEYS
) {
2492 bombout(("expected new-keys packet from server"));
2497 * Authenticate remote host: verify host key. (We've already
2498 * checked the signature of the exchange hash.)
2500 keystr
= hostkey
->fmtkey(hkey
);
2501 fingerprint
= hostkey
->fingerprint(hkey
);
2502 verify_ssh_host_key(savedhost
, savedport
, hostkey
->keytype
,
2503 keystr
, fingerprint
);
2504 if (first_kex
) { /* don't bother logging this in rekeys */
2505 logevent("Host key fingerprint is:");
2506 logevent(fingerprint
);
2510 hostkey
->freekey(hkey
);
2513 * Send SSH2_MSG_NEWKEYS.
2515 ssh2_pkt_init(SSH2_MSG_NEWKEYS
);
2519 * Create and initialise session keys.
2521 cscipher
= cscipher_tobe
;
2522 sccipher
= sccipher_tobe
;
2525 cscomp
= cscomp_tobe
;
2526 sccomp
= sccomp_tobe
;
2527 cscomp
->compress_init();
2528 sccomp
->decompress_init();
2530 * Set IVs after keys. Here we use the exchange hash from the
2531 * _first_ key exchange.
2534 memcpy(ssh2_session_id
, exchange_hash
, sizeof(exchange_hash
));
2535 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'C', keyspace
);
2536 cscipher
->setcskey(keyspace
);
2537 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'D', keyspace
);
2538 sccipher
->setsckey(keyspace
);
2539 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'A', keyspace
);
2540 cscipher
->setcsiv(keyspace
);
2541 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'B', keyspace
);
2542 sccipher
->setsciv(keyspace
);
2543 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'E', keyspace
);
2544 csmac
->setcskey(keyspace
);
2545 ssh2_mkkey(K
, exchange_hash
, ssh2_session_id
, 'F', keyspace
);
2546 scmac
->setsckey(keyspace
);
2549 * If this is the first key exchange phase, we must pass the
2550 * SSH2_MSG_NEWKEYS packet to the next layer, not because it
2551 * wants to see it but because it will need time to initialise
2552 * itself before it sees an actual packet. In subsequent key
2553 * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
2554 * it would only confuse the layer above.
2562 * Now we're encrypting. Begin returning 1 to the protocol main
2563 * function so that other things can run on top of the
2564 * transport. If we ever see a KEXINIT, we must go back to the
2567 while (!(ispkt
&& pktin
.type
== SSH2_MSG_KEXINIT
)) {
2570 logevent("Server initiated key re-exchange");
2571 goto begin_key_exchange
;
2577 * Add data to an SSH2 channel output buffer.
2579 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
, int len
) {
2580 if (c
->v2
.outbufsize
<
2581 c
->v2
.outbuflen
+ len
) {
2583 c
->v2
.outbuflen
+ len
+ 1024;
2584 c
->v2
.outbuffer
= srealloc(c
->v2
.outbuffer
,
2587 memcpy(c
->v2
.outbuffer
+ c
->v2
.outbuflen
,
2589 c
->v2
.outbuflen
+= len
;
2593 * Attempt to send data on an SSH2 channel.
2595 static void ssh2_try_send(struct ssh_channel
*c
) {
2596 while (c
->v2
.remwindow
> 0 &&
2597 c
->v2
.outbuflen
> 0) {
2598 unsigned len
= c
->v2
.remwindow
;
2599 if (len
> c
->v2
.outbuflen
)
2600 len
= c
->v2
.outbuflen
;
2601 if (len
> c
->v2
.remmaxpkt
)
2602 len
= c
->v2
.remmaxpkt
;
2603 ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA
);
2604 ssh2_pkt_adduint32(c
->remoteid
);
2605 ssh2_pkt_addstring_start();
2606 ssh2_pkt_addstring_data(c
->v2
.outbuffer
, len
);
2608 c
->v2
.outbuflen
-= len
;
2609 memmove(c
->v2
.outbuffer
, c
->v2
.outbuffer
+len
,
2611 c
->v2
.remwindow
-= len
;
2616 * Handle the SSH2 userauth and connection layers.
2618 static void do_ssh2_authconn(unsigned char *in
, int inlen
, int ispkt
)
2620 static unsigned long remote_winsize
;
2621 static unsigned long remote_maxpkt
;
2623 AUTH_INVALID
, AUTH_PUBLICKEY_AGENT
, AUTH_PUBLICKEY_FILE
, AUTH_PASSWORD
2627 AUTH_TYPE_PUBLICKEY
,
2628 AUTH_TYPE_PUBLICKEY_OFFER_LOUD
,
2629 AUTH_TYPE_PUBLICKEY_OFFER_QUIET
,
2632 static int gotit
, need_pw
, can_pubkey
, can_passwd
;
2633 static int tried_pubkey_config
, tried_agent
;
2634 static int we_are_in
;
2635 static char username
[100];
2636 static char pwprompt
[200];
2637 static char password
[100];
2642 * Request userauth protocol, and await a response to it.
2644 ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST
);
2645 ssh2_pkt_addstring("ssh-userauth");
2647 crWaitUntilV(ispkt
);
2648 if (pktin
.type
!= SSH2_MSG_SERVICE_ACCEPT
) {
2649 bombout(("Server refused user authentication protocol"));
2654 * We repeat this whole loop, including the username prompt,
2655 * until we manage a successful authentication. If the user
2656 * types the wrong _password_, they are sent back to the
2657 * beginning to try another username. (If they specify a
2658 * username in the config, they are never asked, even if they
2659 * do give a wrong password.)
2661 * I think this best serves the needs of
2663 * - the people who have no configuration, no keys, and just
2664 * want to try repeated (username,password) pairs until they
2665 * type both correctly
2667 * - people who have keys and configuration but occasionally
2668 * need to fall back to passwords
2670 * - people with a key held in Pageant, who might not have
2671 * logged in to a particular machine before; so they want to
2672 * type a username, and then _either_ their key will be
2673 * accepted, _or_ they will type a password. If they mistype
2674 * the username they will want to be able to get back and
2685 if ((flags
& FLAG_INTERACTIVE
) && !*cfg
.username
) {
2686 c_write_str("login as: ");
2689 crWaitUntilV(!ispkt
);
2690 while (inlen
--) switch (c
= *in
++) {
2697 c_write_str("\b \b");
2703 c_write_str("\b \b");
2712 if (((c
>= ' ' && c
<= '~') ||
2713 ((unsigned char)c
>= 160)) && pos
< 40) {
2714 username
[pos
++] = c
;
2720 c_write_str("\r\n");
2721 username
[strcspn(username
, "\n\r")] = '\0';
2724 strncpy(username
, cfg
.username
, 99);
2725 username
[99] = '\0';
2726 if ((flags
& FLAG_VERBOSE
) || (flags
& FLAG_INTERACTIVE
)) {
2727 sprintf(stuff
, "Using username \"%s\".\r\n", username
);
2733 * Send an authentication request using method "none": (a)
2734 * just in case it succeeds, and (b) so that we know what
2735 * authentication methods we can usefully try next.
2737 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
2738 ssh2_pkt_addstring(username
);
2739 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2740 ssh2_pkt_addstring("none"); /* method */
2742 type
= AUTH_TYPE_NONE
;
2746 tried_pubkey_config
= FALSE
;
2747 tried_agent
= FALSE
;
2751 * Wait for the result of the last authentication request.
2754 crWaitUntilV(ispkt
);
2755 while (pktin
.type
== SSH2_MSG_USERAUTH_BANNER
) {
2756 /* FIXME: should support this */
2757 crWaitUntilV(ispkt
);
2759 if (pktin
.type
== SSH2_MSG_USERAUTH_SUCCESS
) {
2760 logevent("Access granted");
2765 if (pktin
.type
!= SSH2_MSG_USERAUTH_FAILURE
) {
2766 bombout(("Strange packet received during authentication: type %d",
2773 * OK, we're now sitting on a USERAUTH_FAILURE message, so
2774 * we can look at the string in it and know what we can
2775 * helpfully try next.
2780 ssh2_pkt_getstring(&methods
, &methlen
);
2781 if (!ssh2_pkt_getbool()) {
2783 * We have received an unequivocal Access
2784 * Denied. This can translate to a variety of
2787 * - if we'd just tried "none" authentication,
2788 * it's not worth printing anything at all
2790 * - if we'd just tried a public key _offer_,
2791 * the message should be "Server refused our
2792 * key" (or no message at all if the key
2793 * came from Pageant)
2795 * - if we'd just tried anything else, the
2796 * message really should be "Access denied".
2798 * Additionally, if we'd just tried password
2799 * authentication, we should break out of this
2800 * whole loop so as to go back to the username
2803 if (type
== AUTH_TYPE_NONE
) {
2805 } else if (type
== AUTH_TYPE_PUBLICKEY_OFFER_LOUD
||
2806 type
== AUTH_TYPE_PUBLICKEY_OFFER_QUIET
) {
2807 if (type
== AUTH_TYPE_PUBLICKEY_OFFER_LOUD
)
2808 c_write_str("Server refused our key\r\n");
2809 logevent("Server refused public key");
2811 c_write_str("Access denied\r\n");
2812 logevent("Access denied");
2813 if (type
== AUTH_TYPE_PASSWORD
) {
2819 c_write_str("Further authentication required\r\n");
2820 logevent("Further authentication required");
2823 can_pubkey
= in_commasep_string("publickey", methods
, methlen
);
2824 can_passwd
= in_commasep_string("password", methods
, methlen
);
2829 if (!method
&& can_pubkey
&& agent_exists
&& !tried_agent
) {
2831 * Attempt public-key authentication using Pageant.
2833 static unsigned char request
[5], *response
, *p
;
2834 static int responselen
;
2835 static int i
, nkeys
;
2836 static int authed
= FALSE
;
2841 logevent("Pageant is running. Requesting keys.");
2843 /* Request the keys held by the agent. */
2844 PUT_32BIT(request
, 1);
2845 request
[4] = SSH2_AGENTC_REQUEST_IDENTITIES
;
2846 agent_query(request
, 5, &r
, &responselen
);
2847 response
= (unsigned char *)r
;
2850 nkeys
= GET_32BIT(p
); p
+= 4;
2851 { char buf
[64]; sprintf(buf
, "Pageant has %d keys", nkeys
);
2853 for (i
= 0; i
< nkeys
; i
++) {
2854 static char *pkblob
, *alg
, *commentp
;
2855 static int pklen
, alglen
, commentlen
;
2856 static int siglen
, retlen
, len
;
2857 static char *q
, *agentreq
, *ret
;
2859 { char buf
[64]; sprintf(buf
, "Trying Pageant key #%d", i
);
2861 pklen
= GET_32BIT(p
); p
+= 4;
2862 pkblob
= p
; p
+= pklen
;
2863 alglen
= GET_32BIT(pkblob
);
2865 commentlen
= GET_32BIT(p
); p
+= 4;
2866 commentp
= p
; p
+= commentlen
;
2867 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
2868 ssh2_pkt_addstring(username
);
2869 ssh2_pkt_addstring("ssh-connection");/* service requested */
2870 ssh2_pkt_addstring("publickey");/* method */
2871 ssh2_pkt_addbool(FALSE
); /* no signature included */
2872 ssh2_pkt_addstring_start();
2873 ssh2_pkt_addstring_data(alg
, alglen
);
2874 ssh2_pkt_addstring_start();
2875 ssh2_pkt_addstring_data(pkblob
, pklen
);
2878 crWaitUntilV(ispkt
);
2879 if (pktin
.type
!= SSH2_MSG_USERAUTH_PK_OK
) {
2880 logevent("Key refused");
2884 c_write_str("Authenticating with public key \"");
2885 c_write(commentp
, commentlen
);
2886 c_write_str("\" from agent\r\n");
2889 * Server is willing to accept the key.
2890 * Construct a SIGN_REQUEST.
2892 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
2893 ssh2_pkt_addstring(username
);
2894 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2895 ssh2_pkt_addstring("publickey"); /* method */
2896 ssh2_pkt_addbool(TRUE
);
2897 ssh2_pkt_addstring_start();
2898 ssh2_pkt_addstring_data(alg
, alglen
);
2899 ssh2_pkt_addstring_start();
2900 ssh2_pkt_addstring_data(pkblob
, pklen
);
2902 siglen
= pktout
.length
- 5 + 4 + 20;
2903 len
= 1; /* message type */
2904 len
+= 4 + pklen
; /* key blob */
2905 len
+= 4 + siglen
; /* data to sign */
2906 len
+= 4; /* flags */
2907 agentreq
= smalloc(4 + len
);
2908 PUT_32BIT(agentreq
, len
);
2910 *q
++ = SSH2_AGENTC_SIGN_REQUEST
;
2911 PUT_32BIT(q
, pklen
); q
+= 4;
2912 memcpy(q
, pkblob
, pklen
); q
+= pklen
;
2913 PUT_32BIT(q
, siglen
); q
+= 4;
2914 /* Now the data to be signed... */
2915 PUT_32BIT(q
, 20); q
+= 4;
2916 memcpy(q
, ssh2_session_id
, 20); q
+= 20;
2917 memcpy(q
, pktout
.data
+5, pktout
.length
-5);
2918 q
+= pktout
.length
-5;
2919 /* And finally the (zero) flags word. */
2921 agent_query(agentreq
, len
+4, &ret
, &retlen
);
2924 if (ret
[4] == SSH2_AGENT_SIGN_RESPONSE
) {
2925 logevent("Sending Pageant's response");
2926 ssh2_pkt_addstring_start();
2927 ssh2_pkt_addstring_data(ret
+9, GET_32BIT(ret
+5));
2932 logevent("Pageant failed to answer challenge");
2942 if (!method
&& can_pubkey
&& *cfg
.keyfile
&& !tried_pubkey_config
) {
2943 unsigned char *pub_blob
;
2944 char *algorithm
, *comment
;
2947 tried_pubkey_config
= TRUE
;
2950 * Try the public key supplied in the configuration.
2952 * First, offer the public blob to see if the server is
2953 * willing to accept it.
2955 pub_blob
= ssh2_userkey_loadpub(cfg
.keyfile
, &algorithm
,
2958 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
2959 ssh2_pkt_addstring(username
);
2960 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2961 ssh2_pkt_addstring("publickey");/* method */
2962 ssh2_pkt_addbool(FALSE
); /* no signature included */
2963 ssh2_pkt_addstring(algorithm
);
2964 ssh2_pkt_addstring_start();
2965 ssh2_pkt_addstring_data(pub_blob
, pub_blob_len
);
2967 logevent("Offered public key"); /* FIXME */
2969 crWaitUntilV(ispkt
);
2970 if (pktin
.type
!= SSH2_MSG_USERAUTH_PK_OK
) {
2972 type
= AUTH_TYPE_PUBLICKEY_OFFER_LOUD
;
2973 continue; /* key refused; give up on it */
2976 logevent("Offer of public key accepted");
2978 * Actually attempt a serious authentication using
2981 if (ssh2_userkey_encrypted(cfg
.keyfile
, &comment
)) {
2982 sprintf(pwprompt
, "Passphrase for key \"%.100s\": ", comment
);
2987 c_write_str("Authenticating with public key \"");
2988 c_write_str(comment
);
2989 c_write_str("\"\r\n");
2990 method
= AUTH_PUBLICKEY_FILE
;
2994 if (!method
&& can_passwd
) {
2995 method
= AUTH_PASSWORD
;
2996 sprintf(pwprompt
, "%.90s@%.90s's password: ", username
, savedhost
);
3001 if (ssh_get_password
) {
3002 if (!ssh_get_password(pwprompt
, password
, sizeof(password
))) {
3004 * get_password failed to get a password (for
3005 * example because one was supplied on the command
3006 * line which has already failed to work).
3009 logevent("No more passwords to try");
3010 ssh_state
= SSH_STATE_CLOSED
;
3017 c_write_str(pwprompt
);
3022 crWaitUntilV(!ispkt
);
3023 while (inlen
--) switch (c
= *in
++) {
3040 if (((c
>= ' ' && c
<= '~') ||
3041 ((unsigned char)c
>= 160)) && pos
< 40)
3042 password
[pos
++] = c
;
3046 c_write_str("\r\n");
3050 if (method
== AUTH_PUBLICKEY_FILE
) {
3052 * We have our passphrase. Now try the actual authentication.
3054 struct ssh2_userkey
*key
;
3056 key
= ssh2_load_userkey(cfg
.keyfile
, password
);
3057 if (key
== SSH2_WRONG_PASSPHRASE
|| key
== NULL
) {
3058 if (key
== SSH2_WRONG_PASSPHRASE
) {
3059 c_write_str("Wrong passphrase\r\n");
3060 tried_pubkey_config
= FALSE
;
3062 c_write_str("Unable to load private key\r\n");
3063 tried_pubkey_config
= TRUE
;
3065 /* Send a spurious AUTH_NONE to return to the top. */
3066 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
3067 ssh2_pkt_addstring(username
);
3068 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3069 ssh2_pkt_addstring("none"); /* method */
3071 type
= AUTH_TYPE_NONE
;
3073 unsigned char *blob
, *sigdata
;
3074 int blob_len
, sigdata_len
;
3077 * We have loaded the private key and the server
3078 * has announced that it's willing to accept it.
3079 * Hallelujah. Generate a signature and send it.
3081 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
3082 ssh2_pkt_addstring(username
);
3083 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3084 ssh2_pkt_addstring("publickey"); /* method */
3085 ssh2_pkt_addbool(TRUE
);
3086 ssh2_pkt_addstring(key
->alg
->name
);
3087 blob
= key
->alg
->public_blob(key
->data
, &blob_len
);
3088 ssh2_pkt_addstring_start();
3089 ssh2_pkt_addstring_data(blob
, blob_len
);
3093 * The data to be signed is:
3097 * followed by everything so far placed in the
3100 sigdata_len
= pktout
.length
- 5 + 4 + 20;
3101 sigdata
= smalloc(sigdata_len
);
3102 PUT_32BIT(sigdata
, 20);
3103 memcpy(sigdata
+4, ssh2_session_id
, 20);
3104 memcpy(sigdata
+24, pktout
.data
+5, pktout
.length
-5);
3105 blob
= key
->alg
->sign(key
->data
, sigdata
, sigdata_len
, &blob_len
);
3106 ssh2_pkt_addstring_start();
3107 ssh2_pkt_addstring_data(blob
, blob_len
);
3112 type
= AUTH_TYPE_PUBLICKEY
;
3114 } else if (method
== AUTH_PASSWORD
) {
3116 * We send the password packet lumped tightly together with
3117 * an SSH_MSG_IGNORE packet. The IGNORE packet contains a
3118 * string long enough to make the total length of the two
3119 * packets constant. This should ensure that a passive
3120 * listener doing traffic analyis can't work out the length
3123 * For this to work, we need an assumption about the
3124 * maximum length of the password packet. I think 256 is
3125 * pretty conservative. Anyone using a password longer than
3126 * that probably doesn't have much to worry about from
3127 * people who find out how long their password is!
3129 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
3130 ssh2_pkt_addstring(username
);
3131 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3132 ssh2_pkt_addstring("password");
3133 ssh2_pkt_addbool(FALSE
);
3134 ssh2_pkt_addstring(password
);
3137 * We'll include a string that's an exact multiple of the
3138 * cipher block size. If the cipher is NULL for some
3139 * reason, we don't do this trick at all because we gain
3144 ssh2_pkt_init(SSH2_MSG_IGNORE
);
3145 ssh2_pkt_addstring_start();
3146 for (i
= deferred_len
; i
<= 256; i
+= cscipher
->blksize
) {
3147 for (j
= 0; j
< cscipher
->blksize
; j
++) {
3148 char c
= (char)random_byte();
3149 ssh2_pkt_addstring_data(&c
, 1);
3154 ssh2_pkt_defersend();
3155 logevent("Sent password");
3156 type
= AUTH_TYPE_PASSWORD
;
3158 c_write_str("No supported authentication methods left to try!\r\n");
3159 logevent("No supported authentications offered. Disconnecting");
3160 ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
3161 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION
);
3162 ssh2_pkt_addstring("No supported authentication methods available");
3163 ssh2_pkt_addstring("en"); /* language tag */
3165 ssh_state
= SSH_STATE_CLOSED
;
3169 } while (!we_are_in
);
3172 * Now we're authenticated for the connection protocol. The
3173 * connection protocol will automatically have started at this
3174 * point; there's no need to send SERVICE_REQUEST.
3178 * So now create a channel with a session in it.
3180 mainchan
= smalloc(sizeof(struct ssh_channel
));
3181 mainchan
->localid
= 100; /* as good as any */
3182 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN
);
3183 ssh2_pkt_addstring("session");
3184 ssh2_pkt_adduint32(mainchan
->localid
);
3185 ssh2_pkt_adduint32(0x8000UL
); /* our window size */
3186 ssh2_pkt_adduint32(0x4000UL
); /* our max pkt size */
3188 crWaitUntilV(ispkt
);
3189 if (pktin
.type
!= SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
) {
3190 bombout(("Server refused to open a session"));
3192 /* FIXME: error data comes back in FAILURE packet */
3194 if (ssh2_pkt_getuint32() != mainchan
->localid
) {
3195 bombout(("Server's channel confirmation cited wrong channel"));
3198 mainchan
->remoteid
= ssh2_pkt_getuint32();
3199 mainchan
->type
= CHAN_MAINSESSION
;
3200 mainchan
->closes
= 0;
3201 mainchan
->v2
.remwindow
= ssh2_pkt_getuint32();
3202 mainchan
->v2
.remmaxpkt
= ssh2_pkt_getuint32();
3203 mainchan
->v2
.outbuffer
= NULL
;
3204 mainchan
->v2
.outbuflen
= mainchan
->v2
.outbufsize
= 0;
3205 ssh_channels
= newtree234(ssh_channelcmp
);
3206 add234(ssh_channels
, mainchan
);
3207 logevent("Opened channel for session");
3210 * Potentially enable X11 forwarding.
3212 if (cfg
.x11_forward
) {
3213 char proto
[20], data
[64];
3214 logevent("Requesting X11 forwarding");
3215 x11_invent_auth(proto
, sizeof(proto
), data
, sizeof(data
));
3216 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
3217 ssh2_pkt_adduint32(mainchan
->remoteid
);
3218 ssh2_pkt_addstring("x11-req");
3219 ssh2_pkt_addbool(1); /* want reply */
3220 ssh2_pkt_addbool(0); /* many connections */
3221 ssh2_pkt_addstring(proto
);
3222 ssh2_pkt_addstring(data
);
3223 ssh2_pkt_adduint32(0); /* screen number */
3227 crWaitUntilV(ispkt
);
3228 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
3229 unsigned i
= ssh2_pkt_getuint32();
3230 struct ssh_channel
*c
;
3231 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3233 continue; /* nonexistent channel */
3234 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
3236 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
3238 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
3239 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
3240 bombout(("Server got confused by X11 forwarding request"));
3243 logevent("X11 forwarding refused");
3245 logevent("X11 forwarding enabled");
3246 ssh_X11_fwd_enabled
= TRUE
;
3251 * Now allocate a pty for the session.
3254 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
3255 ssh2_pkt_adduint32(mainchan
->remoteid
); /* recipient channel */
3256 ssh2_pkt_addstring("pty-req");
3257 ssh2_pkt_addbool(1); /* want reply */
3258 ssh2_pkt_addstring(cfg
.termtype
);
3259 ssh2_pkt_adduint32(cols
);
3260 ssh2_pkt_adduint32(rows
);
3261 ssh2_pkt_adduint32(0); /* pixel width */
3262 ssh2_pkt_adduint32(0); /* pixel height */
3263 ssh2_pkt_addstring_start();
3264 ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */
3266 ssh_state
= SSH_STATE_INTERMED
;
3269 crWaitUntilV(ispkt
);
3270 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
3271 unsigned i
= ssh2_pkt_getuint32();
3272 struct ssh_channel
*c
;
3273 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3275 continue; /* nonexistent channel */
3276 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
3278 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
3280 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
3281 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
3282 bombout(("Server got confused by pty request"));
3285 c_write_str("Server refused to allocate pty\r\n");
3286 ssh_editing
= ssh_echoing
= 1;
3288 logevent("Allocated pty");
3291 ssh_editing
= ssh_echoing
= 1;
3295 * Start a shell or a remote command.
3297 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
3298 ssh2_pkt_adduint32(mainchan
->remoteid
); /* recipient channel */
3299 if (cfg
.ssh_subsys
) {
3300 ssh2_pkt_addstring("subsystem");
3301 ssh2_pkt_addbool(1); /* want reply */
3302 ssh2_pkt_addstring(cfg
.remote_cmd
);
3303 } else if (*cfg
.remote_cmd
) {
3304 ssh2_pkt_addstring("exec");
3305 ssh2_pkt_addbool(1); /* want reply */
3306 ssh2_pkt_addstring(cfg
.remote_cmd
);
3308 ssh2_pkt_addstring("shell");
3309 ssh2_pkt_addbool(1); /* want reply */
3313 crWaitUntilV(ispkt
);
3314 if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
3315 unsigned i
= ssh2_pkt_getuint32();
3316 struct ssh_channel
*c
;
3317 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3319 continue; /* nonexistent channel */
3320 c
->v2
.remwindow
+= ssh2_pkt_getuint32();
3322 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
3323 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
3324 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
3325 bombout(("Server got confused by shell/command request"));
3328 bombout(("Server refused to start a shell/command"));
3331 logevent("Started a shell/command");
3334 ssh_state
= SSH_STATE_SESSION
;
3338 ssh_special(TS_EOF
);
3343 ldisc_send(NULL
, 0); /* cause ldisc to notice changes */
3346 static int try_send
;
3350 if (pktin
.type
== SSH2_MSG_CHANNEL_DATA
||
3351 pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
) {
3354 unsigned i
= ssh2_pkt_getuint32();
3355 struct ssh_channel
*c
;
3356 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3358 continue; /* nonexistent channel */
3359 if (pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
&&
3360 ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR
)
3361 continue; /* extended but not stderr */
3362 ssh2_pkt_getstring(&data
, &length
);
3365 case CHAN_MAINSESSION
:
3366 from_backend(pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
,
3370 x11_send(c
->u
.x11
.s
, data
, length
);
3374 * Enlarge the window again at the remote
3375 * side, just in case it ever runs down and
3376 * they fail to send us any more data.
3378 ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
3379 ssh2_pkt_adduint32(c
->remoteid
);
3380 ssh2_pkt_adduint32(length
);
3383 } else if (pktin
.type
== SSH2_MSG_DISCONNECT
) {
3384 ssh_state
= SSH_STATE_CLOSED
;
3385 logevent("Received disconnect message");
3387 } else if (pktin
.type
== SSH2_MSG_CHANNEL_REQUEST
) {
3388 continue; /* exit status et al; ignore (FIXME?) */
3389 } else if (pktin
.type
== SSH2_MSG_CHANNEL_EOF
) {
3390 unsigned i
= ssh2_pkt_getuint32();
3391 struct ssh_channel
*c
;
3393 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3395 continue; /* nonexistent channel */
3397 if (c
->type
== CHAN_X11
) {
3399 * Remote EOF on an X11 channel means we should
3400 * wrap up and close the channel ourselves.
3402 x11_close(c
->u
.x11
.s
);
3405 } else if (pktin
.type
== SSH2_MSG_CHANNEL_CLOSE
) {
3406 unsigned i
= ssh2_pkt_getuint32();
3407 struct ssh_channel
*c
;
3410 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3412 continue; /* nonexistent channel */
3413 if (c
->closes
== 0) {
3414 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
3415 ssh2_pkt_adduint32(c
->remoteid
);
3418 /* Do pre-close processing on the channel. */
3420 case CHAN_MAINSESSION
:
3421 break; /* nothing to see here, move along */
3425 del234(ssh_channels
, c
);
3426 sfree(c
->v2
.outbuffer
);
3430 * See if that was the last channel left open.
3432 c
= first234(ssh_channels
, &e
);
3434 logevent("All channels closed. Disconnecting");
3435 ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
3436 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION
);
3437 ssh2_pkt_addstring("All open channels closed");
3438 ssh2_pkt_addstring("en"); /* language tag */
3440 ssh_state
= SSH_STATE_CLOSED
;
3443 continue; /* remote sends close; ignore (FIXME) */
3444 } else if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
3445 unsigned i
= ssh2_pkt_getuint32();
3446 struct ssh_channel
*c
;
3447 c
= find234(ssh_channels
, &i
, ssh_channelfind
);
3449 continue; /* nonexistent channel */
3450 mainchan
->v2
.remwindow
+= ssh2_pkt_getuint32();
3452 } else if (pktin
.type
== SSH2_MSG_CHANNEL_OPEN
) {
3456 struct ssh_channel
*c
;
3457 ssh2_pkt_getstring(&type
, &typelen
);
3458 c
= smalloc(sizeof(struct ssh_channel
));
3460 if (typelen
== 3 && !memcmp(type
, "x11", 3)) {
3461 if (!ssh_X11_fwd_enabled
)
3462 error
= "X11 forwarding is not enabled";
3463 else if ( x11_init(&c
->u
.x11
.s
, cfg
.x11_display
, c
) != NULL
) {
3464 error
= "Unable to open an X11 connection";
3469 error
= "Unsupported channel type requested";
3472 c
->remoteid
= ssh2_pkt_getuint32();
3474 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE
);
3475 ssh2_pkt_adduint32(c
->remoteid
);
3476 ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED
);
3477 ssh2_pkt_addstring(error
);
3478 ssh2_pkt_addstring("en"); /* language tag */
3482 struct ssh_channel
*d
;
3486 for (i
=1, d
= first234(ssh_channels
, &e
); d
;
3489 break; /* found a free number */
3494 c
->v2
.remwindow
= ssh2_pkt_getuint32();
3495 c
->v2
.remmaxpkt
= ssh2_pkt_getuint32();
3496 c
->v2
.outbuffer
= NULL
;
3497 c
->v2
.outbuflen
= c
->v2
.outbufsize
= 0;
3498 add234(ssh_channels
, c
);
3499 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
3500 ssh2_pkt_adduint32(c
->remoteid
);
3501 ssh2_pkt_adduint32(c
->localid
);
3502 ssh2_pkt_adduint32(0x8000UL
); /* our window size */
3503 ssh2_pkt_adduint32(0x4000UL
); /* our max pkt size */
3507 bombout(("Strange packet received: type %d", pktin
.type
));
3512 * We have spare data. Add it to the channel buffer.
3514 ssh2_add_channel_data(mainchan
, in
, inlen
);
3519 struct ssh_channel
*c
;
3521 * Try to send data on all channels if we can.
3523 for (c
= first234(ssh_channels
, &e
); c
; c
= next234(&e
))
3532 * Handle the top-level SSH2 protocol.
3534 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
)
3536 if (do_ssh2_transport(in
, inlen
, ispkt
) == 0)
3538 do_ssh2_authconn(in
, inlen
, ispkt
);
3542 * Called to set up the connection.
3544 * Returns an error message, or NULL on success.
3546 static char *ssh_init (char *host
, int port
, char **realhost
) {
3550 if(crypto_startup() == 0)
3551 return "Microsoft high encryption pack not installed!";
3558 p
= connect_to_host(host
, port
, realhost
);
3566 * Called to send data down the Telnet connection.
3568 static void ssh_send (char *buf
, int len
) {
3569 if (s
== NULL
|| ssh_protocol
== NULL
)
3572 ssh_protocol(buf
, len
, 0);
3576 * Called to set the size of the window from SSH's POV.
3578 static void ssh_size(void) {
3579 switch (ssh_state
) {
3580 case SSH_STATE_BEFORE_SIZE
:
3581 case SSH_STATE_PREPACKET
:
3582 case SSH_STATE_CLOSED
:
3583 break; /* do nothing */
3584 case SSH_STATE_INTERMED
:
3585 size_needed
= TRUE
; /* buffer for later */
3587 case SSH_STATE_SESSION
:
3589 if (ssh_version
== 1) {
3590 send_packet(SSH1_CMSG_WINDOW_SIZE
,
3591 PKT_INT
, rows
, PKT_INT
, cols
,
3592 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
3594 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
3595 ssh2_pkt_adduint32(mainchan
->remoteid
);
3596 ssh2_pkt_addstring("window-change");
3597 ssh2_pkt_addbool(0);
3598 ssh2_pkt_adduint32(cols
);
3599 ssh2_pkt_adduint32(rows
);
3600 ssh2_pkt_adduint32(0);
3601 ssh2_pkt_adduint32(0);
3610 * Send Telnet special codes. TS_EOF is useful for `plink', so you
3611 * can send an EOF and collect resulting output (e.g. `plink
3614 static void ssh_special (Telnet_Special code
) {
3615 if (code
== TS_EOF
) {
3616 if (ssh_state
!= SSH_STATE_SESSION
) {
3618 * Buffer the EOF in case we are pre-SESSION, so we can
3619 * send it as soon as we reach SESSION.
3625 if (ssh_version
== 1) {
3626 send_packet(SSH1_CMSG_EOF
, PKT_END
);
3628 ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF
);
3629 ssh2_pkt_adduint32(mainchan
->remoteid
);
3632 logevent("Sent EOF message");
3633 } else if (code
== TS_PING
) {
3634 if (ssh_state
== SSH_STATE_CLOSED
|| ssh_state
== SSH_STATE_PREPACKET
)
3636 if (ssh_version
== 1) {
3637 send_packet(SSH1_MSG_IGNORE
, PKT_STR
, "", PKT_END
);
3639 ssh2_pkt_init(SSH2_MSG_IGNORE
);
3640 ssh2_pkt_addstring_start();
3648 static Socket
ssh_socket(void) { return s
; }
3650 static int ssh_sendok(void) { return ssh_send_ok
; }
3652 static int ssh_ldisc(int option
) {
3653 if (option
== LD_ECHO
) return ssh_echoing
;
3654 if (option
== LD_EDIT
) return ssh_editing
;
3658 Backend ssh_backend
= {