BcTlsRsaVerifier.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Encodings;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  11. {
  12. /// <summary>Operator supporting the verification of RSASSA-PKCS1-v1_5 signatures using the BC light-weight API.
  13. /// </summary>
  14. public class BcTlsRsaVerifier
  15. : BcTlsVerifier
  16. {
  17. public BcTlsRsaVerifier(BcTlsCrypto crypto, RsaKeyParameters publicKey)
  18. : base(crypto, publicKey)
  19. {
  20. }
  21. public override bool VerifyRawSignature(DigitallySigned signedParams, byte[] hash)
  22. {
  23. IDigest nullDigest = new NullDigest();
  24. SignatureAndHashAlgorithm algorithm = signedParams.Algorithm;
  25. ISigner signer;
  26. if (algorithm != null)
  27. {
  28. if (algorithm.Signature != SignatureAlgorithm.rsa)
  29. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  30. /*
  31. * RFC 5246 4.7. In RSA signing, the opaque vector contains the signature generated
  32. * using the RSASSA-PKCS1-v1_5 signature scheme defined in [PKCS1].
  33. */
  34. signer = new RsaDigestSigner(nullDigest, TlsUtilities.GetOidForHashAlgorithm(algorithm.Hash));
  35. }
  36. else
  37. {
  38. /*
  39. * RFC 5246 4.7. Note that earlier versions of TLS used a different RSA signature scheme
  40. * that did not include a DigestInfo encoding.
  41. */
  42. signer = new GenericSigner(new Pkcs1Encoding(new RsaBlindedEngine()), nullDigest);
  43. }
  44. signer.Init(false, m_publicKey);
  45. signer.BlockUpdate(hash, 0, hash.Length);
  46. return signer.VerifySignature(signedParams.Signature);
  47. }
  48. }
  49. }
  50. #pragma warning restore
  51. #endif