Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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 роки тому
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 роки тому
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 роки тому
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 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* ====================================================================
  2. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <openssl/ecdsa.h>
  53. #include <openssl/bn.h>
  54. #include <openssl/err.h>
  55. #include <openssl/mem.h>
  56. #include <openssl/thread.h>
  57. #include "../ec/internal.h"
  58. int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
  59. unsigned int *sig_len, EC_KEY *eckey) {
  60. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  61. return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len, eckey);
  62. }
  63. return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
  64. eckey);
  65. }
  66. int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
  67. const uint8_t *sig, size_t sig_len, EC_KEY *eckey) {
  68. ECDSA_SIG *s;
  69. int ret = 0;
  70. uint8_t *der = NULL;
  71. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  72. return eckey->ecdsa_meth->verify(digest, digest_len, sig, sig_len, eckey);
  73. }
  74. s = ECDSA_SIG_new();
  75. const uint8_t *sigp = sig;
  76. if (s == NULL || d2i_ECDSA_SIG(&s, &sigp, sig_len) == NULL ||
  77. sigp != sig + sig_len) {
  78. goto err;
  79. }
  80. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  81. const int der_len = i2d_ECDSA_SIG(s, &der);
  82. if (der_len < 0 || (size_t) der_len != sig_len || memcmp(sig, der, sig_len)) {
  83. goto err;
  84. }
  85. ret = ECDSA_do_verify(digest, digest_len, s, eckey);
  86. err:
  87. if (der != NULL) {
  88. OPENSSL_free(der);
  89. }
  90. if (s != NULL) {
  91. ECDSA_SIG_free(s);
  92. }
  93. return ret;
  94. }
  95. /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
  96. * number and sets |out| to that value. It then truncates |out| so that it's,
  97. * at most, as long as |order|. It returns one on success and zero otherwise. */
  98. static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
  99. const BIGNUM *order) {
  100. size_t num_bits;
  101. num_bits = BN_num_bits(order);
  102. /* Need to truncate digest if it is too long: first truncate whole
  103. * bytes. */
  104. if (8 * digest_len > num_bits) {
  105. digest_len = (num_bits + 7) / 8;
  106. }
  107. if (!BN_bin2bn(digest, digest_len, out)) {
  108. OPENSSL_PUT_ERROR(ECDSA, digest_to_bn, ERR_R_BN_LIB);
  109. return 0;
  110. }
  111. /* If still too long truncate remaining bits with a shift */
  112. if ((8 * digest_len > num_bits) &&
  113. !BN_rshift(out, out, 8 - (num_bits & 0x7))) {
  114. OPENSSL_PUT_ERROR(ECDSA, digest_to_bn, ERR_R_BN_LIB);
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
  120. EC_KEY *key) {
  121. return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
  122. }
  123. int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
  124. const ECDSA_SIG *sig, EC_KEY *eckey) {
  125. int ret = 0;
  126. BN_CTX *ctx;
  127. BIGNUM *order, *u1, *u2, *m, *X;
  128. EC_POINT *point = NULL;
  129. const EC_GROUP *group;
  130. const EC_POINT *pub_key;
  131. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  132. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_NOT_IMPLEMENTED);
  133. return 0;
  134. }
  135. /* check input values */
  136. if ((group = EC_KEY_get0_group(eckey)) == NULL ||
  137. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
  138. sig == NULL) {
  139. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_MISSING_PARAMETERS);
  140. return 0;
  141. }
  142. ctx = BN_CTX_new();
  143. if (!ctx) {
  144. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  145. return 0;
  146. }
  147. BN_CTX_start(ctx);
  148. order = BN_CTX_get(ctx);
  149. u1 = BN_CTX_get(ctx);
  150. u2 = BN_CTX_get(ctx);
  151. m = BN_CTX_get(ctx);
  152. X = BN_CTX_get(ctx);
  153. if (!X) {
  154. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  155. goto err;
  156. }
  157. if (!EC_GROUP_get_order(group, order, ctx)) {
  158. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  159. goto err;
  160. }
  161. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  162. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  163. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  164. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_BAD_SIGNATURE);
  165. ret = 0; /* signature is invalid */
  166. goto err;
  167. }
  168. /* calculate tmp1 = inv(S) mod order */
  169. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  170. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  171. goto err;
  172. }
  173. if (!digest_to_bn(m, digest, digest_len, order)) {
  174. goto err;
  175. }
  176. /* u1 = m * tmp mod order */
  177. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  178. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  179. goto err;
  180. }
  181. /* u2 = r * w mod q */
  182. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  183. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  184. goto err;
  185. }
  186. point = EC_POINT_new(group);
  187. if (point == NULL) {
  188. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  189. goto err;
  190. }
  191. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  192. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  193. goto err;
  194. }
  195. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  196. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  197. goto err;
  198. }
  199. if (!BN_nnmod(u1, X, order, ctx)) {
  200. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  201. goto err;
  202. }
  203. /* if the signature is correct u1 is equal to sig->r */
  204. ret = (BN_ucmp(u1, sig->r) == 0);
  205. err:
  206. BN_CTX_end(ctx);
  207. BN_CTX_free(ctx);
  208. if (point) {
  209. EC_POINT_free(point);
  210. }
  211. return ret;
  212. }
  213. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  214. BIGNUM **rp, const uint8_t *digest,
  215. size_t digest_len) {
  216. BN_CTX *ctx = NULL;
  217. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  218. EC_POINT *tmp_point = NULL;
  219. const EC_GROUP *group;
  220. int ret = 0;
  221. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  222. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_PASSED_NULL_PARAMETER);
  223. return 0;
  224. }
  225. if (ctx_in == NULL) {
  226. if ((ctx = BN_CTX_new()) == NULL) {
  227. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  228. return 0;
  229. }
  230. } else {
  231. ctx = ctx_in;
  232. }
  233. k = BN_new(); /* this value is later returned in *kinvp */
  234. r = BN_new(); /* this value is later returned in *rp */
  235. order = BN_new();
  236. X = BN_new();
  237. if (!k || !r || !order || !X) {
  238. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  239. goto err;
  240. }
  241. tmp_point = EC_POINT_new(group);
  242. if (tmp_point == NULL) {
  243. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  244. goto err;
  245. }
  246. if (!EC_GROUP_get_order(group, order, ctx)) {
  247. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  248. goto err;
  249. }
  250. do {
  251. /* If possible, we'll include the private key and message digest in the k
  252. * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
  253. * being used. */
  254. do {
  255. int ok;
  256. if (digest_len > 0) {
  257. ok = BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
  258. digest, digest_len, ctx);
  259. } else {
  260. ok = BN_rand_range(k, order);
  261. }
  262. if (!ok) {
  263. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup,
  264. ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  265. goto err;
  266. }
  267. } while (BN_is_zero(k));
  268. /* We do not want timing information to leak the length of k,
  269. * so we compute G*k using an equivalent scalar of fixed
  270. * bit-length. */
  271. if (!BN_add(k, k, order)) {
  272. goto err;
  273. }
  274. if (BN_num_bits(k) <= BN_num_bits(order)) {
  275. if (!BN_add(k, k, order)) {
  276. goto err;
  277. }
  278. }
  279. /* compute r the x-coordinate of generator * k */
  280. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  281. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  282. goto err;
  283. }
  284. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X, NULL, ctx)) {
  285. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  286. goto err;
  287. }
  288. if (!BN_nnmod(r, X, order, ctx)) {
  289. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  290. goto err;
  291. }
  292. } while (BN_is_zero(r));
  293. /* compute the inverse of k */
  294. if (!BN_mod_inverse(k, k, order, ctx)) {
  295. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  296. goto err;
  297. }
  298. /* clear old values if necessary */
  299. if (*rp != NULL) {
  300. BN_clear_free(*rp);
  301. }
  302. if (*kinvp != NULL) {
  303. BN_clear_free(*kinvp);
  304. }
  305. /* save the pre-computed values */
  306. *rp = r;
  307. *kinvp = k;
  308. ret = 1;
  309. err:
  310. if (!ret) {
  311. if (k != NULL) {
  312. BN_clear_free(k);
  313. }
  314. if (r != NULL) {
  315. BN_clear_free(r);
  316. }
  317. }
  318. if (ctx_in == NULL)
  319. BN_CTX_free(ctx);
  320. if (order != NULL)
  321. BN_free(order);
  322. if (tmp_point != NULL)
  323. EC_POINT_free(tmp_point);
  324. if (X)
  325. BN_clear_free(X);
  326. return ret;
  327. }
  328. int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp) {
  329. return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
  330. }
  331. ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
  332. const BIGNUM *in_kinv, const BIGNUM *in_r,
  333. EC_KEY *eckey) {
  334. int ok = 0;
  335. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  336. const BIGNUM *ckinv;
  337. BN_CTX *ctx = NULL;
  338. const EC_GROUP *group;
  339. ECDSA_SIG *ret;
  340. const BIGNUM *priv_key;
  341. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  342. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  343. return NULL;
  344. }
  345. group = EC_KEY_get0_group(eckey);
  346. priv_key = EC_KEY_get0_private_key(eckey);
  347. if (group == NULL || priv_key == NULL) {
  348. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_PASSED_NULL_PARAMETER);
  349. return NULL;
  350. }
  351. ret = ECDSA_SIG_new();
  352. if (!ret) {
  353. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  354. return NULL;
  355. }
  356. s = ret->s;
  357. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  358. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  359. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  360. goto err;
  361. }
  362. if (!EC_GROUP_get_order(group, order, ctx)) {
  363. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_EC_LIB);
  364. goto err;
  365. }
  366. if (!digest_to_bn(m, digest, digest_len, order)) {
  367. goto err;
  368. }
  369. for (;;) {
  370. if (in_kinv == NULL || in_r == NULL) {
  371. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
  372. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_ECDSA_LIB);
  373. goto err;
  374. }
  375. ckinv = kinv;
  376. } else {
  377. ckinv = in_kinv;
  378. if (BN_copy(ret->r, in_r) == NULL) {
  379. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  380. goto err;
  381. }
  382. }
  383. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  384. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  385. goto err;
  386. }
  387. if (!BN_mod_add_quick(s, tmp, m, order)) {
  388. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  389. goto err;
  390. }
  391. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  392. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  393. goto err;
  394. }
  395. if (BN_is_zero(s)) {
  396. /* if kinv and r have been supplied by the caller
  397. * don't to generate new kinv and r values */
  398. if (in_kinv != NULL && in_r != NULL) {
  399. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NEED_NEW_SETUP_VALUES);
  400. goto err;
  401. }
  402. } else {
  403. /* s != 0 => we have a valid signature */
  404. break;
  405. }
  406. }
  407. ok = 1;
  408. err:
  409. if (!ok) {
  410. ECDSA_SIG_free(ret);
  411. ret = NULL;
  412. }
  413. if (ctx)
  414. BN_CTX_free(ctx);
  415. if (m)
  416. BN_clear_free(m);
  417. if (tmp)
  418. BN_clear_free(tmp);
  419. if (order)
  420. BN_free(order);
  421. if (kinv)
  422. BN_clear_free(kinv);
  423. return ret;
  424. }
  425. int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
  426. uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
  427. const BIGNUM *r, EC_KEY *eckey) {
  428. ECDSA_SIG *s = NULL;
  429. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  430. OPENSSL_PUT_ERROR(ECDSA, ECDSA_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  431. *sig_len = 0;
  432. return 0;
  433. }
  434. s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
  435. if (s == NULL) {
  436. *sig_len = 0;
  437. return 0;
  438. }
  439. *sig_len = i2d_ECDSA_SIG(s, &sig);
  440. ECDSA_SIG_free(s);
  441. return 1;
  442. }