]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Merge reconfig fixes from branch 'pre-0.64'.
authorJacob Nevins <jacobn@chiark.greenend.org.uk>
Sun, 9 Nov 2014 00:12:51 +0000 (00:12 +0000)
committerJacob Nevins <jacobn@chiark.greenend.org.uk>
Sun, 9 Nov 2014 00:12:55 +0000 (00:12 +0000)
1  2 
config.c
ssh.c

diff --combined config.c
index e126d4dce4124f458ed319297d8de83343a2edd1,c2acd6287a83558c7147da29229d6605a76d4f9b..0d292b8af4ff64c6606de8639b768b869fba9858
+++ b/config.c
@@@ -433,7 -433,6 +433,7 @@@ static void kexlist_handler(union contr
            { "Diffie-Hellman group 14",        KEX_DHGROUP14 },
            { "Diffie-Hellman group exchange",  KEX_DHGEX },
            { "RSA-based key exchange",         KEX_RSA },
 +            { "ECDH key exchange",              KEX_ECDH },
            { "-- warn below here --",          KEX_WARN }
        };
  
@@@ -2137,7 -2136,8 +2137,8 @@@ void setup_config_box(struct controlbo
        ctrl_settitle(b, "Connection/SSH",
                      "Options controlling SSH connections");
  
-       if (midsession && protcfginfo == 1) {
+       /* SSH-1 or connection-sharing downstream */
+       if (midsession && (protcfginfo == 1 || protcfginfo == -1)) {
            s = ctrl_getset(b, "Connection/SSH", "disclaimer", NULL);
            ctrl_text(s, "Nothing on this panel may be reconfigured in mid-"
                      "session; it is only here so that sub-panels of it can "
                          I(CONF_ssh_no_shell));
        }
  
-       if (!midsession || protcfginfo != 1) {
+       if (!midsession || !(protcfginfo == 1 || protcfginfo == -1)) {
            s = ctrl_getset(b, "Connection/SSH", "protocol", "Protocol options");
  
            ctrl_checkbox(s, "Enable compression", 'e',
  
        /*
         * The Connection/SSH/Kex panel. (Owing to repeat key
-        * exchange, this is all meaningful in mid-session _if_
-        * we're using SSH-2 or haven't decided yet.)
+        * exchange, much of this is meaningful in mid-session _if_
+        * we're using SSH-2 and are not a connection-sharing
+        * downstream, or haven't decided yet.)
         */
-       if (protcfginfo != 1) {
+       if (protcfginfo != 1 && protcfginfo != -1) {
            ctrl_settitle(b, "Connection/SSH/Kex",
                          "Options controlling SSH key exchange");
  
                         I(16));
            ctrl_text(s, "(Use 1M for 1 megabyte, 1G for 1 gigabyte etc)",
                      HELPCTX(ssh_kex_repeat));
+       }
  
+       /*
+        * Manual host key configuration is irrelevant mid-session,
+        * as we enforce that the host key for rekeys is the
+        * same as that used at the start of the session.
+        */
+       if (!midsession) {
            s = ctrl_getset(b, "Connection/SSH/Kex", "hostkeys",
                            "Manually configure host keys for this connection");
  
              ctrl_columns(s, 1, 100);
        }
  
-       if (!midsession || protcfginfo != 1) {
+       if (!midsession || !(protcfginfo == 1 || protcfginfo == -1)) {
            /*
             * The Connection/SSH/Cipher panel.
             */
diff --combined ssh.c
index 2dc8a081d2f4b94b32db89f05a5802e812f414bb,48b3f38f1288ffd58821fbc5b1a51a7c3653781e..c2af9e8039661d5e7e278c6f6e35920728ba4682
--- 1/ssh.c
--- 2/ssh.c
+++ b/ssh.c
@@@ -32,7 -32,6 +32,7 @@@ typedef enum 
      SSH2_PKTCTX_NOKEX,
      SSH2_PKTCTX_DHGROUP,
      SSH2_PKTCTX_DHGEX,
 +    SSH2_PKTCTX_ECDHKEX,
      SSH2_PKTCTX_RSAKEX
  } Pkt_KCtx;
  typedef enum {
@@@ -255,8 -254,6 +255,8 @@@ static char *ssh2_pkt_type(Pkt_KCtx pkt
      translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX);
      translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX);
      translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX);
 +    translatek(SSH2_MSG_KEX_ECDH_INIT, SSH2_PKTCTX_ECDHKEX);
 +    translatek(SSH2_MSG_KEX_ECDH_REPLY, SSH2_PKTCTX_ECDHKEX);
      translate(SSH2_MSG_USERAUTH_REQUEST);
      translate(SSH2_MSG_USERAUTH_FAILURE);
      translate(SSH2_MSG_USERAUTH_SUCCESS);
@@@ -400,10 -397,7 +400,10 @@@ static void ssh_channel_destroy(struct 
  #define OUR_V2_MAXPKT 0x4000UL
  #define OUR_V2_PACKETLIMIT 0x9000UL
  
 -const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss };
 +const static struct ssh_signkey *hostkey_algs[] = {
 +    &ssh_ecdsa_nistp256, &ssh_ecdsa_nistp384, &ssh_ecdsa_nistp521,
 +    &ssh_rsa, &ssh_dss
 +};
  
  const static struct ssh_mac *macs[] = {
      &ssh_hmac_sha256, &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5
@@@ -5928,8 -5922,7 +5928,8 @@@ static void ssh1_protocol(Ssh ssh, voi
  /*
   * Utility routine for decoding comma-separated strings in KEXINIT.
   */
 -static int in_commasep_string(char *needle, char *haystack, int haylen)
 +static int in_commasep_string(char const *needle, char const *haystack,
 +                            int haylen)
  {
      int needlen;
      if (!needle || !haystack)        /* protect against null pointers */
  /*
   * Similar routine for checking whether we have the first string in a list.
   */
 -static int first_in_commasep_string(char *needle, char *haystack, int haylen)
 +static int first_in_commasep_string(char const *needle, char const *haystack,
 +                                  int haylen)
  {
      int needlen;
      if (!needle || !haystack)        /* protect against null pointers */
      return 0;
  }
  
 +/*
 + * Add a value to the comma-separated string at the end of the packet.
 + * If the value is already in the string, don't bother adding it again.
 + */
 +static void ssh2_pkt_addstring_commasep(struct Packet *pkt, const char *data)
 +{
 +    if (in_commasep_string(data, (char *)pkt->data + pkt->savedpos,
 +                         pkt->length - pkt->savedpos)) return;
 +    if (pkt->length - pkt->savedpos > 0)
 +      ssh_pkt_addstring_str(pkt, ",");
 +    ssh_pkt_addstring_str(pkt, data);
 +}
 +
  
  /*
   * SSH-2 key creation method.
@@@ -6047,7 -6026,6 +6047,7 @@@ static void do_ssh2_transport(Ssh ssh, 
        int hostkeylen, siglen, rsakeylen;
        void *hkey;                    /* actual host key */
        void *rsakey;                  /* for RSA kex */
 +        void *eckey;                   /* for ECDH kex */
        unsigned char exchange_hash[SSH2_KEX_MAX_HASH_LEN];
        int n_preferred_kex;
        const struct ssh_kexes *preferred_kex[KEX_MAX];
    begin_key_exchange:
      ssh->pkt_kctx = SSH2_PKTCTX_NOKEX;
      {
 -      int i, j, k, commalist_started;
 +      int i, j, k;
  
        /*
         * Set up the preferred key exchange. (NULL => warn below here)
                s->preferred_kex[s->n_preferred_kex++] =
                    &ssh_rsa_kex;
                break;
 +              case KEX_ECDH:
 +                s->preferred_kex[s->n_preferred_kex++] =
 +                    &ssh_ecdh_kex;
 +                break;
              case KEX_WARN:
                /* Flag for later. Don't bother if it's the last in
                 * the list. */
            ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte());
        /* List key exchange algorithms. */
        ssh2_pkt_addstring_start(s->pktout);
 -      commalist_started = 0;
        for (i = 0; i < s->n_preferred_kex; i++) {
            const struct ssh_kexes *k = s->preferred_kex[i];
            if (!k) continue;          /* warning flag */
 -          for (j = 0; j < k->nkexes; j++) {
 -              if (commalist_started)
 -                  ssh2_pkt_addstring_str(s->pktout, ",");
 -              ssh2_pkt_addstring_str(s->pktout, k->list[j]->name);
 -              commalist_started = 1;
 -          }
 +          for (j = 0; j < k->nkexes; j++)
 +              ssh2_pkt_addstring_commasep(s->pktout, k->list[j]->name);
        }
        /* List server host key algorithms. */
          if (!s->got_session_id) {
               * we're prepared to cope with.
               */
              ssh2_pkt_addstring_start(s->pktout);
 -            for (i = 0; i < lenof(hostkey_algs); i++) {
 -                ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name);
 -                if (i < lenof(hostkey_algs) - 1)
 -                    ssh2_pkt_addstring_str(s->pktout, ",");
 -            }
 +            for (i = 0; i < lenof(hostkey_algs); i++)
 +                ssh2_pkt_addstring_commasep(s->pktout, hostkey_algs[i]->name);
          } else {
              /*
               * In subsequent key exchanges, we list only the kex
        /* List encryption algorithms (client->server then server->client). */
        for (k = 0; k < 2; k++) {
            ssh2_pkt_addstring_start(s->pktout);
 -          commalist_started = 0;
            for (i = 0; i < s->n_preferred_ciphers; i++) {
                const struct ssh2_ciphers *c = s->preferred_ciphers[i];
                if (!c) continue;              /* warning flag */
 -              for (j = 0; j < c->nciphers; j++) {
 -                  if (commalist_started)
 -                      ssh2_pkt_addstring_str(s->pktout, ",");
 -                  ssh2_pkt_addstring_str(s->pktout, c->list[j]->name);
 -                  commalist_started = 1;
 -              }
 +              for (j = 0; j < c->nciphers; j++)
 +                  ssh2_pkt_addstring_commasep(s->pktout, c->list[j]->name);
            }
        }
        /* List MAC algorithms (client->server then server->client). */
        for (j = 0; j < 2; j++) {
            ssh2_pkt_addstring_start(s->pktout);
 -          for (i = 0; i < s->nmacs; i++) {
 -              ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name);
 -              if (i < s->nmacs - 1)
 -                  ssh2_pkt_addstring_str(s->pktout, ",");
 -          }
 +          for (i = 0; i < s->nmacs; i++)
 +              ssh2_pkt_addstring_commasep(s->pktout, s->maclist[i]->name);
        }
        /* List client->server compression algorithms,
         * then server->client compression algorithms. (We use the
            ssh2_pkt_addstring_start(s->pktout);
            assert(lenof(compressions) > 1);
            /* Prefer non-delayed versions */
 -          ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);
 +          ssh2_pkt_addstring_commasep(s->pktout, s->preferred_comp->name);
            /* We don't even list delayed versions of algorithms until
             * they're allowed to be used, to avoid a race. See the end of
             * this function. */
 -          if (s->userauth_succeeded && s->preferred_comp->delayed_name) {
 -              ssh2_pkt_addstring_str(s->pktout, ",");
 -              ssh2_pkt_addstring_str(s->pktout,
 -                                     s->preferred_comp->delayed_name);
 -          }
 +          if (s->userauth_succeeded && s->preferred_comp->delayed_name)
 +              ssh2_pkt_addstring_commasep(s->pktout,
 +                                          s->preferred_comp->delayed_name);
            for (i = 0; i < lenof(compressions); i++) {
                const struct ssh_compress *c = compressions[i];
 -              if (c != s->preferred_comp) {
 -                  ssh2_pkt_addstring_str(s->pktout, ",");
 -                  ssh2_pkt_addstring_str(s->pktout, c->name);
 -                  if (s->userauth_succeeded && c->delayed_name) {
 -                      ssh2_pkt_addstring_str(s->pktout, ",");
 -                      ssh2_pkt_addstring_str(s->pktout, c->delayed_name);
 -                  }
 -              }
 +              ssh2_pkt_addstring_commasep(s->pktout, c->name);
 +              if (s->userauth_succeeded && c->delayed_name)
 +                  ssh2_pkt_addstring_commasep(s->pktout, c->delayed_name);
            }
        }
        /* List client->server languages. Empty list. */
              freebn(s->g);
              freebn(s->p);
          }
 +    } else if (ssh->kex->main_type == KEXTYPE_ECDH) {
 +
 +        logeventf(ssh, "Doing ECDH key exchange with hash %s",
 +                  ssh->kex->hash->text_name);
 +        ssh->pkt_kctx = SSH2_PKTCTX_ECDHKEX;
 +
 +        s->eckey = NULL;
 +        if (!strcmp(ssh->kex->name, "ecdh-sha2-nistp256")) {
 +            s->eckey = ssh_ecdhkex_newkey(ec_p256());
 +        } else if (!strcmp(ssh->kex->name, "ecdh-sha2-nistp384")) {
 +            s->eckey = ssh_ecdhkex_newkey(ec_p384());
 +        } else if (!strcmp(ssh->kex->name, "ecdh-sha2-nistp521")) {
 +            s->eckey = ssh_ecdhkex_newkey(ec_p521());
 +        }
 +        if (!s->eckey) {
 +            bombout(("Unable to generate key for ECDH"));
 +            crStopV;
 +        }
 +
 +        {
 +            char *publicPoint;
 +            int publicPointLength;
 +            publicPoint = ssh_ecdhkex_getpublic(s->eckey, &publicPointLength);
 +            if (!publicPoint) {
 +                ssh_ecdhkex_freekey(s->eckey);
 +                bombout(("Unable to encode public key for ECDH"));
 +                crStopV;
 +            }
 +            s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_ECDH_INIT);
 +            ssh2_pkt_addstring_start(s->pktout);
 +            ssh2_pkt_addstring_data(s->pktout, publicPoint, publicPointLength);
 +            sfree(publicPoint);
 +        }
 +
 +        ssh2_pkt_send_noqueue(ssh, s->pktout);
 +
 +        crWaitUntilV(pktin);
 +        if (pktin->type != SSH2_MSG_KEX_ECDH_REPLY) {
 +            ssh_ecdhkex_freekey(s->eckey);
 +            bombout(("expected ECDH reply packet from server"));
 +            crStopV;
 +        }
 +
 +        ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen);
 +        hash_string(ssh->kex->hash, ssh->exhash, s->hostkeydata, s->hostkeylen);
 +        s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen);
 +
 +        {
 +            char *publicPoint;
 +            int publicPointLength;
 +            publicPoint = ssh_ecdhkex_getpublic(s->eckey, &publicPointLength);
 +            if (!publicPoint) {
 +                ssh_ecdhkex_freekey(s->eckey);
 +                bombout(("Unable to encode public key for ECDH hash"));
 +                crStopV;
 +            }
 +            hash_string(ssh->kex->hash, ssh->exhash,
 +                        publicPoint, publicPointLength);
 +            sfree(publicPoint);
 +        }
 +
 +        {
 +            char *keydata;
 +            int keylen;
 +            ssh_pkt_getstring(pktin, &keydata, &keylen);
 +            hash_string(ssh->kex->hash, ssh->exhash, keydata, keylen);
 +            s->K = ssh_ecdhkex_getkey(s->eckey, keydata, keylen);
 +            if (!s->K) {
 +                ssh_ecdhkex_freekey(s->eckey);
 +                bombout(("point received in ECDH was not valid"));
 +                crStopV;
 +            }
 +        }
 +
 +        ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen);
 +
 +        ssh_ecdhkex_freekey(s->eckey);
      } else {
        logeventf(ssh, "Doing RSA key exchange with hash %s",
                  ssh->kex->hash->text_name);
@@@ -11321,13 -11241,19 +11321,19 @@@ static int ssh_return_exitcode(void *ha
  }
  
  /*
-  * cfg_info for SSH is the currently running version of the
-  * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.)
+  * cfg_info for SSH is the protocol running in this session.
+  * (1 or 2 for the full SSH-1 or SSH-2 protocol; -1 for the bare
+  * SSH-2 connection protocol, i.e. a downstream; 0 for not-decided-yet.)
   */
  static int ssh_cfg_info(void *handle)
  {
      Ssh ssh = (Ssh) handle;
-     return ssh->version;
+     if (ssh->version == 0)
+       return 0; /* don't know yet */
+     else if (ssh->bare_connection)
+       return -1;
+     else
+       return ssh->version;
  }
  
  /*