25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

Fix various certificate fingerprint issues. By using non-DER or invalid encodings outside the signed portion of a certificate the fingerprint can be changed without breaking the signature. Although no details of the signed portion of the certificate can be changed this can cause problems with some applications: e.g. those using the certificate fingerprint for blacklists. 1. Reject signatures with non zero unused bits. If the BIT STRING containing the signature has non zero unused bits reject the signature. All current signature algorithms require zero unused bits. 2. Check certificate algorithm consistency. Check the AlgorithmIdentifier inside TBS matches the one in the certificate signature. NB: this will result in signature failure errors for some broken certificates. 3. Check DSA/ECDSA signatures use DER. Reencode DSA/ECDSA signatures and compare with the original received signature. Return an error if there is a mismatch. This will reject various cases including garbage after signature (thanks to Antti Karjalainen and Tuomo Untinen from the Codenomicon CROSS program for discovering this case) and use of BER or invalid ASN.1 INTEGERs (negative or with leading zeroes). CVE-2014-8275 (Imported from upstream's 85cfc188c06bd046420ae70dd6e302f9efe022a9 and 4c52816d35681c0533c25fdd3abb4b7c6962302d) Change-Id: Ic901aea8ea6457df27dc542a11c30464561e322b Reviewed-on: https://boringssl-review.googlesource.com/2783 Reviewed-by: David Benjamin <davidben@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
9 yıl önce
Fix various certificate fingerprint issues. By using non-DER or invalid encodings outside the signed portion of a certificate the fingerprint can be changed without breaking the signature. Although no details of the signed portion of the certificate can be changed this can cause problems with some applications: e.g. those using the certificate fingerprint for blacklists. 1. Reject signatures with non zero unused bits. If the BIT STRING containing the signature has non zero unused bits reject the signature. All current signature algorithms require zero unused bits. 2. Check certificate algorithm consistency. Check the AlgorithmIdentifier inside TBS matches the one in the certificate signature. NB: this will result in signature failure errors for some broken certificates. 3. Check DSA/ECDSA signatures use DER. Reencode DSA/ECDSA signatures and compare with the original received signature. Return an error if there is a mismatch. This will reject various cases including garbage after signature (thanks to Antti Karjalainen and Tuomo Untinen from the Codenomicon CROSS program for discovering this case) and use of BER or invalid ASN.1 INTEGERs (negative or with leading zeroes). CVE-2014-8275 (Imported from upstream's 85cfc188c06bd046420ae70dd6e302f9efe022a9 and 4c52816d35681c0533c25fdd3abb4b7c6962302d) Change-Id: Ic901aea8ea6457df27dc542a11c30464561e322b Reviewed-on: https://boringssl-review.googlesource.com/2783 Reviewed-by: David Benjamin <davidben@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
9 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. *
  57. * The DSS routines are based on patches supplied by
  58. * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
  59. #include <openssl/dsa.h>
  60. #include <string.h>
  61. #include <openssl/bn.h>
  62. #include <openssl/dh.h>
  63. #include <openssl/digest.h>
  64. #include <openssl/engine.h>
  65. #include <openssl/err.h>
  66. #include <openssl/ex_data.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/rand.h>
  69. #include <openssl/sha.h>
  70. #include <openssl/thread.h>
  71. #include "../internal.h"
  72. #define OPENSSL_DSA_MAX_MODULUS_BITS 10000
  73. /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
  74. * Rabin-Miller */
  75. #define DSS_prime_checks 50
  76. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  77. DSA *DSA_new(void) {
  78. DSA *dsa = OPENSSL_malloc(sizeof(DSA));
  79. if (dsa == NULL) {
  80. OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
  81. return NULL;
  82. }
  83. memset(dsa, 0, sizeof(DSA));
  84. dsa->references = 1;
  85. CRYPTO_MUTEX_init(&dsa->method_mont_lock);
  86. CRYPTO_new_ex_data(&dsa->ex_data);
  87. return dsa;
  88. }
  89. void DSA_free(DSA *dsa) {
  90. if (dsa == NULL) {
  91. return;
  92. }
  93. if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
  94. return;
  95. }
  96. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  97. BN_clear_free(dsa->p);
  98. BN_clear_free(dsa->q);
  99. BN_clear_free(dsa->g);
  100. BN_clear_free(dsa->pub_key);
  101. BN_clear_free(dsa->priv_key);
  102. BN_clear_free(dsa->kinv);
  103. BN_clear_free(dsa->r);
  104. BN_MONT_CTX_free(dsa->method_mont_p);
  105. BN_MONT_CTX_free(dsa->method_mont_q);
  106. CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
  107. OPENSSL_free(dsa);
  108. }
  109. int DSA_up_ref(DSA *dsa) {
  110. CRYPTO_refcount_inc(&dsa->references);
  111. return 1;
  112. }
  113. void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
  114. const BIGNUM **out_priv_key) {
  115. if (out_pub_key != NULL) {
  116. *out_pub_key = dsa->pub_key;
  117. }
  118. if (out_priv_key != NULL) {
  119. *out_priv_key = dsa->priv_key;
  120. }
  121. }
  122. void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
  123. const BIGNUM **out_g) {
  124. if (out_p != NULL) {
  125. *out_p = dsa->p;
  126. }
  127. if (out_q != NULL) {
  128. *out_q = dsa->q;
  129. }
  130. if (out_g != NULL) {
  131. *out_g = dsa->g;
  132. }
  133. }
  134. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  135. size_t seed_len, int *out_counter,
  136. unsigned long *out_h, BN_GENCB *cb) {
  137. int ok = 0;
  138. unsigned char seed[SHA256_DIGEST_LENGTH];
  139. unsigned char md[SHA256_DIGEST_LENGTH];
  140. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  141. BIGNUM *r0, *W, *X, *c, *test;
  142. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  143. BN_MONT_CTX *mont = NULL;
  144. int k, n = 0, m = 0;
  145. unsigned i;
  146. int counter = 0;
  147. int r = 0;
  148. BN_CTX *ctx = NULL;
  149. unsigned int h = 2;
  150. unsigned qsize;
  151. const EVP_MD *evpmd;
  152. evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
  153. qsize = EVP_MD_size(evpmd);
  154. if (bits < 512) {
  155. bits = 512;
  156. }
  157. bits = (bits + 63) / 64 * 64;
  158. if (seed_in != NULL) {
  159. if (seed_len < (size_t)qsize) {
  160. return 0;
  161. }
  162. if (seed_len > (size_t)qsize) {
  163. /* Only consume as much seed as is expected. */
  164. seed_len = qsize;
  165. }
  166. memcpy(seed, seed_in, seed_len);
  167. }
  168. ctx = BN_CTX_new();
  169. if (ctx == NULL) {
  170. goto err;
  171. }
  172. BN_CTX_start(ctx);
  173. mont = BN_MONT_CTX_new();
  174. if (mont == NULL) {
  175. goto err;
  176. }
  177. r0 = BN_CTX_get(ctx);
  178. g = BN_CTX_get(ctx);
  179. W = BN_CTX_get(ctx);
  180. q = BN_CTX_get(ctx);
  181. X = BN_CTX_get(ctx);
  182. c = BN_CTX_get(ctx);
  183. p = BN_CTX_get(ctx);
  184. test = BN_CTX_get(ctx);
  185. if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
  186. goto err;
  187. }
  188. for (;;) {
  189. /* Find q. */
  190. for (;;) {
  191. /* step 1 */
  192. if (!BN_GENCB_call(cb, 0, m++)) {
  193. goto err;
  194. }
  195. int use_random_seed = (seed_in == NULL);
  196. if (use_random_seed) {
  197. if (!RAND_bytes(seed, qsize)) {
  198. goto err;
  199. }
  200. } else {
  201. /* If we come back through, use random seed next time. */
  202. seed_in = NULL;
  203. }
  204. memcpy(buf, seed, qsize);
  205. memcpy(buf2, seed, qsize);
  206. /* precompute "SEED + 1" for step 7: */
  207. for (i = qsize - 1; i < qsize; i--) {
  208. buf[i]++;
  209. if (buf[i] != 0) {
  210. break;
  211. }
  212. }
  213. /* step 2 */
  214. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
  215. !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
  216. goto err;
  217. }
  218. for (i = 0; i < qsize; i++) {
  219. md[i] ^= buf2[i];
  220. }
  221. /* step 3 */
  222. md[0] |= 0x80;
  223. md[qsize - 1] |= 0x01;
  224. if (!BN_bin2bn(md, qsize, q)) {
  225. goto err;
  226. }
  227. /* step 4 */
  228. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
  229. if (r > 0) {
  230. break;
  231. }
  232. if (r != 0) {
  233. goto err;
  234. }
  235. /* do a callback call */
  236. /* step 5 */
  237. }
  238. if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
  239. goto err;
  240. }
  241. /* step 6 */
  242. counter = 0;
  243. /* "offset = 2" */
  244. n = (bits - 1) / 160;
  245. for (;;) {
  246. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
  247. goto err;
  248. }
  249. /* step 7 */
  250. BN_zero(W);
  251. /* now 'buf' contains "SEED + offset - 1" */
  252. for (k = 0; k <= n; k++) {
  253. /* obtain "SEED + offset + k" by incrementing: */
  254. for (i = qsize - 1; i < qsize; i--) {
  255. buf[i]++;
  256. if (buf[i] != 0) {
  257. break;
  258. }
  259. }
  260. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
  261. goto err;
  262. }
  263. /* step 8 */
  264. if (!BN_bin2bn(md, qsize, r0) ||
  265. !BN_lshift(r0, r0, (qsize << 3) * k) ||
  266. !BN_add(W, W, r0)) {
  267. goto err;
  268. }
  269. }
  270. /* more of step 8 */
  271. if (!BN_mask_bits(W, bits - 1) ||
  272. !BN_copy(X, W) ||
  273. !BN_add(X, X, test)) {
  274. goto err;
  275. }
  276. /* step 9 */
  277. if (!BN_lshift1(r0, q) ||
  278. !BN_mod(c, X, r0, ctx) ||
  279. !BN_sub(r0, c, BN_value_one()) ||
  280. !BN_sub(p, X, r0)) {
  281. goto err;
  282. }
  283. /* step 10 */
  284. if (BN_cmp(p, test) >= 0) {
  285. /* step 11 */
  286. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  287. if (r > 0) {
  288. goto end; /* found it */
  289. }
  290. if (r != 0) {
  291. goto err;
  292. }
  293. }
  294. /* step 13 */
  295. counter++;
  296. /* "offset = offset + n + 1" */
  297. /* step 14 */
  298. if (counter >= 4096) {
  299. break;
  300. }
  301. }
  302. }
  303. end:
  304. if (!BN_GENCB_call(cb, 2, 1)) {
  305. goto err;
  306. }
  307. /* We now need to generate g */
  308. /* Set r0=(p-1)/q */
  309. if (!BN_sub(test, p, BN_value_one()) ||
  310. !BN_div(r0, NULL, test, q, ctx)) {
  311. goto err;
  312. }
  313. if (!BN_set_word(test, h) ||
  314. !BN_MONT_CTX_set(mont, p, ctx)) {
  315. goto err;
  316. }
  317. for (;;) {
  318. /* g=test^r0%p */
  319. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
  320. goto err;
  321. }
  322. if (!BN_is_one(g)) {
  323. break;
  324. }
  325. if (!BN_add(test, test, BN_value_one())) {
  326. goto err;
  327. }
  328. h++;
  329. }
  330. if (!BN_GENCB_call(cb, 3, 1)) {
  331. goto err;
  332. }
  333. ok = 1;
  334. err:
  335. if (ok) {
  336. BN_free(dsa->p);
  337. BN_free(dsa->q);
  338. BN_free(dsa->g);
  339. dsa->p = BN_dup(p);
  340. dsa->q = BN_dup(q);
  341. dsa->g = BN_dup(g);
  342. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  343. ok = 0;
  344. goto err;
  345. }
  346. if (out_counter != NULL) {
  347. *out_counter = counter;
  348. }
  349. if (out_h != NULL) {
  350. *out_h = h;
  351. }
  352. }
  353. if (ctx) {
  354. BN_CTX_end(ctx);
  355. BN_CTX_free(ctx);
  356. }
  357. BN_MONT_CTX_free(mont);
  358. return ok;
  359. }
  360. DSA *DSAparams_dup(const DSA *dsa) {
  361. DSA *ret = DSA_new();
  362. if (ret == NULL) {
  363. return NULL;
  364. }
  365. ret->p = BN_dup(dsa->p);
  366. ret->q = BN_dup(dsa->q);
  367. ret->g = BN_dup(dsa->g);
  368. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  369. DSA_free(ret);
  370. return NULL;
  371. }
  372. return ret;
  373. }
  374. int DSA_generate_key(DSA *dsa) {
  375. int ok = 0;
  376. BN_CTX *ctx = NULL;
  377. BIGNUM *pub_key = NULL, *priv_key = NULL;
  378. BIGNUM prk;
  379. ctx = BN_CTX_new();
  380. if (ctx == NULL) {
  381. goto err;
  382. }
  383. priv_key = dsa->priv_key;
  384. if (priv_key == NULL) {
  385. priv_key = BN_new();
  386. if (priv_key == NULL) {
  387. goto err;
  388. }
  389. }
  390. if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
  391. goto err;
  392. }
  393. pub_key = dsa->pub_key;
  394. if (pub_key == NULL) {
  395. pub_key = BN_new();
  396. if (pub_key == NULL) {
  397. goto err;
  398. }
  399. }
  400. BN_init(&prk);
  401. BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME);
  402. if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
  403. dsa->p, ctx) ||
  404. !BN_mod_exp_mont_consttime(pub_key, dsa->g, &prk, dsa->p, ctx,
  405. dsa->method_mont_p)) {
  406. goto err;
  407. }
  408. dsa->priv_key = priv_key;
  409. dsa->pub_key = pub_key;
  410. ok = 1;
  411. err:
  412. if (dsa->pub_key == NULL) {
  413. BN_free(pub_key);
  414. }
  415. if (dsa->priv_key == NULL) {
  416. BN_free(priv_key);
  417. }
  418. BN_CTX_free(ctx);
  419. return ok;
  420. }
  421. DSA_SIG *DSA_SIG_new(void) {
  422. DSA_SIG *sig;
  423. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  424. if (!sig) {
  425. return NULL;
  426. }
  427. sig->r = NULL;
  428. sig->s = NULL;
  429. return sig;
  430. }
  431. void DSA_SIG_free(DSA_SIG *sig) {
  432. if (!sig) {
  433. return;
  434. }
  435. BN_free(sig->r);
  436. BN_free(sig->s);
  437. OPENSSL_free(sig);
  438. }
  439. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  440. BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
  441. BIGNUM m;
  442. BIGNUM xr;
  443. BN_CTX *ctx = NULL;
  444. int reason = ERR_R_BN_LIB;
  445. DSA_SIG *ret = NULL;
  446. int noredo = 0;
  447. BN_init(&m);
  448. BN_init(&xr);
  449. if (!dsa->p || !dsa->q || !dsa->g) {
  450. reason = DSA_R_MISSING_PARAMETERS;
  451. goto err;
  452. }
  453. s = BN_new();
  454. if (s == NULL) {
  455. goto err;
  456. }
  457. ctx = BN_CTX_new();
  458. if (ctx == NULL) {
  459. goto err;
  460. }
  461. redo:
  462. if (dsa->kinv == NULL || dsa->r == NULL) {
  463. if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
  464. goto err;
  465. }
  466. } else {
  467. kinv = dsa->kinv;
  468. dsa->kinv = NULL;
  469. r = dsa->r;
  470. dsa->r = NULL;
  471. noredo = 1;
  472. }
  473. if (digest_len > BN_num_bytes(dsa->q)) {
  474. /* if the digest length is greater than the size of q use the
  475. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  476. * fips 186-3, 4.2 */
  477. digest_len = BN_num_bytes(dsa->q);
  478. }
  479. if (BN_bin2bn(digest, digest_len, &m) == NULL) {
  480. goto err;
  481. }
  482. /* Compute s = inv(k) (m + xr) mod q */
  483. if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
  484. goto err; /* s = xr */
  485. }
  486. if (!BN_add(s, &xr, &m)) {
  487. goto err; /* s = m + xr */
  488. }
  489. if (BN_cmp(s, dsa->q) > 0) {
  490. if (!BN_sub(s, s, dsa->q)) {
  491. goto err;
  492. }
  493. }
  494. if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
  495. goto err;
  496. }
  497. /* Redo if r or s is zero as required by FIPS 186-3: this is
  498. * very unlikely. */
  499. if (BN_is_zero(r) || BN_is_zero(s)) {
  500. if (noredo) {
  501. reason = DSA_R_NEED_NEW_SETUP_VALUES;
  502. goto err;
  503. }
  504. goto redo;
  505. }
  506. ret = DSA_SIG_new();
  507. if (ret == NULL) {
  508. goto err;
  509. }
  510. ret->r = r;
  511. ret->s = s;
  512. err:
  513. if (ret == NULL) {
  514. OPENSSL_PUT_ERROR(DSA, reason);
  515. BN_free(r);
  516. BN_free(s);
  517. }
  518. BN_CTX_free(ctx);
  519. BN_clear_free(&m);
  520. BN_clear_free(&xr);
  521. BN_clear_free(kinv);
  522. return ret;
  523. }
  524. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  525. const DSA *dsa) {
  526. int valid;
  527. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  528. return -1;
  529. }
  530. return valid;
  531. }
  532. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  533. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  534. BN_CTX *ctx;
  535. BIGNUM u1, u2, t1;
  536. int ret = 0;
  537. unsigned i;
  538. *out_valid = 0;
  539. if (!dsa->p || !dsa->q || !dsa->g) {
  540. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  541. return 0;
  542. }
  543. i = BN_num_bits(dsa->q);
  544. /* fips 186-3 allows only different sizes for q */
  545. if (i != 160 && i != 224 && i != 256) {
  546. OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
  547. return 0;
  548. }
  549. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  550. OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
  551. return 0;
  552. }
  553. BN_init(&u1);
  554. BN_init(&u2);
  555. BN_init(&t1);
  556. ctx = BN_CTX_new();
  557. if (ctx == NULL) {
  558. goto err;
  559. }
  560. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  561. BN_ucmp(sig->r, dsa->q) >= 0) {
  562. ret = 1;
  563. goto err;
  564. }
  565. if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
  566. BN_ucmp(sig->s, dsa->q) >= 0) {
  567. ret = 1;
  568. goto err;
  569. }
  570. /* Calculate W = inv(S) mod Q
  571. * save W in u2 */
  572. if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
  573. goto err;
  574. }
  575. /* save M in u1 */
  576. if (digest_len > (i >> 3)) {
  577. /* if the digest length is greater than the size of q use the
  578. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  579. * fips 186-3, 4.2 */
  580. digest_len = (i >> 3);
  581. }
  582. if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
  583. goto err;
  584. }
  585. /* u1 = M * w mod q */
  586. if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
  587. goto err;
  588. }
  589. /* u2 = r * w mod q */
  590. if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
  591. goto err;
  592. }
  593. if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  594. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  595. ctx)) {
  596. goto err;
  597. }
  598. if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
  599. dsa->method_mont_p)) {
  600. goto err;
  601. }
  602. /* BN_copy(&u1,&t1); */
  603. /* let u1 = u1 mod q */
  604. if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
  605. goto err;
  606. }
  607. /* V is now in u1. If the signature is correct, it will be
  608. * equal to R. */
  609. *out_valid = BN_ucmp(&u1, sig->r) == 0;
  610. ret = 1;
  611. err:
  612. if (ret != 1) {
  613. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  614. }
  615. BN_CTX_free(ctx);
  616. BN_free(&u1);
  617. BN_free(&u2);
  618. BN_free(&t1);
  619. return ret;
  620. }
  621. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  622. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  623. DSA_SIG *s;
  624. s = DSA_do_sign(digest, digest_len, dsa);
  625. if (s == NULL) {
  626. *out_siglen = 0;
  627. return 0;
  628. }
  629. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  630. DSA_SIG_free(s);
  631. return 1;
  632. }
  633. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  634. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  635. int valid;
  636. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  637. return -1;
  638. }
  639. return valid;
  640. }
  641. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  642. size_t digest_len, const uint8_t *sig, size_t sig_len,
  643. const DSA *dsa) {
  644. DSA_SIG *s = NULL;
  645. int ret = 0;
  646. uint8_t *der = NULL;
  647. s = DSA_SIG_new();
  648. if (s == NULL) {
  649. goto err;
  650. }
  651. const uint8_t *sigp = sig;
  652. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  653. goto err;
  654. }
  655. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  656. int der_len = i2d_DSA_SIG(s, &der);
  657. if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
  658. goto err;
  659. }
  660. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  661. err:
  662. OPENSSL_free(der);
  663. DSA_SIG_free(s);
  664. return ret;
  665. }
  666. /* der_len_len returns the number of bytes needed to represent a length of |len|
  667. * in DER. */
  668. static size_t der_len_len(size_t len) {
  669. if (len < 0x80) {
  670. return 1;
  671. }
  672. size_t ret = 1;
  673. while (len > 0) {
  674. ret++;
  675. len >>= 8;
  676. }
  677. return ret;
  678. }
  679. int DSA_size(const DSA *dsa) {
  680. size_t order_len = BN_num_bytes(dsa->q);
  681. /* Compute the maximum length of an |order_len| byte integer. Defensively
  682. * assume that the leading 0x00 is included. */
  683. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
  684. if (integer_len < order_len) {
  685. return 0;
  686. }
  687. /* A DSA signature is two INTEGERs. */
  688. size_t value_len = 2 * integer_len;
  689. if (value_len < integer_len) {
  690. return 0;
  691. }
  692. /* Add the header. */
  693. size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
  694. if (ret < value_len) {
  695. return 0;
  696. }
  697. return ret;
  698. }
  699. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
  700. BIGNUM **out_r) {
  701. BN_CTX *ctx;
  702. BIGNUM k, kq, qm2, *kinv = NULL, *r = NULL;
  703. int ret = 0;
  704. if (!dsa->p || !dsa->q || !dsa->g) {
  705. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  706. return 0;
  707. }
  708. BN_init(&k);
  709. BN_init(&kq);
  710. BN_init(&qm2);
  711. ctx = ctx_in;
  712. if (ctx == NULL) {
  713. ctx = BN_CTX_new();
  714. if (ctx == NULL) {
  715. goto err;
  716. }
  717. }
  718. r = BN_new();
  719. if (r == NULL) {
  720. goto err;
  721. }
  722. /* Get random k */
  723. if (!BN_rand_range_ex(&k, 1, dsa->q)) {
  724. goto err;
  725. }
  726. BN_set_flags(&k, BN_FLG_CONSTTIME);
  727. if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  728. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
  729. ctx) ||
  730. !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
  731. (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
  732. ctx)) {
  733. goto err;
  734. }
  735. /* Compute r = (g^k mod p) mod q */
  736. if (!BN_copy(&kq, &k)) {
  737. goto err;
  738. }
  739. /* We do not want timing information to leak the length of k,
  740. * so we compute g^k using an equivalent exponent of fixed length.
  741. *
  742. * (This is a kludge that we need because the BN_mod_exp_mont()
  743. * does not let us specify the desired timing behaviour.) */
  744. if (!BN_add(&kq, &kq, dsa->q)) {
  745. goto err;
  746. }
  747. if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
  748. goto err;
  749. }
  750. BN_set_flags(&kq, BN_FLG_CONSTTIME);
  751. if (!BN_mod_exp_mont_consttime(r, dsa->g, &kq, dsa->p, ctx,
  752. dsa->method_mont_p)) {
  753. goto err;
  754. }
  755. if (!BN_mod(r, r, dsa->q, ctx)) {
  756. goto err;
  757. }
  758. /* Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
  759. * Theorem. */
  760. kinv = BN_new();
  761. if (kinv == NULL ||
  762. !BN_set_word(&qm2, 2) ||
  763. !BN_sub(&qm2, dsa->q, &qm2) ||
  764. !BN_mod_exp_mont(kinv, &k, &qm2, dsa->q, ctx, dsa->method_mont_q)) {
  765. goto err;
  766. }
  767. BN_clear_free(*out_kinv);
  768. *out_kinv = kinv;
  769. kinv = NULL;
  770. BN_clear_free(*out_r);
  771. *out_r = r;
  772. ret = 1;
  773. err:
  774. if (!ret) {
  775. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  776. if (r != NULL) {
  777. BN_clear_free(r);
  778. }
  779. }
  780. if (ctx_in == NULL) {
  781. BN_CTX_free(ctx);
  782. }
  783. BN_clear_free(&k);
  784. BN_clear_free(&kq);
  785. BN_free(&qm2);
  786. return ret;
  787. }
  788. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  789. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  790. int index;
  791. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  792. free_func)) {
  793. return -1;
  794. }
  795. return index;
  796. }
  797. int DSA_set_ex_data(DSA *d, int idx, void *arg) {
  798. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  799. }
  800. void *DSA_get_ex_data(const DSA *d, int idx) {
  801. return CRYPTO_get_ex_data(&d->ex_data, idx);
  802. }
  803. DH *DSA_dup_DH(const DSA *r) {
  804. DH *ret = NULL;
  805. if (r == NULL) {
  806. goto err;
  807. }
  808. ret = DH_new();
  809. if (ret == NULL) {
  810. goto err;
  811. }
  812. if (r->q != NULL) {
  813. ret->priv_length = BN_num_bits(r->q);
  814. if ((ret->q = BN_dup(r->q)) == NULL) {
  815. goto err;
  816. }
  817. }
  818. if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
  819. (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
  820. (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
  821. (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
  822. goto err;
  823. }
  824. return ret;
  825. err:
  826. DH_free(ret);
  827. return NULL;
  828. }