001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Choice;
004: import org.bouncycastle.asn1.ASN1Encodable;
005: import org.bouncycastle.asn1.ASN1TaggedObject;
006: import org.bouncycastle.asn1.DERObject;
007: import org.bouncycastle.asn1.DERBMPString;
008: import org.bouncycastle.asn1.DERIA5String;
009: import org.bouncycastle.asn1.DERUTF8String;
010: import org.bouncycastle.asn1.DERVisibleString;
011: import org.bouncycastle.asn1.DERString;
012:
013: /**
014: * <code>DisplayText</code> class, used in
015: * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
016: *
017: * <p>It stores a string in a chosen encoding.
018: * <pre>
019: * DisplayText ::= CHOICE {
020: * ia5String IA5String (SIZE (1..200)),
021: * visibleString VisibleString (SIZE (1..200)),
022: * bmpString BMPString (SIZE (1..200)),
023: * utf8String UTF8String (SIZE (1..200)) }
024: * </pre>
025: * @see PolicyQualifierInfo
026: * @see PolicyInformation
027: */
028: public class DisplayText extends ASN1Encodable implements ASN1Choice {
029: /**
030: * Constant corresponding to ia5String encoding.
031: *
032: */
033: public static final int CONTENT_TYPE_IA5STRING = 0;
034: /**
035: * Constant corresponding to bmpString encoding.
036: *
037: */
038: public static final int CONTENT_TYPE_BMPSTRING = 1;
039: /**
040: * Constant corresponding to utf8String encoding.
041: *
042: */
043: public static final int CONTENT_TYPE_UTF8STRING = 2;
044: /**
045: * Constant corresponding to visibleString encoding.
046: *
047: */
048: public static final int CONTENT_TYPE_VISIBLESTRING = 3;
049:
050: /**
051: * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
052: *
053: */
054: public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;
055:
056: int contentType;
057: DERString contents;
058:
059: /**
060: * Creates a new <code>DisplayText</code> instance.
061: *
062: * @param type the desired encoding type for the text.
063: * @param text the text to store. Strings longer than 200
064: * characters are truncated.
065: */
066: public DisplayText(int type, String text) {
067: if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
068: // RFC3280 limits these strings to 200 chars
069: // truncate the string
070: text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
071: }
072:
073: contentType = type;
074: switch (type) {
075: case CONTENT_TYPE_IA5STRING:
076: contents = (DERString) new DERIA5String(text);
077: break;
078: case CONTENT_TYPE_UTF8STRING:
079: contents = (DERString) new DERUTF8String(text);
080: break;
081: case CONTENT_TYPE_VISIBLESTRING:
082: contents = (DERString) new DERVisibleString(text);
083: break;
084: case CONTENT_TYPE_BMPSTRING:
085: contents = (DERString) new DERBMPString(text);
086: break;
087: default:
088: contents = (DERString) new DERUTF8String(text);
089: break;
090: }
091: }
092:
093: /**
094: * Creates a new <code>DisplayText</code> instance.
095: *
096: * @param text the text to encapsulate. Strings longer than 200
097: * characters are truncated.
098: */
099: public DisplayText(String text) {
100: // by default use UTF8String
101: if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
102: text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
103: }
104:
105: contentType = CONTENT_TYPE_UTF8STRING;
106: contents = new DERUTF8String(text);
107: }
108:
109: /**
110: * Creates a new <code>DisplayText</code> instance.
111: * <p>Useful when reading back a <code>DisplayText</code> class
112: * from it's ASN1Encodable/DEREncodable form.
113: *
114: * @param de a <code>DEREncodable</code> instance.
115: */
116: public DisplayText(DERString de) {
117: contents = de;
118: }
119:
120: public static DisplayText getInstance(Object de) {
121: if (de instanceof DERString) {
122: return new DisplayText((DERString) de);
123: } else if (de instanceof DisplayText) {
124: return (DisplayText) de;
125: }
126:
127: throw new IllegalArgumentException(
128: "illegal object in getInstance");
129: }
130:
131: public static DisplayText getInstance(ASN1TaggedObject obj,
132: boolean explicit) {
133: return getInstance(obj.getObject()); // must be explicitly tagged
134: }
135:
136: public DERObject toASN1Object() {
137: return (DERObject) contents;
138: }
139:
140: /**
141: * Returns the stored <code>String</code> object.
142: *
143: * @return the stored text as a <code>String</code>.
144: */
145: public String getString() {
146: return contents.getString();
147: }
148: }
|