DerBMPString.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * Der BMPString object.
  10. */
  11. public class DerBmpString
  12. : DerStringBase
  13. {
  14. internal class Meta : Asn1UniversalType
  15. {
  16. internal static readonly Asn1UniversalType Instance = new Meta();
  17. private Meta() : base(typeof(DerBmpString), Asn1Tags.BmpString) {}
  18. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  19. {
  20. return CreatePrimitive(octetString.GetOctets());
  21. }
  22. }
  23. /**
  24. * return a BMP string from the given object.
  25. *
  26. * @param obj the object we want converted.
  27. * @exception ArgumentException if the object cannot be converted.
  28. */
  29. public static DerBmpString GetInstance(object obj)
  30. {
  31. if (obj == null)
  32. return null;
  33. if (obj is DerBmpString derBmpString)
  34. return derBmpString;
  35. if (obj is IAsn1Convertible asn1Convertible)
  36. {
  37. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  38. if (asn1Object is DerBmpString converted)
  39. return converted;
  40. }
  41. else if (obj is byte[] bytes)
  42. {
  43. try
  44. {
  45. return (DerBmpString)Meta.Instance.FromByteArray(bytes);
  46. }
  47. catch (IOException e)
  48. {
  49. throw new ArgumentException("failed to construct BMP string from byte[]: " + e.Message);
  50. }
  51. }
  52. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  53. }
  54. /**
  55. * return a BMP string from a tagged object.
  56. *
  57. * @param taggedObject the tagged object holding the object we want
  58. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  59. * @exception ArgumentException if the tagged object cannot be converted.
  60. */
  61. public static DerBmpString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  62. {
  63. return (DerBmpString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  64. }
  65. private readonly string m_str;
  66. internal DerBmpString(byte[] contents)
  67. {
  68. if (null == contents)
  69. throw new ArgumentNullException("contents");
  70. int byteLen = contents.Length;
  71. if (0 != (byteLen & 1))
  72. throw new ArgumentException("malformed BMPString encoding encountered", "contents");
  73. int charLen = byteLen / 2;
  74. char[] cs = new char[charLen];
  75. for (int i = 0; i != charLen; i++)
  76. {
  77. cs[i] = (char)((contents[2 * i] << 8) | (contents[2 * i + 1] & 0xff));
  78. }
  79. m_str = new string(cs);
  80. }
  81. internal DerBmpString(char[] str)
  82. {
  83. if (str == null)
  84. throw new ArgumentNullException("str");
  85. m_str = new string(str);
  86. }
  87. /**
  88. * basic constructor
  89. */
  90. public DerBmpString(string str)
  91. {
  92. if (str == null)
  93. throw new ArgumentNullException("str");
  94. m_str = str;
  95. }
  96. public override string GetString()
  97. {
  98. return m_str;
  99. }
  100. protected override bool Asn1Equals(Asn1Object asn1Object)
  101. {
  102. DerBmpString that = asn1Object as DerBmpString;
  103. return null != that
  104. && this.m_str.Equals(that.m_str);
  105. }
  106. protected override int Asn1GetHashCode()
  107. {
  108. return m_str.GetHashCode();
  109. }
  110. internal override IAsn1Encoding GetEncoding(int encoding)
  111. {
  112. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.BmpString, GetContents());
  113. }
  114. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  115. {
  116. return new PrimitiveEncoding(tagClass, tagNo, GetContents());
  117. }
  118. private byte[] GetContents()
  119. {
  120. char[] c = m_str.ToCharArray();
  121. byte[] b = new byte[c.Length * 2];
  122. for (int i = 0; i != c.Length; i++)
  123. {
  124. b[2 * i] = (byte)(c[i] >> 8);
  125. b[2 * i + 1] = (byte)c[i];
  126. }
  127. return b;
  128. }
  129. internal static DerBmpString CreatePrimitive(byte[] contents)
  130. {
  131. return new DerBmpString(contents);
  132. }
  133. internal static DerBmpString CreatePrimitive(char[] str)
  134. {
  135. // TODO[asn1] Asn1InputStream has a validator/converter that should be unified in this class somehow
  136. return new DerBmpString(str);
  137. }
  138. }
  139. }
  140. #pragma warning restore
  141. #endif