BcTlsRsaPssSigner.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.Engines;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers;
  8. namespace Best.HTTP.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. if (algorithm == null || SignatureScheme.From(algorithm) != m_signatureScheme)
  25. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  26. int cryptoHashAlgorithm = SignatureScheme.GetCryptoHashAlgorithm(m_signatureScheme);
  27. IDigest digest = m_crypto.CreateDigest(cryptoHashAlgorithm);
  28. PssSigner signer = PssSigner.CreateRawSigner(new RsaBlindedEngine(), digest, digest, digest.GetDigestSize(),
  29. PssSigner.TrailerImplicit);
  30. signer.Init(true, new ParametersWithRandom(m_privateKey, m_crypto.SecureRandom));
  31. signer.BlockUpdate(hash, 0, hash.Length);
  32. try
  33. {
  34. return signer.GenerateSignature();
  35. }
  36. catch (CryptoException e)
  37. {
  38. throw new TlsFatalAlert(AlertDescription.internal_error, e);
  39. }
  40. }
  41. }
  42. }
  43. #pragma warning restore
  44. #endif