BcTlsDH.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  8. {
  9. /// <summary>Support class for ephemeral Diffie-Hellman using the BC light-weight library.</summary>
  10. public class BcTlsDH
  11. : TlsAgreement
  12. {
  13. protected readonly BcTlsDHDomain m_domain;
  14. protected AsymmetricCipherKeyPair m_localKeyPair;
  15. protected DHPublicKeyParameters m_peerPublicKey;
  16. public BcTlsDH(BcTlsDHDomain domain)
  17. {
  18. this.m_domain = domain;
  19. }
  20. /// <exception cref="IOException"/>
  21. public virtual byte[] GenerateEphemeral()
  22. {
  23. this.m_localKeyPair = m_domain.GenerateKeyPair();
  24. return m_domain.EncodePublicKey((DHPublicKeyParameters)m_localKeyPair.Public);
  25. }
  26. /// <exception cref="IOException"/>
  27. public virtual void ReceivePeerValue(byte[] peerValue)
  28. {
  29. this.m_peerPublicKey = m_domain.DecodePublicKey(peerValue);
  30. }
  31. /// <exception cref="IOException"/>
  32. public virtual TlsSecret CalculateSecret()
  33. {
  34. return m_domain.CalculateDHAgreement((DHPrivateKeyParameters)m_localKeyPair.Private, m_peerPublicKey);
  35. }
  36. }
  37. }
  38. #pragma warning restore
  39. #endif