BcTlsECDsa13Verifier.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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>Implementation class for verification of ECDSA signatures in TLS 1.3+ using the BC light-weight API.
  11. /// </summary>
  12. public class BcTlsECDsa13Verifier
  13. : BcTlsVerifier
  14. {
  15. private readonly int m_signatureScheme;
  16. public BcTlsECDsa13Verifier(BcTlsCrypto crypto, ECPublicKeyParameters publicKey, int signatureScheme)
  17. : base(crypto, publicKey)
  18. {
  19. if (!SignatureScheme.IsECDsa(signatureScheme))
  20. throw new ArgumentException("signatureScheme");
  21. this.m_signatureScheme = signatureScheme;
  22. }
  23. public override bool VerifyRawSignature(DigitallySigned signature, byte[] hash)
  24. {
  25. SignatureAndHashAlgorithm algorithm = signature.Algorithm;
  26. if (algorithm == null || SignatureScheme.From(algorithm) != m_signatureScheme)
  27. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  28. int cryptoHashAlgorithm = SignatureScheme.GetCryptoHashAlgorithm(m_signatureScheme);
  29. IDsa dsa = new ECDsaSigner(new HMacDsaKCalculator(m_crypto.CreateDigest(cryptoHashAlgorithm)));
  30. ISigner signer = new DsaDigestSigner(dsa, new NullDigest());
  31. signer.Init(false, m_publicKey);
  32. signer.BlockUpdate(hash, 0, hash.Length);
  33. return signer.VerifySignature(signature.Signature);
  34. }
  35. }
  36. }
  37. #pragma warning restore
  38. #endif