ServerName.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  7. {
  8. public class ServerName
  9. {
  10. protected readonly byte mNameType;
  11. protected readonly object mName;
  12. public ServerName(byte nameType, object name)
  13. {
  14. if (!IsCorrectType(nameType, name))
  15. throw new ArgumentException("not an instance of the correct type", "name");
  16. this.mNameType = nameType;
  17. this.mName = name;
  18. }
  19. public virtual byte NameType
  20. {
  21. get { return mNameType; }
  22. }
  23. public virtual object Name
  24. {
  25. get { return mName; }
  26. }
  27. public virtual string GetHostName()
  28. {
  29. if (!IsCorrectType(Tls.NameType.host_name, mName))
  30. throw new InvalidOperationException("'name' is not a HostName string");
  31. return (string)mName;
  32. }
  33. /**
  34. * Encode this {@link ServerName} to a {@link Stream}.
  35. *
  36. * @param output
  37. * the {@link Stream} to encode to.
  38. * @throws IOException
  39. */
  40. public virtual void Encode(Stream output)
  41. {
  42. TlsUtilities.WriteUint8(mNameType, output);
  43. switch (mNameType)
  44. {
  45. case Tls.NameType.host_name:
  46. byte[] asciiEncoding = Strings.ToAsciiByteArray((string)mName);
  47. if (asciiEncoding.Length < 1)
  48. throw new TlsFatalAlert(AlertDescription.internal_error);
  49. TlsUtilities.WriteOpaque16(asciiEncoding, output);
  50. break;
  51. default:
  52. throw new TlsFatalAlert(AlertDescription.internal_error);
  53. }
  54. }
  55. /**
  56. * Parse a {@link ServerName} from a {@link Stream}.
  57. *
  58. * @param input
  59. * the {@link Stream} to parse from.
  60. * @return a {@link ServerName} object.
  61. * @throws IOException
  62. */
  63. public static ServerName Parse(Stream input)
  64. {
  65. byte name_type = TlsUtilities.ReadUint8(input);
  66. object name;
  67. switch (name_type)
  68. {
  69. case Tls.NameType.host_name:
  70. {
  71. byte[] asciiEncoding = TlsUtilities.ReadOpaque16(input);
  72. if (asciiEncoding.Length < 1)
  73. throw new TlsFatalAlert(AlertDescription.decode_error);
  74. name = Strings.FromAsciiByteArray(asciiEncoding);
  75. break;
  76. }
  77. default:
  78. throw new TlsFatalAlert(AlertDescription.decode_error);
  79. }
  80. return new ServerName(name_type, name);
  81. }
  82. protected static bool IsCorrectType(byte nameType, object name)
  83. {
  84. switch (nameType)
  85. {
  86. case Tls.NameType.host_name:
  87. return name is string;
  88. default:
  89. throw new ArgumentException("unsupported NameType", "nameType");
  90. }
  91. }
  92. }
  93. }
  94. #pragma warning restore
  95. #endif