BcTlsDssSigner.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  9. {
  10. /// <summary>BC light-weight base class for the signers implementing the two DSA style algorithms from FIPS PUB
  11. /// 186-4: DSA and ECDSA.</summary>
  12. public abstract class BcTlsDssSigner
  13. : BcTlsSigner
  14. {
  15. protected BcTlsDssSigner(BcTlsCrypto crypto, AsymmetricKeyParameter privateKey)
  16. : base(crypto, privateKey)
  17. {
  18. }
  19. protected abstract IDsa CreateDsaImpl(int cryptoHashAlgorithm);
  20. protected abstract short SignatureAlgorithm { get; }
  21. public override byte[] GenerateRawSignature(SignatureAndHashAlgorithm algorithm, byte[] hash)
  22. {
  23. if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
  24. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  25. int cryptoHashAlgorithm = (null == algorithm)
  26. ? CryptoHashAlgorithm.sha1
  27. : TlsCryptoUtilities.GetHash(algorithm.Hash);
  28. ISigner signer = new DsaDigestSigner(CreateDsaImpl(cryptoHashAlgorithm), new NullDigest());
  29. signer.Init(true, new ParametersWithRandom(m_privateKey, m_crypto.SecureRandom));
  30. if (algorithm == null)
  31. {
  32. // Note: Only use the SHA1 part of the (MD5/SHA1) hash
  33. signer.BlockUpdate(hash, 16, 20);
  34. }
  35. else
  36. {
  37. signer.BlockUpdate(hash, 0, hash.Length);
  38. }
  39. try
  40. {
  41. return signer.GenerateSignature();
  42. }
  43. catch (CryptoException e)
  44. {
  45. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  46. }
  47. }
  48. }
  49. }
  50. #pragma warning restore
  51. #endif