DerPrintableString.cs 3.7 KB

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