001: package org.bouncycastle.asn1.x509.sigi;
002:
003: import org.bouncycastle.asn1.ASN1Choice;
004: import org.bouncycastle.asn1.ASN1Encodable;
005: import org.bouncycastle.asn1.ASN1EncodableVector;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.DERObject;
008: import org.bouncycastle.asn1.DERSequence;
009: import org.bouncycastle.asn1.DERString;
010: import org.bouncycastle.asn1.x500.DirectoryString;
011:
012: import java.util.Enumeration;
013:
014: /**
015: * Structure for a name or pseudonym.
016: *
017: * <pre>
018: * NameOrPseudonym ::= CHOICE {
019: * surAndGivenName SEQUENCE {
020: * surName DirectoryString,
021: * givenName SEQUENCE OF DirectoryString
022: * },
023: * pseudonym DirectoryString
024: * }
025: * </pre>
026: *
027: * @see org.bouncycastle.asn1.x509.sigi.PersonalData
028: *
029: */
030: public class NameOrPseudonym extends ASN1Encodable implements
031: ASN1Choice {
032: private DirectoryString pseudonym;
033:
034: private DirectoryString surname;
035:
036: private ASN1Sequence givenName;
037:
038: public static NameOrPseudonym getInstance(Object obj) {
039: if (obj == null || obj instanceof NameOrPseudonym) {
040: return (NameOrPseudonym) obj;
041: }
042:
043: if (obj instanceof DERString) {
044: return new NameOrPseudonym(DirectoryString.getInstance(obj));
045: }
046:
047: if (obj instanceof ASN1Sequence) {
048: return new NameOrPseudonym((ASN1Sequence) obj);
049: }
050:
051: throw new IllegalArgumentException(
052: "illegal object in getInstance: "
053: + obj.getClass().getName());
054: }
055:
056: /**
057: * Constructor from DERString.
058: * <p/>
059: * The sequence is of type NameOrPseudonym:
060: * <p/>
061: * <pre>
062: * NameOrPseudonym ::= CHOICE {
063: * surAndGivenName SEQUENCE {
064: * surName DirectoryString,
065: * givenName SEQUENCE OF DirectoryString
066: * },
067: * pseudonym DirectoryString
068: * }
069: * </pre>
070: * @param pseudonym pseudonym value to use.
071: */
072: public NameOrPseudonym(DirectoryString pseudonym) {
073: this .pseudonym = pseudonym;
074: }
075:
076: /**
077: * Constructor from ASN1Sequence.
078: * <p/>
079: * The sequence is of type NameOrPseudonym:
080: * <p/>
081: * <pre>
082: * NameOrPseudonym ::= CHOICE {
083: * surAndGivenName SEQUENCE {
084: * surName DirectoryString,
085: * givenName SEQUENCE OF DirectoryString
086: * },
087: * pseudonym DirectoryString
088: * }
089: * </pre>
090: *
091: * @param seq The ASN.1 sequence.
092: */
093: private NameOrPseudonym(ASN1Sequence seq) {
094: if (seq.size() != 2) {
095: throw new IllegalArgumentException("Bad sequence size: "
096: + seq.size());
097: }
098:
099: if (!(seq.getObjectAt(0) instanceof DERString)) {
100: throw new IllegalArgumentException(
101: "Bad object encountered: "
102: + seq.getObjectAt(0).getClass());
103: }
104:
105: surname = DirectoryString.getInstance(seq.getObjectAt(0));
106: givenName = ASN1Sequence.getInstance(seq.getObjectAt(1));
107: }
108:
109: /**
110: * Constructor from a given details.
111: *
112: * @param pseudonym The pseudonym.
113: */
114: public NameOrPseudonym(String pseudonym) {
115: this (new DirectoryString(pseudonym));
116: }
117:
118: /**
119: * Constructor from a given details.
120: *
121: * @param surname The surname.
122: * @param givenName A sequence of directory strings making up the givenName
123: */
124: public NameOrPseudonym(DirectoryString surname,
125: ASN1Sequence givenName) {
126: this .surname = surname;
127: this .givenName = givenName;
128: }
129:
130: public DirectoryString getPseudonym() {
131: return pseudonym;
132: }
133:
134: public DirectoryString getSurname() {
135: return surname;
136: }
137:
138: public DirectoryString[] getGivenName() {
139: DirectoryString[] items = new DirectoryString[givenName.size()];
140: int count = 0;
141: for (Enumeration e = givenName.getObjects(); e
142: .hasMoreElements();) {
143: items[count++] = DirectoryString.getInstance(e
144: .nextElement());
145: }
146: return items;
147: }
148:
149: /**
150: * Produce an object suitable for an ASN1OutputStream.
151: * <p/>
152: * Returns:
153: * <p/>
154: * <pre>
155: * NameOrPseudonym ::= CHOICE {
156: * surAndGivenName SEQUENCE {
157: * surName DirectoryString,
158: * givenName SEQUENCE OF DirectoryString
159: * },
160: * pseudonym DirectoryString
161: * }
162: * </pre>
163: *
164: * @return a DERObject
165: */
166: public DERObject toASN1Object() {
167: if (pseudonym != null) {
168: return pseudonym.toASN1Object();
169: } else {
170: ASN1EncodableVector vec1 = new ASN1EncodableVector();
171: vec1.add(surname);
172: vec1.add(givenName);
173: return new DERSequence(vec1);
174: }
175: }
176: }
|