BcTlsRsaPssSigner.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.Engines;
  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>Operator supporting the generation of RSASSA-PSS signatures using the BC light-weight API.</summary>
  11. public class BcTlsRsaPssSigner
  12. : BcTlsSigner
  13. {
  14. private readonly int m_signatureScheme;
  15. public BcTlsRsaPssSigner(BcTlsCrypto crypto, RsaKeyParameters privateKey, int signatureScheme)
  16. : base(crypto, privateKey)
  17. {
  18. if (!SignatureScheme.IsRsaPss(signatureScheme))
  19. throw new ArgumentException("signatureScheme");
  20. this.m_signatureScheme = signatureScheme;
  21. }
  22. public override byte[] GenerateRawSignature(SignatureAndHashAlgorithm algorithm, byte[] hash)
  23. {
  24. throw new NotSupportedException();
  25. }
  26. public override TlsStreamSigner GetStreamSigner(SignatureAndHashAlgorithm algorithm)
  27. {
  28. if (algorithm == null || SignatureScheme.From(algorithm) != m_signatureScheme)
  29. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  30. int cryptoHashAlgorithm = SignatureScheme.GetCryptoHashAlgorithm(m_signatureScheme);
  31. IDigest digest = m_crypto.CreateDigest(cryptoHashAlgorithm);
  32. PssSigner signer = new PssSigner(new RsaBlindedEngine(), digest, digest.GetDigestSize());
  33. signer.Init(true, new ParametersWithRandom(m_privateKey, m_crypto.SecureRandom));
  34. return new BcTlsStreamSigner(signer);
  35. }
  36. }
  37. }
  38. #pragma warning restore
  39. #endif