DerUTF8String.cs 4.0 KB

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