ServerDHParams.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. using Org.BouncyCastle.Crypto.Parameters;
  5. using Org.BouncyCastle.Math;
  6. namespace Org.BouncyCastle.Crypto.Tls
  7. {
  8. public class ServerDHParams
  9. {
  10. protected readonly DHPublicKeyParameters mPublicKey;
  11. public ServerDHParams(DHPublicKeyParameters publicKey)
  12. {
  13. if (publicKey == null)
  14. throw new ArgumentNullException("publicKey");
  15. this.mPublicKey = publicKey;
  16. }
  17. public virtual DHPublicKeyParameters PublicKey
  18. {
  19. get { return mPublicKey; }
  20. }
  21. /**
  22. * Encode this {@link ServerDHParams} to a {@link Stream}.
  23. *
  24. * @param output
  25. * the {@link Stream} to encode to.
  26. * @throws IOException
  27. */
  28. public virtual void Encode(Stream output)
  29. {
  30. DHParameters dhParameters = mPublicKey.Parameters;
  31. BigInteger Ys = mPublicKey.Y;
  32. TlsDHUtilities.WriteDHParameter(dhParameters.P, output);
  33. TlsDHUtilities.WriteDHParameter(dhParameters.G, output);
  34. TlsDHUtilities.WriteDHParameter(Ys, output);
  35. }
  36. /**
  37. * Parse a {@link ServerDHParams} from a {@link Stream}.
  38. *
  39. * @param input
  40. * the {@link Stream} to parse from.
  41. * @return a {@link ServerDHParams} object.
  42. * @throws IOException
  43. */
  44. public static ServerDHParams Parse(Stream input)
  45. {
  46. BigInteger p = TlsDHUtilities.ReadDHParameter(input);
  47. BigInteger g = TlsDHUtilities.ReadDHParameter(input);
  48. BigInteger Ys = TlsDHUtilities.ReadDHParameter(input);
  49. return new ServerDHParams(
  50. TlsDHUtilities.ValidateDHPublicKey(new DHPublicKeyParameters(Ys, new DHParameters(p, g))));
  51. }
  52. }
  53. }
  54. #endif