BcTlsECDsa13Signer.cs 2.0 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.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 generation of ECDSA signatures in TLS 1.3+ using the BC light-weight API.
  11. /// </summary>
  12. public class BcTlsECDsa13Signer
  13. : BcTlsSigner
  14. {
  15. private readonly int m_signatureScheme;
  16. public BcTlsECDsa13Signer(BcTlsCrypto crypto, ECPrivateKeyParameters privateKey, int signatureScheme)
  17. : base(crypto, privateKey)
  18. {
  19. if (!SignatureScheme.IsECDsa(signatureScheme))
  20. throw new ArgumentException("signatureScheme");
  21. this.m_signatureScheme = signatureScheme;
  22. }
  23. public override byte[] GenerateRawSignature(SignatureAndHashAlgorithm algorithm, byte[] hash)
  24. {
  25. if (algorithm == null || SignatureScheme.From(algorithm) != m_signatureScheme)
  26. throw new InvalidOperationException("Invalid algorithm: " + algorithm);
  27. int cryptoHashAlgorithm = SignatureScheme.GetCryptoHashAlgorithm(m_signatureScheme);
  28. IDsa dsa = new ECDsaSigner(new HMacDsaKCalculator(m_crypto.CreateDigest(cryptoHashAlgorithm)));
  29. ISigner signer = new DsaDigestSigner(dsa, new NullDigest());
  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