BcTlsDssVerifier.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.Signers;
  7. namespace BestHTTP.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(int cryptoHashAlgorithm);
  19. protected abstract short SignatureAlgorithm { get; }
  20. public override bool VerifyRawSignature(DigitallySigned signedParams, byte[] hash)
  21. {
  22. SignatureAndHashAlgorithm algorithm = signedParams.Algorithm;
  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(false, m_publicKey);
  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. return signer.VerifySignature(signedParams.Signature);
  40. }
  41. }
  42. }
  43. #pragma warning restore
  44. #endif