DisplayText.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * <code>DisplayText</code> class, used in
  9. * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
  10. *
  11. * <p>It stores a string in a chosen encoding.
  12. * <pre>
  13. * DisplayText ::= CHOICE {
  14. * ia5String IA5String (SIZE (1..200)),
  15. * visibleString VisibleString (SIZE (1..200)),
  16. * bmpString BMPString (SIZE (1..200)),
  17. * utf8String UTF8String (SIZE (1..200)) }
  18. * </pre></p>
  19. * @see PolicyQualifierInfo
  20. * @see PolicyInformation
  21. */
  22. public class DisplayText
  23. : Asn1Encodable, IAsn1Choice
  24. {
  25. /**
  26. * Constant corresponding to ia5String encoding.
  27. *
  28. */
  29. public const int ContentTypeIA5String = 0;
  30. /**
  31. * Constant corresponding to bmpString encoding.
  32. *
  33. */
  34. public const int ContentTypeBmpString = 1;
  35. /**
  36. * Constant corresponding to utf8String encoding.
  37. *
  38. */
  39. public const int ContentTypeUtf8String = 2;
  40. /**
  41. * Constant corresponding to visibleString encoding.
  42. *
  43. */
  44. public const int ContentTypeVisibleString = 3;
  45. /**
  46. * Describe constant <code>DisplayTextMaximumSize</code> here.
  47. *
  48. */
  49. public const int DisplayTextMaximumSize = 200;
  50. internal readonly int contentType;
  51. internal readonly IAsn1String contents;
  52. /**
  53. * Creates a new <code>DisplayText</code> instance.
  54. *
  55. * @param type the desired encoding type for the text.
  56. * @param text the text to store. Strings longer than 200
  57. * characters are truncated.
  58. */
  59. public DisplayText(
  60. int type,
  61. string text)
  62. {
  63. if (text.Length > DisplayTextMaximumSize)
  64. {
  65. // RFC3280 limits these strings to 200 chars
  66. // truncate the string
  67. text = text.Substring(0, DisplayTextMaximumSize);
  68. }
  69. contentType = type;
  70. switch (type)
  71. {
  72. case ContentTypeIA5String:
  73. contents = (IAsn1String)new DerIA5String (text);
  74. break;
  75. case ContentTypeUtf8String:
  76. contents = (IAsn1String)new DerUtf8String(text);
  77. break;
  78. case ContentTypeVisibleString:
  79. contents = (IAsn1String)new DerVisibleString(text);
  80. break;
  81. case ContentTypeBmpString:
  82. contents = (IAsn1String)new DerBmpString(text);
  83. break;
  84. default:
  85. contents = (IAsn1String)new DerUtf8String(text);
  86. break;
  87. }
  88. }
  89. // /**
  90. // * return true if the passed in string can be represented without
  91. // * loss as a PrintableString, false otherwise.
  92. // */
  93. // private bool CanBePrintable(
  94. // string str)
  95. // {
  96. // for (int i = str.Length - 1; i >= 0; i--)
  97. // {
  98. // if (str[i] > 0x007f)
  99. // {
  100. // return false;
  101. // }
  102. // }
  103. //
  104. // return true;
  105. // }
  106. /**
  107. * Creates a new <code>DisplayText</code> instance.
  108. *
  109. * @param text the text to encapsulate. Strings longer than 200
  110. * characters are truncated.
  111. */
  112. public DisplayText(
  113. string text)
  114. {
  115. // by default use UTF8String
  116. if (text.Length > DisplayTextMaximumSize)
  117. {
  118. text = text.Substring(0, DisplayTextMaximumSize);
  119. }
  120. contentType = ContentTypeUtf8String;
  121. contents = new DerUtf8String(text);
  122. }
  123. /**
  124. * Creates a new <code>DisplayText</code> instance.
  125. * <p>Useful when reading back a <code>DisplayText</code> class
  126. * from it's Asn1Encodable form.</p>
  127. *
  128. * @param contents an <code>Asn1Encodable</code> instance.
  129. */
  130. public DisplayText(
  131. IAsn1String contents)
  132. {
  133. this.contents = contents;
  134. }
  135. public static DisplayText GetInstance(
  136. object obj)
  137. {
  138. if (obj is IAsn1String)
  139. {
  140. return new DisplayText((IAsn1String) obj);
  141. }
  142. if (obj is DisplayText)
  143. {
  144. return (DisplayText) obj;
  145. }
  146. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  147. }
  148. public override Asn1Object ToAsn1Object()
  149. {
  150. return (Asn1Object) contents;
  151. }
  152. /**
  153. * Returns the stored <code>string</code> object.
  154. *
  155. * @return the stored text as a <code>string</code>.
  156. */
  157. public string GetString()
  158. {
  159. return contents.GetString();
  160. }
  161. }
  162. }
  163. #pragma warning restore
  164. #endif