DerIA5String.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. /**
  9. * Der IA5String object - this is an ascii string.
  10. */
  11. public class DerIA5String
  12. : DerStringBase
  13. {
  14. private readonly string str;
  15. /**
  16. * return a IA5 string from the passed in object
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerIA5String GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerIA5String)
  24. {
  25. return (DerIA5String)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return an IA5 string from a tagged object.
  31. *
  32. * @param obj the tagged object holding the object we want
  33. * @param explicitly true if the object is meant to be explicitly
  34. * tagged false otherwise.
  35. * @exception ArgumentException if the tagged object cannot
  36. * be converted.
  37. */
  38. public static DerIA5String GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerIA5String)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerIA5String(((Asn1OctetString)o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - with bytes.
  51. */
  52. public DerIA5String(
  53. byte[] str)
  54. : this(Strings.FromAsciiByteArray(str), false)
  55. {
  56. }
  57. /**
  58. * basic constructor - without validation.
  59. */
  60. public DerIA5String(
  61. string str)
  62. : this(str, false)
  63. {
  64. }
  65. /**
  66. * Constructor with optional validation.
  67. *
  68. * @param string the base string to wrap.
  69. * @param validate whether or not to check the string.
  70. * @throws ArgumentException if validate is true and the string
  71. * contains characters that should not be in an IA5String.
  72. */
  73. public DerIA5String(
  74. string str,
  75. bool validate)
  76. {
  77. if (str == null)
  78. throw new ArgumentNullException("str");
  79. if (validate && !IsIA5String(str))
  80. throw new ArgumentException("string contains illegal characters", "str");
  81. this.str = str;
  82. }
  83. public override string GetString()
  84. {
  85. return str;
  86. }
  87. public byte[] GetOctets()
  88. {
  89. return Strings.ToAsciiByteArray(str);
  90. }
  91. internal override int EncodedLength(bool withID)
  92. {
  93. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length);
  94. }
  95. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  96. {
  97. asn1Out.WriteEncodingDL(withID, Asn1Tags.IA5String, GetOctets());
  98. }
  99. protected override int Asn1GetHashCode()
  100. {
  101. return this.str.GetHashCode();
  102. }
  103. protected override bool Asn1Equals(
  104. Asn1Object asn1Object)
  105. {
  106. DerIA5String other = asn1Object as DerIA5String;
  107. if (other == null)
  108. return false;
  109. return this.str.Equals(other.str);
  110. }
  111. /**
  112. * return true if the passed in String can be represented without
  113. * loss as an IA5String, false otherwise.
  114. *
  115. * @return true if in printable set, false otherwise.
  116. */
  117. public static bool IsIA5String(
  118. string str)
  119. {
  120. foreach (char ch in str)
  121. {
  122. if (ch > 0x007f)
  123. {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif