DefaultTlsCredentialedSigner.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. /// <summary>Container class for generating signatures that carries the signature type, parameters, public key
  9. /// certificate and public key's associated signer object.</summary>
  10. public class DefaultTlsCredentialedSigner
  11. : TlsCredentialedSigner
  12. {
  13. protected readonly TlsCryptoParameters m_cryptoParams;
  14. protected readonly Certificate m_certificate;
  15. protected readonly SignatureAndHashAlgorithm m_signatureAndHashAlgorithm;
  16. protected readonly TlsSigner m_signer;
  17. public DefaultTlsCredentialedSigner(TlsCryptoParameters cryptoParams, TlsSigner signer,
  18. Certificate certificate, SignatureAndHashAlgorithm signatureAndHashAlgorithm)
  19. {
  20. if (certificate == null)
  21. throw new ArgumentNullException("certificate");
  22. if (certificate.IsEmpty)
  23. throw new ArgumentException("cannot be empty", "certificate");
  24. if (signer == null)
  25. throw new ArgumentNullException("signer");
  26. this.m_cryptoParams = cryptoParams;
  27. this.m_certificate = certificate;
  28. this.m_signatureAndHashAlgorithm = signatureAndHashAlgorithm;
  29. this.m_signer = signer;
  30. }
  31. public virtual Certificate Certificate
  32. {
  33. get { return m_certificate; }
  34. }
  35. public virtual byte[] GenerateRawSignature(byte[] hash)
  36. {
  37. return m_signer.GenerateRawSignature(GetEffectiveAlgorithm(), hash);
  38. }
  39. public virtual SignatureAndHashAlgorithm SignatureAndHashAlgorithm
  40. {
  41. get { return m_signatureAndHashAlgorithm; }
  42. }
  43. public virtual TlsStreamSigner GetStreamSigner()
  44. {
  45. return m_signer.GetStreamSigner(GetEffectiveAlgorithm());
  46. }
  47. protected virtual SignatureAndHashAlgorithm GetEffectiveAlgorithm()
  48. {
  49. SignatureAndHashAlgorithm algorithm = null;
  50. if (TlsImplUtilities.IsTlsV12(m_cryptoParams))
  51. {
  52. algorithm = SignatureAndHashAlgorithm;
  53. if (algorithm == null)
  54. throw new InvalidOperationException("'signatureAndHashAlgorithm' cannot be null for (D)TLS 1.2+");
  55. }
  56. return algorithm;
  57. }
  58. }
  59. }
  60. #pragma warning restore
  61. #endif