ServerNameList.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  9. {
  10. public class ServerNameList
  11. {
  12. protected readonly IList mServerNameList;
  13. /**
  14. * @param serverNameList an {@link IList} of {@link ServerName}.
  15. */
  16. public ServerNameList(IList serverNameList)
  17. {
  18. if (serverNameList == null)
  19. throw new ArgumentNullException("serverNameList");
  20. this.mServerNameList = serverNameList;
  21. }
  22. /**
  23. * @return an {@link IList} of {@link ServerName}.
  24. */
  25. public virtual IList ServerNames
  26. {
  27. get { return mServerNameList; }
  28. }
  29. /**
  30. * Encode this {@link ServerNameList} to a {@link Stream}.
  31. *
  32. * @param output
  33. * the {@link Stream} to encode to.
  34. * @throws IOException
  35. */
  36. public virtual void Encode(Stream output)
  37. {
  38. MemoryStream buf = new MemoryStream();
  39. byte[] nameTypesSeen = TlsUtilities.EmptyBytes;
  40. foreach (ServerName entry in ServerNames)
  41. {
  42. nameTypesSeen = CheckNameType(nameTypesSeen, entry.NameType);
  43. if (nameTypesSeen == null)
  44. throw new TlsFatalAlert(AlertDescription.internal_error);
  45. entry.Encode(buf);
  46. }
  47. TlsUtilities.CheckUint16(buf.Length);
  48. TlsUtilities.WriteUint16((int)buf.Length, output);
  49. Streams.WriteBufTo(buf, output);
  50. }
  51. /**
  52. * Parse a {@link ServerNameList} from a {@link Stream}.
  53. *
  54. * @param input
  55. * the {@link Stream} to parse from.
  56. * @return a {@link ServerNameList} object.
  57. * @throws IOException
  58. */
  59. public static ServerNameList Parse(Stream input)
  60. {
  61. int length = TlsUtilities.ReadUint16(input);
  62. if (length < 1)
  63. throw new TlsFatalAlert(AlertDescription.decode_error);
  64. byte[] data = TlsUtilities.ReadFully(length, input);
  65. MemoryStream buf = new MemoryStream(data, false);
  66. byte[] nameTypesSeen = TlsUtilities.EmptyBytes;
  67. IList server_name_list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  68. while (buf.Position < buf.Length)
  69. {
  70. ServerName entry = ServerName.Parse(buf);
  71. nameTypesSeen = CheckNameType(nameTypesSeen, entry.NameType);
  72. if (nameTypesSeen == null)
  73. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  74. server_name_list.Add(entry);
  75. }
  76. return new ServerNameList(server_name_list);
  77. }
  78. private static byte[] CheckNameType(byte[] nameTypesSeen, byte nameType)
  79. {
  80. /*
  81. * RFC 6066 3. The ServerNameList MUST NOT contain more than one name of the same
  82. * name_type.
  83. */
  84. if (!NameType.IsValid(nameType) || Arrays.Contains(nameTypesSeen, nameType))
  85. return null;
  86. return Arrays.Append(nameTypesSeen, nameType);
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif