long maxlen;
};
-static SHA_State exhash;
+static SHA_State exhash, exhashbase;
static Socket s = NULL;
static struct ssh_channel *mainchan; /* primary session channel */
static enum {
+ SSH_STATE_PREPACKET,
SSH_STATE_BEFORE_SIZE,
SSH_STATE_INTERMED,
SSH_STATE_SESSION,
SSH_STATE_CLOSED
-} ssh_state = SSH_STATE_BEFORE_SIZE;
+} ssh_state = SSH_STATE_PREPACKET;
-static int size_needed = FALSE;
+static int size_needed = FALSE, eof_needed = FALSE;
static struct Packet pktin = { 0, 0, NULL, NULL, 0 };
static struct Packet pktout = { 0, 0, NULL, NULL, 0 };
static void ssh1_protocol(unsigned char *in, int inlen, int ispkt);
static void ssh2_protocol(unsigned char *in, int inlen, int ispkt);
static void ssh_size(void);
+static void ssh_special (Telnet_Special);
static int (*s_rdpkt)(unsigned char **data, int *datalen);
* This is a v2 server. Begin v2 protocol.
*/
char *verstring = "SSH-2.0-PuTTY";
- SHA_Init(&exhash);
+ SHA_Init(&exhashbase);
/*
* Hash our version string and their version string.
*/
- sha_string(&exhash, verstring, strlen(verstring));
- sha_string(&exhash, vstring, strcspn(vstring, "\r\n"));
+ sha_string(&exhashbase, verstring, strlen(verstring));
+ sha_string(&exhashbase, vstring, strcspn(vstring, "\r\n"));
sprintf(vstring, "%s\n", verstring);
sprintf(vlog, "We claim version: %s", verstring);
logevent(vlog);
ssh_version = 1;
s_rdpkt = ssh1_rdpkt;
}
+ ssh_state = SSH_STATE_BEFORE_SIZE;
crFinish(0);
}
ssh_state = SSH_STATE_SESSION;
if (size_needed)
ssh_size();
+ if (eof_needed)
+ ssh_special(TS_EOF);
ssh_send_ok = 1;
ssh_channels = newtree234(ssh_channelcmp);
static unsigned char keyspace[40];
static const struct ssh_cipher *preferred_cipher;
static const struct ssh_compress *preferred_comp;
+ static int first_kex;
crBegin;
random_init();
+ first_kex = 1;
/*
* Set up the preferred cipher and compression.
ssh2_pkt_addbool(FALSE);
/* Reserved. */
ssh2_pkt_adduint32(0);
+
+ exhash = exhashbase;
sha_string(&exhash, pktout.data+5, pktout.length-5);
+
ssh2_pkt_send();
if (!ispkt) crWaitUntil(ispkt);
ssh2_mkkey(K, exchange_hash, 'F', keyspace); scmac->setsckey(keyspace);
/*
+ * If this is the first key exchange phase, we must pass the
+ * SSH2_MSG_NEWKEYS packet to the next layer, not because it
+ * wants to see it but because it will need time to initialise
+ * itself before it sees an actual packet. In subsequent key
+ * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
+ * it would only confuse the layer above.
+ */
+ if (!first_kex) {
+ crReturn(0);
+ }
+ first_kex = 0;
+
+ /*
* Now we're encrypting. Begin returning 1 to the protocol main
* function so that other things can run on top of the
* transport. If we ever see a KEXINIT, we must go back to the
ssh_state = SSH_STATE_SESSION;
if (size_needed)
ssh_size();
+ if (eof_needed)
+ ssh_special(TS_EOF);
/*
* Transfer data!
static void ssh_size(void) {
switch (ssh_state) {
case SSH_STATE_BEFORE_SIZE:
+ case SSH_STATE_PREPACKET:
case SSH_STATE_CLOSED:
break; /* do nothing */
case SSH_STATE_INTERMED:
ssh2_pkt_send();
}
}
+ break;
}
}
*/
static void ssh_special (Telnet_Special code) {
if (code == TS_EOF) {
+ if (ssh_state != SSH_STATE_SESSION) {
+ /*
+ * Buffer the EOF in case we are pre-SESSION, so we can
+ * send it as soon as we reach SESSION.
+ */
+ if (code == TS_EOF)
+ eof_needed = TRUE;
+ return;
+ }
if (ssh_version == 1) {
send_packet(SSH1_CMSG_EOF, PKT_END);
} else {
}
logevent("Sent EOF message");
} else if (code == TS_PING) {
+ if (ssh_state == SSH_STATE_CLOSED || ssh_state == SSH_STATE_PREPACKET)
+ return;
if (ssh_version == 1) {
send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);
} else {