sshdss.c 14 KB

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