sshdss.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Digital Signature Standard implementation for PuTTY.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include "ssh.h"
  8. #include "mpint.h"
  9. #include "misc.h"
  10. static void dss_freekey(ssh_key *key); /* forward reference */
  11. static ssh_key *dss_new_pub(const ssh_keyalg *self, ptrlen data)
  12. {
  13. BinarySource src[1];
  14. struct dss_key *dss;
  15. BinarySource_BARE_INIT_PL(src, data);
  16. if (!ptrlen_eq_string(get_string(src), "ssh-dss"))
  17. return NULL;
  18. dss = snew(struct dss_key);
  19. dss->sshk.vt = &ssh_dss;
  20. dss->p = get_mp_ssh2(src);
  21. dss->q = get_mp_ssh2(src);
  22. dss->g = get_mp_ssh2(src);
  23. dss->y = get_mp_ssh2(src);
  24. dss->x = NULL;
  25. if (get_err(src) ||
  26. mp_eq_integer(dss->p, 0) || mp_eq_integer(dss->q, 0)) {
  27. /* Invalid key. */
  28. dss_freekey(&dss->sshk);
  29. return NULL;
  30. }
  31. return &dss->sshk;
  32. }
  33. static void dss_freekey(ssh_key *key)
  34. {
  35. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  36. if (dss->p)
  37. mp_free(dss->p);
  38. if (dss->q)
  39. mp_free(dss->q);
  40. if (dss->g)
  41. mp_free(dss->g);
  42. if (dss->y)
  43. mp_free(dss->y);
  44. if (dss->x)
  45. mp_free(dss->x);
  46. sfree(dss);
  47. }
  48. static void append_hex_to_strbuf(strbuf *sb, mp_int *x)
  49. {
  50. if (sb->len > 0)
  51. put_byte(sb, ',');
  52. put_data(sb, "0x", 2);
  53. { // WINSCP
  54. char *hex = mp_get_hex(x);
  55. size_t hexlen = strlen(hex);
  56. put_data(sb, hex, hexlen);
  57. smemclr(hex, hexlen);
  58. sfree(hex);
  59. } // WINSCP
  60. }
  61. static char *dss_cache_str(ssh_key *key)
  62. {
  63. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  64. strbuf *sb = strbuf_new();
  65. if (!dss->p) {
  66. strbuf_free(sb);
  67. return NULL;
  68. }
  69. append_hex_to_strbuf(sb, dss->p);
  70. append_hex_to_strbuf(sb, dss->q);
  71. append_hex_to_strbuf(sb, dss->g);
  72. append_hex_to_strbuf(sb, dss->y);
  73. return strbuf_to_str(sb);
  74. }
  75. static char *dss_invalid(ssh_key *key, unsigned flags)
  76. {
  77. /* No validity criterion will stop us from using a DSA key at all */
  78. return NULL;
  79. }
  80. static bool dss_verify(ssh_key *key, ptrlen sig, ptrlen data)
  81. {
  82. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  83. BinarySource src[1];
  84. unsigned char hash[20];
  85. bool toret;
  86. if (!dss->p)
  87. return false;
  88. BinarySource_BARE_INIT_PL(src, sig);
  89. /*
  90. * Commercial SSH (2.0.13) and OpenSSH disagree over the format
  91. * of a DSA signature. OpenSSH is in line with RFC 4253:
  92. * it uses a string "ssh-dss", followed by a 40-byte string
  93. * containing two 160-bit integers end-to-end. Commercial SSH
  94. * can't be bothered with the header bit, and considers a DSA
  95. * signature blob to be _just_ the 40-byte string containing
  96. * the two 160-bit integers. We tell them apart by measuring
  97. * the length: length 40 means the commercial-SSH bug, anything
  98. * else is assumed to be RFC-compliant.
  99. */
  100. if (sig.len != 40) { /* bug not present; read admin fields */
  101. ptrlen type = get_string(src);
  102. sig = get_string(src);
  103. if (get_err(src) || !ptrlen_eq_string(type, "ssh-dss") ||
  104. sig.len != 40)
  105. return false;
  106. }
  107. /* Now we're sitting on a 40-byte string for sure. */
  108. { // WINSCP
  109. mp_int *r = mp_from_bytes_be(make_ptrlen(sig.ptr, 20));
  110. mp_int *s = mp_from_bytes_be(make_ptrlen((const char *)sig.ptr + 20, 20));
  111. if (!r || !s) {
  112. if (r)
  113. mp_free(r);
  114. if (s)
  115. mp_free(s);
  116. return false;
  117. }
  118. /* Basic sanity checks: 0 < r,s < q */
  119. { // WINSCP
  120. unsigned invalid = 0;
  121. invalid |= mp_eq_integer(r, 0);
  122. invalid |= mp_eq_integer(s, 0);
  123. invalid |= mp_cmp_hs(r, dss->q);
  124. invalid |= mp_cmp_hs(s, dss->q);
  125. if (invalid) {
  126. mp_free(r);
  127. mp_free(s);
  128. return false;
  129. }
  130. /*
  131. * Step 1. w <- s^-1 mod q.
  132. */
  133. { // WINSCP
  134. mp_int *w = mp_invert(s, dss->q);
  135. if (!w) {
  136. mp_free(r);
  137. mp_free(s);
  138. return false;
  139. }
  140. /*
  141. * Step 2. u1 <- SHA(message) * w mod q.
  142. */
  143. hash_simple(&ssh_sha1, data, hash);
  144. { // WINSCP
  145. mp_int *sha = mp_from_bytes_be(make_ptrlen(hash, 20));
  146. mp_int *u1 = mp_modmul(sha, w, dss->q);
  147. /*
  148. * Step 3. u2 <- r * w mod q.
  149. */
  150. mp_int *u2 = mp_modmul(r, w, dss->q);
  151. /*
  152. * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
  153. */
  154. mp_int *gu1p = mp_modpow(dss->g, u1, dss->p);
  155. mp_int *yu2p = mp_modpow(dss->y, u2, dss->p);
  156. mp_int *gu1yu2p = mp_modmul(gu1p, yu2p, dss->p);
  157. mp_int *v = mp_mod(gu1yu2p, dss->q);
  158. /*
  159. * Step 5. v should now be equal to r.
  160. */
  161. toret = mp_cmp_eq(v, r);
  162. mp_free(w);
  163. mp_free(sha);
  164. mp_free(u1);
  165. mp_free(u2);
  166. mp_free(gu1p);
  167. mp_free(yu2p);
  168. mp_free(gu1yu2p);
  169. mp_free(v);
  170. mp_free(r);
  171. mp_free(s);
  172. } // WINSCP
  173. } // WINSCP
  174. } // WINSCP
  175. } // WINSCP
  176. return toret;
  177. }
  178. static void dss_public_blob(ssh_key *key, BinarySink *bs)
  179. {
  180. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  181. put_stringz(bs, "ssh-dss");
  182. put_mp_ssh2(bs, dss->p);
  183. put_mp_ssh2(bs, dss->q);
  184. put_mp_ssh2(bs, dss->g);
  185. put_mp_ssh2(bs, dss->y);
  186. }
  187. static void dss_private_blob(ssh_key *key, BinarySink *bs)
  188. {
  189. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  190. put_mp_ssh2(bs, dss->x);
  191. }
  192. static ssh_key *dss_new_priv(const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  193. {
  194. BinarySource src[1];
  195. ssh_key *sshk;
  196. struct dss_key *dss;
  197. ptrlen hash;
  198. unsigned char digest[20];
  199. mp_int *ytest;
  200. sshk = dss_new_pub(self, pub);
  201. if (!sshk)
  202. return NULL;
  203. dss = container_of(sshk, struct dss_key, sshk);
  204. BinarySource_BARE_INIT_PL(src, priv);
  205. dss->x = get_mp_ssh2(src);
  206. if (get_err(src)) {
  207. dss_freekey(&dss->sshk);
  208. return NULL;
  209. }
  210. /*
  211. * Check the obsolete hash in the old DSS key format.
  212. */
  213. hash = get_string(src);
  214. if (hash.len == 20) {
  215. ssh_hash *h = ssh_hash_new(&ssh_sha1);
  216. put_mp_ssh2(h, dss->p);
  217. put_mp_ssh2(h, dss->q);
  218. put_mp_ssh2(h, dss->g);
  219. ssh_hash_final(h, digest);
  220. if (!smemeq(hash.ptr, digest, 20)) {
  221. dss_freekey(&dss->sshk);
  222. return NULL;
  223. }
  224. }
  225. /*
  226. * Now ensure g^x mod p really is y.
  227. */
  228. ytest = mp_modpow(dss->g, dss->x, dss->p);
  229. if (!mp_cmp_eq(ytest, dss->y)) {
  230. mp_free(ytest);
  231. dss_freekey(&dss->sshk);
  232. return NULL;
  233. }
  234. mp_free(ytest);
  235. return &dss->sshk;
  236. }
  237. static ssh_key *dss_new_priv_openssh(const ssh_keyalg *self,
  238. BinarySource *src)
  239. {
  240. struct dss_key *dss;
  241. dss = snew(struct dss_key);
  242. dss->sshk.vt = &ssh_dss;
  243. dss->p = get_mp_ssh2(src);
  244. dss->q = get_mp_ssh2(src);
  245. dss->g = get_mp_ssh2(src);
  246. dss->y = get_mp_ssh2(src);
  247. dss->x = get_mp_ssh2(src);
  248. if (get_err(src) ||
  249. mp_eq_integer(dss->q, 0) || mp_eq_integer(dss->p, 0)) {
  250. /* Invalid key. */
  251. dss_freekey(&dss->sshk);
  252. return NULL;
  253. }
  254. return &dss->sshk;
  255. }
  256. static void dss_openssh_blob(ssh_key *key, BinarySink *bs)
  257. {
  258. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  259. put_mp_ssh2(bs, dss->p);
  260. put_mp_ssh2(bs, dss->q);
  261. put_mp_ssh2(bs, dss->g);
  262. put_mp_ssh2(bs, dss->y);
  263. put_mp_ssh2(bs, dss->x);
  264. }
  265. static int dss_pubkey_bits(const ssh_keyalg *self, ptrlen pub)
  266. {
  267. ssh_key *sshk;
  268. struct dss_key *dss;
  269. int ret;
  270. sshk = dss_new_pub(self, pub);
  271. if (!sshk)
  272. return -1;
  273. dss = container_of(sshk, struct dss_key, sshk);
  274. ret = mp_get_nbits(dss->p);
  275. dss_freekey(&dss->sshk);
  276. return ret;
  277. }
  278. mp_int *dss_gen_k(const char *id_string, mp_int *modulus,
  279. mp_int *private_key,
  280. unsigned char *digest, int digest_len)
  281. {
  282. /*
  283. * The basic DSS signing algorithm is:
  284. *
  285. * - invent a random k between 1 and q-1 (exclusive).
  286. * - Compute r = (g^k mod p) mod q.
  287. * - Compute s = k^-1 * (hash + x*r) mod q.
  288. *
  289. * This has the dangerous properties that:
  290. *
  291. * - if an attacker in possession of the public key _and_ the
  292. * signature (for example, the host you just authenticated
  293. * to) can guess your k, he can reverse the computation of s
  294. * and work out x = r^-1 * (s*k - hash) mod q. That is, he
  295. * can deduce the private half of your key, and masquerade
  296. * as you for as long as the key is still valid.
  297. *
  298. * - since r is a function purely of k and the public key, if
  299. * the attacker only has a _range of possibilities_ for k
  300. * it's easy for him to work through them all and check each
  301. * one against r; he'll never be unsure of whether he's got
  302. * the right one.
  303. *
  304. * - if you ever sign two different hashes with the same k, it
  305. * will be immediately obvious because the two signatures
  306. * will have the same r, and moreover an attacker in
  307. * possession of both signatures (and the public key of
  308. * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
  309. * and from there deduce x as before.
  310. *
  311. * - the Bleichenbacher attack on DSA makes use of methods of
  312. * generating k which are significantly non-uniformly
  313. * distributed; in particular, generating a 160-bit random
  314. * number and reducing it mod q is right out.
  315. *
  316. * For this reason we must be pretty careful about how we
  317. * generate our k. Since this code runs on Windows, with no
  318. * particularly good system entropy sources, we can't trust our
  319. * RNG itself to produce properly unpredictable data. Hence, we
  320. * use a totally different scheme instead.
  321. *
  322. * What we do is to take a SHA-512 (_big_) hash of the private
  323. * key x, and then feed this into another SHA-512 hash that
  324. * also includes the message hash being signed. That is:
  325. *
  326. * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
  327. *
  328. * This number is 512 bits long, so reducing it mod q won't be
  329. * noticeably non-uniform. So
  330. *
  331. * k = proto_k mod q
  332. *
  333. * This has the interesting property that it's _deterministic_:
  334. * signing the same hash twice with the same key yields the
  335. * same signature.
  336. *
  337. * Despite this determinism, it's still not predictable to an
  338. * attacker, because in order to repeat the SHA-512
  339. * construction that created it, the attacker would have to
  340. * know the private key value x - and by assumption he doesn't,
  341. * because if he knew that he wouldn't be attacking k!
  342. *
  343. * (This trick doesn't, _per se_, protect against reuse of k.
  344. * Reuse of k is left to chance; all it does is prevent
  345. * _excessively high_ chances of reuse of k due to entropy
  346. * problems.)
  347. *
  348. * Thanks to Colin Plumb for the general idea of using x to
  349. * ensure k is hard to guess, and to the Cambridge University
  350. * Computer Security Group for helping to argue out all the
  351. * fine details.
  352. */
  353. ssh_hash *h;
  354. unsigned char digest512[64];
  355. /*
  356. * Hash some identifying text plus x.
  357. */
  358. h = ssh_hash_new(&ssh_sha512);
  359. put_asciz(h, id_string);
  360. put_mp_ssh2(h, private_key);
  361. ssh_hash_final(h, digest512);
  362. /*
  363. * Now hash that digest plus the message hash.
  364. */
  365. h = ssh_hash_new(&ssh_sha512);
  366. put_data(h, digest512, sizeof(digest512));
  367. put_data(h, digest, digest_len);
  368. ssh_hash_final(h, digest512);
  369. /*
  370. * Now convert the result into a bignum, and coerce it to the
  371. * range [2,q), which we do by reducing it mod q-2 and adding 2.
  372. */
  373. { // WINSCP
  374. mp_int *modminus2 = mp_copy(modulus);
  375. mp_sub_integer_into(modminus2, modminus2, 2);
  376. { // WINSCP
  377. mp_int *proto_k = mp_from_bytes_be(make_ptrlen(digest512, 64));
  378. mp_int *k = mp_mod(proto_k, modminus2);
  379. mp_free(proto_k);
  380. mp_free(modminus2);
  381. mp_add_integer_into(k, k, 2);
  382. smemclr(digest512, sizeof(digest512));
  383. return k;
  384. } // WINSCP
  385. } // WINSCP
  386. }
  387. static void dss_sign(ssh_key *key, ptrlen data, unsigned flags, BinarySink *bs)
  388. {
  389. struct dss_key *dss = container_of(key, struct dss_key, sshk);
  390. unsigned char digest[20];
  391. int i;
  392. hash_simple(&ssh_sha1, data, digest);
  393. { // WINSCP
  394. mp_int *k = dss_gen_k("DSA deterministic k generator", dss->q, dss->x,
  395. digest, sizeof(digest));
  396. mp_int *kinv = mp_invert(k, dss->q); /* k^-1 mod q */
  397. /*
  398. * Now we have k, so just go ahead and compute the signature.
  399. */
  400. mp_int *gkp = mp_modpow(dss->g, k, dss->p); /* g^k mod p */
  401. mp_int *r = mp_mod(gkp, dss->q); /* r = (g^k mod p) mod q */
  402. mp_free(gkp);
  403. { // WINSCP
  404. mp_int *hash = mp_from_bytes_be(make_ptrlen(digest, 20));
  405. mp_int *xr = mp_mul(dss->x, r);
  406. mp_int *hxr = mp_add(xr, hash); /* hash + x*r */
  407. { // WINSCP
  408. mp_int *s = mp_modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash+x*r) mod q */
  409. mp_free(hxr);
  410. mp_free(xr);
  411. mp_free(kinv);
  412. mp_free(k);
  413. mp_free(hash);
  414. put_stringz(bs, "ssh-dss");
  415. put_uint32(bs, 40);
  416. for (i = 0; i < 20; i++)
  417. put_byte(bs, mp_get_byte(r, 19 - i));
  418. for (i = 0; i < 20; i++)
  419. put_byte(bs, mp_get_byte(s, 19 - i));
  420. mp_free(r);
  421. mp_free(s);
  422. } // WINSCP
  423. } // WINSCP
  424. } // WINSCP
  425. }
  426. const ssh_keyalg ssh_dss = {
  427. dss_new_pub,
  428. dss_new_priv,
  429. dss_new_priv_openssh,
  430. dss_freekey,
  431. dss_invalid,
  432. dss_sign,
  433. dss_verify,
  434. dss_public_blob,
  435. dss_private_blob,
  436. dss_openssh_blob,
  437. dss_cache_str,
  438. dss_pubkey_bits,
  439. "ssh-dss",
  440. "dss",
  441. NULL,
  442. 0, /* no supported flags */
  443. };