DerPrintableString.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 PrintableString object.
  10. */
  11. public class DerPrintableString
  12. : DerStringBase
  13. {
  14. private readonly string str;
  15. /**
  16. * return a printable string from the passed in object.
  17. *
  18. * @exception ArgumentException if the object cannot be converted.
  19. */
  20. public static DerPrintableString GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is DerPrintableString)
  24. {
  25. return (DerPrintableString)obj;
  26. }
  27. throw new ArgumentException("illegal object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  28. }
  29. /**
  30. * return a Printable 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 DerPrintableString GetInstance(
  39. Asn1TaggedObject obj,
  40. bool isExplicit)
  41. {
  42. Asn1Object o = obj.GetObject();
  43. if (isExplicit || o is DerPrintableString)
  44. {
  45. return GetInstance(o);
  46. }
  47. return new DerPrintableString(Asn1OctetString.GetInstance(o).GetOctets());
  48. }
  49. /**
  50. * basic constructor - byte encoded string.
  51. */
  52. public DerPrintableString(
  53. byte[] str)
  54. : this(Strings.FromAsciiByteArray(str), false)
  55. {
  56. }
  57. /**
  58. * basic constructor - this does not validate the string
  59. */
  60. public DerPrintableString(
  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 a PrintableString.
  72. */
  73. public DerPrintableString(
  74. string str,
  75. bool validate)
  76. {
  77. if (str == null)
  78. throw new ArgumentNullException("str");
  79. if (validate && !IsPrintableString(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.PrintableString, GetOctets());
  98. }
  99. protected override bool Asn1Equals(
  100. Asn1Object asn1Object)
  101. {
  102. DerPrintableString other = asn1Object as DerPrintableString;
  103. if (other == null)
  104. return false;
  105. return this.str.Equals(other.str);
  106. }
  107. /**
  108. * return true if the passed in String can be represented without
  109. * loss as a PrintableString, false otherwise.
  110. *
  111. * @return true if in printable set, false otherwise.
  112. */
  113. public static bool IsPrintableString(
  114. string str)
  115. {
  116. foreach (char ch in str)
  117. {
  118. if (ch > 0x007f)
  119. return false;
  120. if (char.IsLetterOrDigit(ch))
  121. continue;
  122. // if (char.IsPunctuation(ch))
  123. // continue;
  124. switch (ch)
  125. {
  126. case ' ':
  127. case '\'':
  128. case '(':
  129. case ')':
  130. case '+':
  131. case '-':
  132. case '.':
  133. case ':':
  134. case '=':
  135. case '?':
  136. case '/':
  137. case ',':
  138. continue;
  139. }
  140. return false;
  141. }
  142. return true;
  143. }
  144. }
  145. }
  146. #pragma warning restore
  147. #endif