TlsSrpUtilities.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  9. {
  10. public abstract class TlsSrpUtilities
  11. {
  12. /// <exception cref="IOException"/>
  13. public static void AddSrpExtension(IDictionary extensions, byte[] identity)
  14. {
  15. extensions[ExtensionType.srp] = CreateSrpExtension(identity);
  16. }
  17. /// <exception cref="IOException"/>
  18. public static byte[] GetSrpExtension(IDictionary extensions)
  19. {
  20. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.srp);
  21. return extensionData == null ? null : ReadSrpExtension(extensionData);
  22. }
  23. /// <exception cref="IOException"/>
  24. public static byte[] CreateSrpExtension(byte[] identity)
  25. {
  26. if (identity == null)
  27. throw new TlsFatalAlert(AlertDescription.internal_error);
  28. return TlsUtilities.EncodeOpaque8(identity);
  29. }
  30. /// <exception cref="IOException"/>
  31. public static byte[] ReadSrpExtension(byte[] extensionData)
  32. {
  33. if (extensionData == null)
  34. throw new ArgumentNullException("extensionData");
  35. return TlsUtilities.DecodeOpaque8(extensionData, 1);
  36. }
  37. /// <exception cref="IOException"/>
  38. public static BigInteger ReadSrpParameter(Stream input)
  39. {
  40. return new BigInteger(1, TlsUtilities.ReadOpaque16(input, 1));
  41. }
  42. /// <exception cref="IOException"/>
  43. public static void WriteSrpParameter(BigInteger x, Stream output)
  44. {
  45. TlsUtilities.WriteOpaque16(BigIntegers.AsUnsignedByteArray(x), output);
  46. }
  47. public static bool IsSrpCipherSuite(int cipherSuite)
  48. {
  49. switch (TlsUtilities.GetKeyExchangeAlgorithm(cipherSuite))
  50. {
  51. case KeyExchangeAlgorithm.SRP:
  52. case KeyExchangeAlgorithm.SRP_DSS:
  53. case KeyExchangeAlgorithm.SRP_RSA:
  54. return true;
  55. default:
  56. return false;
  57. }
  58. }
  59. }
  60. }
  61. #pragma warning restore
  62. #endif