DerVisibleString.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * VisibleString object.
  10. */
  11. public class DerVisibleString
  12. : DerStringBase
  13. {
  14. internal class Meta : Asn1UniversalType
  15. {
  16. internal static readonly Asn1UniversalType Instance = new Meta();
  17. private Meta() : base(typeof(DerVisibleString), Asn1Tags.VisibleString) {}
  18. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  19. {
  20. return CreatePrimitive(octetString.GetOctets());
  21. }
  22. }
  23. /**
  24. * return a visible string from the passed in object.
  25. *
  26. * @exception ArgumentException if the object cannot be converted.
  27. */
  28. public static DerVisibleString GetInstance(object obj)
  29. {
  30. if (obj == null)
  31. return null;
  32. if (obj is DerVisibleString derVisibleString)
  33. return derVisibleString;
  34. if (obj is IAsn1Convertible asn1Convertible)
  35. {
  36. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  37. if (asn1Object is DerVisibleString converted)
  38. return converted;
  39. }
  40. else if (obj is byte[] bytes)
  41. {
  42. try
  43. {
  44. return (DerVisibleString)Meta.Instance.FromByteArray(bytes);
  45. }
  46. catch (IOException e)
  47. {
  48. throw new ArgumentException("failed to construct visible string from byte[]: " + e.Message);
  49. }
  50. }
  51. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  52. }
  53. /**
  54. * return a visible string from a tagged object.
  55. *
  56. * @param taggedObject the tagged object holding the object we want
  57. * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
  58. * @exception ArgumentException if the tagged object cannot be converted.
  59. */
  60. public static DerVisibleString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  61. {
  62. return (DerVisibleString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  63. }
  64. private readonly byte[] m_contents;
  65. public DerVisibleString(string str)
  66. {
  67. if (str == null)
  68. throw new ArgumentNullException("str");
  69. m_contents = Strings.ToAsciiByteArray(str);
  70. }
  71. public DerVisibleString(byte[] contents)
  72. : this(contents, true)
  73. {
  74. }
  75. internal DerVisibleString(byte[] contents, bool clone)
  76. {
  77. if (null == contents)
  78. throw new ArgumentNullException("contents");
  79. m_contents = clone ? Arrays.Clone(contents) : contents;
  80. }
  81. public override string GetString()
  82. {
  83. return Strings.FromAsciiByteArray(m_contents);
  84. }
  85. public byte[] GetOctets()
  86. {
  87. return Arrays.Clone(m_contents);
  88. }
  89. internal override IAsn1Encoding GetEncoding(int encoding)
  90. {
  91. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.VisibleString, m_contents);
  92. }
  93. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  94. {
  95. return new PrimitiveEncoding(tagClass, tagNo, m_contents);
  96. }
  97. protected override bool Asn1Equals(Asn1Object asn1Object)
  98. {
  99. DerVisibleString that = asn1Object as DerVisibleString;
  100. return null != that
  101. && Arrays.AreEqual(this.m_contents, that.m_contents);
  102. }
  103. protected override int Asn1GetHashCode()
  104. {
  105. return Arrays.GetHashCode(m_contents);
  106. }
  107. internal static DerVisibleString CreatePrimitive(byte[] contents)
  108. {
  109. return new DerVisibleString(contents, false);
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif