DerPrintableString.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 PrintableString object.
  10. */
  11. public class DerPrintableString
  12. : DerStringBase
  13. {
  14. internal class Meta : Asn1UniversalType
  15. {
  16. internal static readonly Asn1UniversalType Instance = new Meta();
  17. private Meta() : base(typeof(DerPrintableString), Asn1Tags.PrintableString) {}
  18. internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
  19. {
  20. return CreatePrimitive(octetString.GetOctets());
  21. }
  22. }
  23. /**
  24. * return a printable string from the passed in object.
  25. *
  26. * @exception ArgumentException if the object cannot be converted.
  27. */
  28. public static DerPrintableString GetInstance(object obj)
  29. {
  30. if (obj == null)
  31. return null;
  32. if (obj is DerPrintableString derPrintableString)
  33. return derPrintableString;
  34. if (obj is IAsn1Convertible asn1Convertible)
  35. {
  36. Asn1Object asn1Object = asn1Convertible.ToAsn1Object();
  37. if (asn1Object is DerPrintableString converted)
  38. return converted;
  39. }
  40. else if (obj is byte[] bytes)
  41. {
  42. try
  43. {
  44. return (DerPrintableString)Meta.Instance.FromByteArray(bytes);
  45. }
  46. catch (IOException e)
  47. {
  48. throw new ArgumentException("failed to construct printable 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 printable 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 DerPrintableString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
  61. {
  62. return (DerPrintableString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
  63. }
  64. private readonly byte[] m_contents;
  65. public DerPrintableString(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 a PrintableString.
  76. */
  77. public DerPrintableString(string str, bool validate)
  78. {
  79. if (str == null)
  80. throw new ArgumentNullException("str");
  81. if (validate && !IsPrintableString(str))
  82. throw new ArgumentException("string contains illegal characters", "str");
  83. m_contents = Strings.ToAsciiByteArray(str);
  84. }
  85. public DerPrintableString(byte[] contents)
  86. : this(contents, true)
  87. {
  88. }
  89. internal DerPrintableString(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.PrintableString, 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(
  112. Asn1Object asn1Object)
  113. {
  114. DerPrintableString that = asn1Object as DerPrintableString;
  115. return null != that
  116. && Arrays.AreEqual(this.m_contents, that.m_contents);
  117. }
  118. protected override int Asn1GetHashCode()
  119. {
  120. return Arrays.GetHashCode(m_contents);
  121. }
  122. /**
  123. * return true if the passed in String can be represented without
  124. * loss as a PrintableString, false otherwise.
  125. *
  126. * @return true if in printable set, false otherwise.
  127. */
  128. public static bool IsPrintableString(string str)
  129. {
  130. foreach (char ch in str)
  131. {
  132. if (ch > 0x007f)
  133. return false;
  134. if (char.IsLetterOrDigit(ch))
  135. continue;
  136. // if (char.IsPunctuation(ch))
  137. // continue;
  138. switch (ch)
  139. {
  140. case ' ':
  141. case '\'':
  142. case '(':
  143. case ')':
  144. case '+':
  145. case '-':
  146. case '.':
  147. case ':':
  148. case '=':
  149. case '?':
  150. case '/':
  151. case ',':
  152. continue;
  153. }
  154. return false;
  155. }
  156. return true;
  157. }
  158. internal static DerPrintableString CreatePrimitive(byte[] contents)
  159. {
  160. return new DerPrintableString(contents, false);
  161. }
  162. }
  163. }
  164. #pragma warning restore
  165. #endif