001: package org.bouncycastle.asn1.x509.sigi;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERGeneralizedTime;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERPrintableString;
011: import org.bouncycastle.asn1.DERSequence;
012: import org.bouncycastle.asn1.DERTaggedObject;
013: import org.bouncycastle.asn1.x500.DirectoryString;
014:
015: import java.math.BigInteger;
016: import java.util.Enumeration;
017:
018: /**
019: * Contains personal data for the otherName field in the subjectAltNames
020: * extension.
021: * <p/>
022: * <pre>
023: * PersonalData ::= SEQUENCE {
024: * nameOrPseudonym NameOrPseudonym,
025: * nameDistinguisher [0] INTEGER OPTIONAL,
026: * dateOfBirth [1] GeneralizedTime OPTIONAL,
027: * placeOfBirth [2] DirectoryString OPTIONAL,
028: * gender [3] PrintableString OPTIONAL,
029: * postalAddress [4] DirectoryString OPTIONAL
030: * }
031: * </pre>
032: *
033: * @see org.bouncycastle.asn1.x509.sigi.NameOrPseudonym
034: * @see org.bouncycastle.asn1.x509.sigi.SigIObjectIdentifiers
035: */
036: public class PersonalData extends ASN1Encodable {
037: private NameOrPseudonym nameOrPseudonym;
038: private BigInteger nameDistinguisher;
039: private DERGeneralizedTime dateOfBirth;
040: private DirectoryString placeOfBirth;
041: private String gender;
042: private DirectoryString postalAddress;
043:
044: public static PersonalData getInstance(Object obj) {
045: if (obj == null || obj instanceof PersonalData) {
046: return (PersonalData) obj;
047: }
048:
049: if (obj instanceof ASN1Sequence) {
050: return new PersonalData((ASN1Sequence) obj);
051: }
052:
053: throw new IllegalArgumentException(
054: "illegal object in getInstance: "
055: + obj.getClass().getName());
056: }
057:
058: /**
059: * Constructor from ASN1Sequence.
060: * <p/>
061: * The sequence is of type NameOrPseudonym:
062: * <p/>
063: * <pre>
064: * PersonalData ::= SEQUENCE {
065: * nameOrPseudonym NameOrPseudonym,
066: * nameDistinguisher [0] INTEGER OPTIONAL,
067: * dateOfBirth [1] GeneralizedTime OPTIONAL,
068: * placeOfBirth [2] DirectoryString OPTIONAL,
069: * gender [3] PrintableString OPTIONAL,
070: * postalAddress [4] DirectoryString OPTIONAL
071: * }
072: * </pre>
073: *
074: * @param seq The ASN.1 sequence.
075: */
076: private PersonalData(ASN1Sequence seq) {
077: if (seq.size() < 1) {
078: throw new IllegalArgumentException("Bad sequence size: "
079: + seq.size());
080: }
081:
082: Enumeration e = seq.getObjects();
083:
084: nameOrPseudonym = NameOrPseudonym.getInstance(e.nextElement());
085:
086: while (e.hasMoreElements()) {
087: ASN1TaggedObject o = ASN1TaggedObject.getInstance(e
088: .nextElement());
089: int tag = o.getTagNo();
090: switch (tag) {
091: case 0:
092: nameDistinguisher = DERInteger.getInstance(o, false)
093: .getValue();
094: break;
095: case 1:
096: dateOfBirth = DERGeneralizedTime.getInstance(o, false);
097: break;
098: case 2:
099: placeOfBirth = DirectoryString.getInstance(o, true);
100: break;
101: case 3:
102: gender = DERPrintableString.getInstance(o, false)
103: .getString();
104: break;
105: case 4:
106: postalAddress = DirectoryString.getInstance(o, true);
107: break;
108: default:
109: throw new IllegalArgumentException("Bad tag number: "
110: + o.getTagNo());
111: }
112: }
113: }
114:
115: /**
116: * Constructor from a given details.
117: *
118: * @param nameOrPseudonym Name or pseudonym.
119: * @param nameDistinguisher Name distinguisher.
120: * @param dateOfBirth Date of birth.
121: * @param placeOfBirth Place of birth.
122: * @param gender Gender.
123: * @param postalAddress Postal Address.
124: */
125: public PersonalData(NameOrPseudonym nameOrPseudonym,
126: BigInteger nameDistinguisher,
127: DERGeneralizedTime dateOfBirth,
128: DirectoryString placeOfBirth, String gender,
129: DirectoryString postalAddress) {
130: this .nameOrPseudonym = nameOrPseudonym;
131: this .dateOfBirth = dateOfBirth;
132: this .gender = gender;
133: this .nameDistinguisher = nameDistinguisher;
134: this .postalAddress = postalAddress;
135: this .placeOfBirth = placeOfBirth;
136: }
137:
138: public NameOrPseudonym getNameOrPseudonym() {
139: return nameOrPseudonym;
140: }
141:
142: public BigInteger getNameDistinguisher() {
143: return nameDistinguisher;
144: }
145:
146: public DERGeneralizedTime getDateOfBirth() {
147: return dateOfBirth;
148: }
149:
150: public DirectoryString getPlaceOfBirth() {
151: return placeOfBirth;
152: }
153:
154: public String getGender() {
155: return gender;
156: }
157:
158: public DirectoryString getPostalAddress() {
159: return postalAddress;
160: }
161:
162: /**
163: * Produce an object suitable for an ASN1OutputStream.
164: * <p/>
165: * Returns:
166: * <p/>
167: * <pre>
168: * PersonalData ::= SEQUENCE {
169: * nameOrPseudonym NameOrPseudonym,
170: * nameDistinguisher [0] INTEGER OPTIONAL,
171: * dateOfBirth [1] GeneralizedTime OPTIONAL,
172: * placeOfBirth [2] DirectoryString OPTIONAL,
173: * gender [3] PrintableString OPTIONAL,
174: * postalAddress [4] DirectoryString OPTIONAL
175: * }
176: * </pre>
177: *
178: * @return a DERObject
179: */
180: public DERObject toASN1Object() {
181: ASN1EncodableVector vec = new ASN1EncodableVector();
182: vec.add(nameOrPseudonym);
183: if (nameDistinguisher != null) {
184: vec.add(new DERTaggedObject(false, 0, new DERInteger(
185: nameDistinguisher)));
186: }
187: if (dateOfBirth != null) {
188: vec.add(new DERTaggedObject(false, 1, dateOfBirth));
189: }
190: if (placeOfBirth != null) {
191: vec.add(new DERTaggedObject(true, 2, placeOfBirth));
192: }
193: if (gender != null) {
194: vec.add(new DERTaggedObject(false, 3,
195: new DERPrintableString(gender, true)));
196: }
197: if (postalAddress != null) {
198: vec.add(new DERTaggedObject(true, 4, postalAddress));
199: }
200: return new DERSequence(vec);
201: }
202: }
|