dsa_sign.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/bn.h>
  15. #include "internal/cryptlib.h"
  16. #include "dsa_local.h"
  17. #include "crypto/asn1_dsa.h"
  18. #include "crypto/dsa.h"
  19. DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  20. {
  21. return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
  22. }
  23. #ifndef OPENSSL_NO_DEPRECATED_3_0
  24. int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
  25. {
  26. return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
  27. }
  28. #endif
  29. DSA_SIG *DSA_SIG_new(void)
  30. {
  31. DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
  32. if (sig == NULL)
  33. ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
  34. return sig;
  35. }
  36. void DSA_SIG_free(DSA_SIG *sig)
  37. {
  38. if (sig == NULL)
  39. return;
  40. BN_clear_free(sig->r);
  41. BN_clear_free(sig->s);
  42. OPENSSL_free(sig);
  43. }
  44. DSA_SIG *d2i_DSA_SIG(DSA_SIG **psig, const unsigned char **ppin, long len)
  45. {
  46. DSA_SIG *sig;
  47. if (len < 0)
  48. return NULL;
  49. if (psig != NULL && *psig != NULL) {
  50. sig = *psig;
  51. } else {
  52. sig = DSA_SIG_new();
  53. if (sig == NULL)
  54. return NULL;
  55. }
  56. if (sig->r == NULL)
  57. sig->r = BN_new();
  58. if (sig->s == NULL)
  59. sig->s = BN_new();
  60. if (ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
  61. if (psig == NULL || *psig == NULL)
  62. DSA_SIG_free(sig);
  63. return NULL;
  64. }
  65. if (psig != NULL && *psig == NULL)
  66. *psig = sig;
  67. return sig;
  68. }
  69. int i2d_DSA_SIG(const DSA_SIG *sig, unsigned char **ppout)
  70. {
  71. BUF_MEM *buf = NULL;
  72. size_t encoded_len;
  73. WPACKET pkt;
  74. if (ppout == NULL) {
  75. if (!WPACKET_init_null(&pkt, 0))
  76. return -1;
  77. } else if (*ppout == NULL) {
  78. if ((buf = BUF_MEM_new()) == NULL
  79. || !WPACKET_init_len(&pkt, buf, 0)) {
  80. BUF_MEM_free(buf);
  81. return -1;
  82. }
  83. } else {
  84. if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
  85. return -1;
  86. }
  87. if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s)
  88. || !WPACKET_get_total_written(&pkt, &encoded_len)
  89. || !WPACKET_finish(&pkt)) {
  90. BUF_MEM_free(buf);
  91. WPACKET_cleanup(&pkt);
  92. return -1;
  93. }
  94. if (ppout != NULL) {
  95. if (*ppout == NULL) {
  96. *ppout = (unsigned char *)buf->data;
  97. buf->data = NULL;
  98. BUF_MEM_free(buf);
  99. } else {
  100. *ppout += encoded_len;
  101. }
  102. }
  103. return (int)encoded_len;
  104. }
  105. int DSA_size(const DSA *dsa)
  106. {
  107. int ret = -1;
  108. DSA_SIG sig;
  109. if (dsa->params.q != NULL) {
  110. sig.r = sig.s = dsa->params.q;
  111. ret = i2d_DSA_SIG(&sig, NULL);
  112. if (ret < 0)
  113. ret = 0;
  114. }
  115. return ret;
  116. }
  117. void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  118. {
  119. if (pr != NULL)
  120. *pr = sig->r;
  121. if (ps != NULL)
  122. *ps = sig->s;
  123. }
  124. int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  125. {
  126. if (r == NULL || s == NULL)
  127. return 0;
  128. BN_clear_free(sig->r);
  129. BN_clear_free(sig->s);
  130. sig->r = r;
  131. sig->s = s;
  132. return 1;
  133. }
  134. int ossl_dsa_sign_int(int type, const unsigned char *dgst, int dlen,
  135. unsigned char *sig, unsigned int *siglen, DSA *dsa)
  136. {
  137. DSA_SIG *s;
  138. /* legacy case uses the method table */
  139. if (dsa->libctx == NULL || dsa->meth != DSA_get_default_method())
  140. s = DSA_do_sign(dgst, dlen, dsa);
  141. else
  142. s = ossl_dsa_do_sign_int(dgst, dlen, dsa);
  143. if (s == NULL) {
  144. *siglen = 0;
  145. return 0;
  146. }
  147. *siglen = i2d_DSA_SIG(s, &sig);
  148. DSA_SIG_free(s);
  149. return 1;
  150. }
  151. int DSA_sign(int type, const unsigned char *dgst, int dlen,
  152. unsigned char *sig, unsigned int *siglen, DSA *dsa)
  153. {
  154. return ossl_dsa_sign_int(type, dgst, dlen, sig, siglen, dsa);
  155. }
  156. /* data has already been hashed (probably with SHA or SHA-1). */
  157. /*-
  158. * returns
  159. * 1: correct signature
  160. * 0: incorrect signature
  161. * -1: error
  162. */
  163. int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
  164. const unsigned char *sigbuf, int siglen, DSA *dsa)
  165. {
  166. DSA_SIG *s;
  167. const unsigned char *p = sigbuf;
  168. unsigned char *der = NULL;
  169. int derlen = -1;
  170. int ret = -1;
  171. s = DSA_SIG_new();
  172. if (s == NULL)
  173. return ret;
  174. if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
  175. goto err;
  176. /* Ensure signature uses DER and doesn't have trailing garbage */
  177. derlen = i2d_DSA_SIG(s, &der);
  178. if (derlen != siglen || memcmp(sigbuf, der, derlen))
  179. goto err;
  180. ret = DSA_do_verify(dgst, dgst_len, s, dsa);
  181. err:
  182. OPENSSL_clear_free(der, derlen);
  183. DSA_SIG_free(s);
  184. return ret;
  185. }