+ * SSH2 packet construction functions.
+ */
+void ssh2_pkt_adddata(void *data, int len) {
+ pktout.length += len;
+ if (pktout.maxlen < pktout.length) {
+ pktout.maxlen = pktout.length + 256;
+ pktout.data = (pktout.data == NULL ? malloc(pktout.maxlen+APIEXTRA) :
+ realloc(pktout.data, pktout.maxlen+APIEXTRA));
+ if (!pktout.data)
+ fatalbox("Out of memory");
+ }
+ memcpy(pktout.data+pktout.length-len, data, len);
+}
+void ssh2_pkt_addbyte(unsigned char byte) {
+ ssh2_pkt_adddata(&byte, 1);
+}
+void ssh2_pkt_init(int pkt_type) {
+ pktout.length = 5;
+ ssh2_pkt_addbyte((unsigned char)pkt_type);
+}
+void ssh2_pkt_addbool(unsigned char value) {
+ ssh2_pkt_adddata(&value, 1);
+}
+void ssh2_pkt_adduint32(unsigned long value) {
+ unsigned char x[4];
+ PUT_32BIT(x, value);
+ ssh2_pkt_adddata(x, 4);
+}
+void ssh2_pkt_addstring_start(void) {
+ ssh2_pkt_adduint32(0);
+ pktout.savedpos = pktout.length;
+}
+void ssh2_pkt_addstring_str(char *data) {
+ ssh2_pkt_adddata(data, strlen(data));
+ PUT_32BIT(pktout.data + pktout.savedpos - 4,
+ pktout.length - pktout.savedpos);
+}
+void ssh2_pkt_addstring_data(char *data, int len) {
+ ssh2_pkt_adddata(data, len);
+ PUT_32BIT(pktout.data + pktout.savedpos - 4,
+ pktout.length - pktout.savedpos);
+}
+void ssh2_pkt_addstring(char *data) {
+ ssh2_pkt_addstring_start();
+ ssh2_pkt_addstring_str(data);
+}
+char *ssh2_mpint_fmt(Bignum b, int *len) {
+ unsigned char *p;
+ int i, n = b[0];
+ p = malloc(n * 2 + 1);
+ if (!p)
+ fatalbox("out of memory");
+ p[0] = 0;
+ for (i = 0; i < n; i++) {
+ p[i*2+1] = (b[n-i] >> 8) & 0xFF;
+ p[i*2+2] = (b[n-i] ) & 0xFF;
+ }
+ i = 0;
+ while (p[i] == 0 && (p[i+1] & 0x80) == 0)
+ i++;
+ memmove(p, p+i, n*2+1-i);
+ *len = n*2+1-i;
+ return p;
+}
+void ssh2_pkt_addmp(Bignum b) {
+ unsigned char *p;
+ int len;
+ p = ssh2_mpint_fmt(b, &len);
+ ssh2_pkt_addstring_start();
+ ssh2_pkt_addstring_data(p, len);
+ free(p);
+}
+void ssh2_pkt_send(void) {
+ int cipherblk, maclen, padding, i;
+ unsigned long outgoing_sequence = 0;
+
+ /*
+ * Add padding. At least four bytes, and must also bring total
+ * length (minus MAC) up to a multiple of the block size.
+ */
+ cipherblk = cipher ? cipher->blksize : 8; /* block size */
+ cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
+ padding = 4;
+ padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk;
+ pktout.data[4] = padding;
+ for (i = 0; i < padding; i++)
+ pktout.data[pktout.length + i] = random_byte();
+ PUT_32BIT(pktout.data, pktout.length + padding - 4);
+ if (csmac)
+ csmac->generate(pktout.data, outgoing_sequence++,
+ pktout.length + padding);
+ if (cscipher)
+ cscipher->encrypt(pktout.data, pktout.length + padding);
+ maclen = csmac ? csmac->len : 0;
+#if 0
+ debug(("Sending packet len=%d\r\n", pktout.length+padding+maclen));
+ for (i = 0; i < pktout.length+padding+maclen; i++)
+ debug((" %02x", (unsigned char)pktout.data[i]));
+ debug(("\r\n"));
+#endif
+ s_write(pktout.data, pktout.length + padding + maclen);
+}
+
+void sha_mpint(SHA_State *s, Bignum b) {
+ unsigned char *p;
+ int len;
+ p = ssh2_mpint_fmt(b, &len);
+ sha_string(s, p, len);
+ free(p);
+}
+
+/*
+ * SSH2 packet decode functions.
+ */
+void ssh2_pkt_getstring(char **p, int *length) {
+ *p = NULL;
+ if (pktin.length - pktin.savedpos < 4)
+ return;
+ *length = GET_32BIT(pktin.data+pktin.savedpos);
+ pktin.savedpos += 4;
+ if (pktin.length - pktin.savedpos < *length)
+ return;
+ *p = pktin.data+pktin.savedpos;
+ pktin.savedpos += *length;
+}
+Bignum ssh2_pkt_getmp(void) {
+ char *p;
+ int i, j, length;
+ Bignum b;
+
+ ssh2_pkt_getstring(&p, &length);
+ if (!p)
+ return NULL;
+ if (p[0] & 0x80)
+ fatalbox("internal error: Can't handle negative mpints");
+ b = newbn((length+1)/2);
+ for (i = 0; i < length; i++) {
+ j = length - 1 - i;
+ if (j & 1)
+ b[j/2+1] |= ((unsigned char)p[i]) << 8;
+ else
+ b[j/2+1] |= ((unsigned char)p[i]);
+ }
+ return b;
+}
+
+void bndebug(char *string, Bignum b) {
+ unsigned char *p;
+ int i, len;
+ p = ssh2_mpint_fmt(b, &len);
+ debug(("%s", string));
+ for (i = 0; i < len; i++)
+ debug((" %02x", p[i]));
+ debug(("\r\n"));
+ free(p);
+}
+
+/*
+ * Utility routine for decoding comma-separated strings in KEXINIT.
+ */
+int in_commasep_string(char *needle, char *haystack, int haylen) {
+ int needlen = strlen(needle);
+ while (1) {
+ /*
+ * Is it at the start of the string?
+ */
+ if (haylen >= needlen && /* haystack is long enough */
+ !memcmp(needle, haystack, needlen) && /* initial match */
+ (haylen == needlen || haystack[needlen] == ',')
+ /* either , or EOS follows */
+ )
+ return 1;
+ /*
+ * If not, search for the next comma and resume after that.
+ * If no comma found, terminate.
+ */
+ while (haylen > 0 && *haystack != ',')
+ haylen--, haystack++;
+ if (haylen == 0)
+ return 0;
+ haylen--, haystack++; /* skip over comma itself */
+ }
+}
+
+/*
+ * Handle the SSH2 key exchange phase.
+ */
+static int do_ssh2_kex(unsigned char *in, int inlen, int ispkt)
+{
+ static int i, len;
+ static char *str;
+ static Bignum e, f, K;
+ static struct ssh_cipher *cscipher_tobe = NULL;
+ static struct ssh_cipher *sccipher_tobe = NULL;
+ static struct ssh_mac *csmac_tobe = NULL;
+ static struct ssh_mac *scmac_tobe = NULL;
+ static struct ssh_compress *cscomp_tobe = NULL;
+ static struct ssh_compress *sccomp_tobe = NULL;
+ static char *hostkeydata, *sigdata;
+ static int hostkeylen, siglen;
+ static unsigned char exchange_hash[20];
+
+ crBegin;
+
+ /*
+ * Construct and send our key exchange packet.
+ */
+ ssh2_pkt_init(SSH2_MSG_KEXINIT);
+ for (i = 0; i < 16; i++)
+ ssh2_pkt_addbyte((unsigned char)random_byte());
+ /* List key exchange algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(kex_algs); i++) {
+ ssh2_pkt_addstring_str(kex_algs[i]->name);
+ if (i < lenof(kex_algs)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List server host key algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(hostkey_algs); i++) {
+ ssh2_pkt_addstring_str(hostkey_algs[i]->name);
+ if (i < lenof(hostkey_algs)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List client->server encryption algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(ciphers); i++) {
+ ssh2_pkt_addstring_str(ciphers[i]->name);
+ if (i < lenof(ciphers)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List server->client encryption algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(ciphers); i++) {
+ ssh2_pkt_addstring_str(ciphers[i]->name);
+ if (i < lenof(ciphers)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List client->server MAC algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(macs); i++) {
+ ssh2_pkt_addstring_str(macs[i]->name);
+ if (i < lenof(macs)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List server->client MAC algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(macs); i++) {
+ ssh2_pkt_addstring_str(macs[i]->name);
+ if (i < lenof(macs)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List client->server compression algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(compressions); i++) {
+ ssh2_pkt_addstring_str(compressions[i]->name);
+ if (i < lenof(compressions)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List server->client compression algorithms. */
+ ssh2_pkt_addstring_start();
+ for (i = 0; i < lenof(compressions); i++) {
+ ssh2_pkt_addstring_str(compressions[i]->name);
+ if (i < lenof(compressions)-1)
+ ssh2_pkt_addstring_str(",");
+ }
+ /* List client->server languages. Empty list. */
+ ssh2_pkt_addstring_start();
+ /* List server->client languages. Empty list. */
+ ssh2_pkt_addstring_start();
+ /* First KEX packet does _not_ follow, because we're not that brave. */
+ ssh2_pkt_addbool(FALSE);
+ /* Reserved. */
+ ssh2_pkt_adduint32(0);
+ sha_string(&exhash, pktout.data+5, pktout.length-5);
+ ssh2_pkt_send();
+
+ if (!ispkt) crWaitUntil(ispkt);
+ sha_string(&exhash, pktin.data+5, pktin.length-5);
+
+ /*
+ * Now examine the other side's KEXINIT to see what we're up
+ * to.
+ */
+ if (pktin.type != SSH2_MSG_KEXINIT)
+ fatalbox("expected key exchange packet from server");
+ kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL;
+ csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL;
+ pktin.savedpos += 16; /* skip garbage cookie */
+ ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */
+ for (i = 0; i < lenof(kex_algs); i++) {
+ if (in_commasep_string(kex_algs[i]->name, str, len)) {
+ kex = kex_algs[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* host key algorithms */
+ for (i = 0; i < lenof(hostkey_algs); i++) {
+ if (in_commasep_string(hostkey_algs[i]->name, str, len)) {
+ hostkey = hostkey_algs[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* client->server cipher */
+ for (i = 0; i < lenof(ciphers); i++) {
+ if (in_commasep_string(ciphers[i]->name, str, len)) {
+ cscipher_tobe = ciphers[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* server->client cipher */
+ for (i = 0; i < lenof(ciphers); i++) {
+ if (in_commasep_string(ciphers[i]->name, str, len)) {
+ sccipher_tobe = ciphers[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* client->server mac */
+ for (i = 0; i < lenof(macs); i++) {
+ if (in_commasep_string(macs[i]->name, str, len)) {
+ csmac_tobe = macs[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* server->client mac */
+ for (i = 0; i < lenof(macs); i++) {
+ if (in_commasep_string(macs[i]->name, str, len)) {
+ scmac_tobe = macs[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* client->server compression */
+ for (i = 0; i < lenof(compressions); i++) {
+ if (in_commasep_string(compressions[i]->name, str, len)) {
+ cscomp_tobe = compressions[i];
+ break;
+ }
+ }
+ ssh2_pkt_getstring(&str, &len); /* server->client compression */
+ for (i = 0; i < lenof(compressions); i++) {
+ if (in_commasep_string(compressions[i]->name, str, len)) {
+ sccomp_tobe = compressions[i];
+ break;
+ }
+ }
+ debug(("key exchange is %s\r\n", kex ? kex->name : NULL));
+ debug(("host key alg is %s\r\n", hostkey ? hostkey->name : NULL));
+ debug(("cscipher alg is %s\r\n", cscipher_tobe ? cscipher_tobe->name : NULL));
+ debug(("sccipher alg is %s\r\n", sccipher_tobe ? sccipher_tobe->name : NULL));
+ debug(("csmac alg is %s\r\n", csmac_tobe ? csmac_tobe->name : NULL));
+ debug(("scmac alg is %s\r\n", scmac_tobe ? scmac_tobe->name : NULL));
+ debug(("cscomp alg is %s\r\n", cscomp_tobe ? cscomp_tobe->name : NULL));
+ debug(("sccomp alg is %s\r\n", sccomp_tobe ? sccomp_tobe->name : NULL));
+
+ /*
+ * Currently we only support Diffie-Hellman and DSS, so let's
+ * bomb out if those aren't selected.
+ */
+ if (kex != &ssh_diffiehellman || hostkey != &ssh_dss)
+ fatalbox("internal fault: chaos in SSH 2 transport layer");
+
+ /*
+ * Now we begin the fun. Generate and send e for Diffie-Hellman.
+ */
+ e = dh_create_e();
+ bndebug("e=", e);
+ ssh2_pkt_init(SSH2_MSG_KEXDH_INIT);
+ ssh2_pkt_addmp(e);
+ ssh2_pkt_send();
+
+ crWaitUntil(ispkt);
+ if (pktin.type != SSH2_MSG_KEXDH_REPLY)
+ fatalbox("expected key exchange packet from server");
+ ssh2_pkt_getstring(&hostkeydata, &hostkeylen);
+ f = ssh2_pkt_getmp();
+ bndebug("f=", f);
+ ssh2_pkt_getstring(&sigdata, &siglen);
+
+ K = dh_find_K(f);
+ bndebug("K=", K);
+
+ sha_string(&exhash, hostkeydata, hostkeylen);
+ sha_mpint(&exhash, e);
+ sha_mpint(&exhash, f);
+ sha_mpint(&exhash, K);
+ SHA_Final(&exhash, exchange_hash);
+
+ debug(("Exchange hash is:\r\n"));
+ for (i = 0; i < 20; i++)
+ debug((" %02x", exchange_hash[i]));
+ debug(("\r\n"));
+
+ /*
+ * FIXME: verify hostkeydata and sigdata.
+ */
+
+ crWaitUntil(0);
+
+ crFinish(1);
+}
+
+static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) {
+ crBegin;
+
+ random_init();
+
+ while (!do_ssh2_kex(in, inlen, ispkt)) {
+ crReturnV;
+ }
+
+ crFinishV;
+}
+
+/*