DerUniversalString.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Text;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  9. {
  10. /**
  11. * UniversalString object.
  12. */
  13. public class DerUniversalString
  14. : DerStringBase
  15. {
  16. internal class Meta : Asn1UniversalType
  17. {
  18. internal static readonly Asn1UniversalType Instance = new Meta();
  19. private Meta() : base(typeof(DerUniversalString), Asn1Tags.UniversalString) {}
  20. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  21. {
  22. return CreatePrimitive(octetString.GetOctets());
  23. }
  24. }
  25. private static readonly char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  26. /**
  27. * return a universal string from the passed in object.
  28. *
  29. * @exception ArgumentException if the object cannot be converted.
  30. */
  31. public static DerUniversalString GetInstance(object obj)
  32. {
  33. if (obj == null)
  34. return null;
  35. if (obj is DerUniversalString derUniversalString)
  36. return derUniversalString;
  37. if (obj is IAsn1Convertible asn1Convertible)
  38. {
  39. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  40. if (asn1Object is DerUniversalString converted)
  41. return converted;
  42. }
  43. else if (obj is byte[] bytes)
  44. {
  45. try
  46. {
  47. return (DerUniversalString)Meta.Instance.FromByteArray(bytes);
  48. }
  49. catch (IOException e)
  50. {
  51. throw new ArgumentException("failed to construct universal string from byte[]: " + e.Message);
  52. }
  53. }
  54. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  55. }
  56. /**
  57. * return a universal string from a tagged object.
  58. *
  59. * @param taggedObject the tagged object holding the object we want
  60. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  61. * @exception ArgumentException if the tagged object cannot be converted.
  62. */
  63. public static DerUniversalString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  64. {
  65. return (DerUniversalString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  66. }
  67. private readonly byte[] m_contents;
  68. public DerUniversalString(byte[] contents)
  69. : this(contents, true)
  70. {
  71. }
  72. internal DerUniversalString(byte[] contents, bool clone)
  73. {
  74. if (null == contents)
  75. throw new ArgumentNullException("contents");
  76. m_contents = clone ? Arrays.Clone(contents) : contents;
  77. }
  78. public override string GetString()
  79. {
  80. int dl = m_contents.Length;
  81. int capacity = 3 + 2 * (Asn1OutputStream.GetLengthOfDL(dl) + dl);
  82. StringBuilder buf = new StringBuilder("#1C", capacity);
  83. EncodeHexDL(buf, dl);
  84. for (int i = 0; i < dl; ++i)
  85. {
  86. EncodeHexByte(buf, m_contents[i]);
  87. }
  88. Debug.Assert(buf.Length == capacity);
  89. return buf.ToString();
  90. }
  91. public byte[] GetOctets()
  92. {
  93. return Arrays.Clone(m_contents);
  94. }
  95. internal override IAsn1Encoding GetEncoding(int encoding)
  96. {
  97. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.UniversalString, m_contents);
  98. }
  99. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  100. {
  101. return new PrimitiveEncoding(tagClass, tagNo, m_contents);
  102. }
  103. protected override bool Asn1Equals(Asn1Object asn1Object)
  104. {
  105. DerUniversalString that = asn1Object as DerUniversalString;
  106. return null != that
  107. && Arrays.AreEqual(this.m_contents, that.m_contents);
  108. }
  109. protected override int Asn1GetHashCode()
  110. {
  111. return Arrays.GetHashCode(m_contents);
  112. }
  113. internal static DerUniversalString CreatePrimitive(byte[] contents)
  114. {
  115. return new DerUniversalString(contents, false);
  116. }
  117. private static void EncodeHexByte(StringBuilder buf, int i)
  118. {
  119. buf.Append(table[(i >> 4) & 0xF]);
  120. buf.Append(table[i & 0xF]);
  121. }
  122. private static void EncodeHexDL(StringBuilder buf, int dl)
  123. {
  124. if (dl < 128)
  125. {
  126. EncodeHexByte(buf, dl);
  127. return;
  128. }
  129. byte[] stack = new byte[5];
  130. int pos = 5;
  131. do
  132. {
  133. stack[--pos] = (byte)dl;
  134. dl >>= 8;
  135. }
  136. while (dl != 0);
  137. int count = stack.Length - pos;
  138. stack[--pos] = (byte)(0x80 | count);
  139. do
  140. {
  141. EncodeHexByte(buf, stack[pos++]);
  142. }
  143. while (pos < stack.Length);
  144. }
  145. }
  146. }
  147. #pragma warning restore
  148. #endif