DsaDigestSigner.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Text;
  6. using Org.BouncyCastle.Asn1;
  7. using Org.BouncyCastle.Crypto.Signers;
  8. using Org.BouncyCastle.Crypto.Parameters;
  9. using Org.BouncyCastle.Math;
  10. using Org.BouncyCastle.Security;
  11. namespace Org.BouncyCastle.Crypto.Signers
  12. {
  13. public class DsaDigestSigner
  14. : ISigner
  15. {
  16. private readonly IDigest digest;
  17. private readonly IDsa dsaSigner;
  18. private bool forSigning;
  19. public DsaDigestSigner(
  20. IDsa signer,
  21. IDigest digest)
  22. {
  23. this.digest = digest;
  24. this.dsaSigner = signer;
  25. }
  26. public virtual string AlgorithmName
  27. {
  28. get { return digest.AlgorithmName + "with" + dsaSigner.AlgorithmName; }
  29. }
  30. public virtual void Init(
  31. bool forSigning,
  32. ICipherParameters parameters)
  33. {
  34. this.forSigning = forSigning;
  35. AsymmetricKeyParameter k;
  36. if (parameters is ParametersWithRandom)
  37. {
  38. k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
  39. }
  40. else
  41. {
  42. k = (AsymmetricKeyParameter)parameters;
  43. }
  44. if (forSigning && !k.IsPrivate)
  45. throw new InvalidKeyException("Signing Requires Private Key.");
  46. if (!forSigning && k.IsPrivate)
  47. throw new InvalidKeyException("Verification Requires Public Key.");
  48. Reset();
  49. dsaSigner.Init(forSigning, parameters);
  50. }
  51. /**
  52. * update the internal digest with the byte b
  53. */
  54. public virtual void Update(
  55. byte input)
  56. {
  57. digest.Update(input);
  58. }
  59. /**
  60. * update the internal digest with the byte array in
  61. */
  62. public virtual void BlockUpdate(
  63. byte[] input,
  64. int inOff,
  65. int length)
  66. {
  67. digest.BlockUpdate(input, inOff, length);
  68. }
  69. /**
  70. * Generate a signature for the message we've been loaded with using
  71. * the key we were initialised with.
  72. */
  73. public virtual byte[] GenerateSignature()
  74. {
  75. if (!forSigning)
  76. throw new InvalidOperationException("DSADigestSigner not initialised for signature generation.");
  77. byte[] hash = new byte[digest.GetDigestSize()];
  78. digest.DoFinal(hash, 0);
  79. BigInteger[] sig = dsaSigner.GenerateSignature(hash);
  80. return DerEncode(sig[0], sig[1]);
  81. }
  82. /// <returns>true if the internal state represents the signature described in the passed in array.</returns>
  83. public virtual bool VerifySignature(
  84. byte[] signature)
  85. {
  86. if (forSigning)
  87. throw new InvalidOperationException("DSADigestSigner not initialised for verification");
  88. byte[] hash = new byte[digest.GetDigestSize()];
  89. digest.DoFinal(hash, 0);
  90. try
  91. {
  92. BigInteger[] sig = DerDecode(signature);
  93. return dsaSigner.VerifySignature(hash, sig[0], sig[1]);
  94. }
  95. catch (IOException)
  96. {
  97. return false;
  98. }
  99. }
  100. /// <summary>Reset the internal state</summary>
  101. public virtual void Reset()
  102. {
  103. digest.Reset();
  104. }
  105. private byte[] DerEncode(
  106. BigInteger r,
  107. BigInteger s)
  108. {
  109. return new DerSequence(new DerInteger(r), new DerInteger(s)).GetDerEncoded();
  110. }
  111. private BigInteger[] DerDecode(
  112. byte[] encoding)
  113. {
  114. Asn1Sequence s = (Asn1Sequence) Asn1Object.FromByteArray(encoding);
  115. return new BigInteger[]
  116. {
  117. ((DerInteger) s[0]).Value,
  118. ((DerInteger) s[1]).Value
  119. };
  120. }
  121. }
  122. }
  123. #endif