DerIA5String.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * IA5String object - this is an Ascii string.
  10. */
  11. public class DerIA5String
  12. : DerStringBase
  13. {
  14. internal class Meta : Asn1UniversalType
  15. {
  16. internal static readonly Asn1UniversalType Instance = new Meta();
  17. private Meta() : base(typeof(DerIA5String), Asn1Tags.IA5String) {}
  18. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  19. {
  20. return CreatePrimitive(octetString.GetOctets());
  21. }
  22. }
  23. /**
  24. * return an IA5 string from the passed in object
  25. *
  26. * @exception ArgumentException if the object cannot be converted.
  27. */
  28. public static DerIA5String GetInstance(object obj)
  29. {
  30. if (obj == null)
  31. return null;
  32. if (obj is DerIA5String derIA5String)
  33. return derIA5String;
  34. if (obj is IAsn1Convertible asn1Convertible)
  35. {
  36. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  37. if (asn1Object is DerIA5String converted)
  38. return converted;
  39. }
  40. else if (obj is byte[] bytes)
  41. {
  42. try
  43. {
  44. return (DerIA5String)Meta.Instance.FromByteArray(bytes);
  45. }
  46. catch (IOException e)
  47. {
  48. throw new ArgumentException("failed to construct IA5 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 an IA5 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 DerIA5String GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  61. {
  62. return (DerIA5String)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  63. }
  64. private readonly byte[] m_contents;
  65. public DerIA5String(string str)
  66. : this(str, false)
  67. {
  68. }
  69. /**
  70. * Constructor with optional validation.
  71. *
  72. * @param string the base string to wrap.
  73. * @param validate whether or not to check the string.
  74. * @throws ArgumentException if validate is true and the string
  75. * contains characters that should not be in an IA5String.
  76. */
  77. public DerIA5String(string str, bool validate)
  78. {
  79. if (str == null)
  80. throw new ArgumentNullException("str");
  81. if (validate && !IsIA5String(str))
  82. throw new ArgumentException("string contains illegal characters", "str");
  83. m_contents = Strings.ToAsciiByteArray(str);
  84. }
  85. public DerIA5String(byte[] contents)
  86. : this(contents, true)
  87. {
  88. }
  89. internal DerIA5String(byte[] contents, bool clone)
  90. {
  91. if (null == contents)
  92. throw new ArgumentNullException("contents");
  93. m_contents = clone ? Arrays.Clone(contents) : contents;
  94. }
  95. public override string GetString()
  96. {
  97. return Strings.FromAsciiByteArray(m_contents);
  98. }
  99. public byte[] GetOctets()
  100. {
  101. return Arrays.Clone(m_contents);
  102. }
  103. internal override IAsn1Encoding GetEncoding(int encoding)
  104. {
  105. return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.IA5String, m_contents);
  106. }
  107. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  108. {
  109. return new PrimitiveEncoding(tagClass, tagNo, m_contents);
  110. }
  111. protected override bool Asn1Equals(Asn1Object asn1Object)
  112. {
  113. DerIA5String that = asn1Object as DerIA5String;
  114. return null != that
  115. && Arrays.AreEqual(this.m_contents, that.m_contents);
  116. }
  117. protected override int Asn1GetHashCode()
  118. {
  119. return Arrays.GetHashCode(m_contents);
  120. }
  121. /**
  122. * return true if the passed in String can be represented without
  123. * loss as an IA5String, false otherwise.
  124. *
  125. * @return true if in printable set, false otherwise.
  126. */
  127. public static bool IsIA5String(string str)
  128. {
  129. foreach (char ch in str)
  130. {
  131. if (ch > 0x007f)
  132. return false;
  133. }
  134. return true;
  135. }
  136. internal static DerIA5String CreatePrimitive(byte[] contents)
  137. {
  138. return new DerIA5String(contents, false);
  139. }
  140. }
  141. }
  142. #pragma warning restore
  143. #endif