TrustedAuthority.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  9. {
  10. public sealed class TrustedAuthority
  11. {
  12. private readonly short m_identifierType;
  13. private readonly object m_identifier;
  14. public TrustedAuthority(short identifierType, object identifier)
  15. {
  16. if (!IsCorrectType(identifierType, identifier))
  17. throw new ArgumentException("not an instance of the correct type", "identifier");
  18. this.m_identifierType = identifierType;
  19. this.m_identifier = identifier;
  20. }
  21. public short IdentifierType
  22. {
  23. get { return m_identifierType; }
  24. }
  25. public object Identifier
  26. {
  27. get { return m_identifier; }
  28. }
  29. public byte[] GetCertSha1Hash()
  30. {
  31. return Arrays.Clone((byte[])m_identifier);
  32. }
  33. public byte[] GetKeySha1Hash()
  34. {
  35. return Arrays.Clone((byte[])m_identifier);
  36. }
  37. public X509Name X509Name
  38. {
  39. get
  40. {
  41. CheckCorrectType(Tls.IdentifierType.x509_name);
  42. return (X509Name)m_identifier;
  43. }
  44. }
  45. /// <summary>Encode this <see cref="TrustedAuthority"/> to a <see cref="Stream"/>.</summary>
  46. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  47. /// <exception cref="IOException"/>
  48. public void Encode(Stream output)
  49. {
  50. TlsUtilities.WriteUint8(m_identifierType, output);
  51. switch (m_identifierType)
  52. {
  53. case Tls.IdentifierType.cert_sha1_hash:
  54. case Tls.IdentifierType.key_sha1_hash:
  55. {
  56. byte[] sha1Hash = (byte[])m_identifier;
  57. output.Write(sha1Hash, 0, sha1Hash.Length);
  58. break;
  59. }
  60. case Tls.IdentifierType.pre_agreed:
  61. {
  62. break;
  63. }
  64. case Tls.IdentifierType.x509_name:
  65. {
  66. X509Name dn = (X509Name)m_identifier;
  67. byte[] derEncoding = dn.GetEncoded(Asn1Encodable.Der);
  68. TlsUtilities.WriteOpaque16(derEncoding, output);
  69. break;
  70. }
  71. default:
  72. throw new TlsFatalAlert(AlertDescription.internal_error);
  73. }
  74. }
  75. /// <summary>Parse a <see cref="TrustedAuthority"/> from a <see cref="Stream"/>.</summary>
  76. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  77. /// <returns>a <see cref="TrustedAuthority"/> object.</returns>
  78. /// <exception cref="IOException"/>
  79. public static TrustedAuthority Parse(Stream input)
  80. {
  81. short identifier_type = TlsUtilities.ReadUint8(input);
  82. object identifier;
  83. switch (identifier_type)
  84. {
  85. case Tls.IdentifierType.cert_sha1_hash:
  86. case Tls.IdentifierType.key_sha1_hash:
  87. {
  88. identifier = TlsUtilities.ReadFully(20, input);
  89. break;
  90. }
  91. case Tls.IdentifierType.pre_agreed:
  92. {
  93. identifier = null;
  94. break;
  95. }
  96. case Tls.IdentifierType.x509_name:
  97. {
  98. byte[] derEncoding = TlsUtilities.ReadOpaque16(input, 1);
  99. Asn1Object asn1 = TlsUtilities.ReadDerObject(derEncoding);
  100. identifier = X509Name.GetInstance(asn1);
  101. break;
  102. }
  103. default:
  104. throw new TlsFatalAlert(AlertDescription.decode_error);
  105. }
  106. return new TrustedAuthority(identifier_type, identifier);
  107. }
  108. private void CheckCorrectType(short expectedIdentifierType)
  109. {
  110. if (m_identifierType != expectedIdentifierType || !IsCorrectType(expectedIdentifierType, m_identifier))
  111. throw new InvalidOperationException("TrustedAuthority is not of type "
  112. + Tls.IdentifierType.GetName(expectedIdentifierType));
  113. }
  114. private static bool IsCorrectType(short identifierType, object identifier)
  115. {
  116. switch (identifierType)
  117. {
  118. case Tls.IdentifierType.cert_sha1_hash:
  119. case Tls.IdentifierType.key_sha1_hash:
  120. return IsSha1Hash(identifier);
  121. case Tls.IdentifierType.pre_agreed:
  122. return identifier == null;
  123. case Tls.IdentifierType.x509_name:
  124. return identifier is X509Name;
  125. default:
  126. throw new ArgumentException("unsupported IdentifierType", "identifierType");
  127. }
  128. }
  129. private static bool IsSha1Hash(object identifier)
  130. {
  131. return identifier is byte[] && ((byte[])identifier).Length == 20;
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif