BcTlsDssVerifier.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  8. {
  9. /// <summary>BC light-weight base class for the verifiers supporting the two DSA style algorithms from FIPS PUB
  10. /// 186-4: DSA and ECDSA.</summary>
  11. public abstract class BcTlsDssVerifier
  12. : BcTlsVerifier
  13. {
  14. protected BcTlsDssVerifier(BcTlsCrypto crypto, AsymmetricKeyParameter publicKey)
  15. : base(crypto, publicKey)
  16. {
  17. }
  18. protected abstract IDsa CreateDsaImpl();
  19. protected abstract short SignatureAlgorithm { get; }
  20. public override bool VerifyRawSignature(DigitallySigned digitallySigned, byte[] hash)
  21. {
  22. SignatureAndHashAlgorithm algorithm = digitallySigned.Algorithm;
  23. if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
  24. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  25. ISigner signer = new DsaDigestSigner(CreateDsaImpl(), new NullDigest());
  26. signer.Init(false, m_publicKey);
  27. if (algorithm == null)
  28. {
  29. // Note: Only use the SHA1 part of the (MD5/SHA1) hash
  30. signer.BlockUpdate(hash, 16, 20);
  31. }
  32. else
  33. {
  34. signer.BlockUpdate(hash, 0, hash.Length);
  35. }
  36. return signer.VerifySignature(digitallySigned.Signature);
  37. }
  38. }
  39. }
  40. #pragma warning restore
  41. #endif