Work around DSA formatting bug in commercial-SSH 2.0.13
[sgt/putty] / sshdss.c
index 86a1617..01c452b 100644 (file)
--- a/sshdss.c
+++ b/sshdss.c
@@ -101,15 +101,15 @@ static char *dss_fmtkey(void) {
     nibbles = (3 + ssh1_bignum_bitcount(dss_p))/4; if (nibbles<1) nibbles=1;
     for (i=nibbles; i-- ;)
         p[pos++] = hex[(bignum_byte(dss_p, i/2) >> (4*(i%2))) & 0xF];
-    pos += sprintf(p+pos, "0x");
+    pos += sprintf(p+pos, ",0x");
     nibbles = (3 + ssh1_bignum_bitcount(dss_q))/4; if (nibbles<1) nibbles=1;
     for (i=nibbles; i-- ;)
         p[pos++] = hex[(bignum_byte(dss_q, i/2) >> (4*(i%2))) & 0xF];
-    pos += sprintf(p+pos, "0x");
+    pos += sprintf(p+pos, ",0x");
     nibbles = (3 + ssh1_bignum_bitcount(dss_g))/4; if (nibbles<1) nibbles=1;
     for (i=nibbles; i-- ;)
         p[pos++] = hex[(bignum_byte(dss_g, i/2) >> (4*(i%2))) & 0xF];
-    pos += sprintf(p+pos, "0x");
+    pos += sprintf(p+pos, ",0x");
     nibbles = (3 + ssh1_bignum_bitcount(dss_y))/4; if (nibbles<1) nibbles=1;
     for (i=nibbles; i-- ;)
         p[pos++] = hex[(bignum_byte(dss_y, i/2) >> (4*(i%2))) & 0xF];
@@ -161,11 +161,24 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
     if (!dss_p)
         return 0;
 
-    getstring(&sig, &siglen, &p, &slen);
-    if (!p || memcmp(p, "ssh-dss", 7)) {
-        return 0;
+    /*
+     * Commercial SSH (2.0.13) and OpenSSH disagree over the format
+     * of a DSA signature. OpenSSH is in line with the IETF drafts:
+     * it uses a string "ssh-dss", followed by a 40-byte string
+     * containing two 160-bit integers end-to-end. Commercial SSH
+     * can't be bothered with the header bit, and considers a DSA
+     * signature blob to be _just_ the 40-byte string containing
+     * the two 160-bit integers. We tell them apart by measuring
+     * the length: length 40 means the commercial-SSH bug, anything
+     * else is assumed to be IETF-compliant.
+     */
+    if (siglen != 40) {                /* bug not present; read admin fields */
+        getstring(&sig, &siglen, &p, &slen);
+        if (!p || memcmp(p, "ssh-dss", 7)) {
+            return 0;
+        }
+        sig += 4, siglen -= 4;             /* skip yet another length field */
     }
-    sig += 4, siglen -= 4;             /* skip yet another length field */
     r = get160(&sig, &siglen);
     s = get160(&sig, &siglen);
     if (!r || !s)