| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "ssh.h" |
| 5 | |
| 6 | #define GET_32BIT(cp) \ |
| 7 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
| 8 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
| 9 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
| 10 | ((unsigned long)(unsigned char)(cp)[3])) |
| 11 | |
| 12 | #define PUT_32BIT(cp, value) { \ |
| 13 | (cp)[0] = (unsigned char)((value) >> 24); \ |
| 14 | (cp)[1] = (unsigned char)((value) >> 16); \ |
| 15 | (cp)[2] = (unsigned char)((value) >> 8); \ |
| 16 | (cp)[3] = (unsigned char)(value); } |
| 17 | |
| 18 | #if 0 |
| 19 | /* |
| 20 | * Condition this section in for debugging of DSS. |
| 21 | */ |
| 22 | static void diagbn(char *prefix, Bignum md) { |
| 23 | int i, nibbles, morenibbles; |
| 24 | static const char hex[] = "0123456789ABCDEF"; |
| 25 | |
| 26 | printf("%s0x", prefix ? prefix : ""); |
| 27 | |
| 28 | nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1; |
| 29 | morenibbles = 4*md[0] - nibbles; |
| 30 | for (i=0; i<morenibbles; i++) putchar('-'); |
| 31 | for (i=nibbles; i-- ;) |
| 32 | putchar(hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF]); |
| 33 | |
| 34 | if (prefix) putchar('\n'); |
| 35 | } |
| 36 | #define DEBUG_DSS |
| 37 | #else |
| 38 | #define diagbn(x,y) |
| 39 | #endif |
| 40 | |
| 41 | static void getstring(char **data, int *datalen, char **p, int *length) { |
| 42 | *p = NULL; |
| 43 | if (*datalen < 4) |
| 44 | return; |
| 45 | *length = GET_32BIT(*data); |
| 46 | *datalen -= 4; *data += 4; |
| 47 | if (*datalen < *length) |
| 48 | return; |
| 49 | *p = *data; |
| 50 | *data += *length; *datalen -= *length; |
| 51 | } |
| 52 | static Bignum getmp(char **data, int *datalen) { |
| 53 | char *p; |
| 54 | int i, j, length; |
| 55 | Bignum b; |
| 56 | |
| 57 | getstring(data, datalen, &p, &length); |
| 58 | if (!p) |
| 59 | return NULL; |
| 60 | if (p[0] & 0x80) |
| 61 | return NULL; /* negative mp */ |
| 62 | b = newbn((length+1)/2); |
| 63 | for (i = 0; i < length; i++) { |
| 64 | j = length - 1 - i; |
| 65 | if (j & 1) |
| 66 | b[j/2+1] |= ((unsigned char)p[i]) << 8; |
| 67 | else |
| 68 | b[j/2+1] |= ((unsigned char)p[i]); |
| 69 | } |
| 70 | while (b[0] > 1 && b[b[0]] == 0) b[0]--; |
| 71 | return b; |
| 72 | } |
| 73 | |
| 74 | static Bignum get160(char **data, int *datalen) { |
| 75 | char *p; |
| 76 | int i, j, length; |
| 77 | Bignum b; |
| 78 | |
| 79 | p = *data; |
| 80 | *data += 20; *datalen -= 20; |
| 81 | |
| 82 | length = 20; |
| 83 | while (length > 0 && !p[0]) |
| 84 | p++, length--; |
| 85 | b = newbn((length+1)/2); |
| 86 | for (i = 0; i < length; i++) { |
| 87 | j = length - 1 - i; |
| 88 | if (j & 1) |
| 89 | b[j/2+1] |= ((unsigned char)p[i]) << 8; |
| 90 | else |
| 91 | b[j/2+1] |= ((unsigned char)p[i]); |
| 92 | } |
| 93 | return b; |
| 94 | } |
| 95 | |
| 96 | static Bignum dss_p, dss_q, dss_g, dss_y; |
| 97 | |
| 98 | static void dss_setkey(char *data, int len) { |
| 99 | char *p; |
| 100 | int slen; |
| 101 | getstring(&data, &len, &p, &slen); |
| 102 | |
| 103 | #ifdef DEBUG_DSS |
| 104 | { |
| 105 | int i; |
| 106 | printf("key:"); |
| 107 | for (i=0;i<len;i++) |
| 108 | printf(" %02x", (unsigned char)(data[i])); |
| 109 | printf("\n"); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | if (!p || memcmp(p, "ssh-dss", 7)) { |
| 114 | dss_p = NULL; |
| 115 | return; |
| 116 | } |
| 117 | dss_p = getmp(&data, &len); |
| 118 | dss_q = getmp(&data, &len); |
| 119 | dss_g = getmp(&data, &len); |
| 120 | dss_y = getmp(&data, &len); |
| 121 | } |
| 122 | |
| 123 | static char *dss_fmtkey(void) { |
| 124 | char *p; |
| 125 | int len, i, pos, nibbles; |
| 126 | static const char hex[] = "0123456789abcdef"; |
| 127 | if (!dss_p) |
| 128 | return NULL; |
| 129 | len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */ |
| 130 | len += 4 * (dss_p[0] + dss_q[0] + dss_g[0] + dss_y[0]); /* digits */ |
| 131 | p = malloc(len); |
| 132 | if (!p) return NULL; |
| 133 | |
| 134 | pos = 0; |
| 135 | pos += sprintf(p+pos, "0x"); |
| 136 | nibbles = (3 + ssh1_bignum_bitcount(dss_p))/4; if (nibbles<1) nibbles=1; |
| 137 | for (i=nibbles; i-- ;) |
| 138 | p[pos++] = hex[(bignum_byte(dss_p, i/2) >> (4*(i%2))) & 0xF]; |
| 139 | pos += sprintf(p+pos, ",0x"); |
| 140 | nibbles = (3 + ssh1_bignum_bitcount(dss_q))/4; if (nibbles<1) nibbles=1; |
| 141 | for (i=nibbles; i-- ;) |
| 142 | p[pos++] = hex[(bignum_byte(dss_q, i/2) >> (4*(i%2))) & 0xF]; |
| 143 | pos += sprintf(p+pos, ",0x"); |
| 144 | nibbles = (3 + ssh1_bignum_bitcount(dss_g))/4; if (nibbles<1) nibbles=1; |
| 145 | for (i=nibbles; i-- ;) |
| 146 | p[pos++] = hex[(bignum_byte(dss_g, i/2) >> (4*(i%2))) & 0xF]; |
| 147 | pos += sprintf(p+pos, ",0x"); |
| 148 | nibbles = (3 + ssh1_bignum_bitcount(dss_y))/4; if (nibbles<1) nibbles=1; |
| 149 | for (i=nibbles; i-- ;) |
| 150 | p[pos++] = hex[(bignum_byte(dss_y, i/2) >> (4*(i%2))) & 0xF]; |
| 151 | p[pos] = '\0'; |
| 152 | return p; |
| 153 | } |
| 154 | |
| 155 | static char *dss_fingerprint(void) { |
| 156 | struct MD5Context md5c; |
| 157 | unsigned char digest[16], lenbuf[4]; |
| 158 | char buffer[16*3+40]; |
| 159 | char *ret; |
| 160 | int numlen, i; |
| 161 | |
| 162 | MD5Init(&md5c); |
| 163 | MD5Update(&md5c, "\0\0\0\7ssh-dss", 11); |
| 164 | |
| 165 | #define ADD_BIGNUM(bignum) \ |
| 166 | numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \ |
| 167 | PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ |
| 168 | for (i = numlen; i-- ;) { \ |
| 169 | unsigned char c = bignum_byte(bignum, i); \ |
| 170 | MD5Update(&md5c, &c, 1); \ |
| 171 | } |
| 172 | ADD_BIGNUM(dss_p); |
| 173 | ADD_BIGNUM(dss_q); |
| 174 | ADD_BIGNUM(dss_g); |
| 175 | ADD_BIGNUM(dss_y); |
| 176 | #undef ADD_BIGNUM |
| 177 | |
| 178 | MD5Final(digest, &md5c); |
| 179 | |
| 180 | sprintf(buffer, "%d ", ssh1_bignum_bitcount(dss_p)); |
| 181 | for (i = 0; i < 16; i++) |
| 182 | sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]); |
| 183 | ret = malloc(strlen(buffer)+1); |
| 184 | if (ret) |
| 185 | strcpy(ret, buffer); |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static int dss_verifysig(char *sig, int siglen, char *data, int datalen) { |
| 190 | char *p; |
| 191 | int slen; |
| 192 | char hash[20]; |
| 193 | Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v; |
| 194 | int ret; |
| 195 | |
| 196 | if (!dss_p) |
| 197 | return 0; |
| 198 | |
| 199 | #ifdef DEBUG_DSS |
| 200 | { |
| 201 | int i; |
| 202 | printf("sig:"); |
| 203 | for (i=0;i<siglen;i++) |
| 204 | printf(" %02xf", (unsigned char)(sig[i])); |
| 205 | printf("\n"); |
| 206 | } |
| 207 | #endif |
| 208 | /* |
| 209 | * Commercial SSH (2.0.13) and OpenSSH disagree over the format |
| 210 | * of a DSA signature. OpenSSH is in line with the IETF drafts: |
| 211 | * it uses a string "ssh-dss", followed by a 40-byte string |
| 212 | * containing two 160-bit integers end-to-end. Commercial SSH |
| 213 | * can't be bothered with the header bit, and considers a DSA |
| 214 | * signature blob to be _just_ the 40-byte string containing |
| 215 | * the two 160-bit integers. We tell them apart by measuring |
| 216 | * the length: length 40 means the commercial-SSH bug, anything |
| 217 | * else is assumed to be IETF-compliant. |
| 218 | */ |
| 219 | if (siglen != 40) { /* bug not present; read admin fields */ |
| 220 | getstring(&sig, &siglen, &p, &slen); |
| 221 | if (!p || memcmp(p, "ssh-dss", 7)) { |
| 222 | return 0; |
| 223 | } |
| 224 | sig += 4, siglen -= 4; /* skip yet another length field */ |
| 225 | } |
| 226 | diagbn("p=", dss_p); |
| 227 | diagbn("q=", dss_q); |
| 228 | diagbn("g=", dss_g); |
| 229 | diagbn("y=", dss_y); |
| 230 | r = get160(&sig, &siglen); |
| 231 | diagbn("r=", r); |
| 232 | s = get160(&sig, &siglen); |
| 233 | diagbn("s=", s); |
| 234 | if (!r || !s) |
| 235 | return 0; |
| 236 | |
| 237 | /* |
| 238 | * Step 1. w <- s^-1 mod q. |
| 239 | */ |
| 240 | w = modinv(s, dss_q); |
| 241 | diagbn("w=", w); |
| 242 | |
| 243 | /* |
| 244 | * Step 2. u1 <- SHA(message) * w mod q. |
| 245 | */ |
| 246 | SHA_Simple(data, datalen, hash); |
| 247 | p = hash; slen = 20; sha = get160(&p, &slen); |
| 248 | diagbn("sha=", sha); |
| 249 | u1 = modmul(sha, w, dss_q); |
| 250 | diagbn("u1=", u1); |
| 251 | |
| 252 | /* |
| 253 | * Step 3. u2 <- r * w mod q. |
| 254 | */ |
| 255 | u2 = modmul(r, w, dss_q); |
| 256 | diagbn("u2=", u2); |
| 257 | |
| 258 | /* |
| 259 | * Step 4. v <- (g^u1 * y^u2 mod p) mod q. |
| 260 | */ |
| 261 | gu1p = modpow(dss_g, u1, dss_p); |
| 262 | diagbn("gu1p=", gu1p); |
| 263 | yu2p = modpow(dss_y, u2, dss_p); |
| 264 | diagbn("yu2p=", yu2p); |
| 265 | gu1yu2p = modmul(gu1p, yu2p, dss_p); |
| 266 | diagbn("gu1yu2p=", gu1yu2p); |
| 267 | v = modmul(gu1yu2p, One, dss_q); |
| 268 | diagbn("gu1yu2q=v=", v); |
| 269 | diagbn("r=", r); |
| 270 | |
| 271 | /* |
| 272 | * Step 5. v should now be equal to r. |
| 273 | */ |
| 274 | |
| 275 | ret = !bignum_cmp(v, r); |
| 276 | |
| 277 | freebn(w); |
| 278 | freebn(sha); |
| 279 | freebn(gu1p); |
| 280 | freebn(yu2p); |
| 281 | freebn(gu1yu2p); |
| 282 | freebn(v); |
| 283 | freebn(r); |
| 284 | freebn(s); |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | struct ssh_hostkey ssh_dss = { |
| 290 | dss_setkey, |
| 291 | dss_fmtkey, |
| 292 | dss_fingerprint, |
| 293 | dss_verifysig, |
| 294 | "ssh-dss", |
| 295 | "dss" |
| 296 | }; |