1 2package org.bouncycastle.asn1.x509; 3 4import org.bouncycastle.asn1.ASN1Choice; 5import org.bouncycastle.asn1.ASN1Encodable; 6import org.bouncycastle.asn1.ASN1TaggedObject; 7import org.bouncycastle.asn1.DERObject; 8import org.bouncycastle.asn1.DERBMPString; 9import org.bouncycastle.asn1.DERIA5String; 10import org.bouncycastle.asn1.DERUTF8String; 11import org.bouncycastle.asn1.DERVisibleString; 12import org.bouncycastle.asn1.DERString; 13 14/** 15 * <code>DisplayText</code> class, used in 16 * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers). 17 * 18 * <p>It stores a string in a chosen encoding. 19 * <pre> 20 * DisplayText ::= CHOICE { 21 * ia5String IA5String (SIZE (1..200)), 22 * visibleString VisibleString (SIZE (1..200)), 23 * bmpString BMPString (SIZE (1..200)), 24 * utf8String UTF8String (SIZE (1..200)) } 25 * </pre> 26 * @see PolicyQualifierInfo 27 * @see PolicyInformation 28 */ 29public class DisplayText 30 extends ASN1Encodable 31 implements ASN1Choice 32{ 33 /** 34 * Constant corresponding to ia5String encoding. 35 * 36 */ 37 public static final int CONTENT_TYPE_IA5STRING = 0; 38 /** 39 * Constant corresponding to bmpString encoding. 40 * 41 */ 42 public static final int CONTENT_TYPE_BMPSTRING = 1; 43 /** 44 * Constant corresponding to utf8String encoding. 45 * 46 */ 47 public static final int CONTENT_TYPE_UTF8STRING = 2; 48 /** 49 * Constant corresponding to visibleString encoding. 50 * 51 */ 52 public static final int CONTENT_TYPE_VISIBLESTRING = 3; 53 54 /** 55 * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here. 56 * 57 */ 58 public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200; 59 60 int contentType; 61 DERString contents; 62 63 /** 64 * Creates a new <code>DisplayText</code> instance. 65 * 66 * @param type the desired encoding type for the text. 67 * @param text the text to store. Strings longer than 200 68 * characters are truncated. 69 */ 70 public DisplayText (int type, String text) 71 { 72 if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) 73 { 74 // RFC3280 limits these strings to 200 chars 75 // truncate the string 76 text = text.substring (0, DISPLAY_TEXT_MAXIMUM_SIZE); 77 } 78 79 contentType = type; 80 switch (type) 81 { 82 case CONTENT_TYPE_IA5STRING: 83 contents = (DERString)new DERIA5String (text); 84 break; 85 case CONTENT_TYPE_UTF8STRING: 86 contents = (DERString)new DERUTF8String(text); 87 break; 88 case CONTENT_TYPE_VISIBLESTRING: 89 contents = (DERString)new DERVisibleString(text); 90 break; 91 case CONTENT_TYPE_BMPSTRING: 92 contents = (DERString)new DERBMPString(text); 93 break; 94 default: 95 contents = (DERString)new DERUTF8String(text); 96 break; 97 } 98 } 99 100 /** 101 * Creates a new <code>DisplayText</code> instance. 102 * 103 * @param text the text to encapsulate. Strings longer than 200 104 * characters are truncated. 105 */ 106 public DisplayText (String text) 107 { 108 // by default use UTF8String 109 if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) 110 { 111 text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE); 112 } 113 114 contentType = CONTENT_TYPE_UTF8STRING; 115 contents = new DERUTF8String(text); 116 } 117 118 /** 119 * Creates a new <code>DisplayText</code> instance. 120 * <p>Useful when reading back a <code>DisplayText</code> class 121 * from it's ASN1Encodable/DEREncodable form. 122 * 123 * @param de a <code>DEREncodable</code> instance. 124 */ 125 public DisplayText(DERString de) 126 { 127 contents = de; 128 } 129 130 public static DisplayText getInstance(Object de) 131 { 132 if (de instanceof DERString) 133 { 134 return new DisplayText((DERString)de); 135 } 136 else if (de instanceof DisplayText) 137 { 138 return (DisplayText)de; 139 } 140 141 throw new IllegalArgumentException("illegal object in getInstance"); 142 } 143 144 public static DisplayText getInstance( 145 ASN1TaggedObject obj, 146 boolean explicit) 147 { 148 return getInstance(obj.getObject()); // must be explicitly tagged 149 } 150 151 public DERObject toASN1Object() 152 { 153 return (DERObject)contents; 154 } 155 156 /** 157 * Returns the stored <code>String</code> object. 158 * 159 * @return the stored text as a <code>String</code>. 160 */ 161 public String getString() 162 { 163 return contents.getString(); 164 } 165} 166