001: package org.bouncycastle.asn1.esf;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.DERObject;
007: import org.bouncycastle.asn1.DERSequence;
008: import org.bouncycastle.asn1.DERTaggedObject;
009: import org.bouncycastle.asn1.DERUTF8String;
010:
011: import java.util.Enumeration;
012:
013: /**
014: * Signer-Location attribute (RFC3126).
015: *
016: * <pre>
017: * SignerLocation ::= SEQUENCE {
018: * countryName [0] DirectoryString OPTIONAL,
019: * localityName [1] DirectoryString OPTIONAL,
020: * postalAddress [2] PostalAddress OPTIONAL }
021: *
022: * PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
023: * </pre>
024: */
025: public class SignerLocation extends ASN1Encodable {
026: private DERUTF8String countryName;
027: private DERUTF8String localityName;
028: private ASN1Sequence postalAddress;
029:
030: public SignerLocation(ASN1Sequence seq) {
031: Enumeration e = seq.getObjects();
032:
033: while (e.hasMoreElements()) {
034: DERTaggedObject o = (DERTaggedObject) e.nextElement();
035:
036: switch (o.getTagNo()) {
037: case 0:
038: this .countryName = DERUTF8String.getInstance(o, true);
039: break;
040: case 1:
041: this .localityName = DERUTF8String.getInstance(o, true);
042: break;
043: case 2:
044: if (o.isExplicit()) {
045: this .postalAddress = ASN1Sequence.getInstance(o,
046: true);
047: } else // handle erroneous implicitly tagged sequences
048: {
049: this .postalAddress = ASN1Sequence.getInstance(o,
050: false);
051: }
052: if (postalAddress != null && postalAddress.size() > 6) {
053: throw new IllegalArgumentException(
054: "postal address must contain less than 6 strings");
055: }
056: break;
057: default:
058: throw new IllegalArgumentException("illegal tag");
059: }
060: }
061: }
062:
063: public SignerLocation(DERUTF8String countryName,
064: DERUTF8String localityName, ASN1Sequence postalAddress) {
065: if (postalAddress != null && postalAddress.size() > 6) {
066: throw new IllegalArgumentException(
067: "postal address must contain less than 6 strings");
068: }
069:
070: if (countryName != null) {
071: this .countryName = DERUTF8String.getInstance(countryName
072: .toASN1Object());
073: }
074:
075: if (localityName != null) {
076: this .localityName = DERUTF8String.getInstance(localityName
077: .toASN1Object());
078: }
079:
080: if (postalAddress != null) {
081: this .postalAddress = ASN1Sequence.getInstance(postalAddress
082: .toASN1Object());
083: }
084: }
085:
086: public static SignerLocation getInstance(Object obj) {
087: if (obj == null || obj instanceof SignerLocation) {
088: return (SignerLocation) obj;
089: }
090:
091: return new SignerLocation(ASN1Sequence.getInstance(obj));
092: }
093:
094: public DERUTF8String getCountryName() {
095: return countryName;
096: }
097:
098: public DERUTF8String getLocalityName() {
099: return localityName;
100: }
101:
102: public ASN1Sequence getPostalAddress() {
103: return postalAddress;
104: }
105:
106: /**
107: * <pre>
108: * SignerLocation ::= SEQUENCE {
109: * countryName [0] DirectoryString OPTIONAL,
110: * localityName [1] DirectoryString OPTIONAL,
111: * postalAddress [2] PostalAddress OPTIONAL }
112: *
113: * PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
114: *
115: * DirectoryString ::= CHOICE {
116: * teletexString TeletexString (SIZE (1..MAX)),
117: * printableString PrintableString (SIZE (1..MAX)),
118: * universalString UniversalString (SIZE (1..MAX)),
119: * utf8String UTF8String (SIZE (1.. MAX)),
120: * bmpString BMPString (SIZE (1..MAX)) }
121: * </pre>
122: */
123: public DERObject toASN1Object() {
124: ASN1EncodableVector v = new ASN1EncodableVector();
125:
126: if (countryName != null) {
127: v.add(new DERTaggedObject(true, 0, countryName));
128: }
129:
130: if (localityName != null) {
131: v.add(new DERTaggedObject(true, 1, localityName));
132: }
133:
134: if (postalAddress != null) {
135: v.add(new DERTaggedObject(true, 2, postalAddress));
136: }
137:
138: return new DERSequence(v);
139: }
140: }
|