dsa_asn1.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "dsa_locl.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/rand.h>
  15. ASN1_SEQUENCE(DSA_SIG) = {
  16. ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
  17. ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
  18. } static_ASN1_SEQUENCE_END(DSA_SIG)
  19. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA_SIG, DSA_SIG, DSA_SIG)
  20. DSA_SIG *DSA_SIG_new(void)
  21. {
  22. DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
  23. if (sig == NULL)
  24. DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
  25. return sig;
  26. }
  27. void DSA_SIG_free(DSA_SIG *sig)
  28. {
  29. if (sig == NULL)
  30. return;
  31. BN_clear_free(sig->r);
  32. BN_clear_free(sig->s);
  33. OPENSSL_free(sig);
  34. }
  35. void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  36. {
  37. if (pr != NULL)
  38. *pr = sig->r;
  39. if (ps != NULL)
  40. *ps = sig->s;
  41. }
  42. int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  43. {
  44. if (r == NULL || s == NULL)
  45. return 0;
  46. BN_clear_free(sig->r);
  47. BN_clear_free(sig->s);
  48. sig->r = r;
  49. sig->s = s;
  50. return 1;
  51. }
  52. /* Override the default free and new methods */
  53. static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  54. void *exarg)
  55. {
  56. if (operation == ASN1_OP_NEW_PRE) {
  57. *pval = (ASN1_VALUE *)DSA_new();
  58. if (*pval != NULL)
  59. return 2;
  60. return 0;
  61. } else if (operation == ASN1_OP_FREE_PRE) {
  62. DSA_free((DSA *)*pval);
  63. *pval = NULL;
  64. return 2;
  65. }
  66. return 1;
  67. }
  68. ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
  69. ASN1_SIMPLE(DSA, version, LONG),
  70. ASN1_SIMPLE(DSA, p, BIGNUM),
  71. ASN1_SIMPLE(DSA, q, BIGNUM),
  72. ASN1_SIMPLE(DSA, g, BIGNUM),
  73. ASN1_SIMPLE(DSA, pub_key, BIGNUM),
  74. ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
  75. } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
  76. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
  77. ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
  78. ASN1_SIMPLE(DSA, p, BIGNUM),
  79. ASN1_SIMPLE(DSA, q, BIGNUM),
  80. ASN1_SIMPLE(DSA, g, BIGNUM),
  81. } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
  82. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
  83. ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
  84. ASN1_SIMPLE(DSA, pub_key, BIGNUM),
  85. ASN1_SIMPLE(DSA, p, BIGNUM),
  86. ASN1_SIMPLE(DSA, q, BIGNUM),
  87. ASN1_SIMPLE(DSA, g, BIGNUM)
  88. } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
  89. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
  90. DSA *DSAparams_dup(DSA *dsa)
  91. {
  92. return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
  93. }
  94. int DSA_sign(int type, const unsigned char *dgst, int dlen,
  95. unsigned char *sig, unsigned int *siglen, DSA *dsa)
  96. {
  97. DSA_SIG *s;
  98. RAND_seed(dgst, dlen);
  99. s = DSA_do_sign(dgst, dlen, dsa);
  100. if (s == NULL) {
  101. *siglen = 0;
  102. return (0);
  103. }
  104. *siglen = i2d_DSA_SIG(s, &sig);
  105. DSA_SIG_free(s);
  106. return (1);
  107. }
  108. /* data has already been hashed (probably with SHA or SHA-1). */
  109. /*-
  110. * returns
  111. * 1: correct signature
  112. * 0: incorrect signature
  113. * -1: error
  114. */
  115. int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
  116. const unsigned char *sigbuf, int siglen, DSA *dsa)
  117. {
  118. DSA_SIG *s;
  119. const unsigned char *p = sigbuf;
  120. unsigned char *der = NULL;
  121. int derlen = -1;
  122. int ret = -1;
  123. s = DSA_SIG_new();
  124. if (s == NULL)
  125. return (ret);
  126. if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
  127. goto err;
  128. /* Ensure signature uses DER and doesn't have trailing garbage */
  129. derlen = i2d_DSA_SIG(s, &der);
  130. if (derlen != siglen || memcmp(sigbuf, der, derlen))
  131. goto err;
  132. ret = DSA_do_verify(dgst, dgst_len, s, dsa);
  133. err:
  134. OPENSSL_clear_free(der, derlen);
  135. DSA_SIG_free(s);
  136. return (ret);
  137. }