using System; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.X509 { /** * DisplayText class, used in * CertificatePolicies X509 V3 extensions (in policy qualifiers). * *

It stores a string in a chosen encoding. *

	 * DisplayText ::= CHOICE {
	 *      ia5String        IA5String      (SIZE (1..200)),
	 *      visibleString    VisibleString  (SIZE (1..200)),
	 *      bmpString        BMPString      (SIZE (1..200)),
	 *      utf8String       UTF8String     (SIZE (1..200)) }
	 * 

* @see PolicyQualifierInfo * @see PolicyInformation */ public class DisplayText : Asn1Encodable, IAsn1Choice { /** * Constant corresponding to ia5String encoding. * */ public const int ContentTypeIA5String = 0; /** * Constant corresponding to bmpString encoding. * */ public const int ContentTypeBmpString = 1; /** * Constant corresponding to utf8String encoding. * */ public const int ContentTypeUtf8String = 2; /** * Constant corresponding to visibleString encoding. * */ public const int ContentTypeVisibleString = 3; /** * Describe constant DisplayTextMaximumSize here. * */ public const int DisplayTextMaximumSize = 200; internal readonly int contentType; internal readonly IAsn1String contents; /** * Creates a new DisplayText instance. * * @param type the desired encoding type for the text. * @param text the text to store. Strings longer than 200 * characters are truncated. */ public DisplayText( int type, string text) { if (text.Length > DisplayTextMaximumSize) { // RFC3280 limits these strings to 200 chars // truncate the string text = text.Substring(0, DisplayTextMaximumSize); } contentType = type; switch (type) { case ContentTypeIA5String: contents = (IAsn1String)new DerIA5String (text); break; case ContentTypeUtf8String: contents = (IAsn1String)new DerUtf8String(text); break; case ContentTypeVisibleString: contents = (IAsn1String)new DerVisibleString(text); break; case ContentTypeBmpString: contents = (IAsn1String)new DerBmpString(text); break; default: contents = (IAsn1String)new DerUtf8String(text); break; } } // /** // * return true if the passed in string can be represented without // * loss as a PrintableString, false otherwise. // */ // private bool CanBePrintable( // string str) // { // for (int i = str.Length - 1; i >= 0; i--) // { // if (str[i] > 0x007f) // { // return false; // } // } // // return true; // } /** * Creates a new DisplayText instance. * * @param text the text to encapsulate. Strings longer than 200 * characters are truncated. */ public DisplayText( string text) { // by default use UTF8String if (text.Length > DisplayTextMaximumSize) { text = text.Substring(0, DisplayTextMaximumSize); } contentType = ContentTypeUtf8String; contents = new DerUtf8String(text); } /** * Creates a new DisplayText instance. *

Useful when reading back a DisplayText class * from it's Asn1Encodable form.

* * @param contents an Asn1Encodable instance. */ public DisplayText( IAsn1String contents) { this.contents = contents; } public static DisplayText GetInstance(object obj) { if (obj is IAsn1String asn1String) return new DisplayText(asn1String); if (obj is DisplayText displayText) return displayText; throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); } public static DisplayText GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { return Asn1Utilities.GetInstanceFromChoice(taggedObject, declaredExplicit, GetInstance); } public override Asn1Object ToAsn1Object() { return (Asn1Object) contents; } /** * Returns the stored string object. * * @return the stored text as a string. */ public string GetString() { return contents.GetString(); } } }