CertificateUrl.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /*
  11. * RFC 3546 3.3
  12. */
  13. public class CertificateUrl
  14. {
  15. protected readonly byte mType;
  16. protected readonly IList mUrlAndHashList;
  17. /**
  18. * @param type
  19. * see {@link CertChainType} for valid constants.
  20. * @param urlAndHashList
  21. * a {@link IList} of {@link UrlAndHash}.
  22. */
  23. public CertificateUrl(byte type, IList urlAndHashList)
  24. {
  25. if (!CertChainType.IsValid(type))
  26. throw new ArgumentException("not a valid CertChainType value", "type");
  27. if (urlAndHashList == null || urlAndHashList.Count < 1)
  28. throw new ArgumentException("must have length > 0", "urlAndHashList");
  29. this.mType = type;
  30. this.mUrlAndHashList = urlAndHashList;
  31. }
  32. /**
  33. * @return {@link CertChainType}
  34. */
  35. public virtual byte Type
  36. {
  37. get { return mType; }
  38. }
  39. /**
  40. * @return an {@link IList} of {@link UrlAndHash}
  41. */
  42. public virtual IList UrlAndHashList
  43. {
  44. get { return mUrlAndHashList; }
  45. }
  46. /**
  47. * Encode this {@link CertificateUrl} to a {@link Stream}.
  48. *
  49. * @param output the {@link Stream} to encode to.
  50. * @throws IOException
  51. */
  52. public virtual void Encode(Stream output)
  53. {
  54. TlsUtilities.WriteUint8(this.mType, output);
  55. ListBuffer16 buf = new ListBuffer16();
  56. foreach (UrlAndHash urlAndHash in this.mUrlAndHashList)
  57. {
  58. urlAndHash.Encode(buf);
  59. }
  60. buf.EncodeTo(output);
  61. }
  62. /**
  63. * Parse a {@link CertificateUrl} from a {@link Stream}.
  64. *
  65. * @param context
  66. * the {@link TlsContext} of the current connection.
  67. * @param input
  68. * the {@link Stream} to parse from.
  69. * @return a {@link CertificateUrl} object.
  70. * @throws IOException
  71. */
  72. public static CertificateUrl parse(TlsContext context, Stream input)
  73. {
  74. byte type = TlsUtilities.ReadUint8(input);
  75. if (!CertChainType.IsValid(type))
  76. throw new TlsFatalAlert(AlertDescription.decode_error);
  77. int totalLength = TlsUtilities.ReadUint16(input);
  78. if (totalLength < 1)
  79. throw new TlsFatalAlert(AlertDescription.decode_error);
  80. byte[] urlAndHashListData = TlsUtilities.ReadFully(totalLength, input);
  81. MemoryStream buf = new MemoryStream(urlAndHashListData, false);
  82. IList url_and_hash_list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  83. while (buf.Position < buf.Length)
  84. {
  85. UrlAndHash url_and_hash = UrlAndHash.Parse(context, buf);
  86. url_and_hash_list.Add(url_and_hash);
  87. }
  88. return new CertificateUrl(type, url_and_hash_list);
  89. }
  90. // TODO Could be more generally useful
  91. internal class ListBuffer16
  92. : MemoryStream
  93. {
  94. internal ListBuffer16()
  95. {
  96. // Reserve space for length
  97. TlsUtilities.WriteUint16(0, this);
  98. }
  99. internal void EncodeTo(Stream output)
  100. {
  101. // Patch actual length back in
  102. long length = Length - 2;
  103. TlsUtilities.CheckUint16(length);
  104. this.Position = 0;
  105. TlsUtilities.WriteUint16((int)length, this);
  106. Streams.WriteBufTo(this, output);
  107. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(this);
  108. }
  109. }
  110. }
  111. }
  112. #pragma warning restore
  113. #endif