001: package org.bouncycastle.asn1.isismtt.x509;
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.ASN1TaggedObject;
008: import org.bouncycastle.asn1.DERBoolean;
009: import org.bouncycastle.asn1.DERGeneralizedTime;
010: import org.bouncycastle.asn1.DERInteger;
011: import org.bouncycastle.asn1.DERObject;
012: import org.bouncycastle.asn1.DERPrintableString;
013: import org.bouncycastle.asn1.DERSequence;
014: import org.bouncycastle.asn1.DERTaggedObject;
015:
016: /**
017: * A declaration of majority.
018: * <p/>
019: * <pre>
020: * DeclarationOfMajoritySyntax ::= CHOICE
021: * {
022: * notYoungerThan [0] IMPLICIT INTEGER,
023: * fullAgeAtCountry [1] IMPLICIT SEQUENCE
024: * {
025: * fullAge BOOLEAN DEFAULT TRUE,
026: * country PrintableString (SIZE(2))
027: * }
028: * dateOfBirth [2] IMPLICIT GeneralizedTime
029: * }
030: * </pre>
031: * <p/>
032: * fullAgeAtCountry indicates the majority of the owner with respect to the laws
033: * of a specific country.
034: */
035: public class DeclarationOfMajority extends ASN1Encodable implements
036: ASN1Choice {
037: public static final int notYoungerThan = 0;
038: public static final int fullAgeAtCountry = 1;
039: public static final int dateOfBirth = 2;
040:
041: private ASN1TaggedObject declaration;
042:
043: public DeclarationOfMajority(int notYoungerThan) {
044: declaration = new DERTaggedObject(false, 0, new DERInteger(
045: notYoungerThan));
046: }
047:
048: public DeclarationOfMajority(boolean fullAge, String country) {
049: if (country.length() > 2) {
050: throw new IllegalArgumentException(
051: "country can only be 2 characters");
052: }
053:
054: if (fullAge) {
055: declaration = new DERTaggedObject(false, 1,
056: new DERSequence(new DERPrintableString(country,
057: true)));
058: } else {
059: ASN1EncodableVector v = new ASN1EncodableVector();
060:
061: v.add(DERBoolean.FALSE);
062: v.add(new DERPrintableString(country, true));
063:
064: declaration = new DERTaggedObject(false, 1,
065: new DERSequence(v));
066: }
067: }
068:
069: public DeclarationOfMajority(DERGeneralizedTime dateOfBirth) {
070: declaration = new DERTaggedObject(false, 2, dateOfBirth);
071: }
072:
073: public static DeclarationOfMajority getInstance(Object obj) {
074: if (obj == null || obj instanceof DeclarationOfMajority) {
075: return (DeclarationOfMajority) obj;
076: }
077:
078: if (obj instanceof ASN1TaggedObject) {
079: return new DeclarationOfMajority((ASN1TaggedObject) obj);
080: }
081:
082: throw new IllegalArgumentException(
083: "illegal object in getInstance: "
084: + obj.getClass().getName());
085: }
086:
087: private DeclarationOfMajority(ASN1TaggedObject o) {
088: if (o.getTagNo() > 2) {
089: throw new IllegalArgumentException("Bad tag number: "
090: + o.getTagNo());
091: }
092: declaration = o;
093: }
094:
095: /**
096: * Produce an object suitable for an ASN1OutputStream.
097: * <p/>
098: * Returns:
099: * <p/>
100: * <pre>
101: * DeclarationOfMajoritySyntax ::= CHOICE
102: * {
103: * notYoungerThan [0] IMPLICIT INTEGER,
104: * fullAgeAtCountry [1] IMPLICIT SEQUENCE
105: * {
106: * fullAge BOOLEAN DEFAULT TRUE,
107: * country PrintableString (SIZE(2))
108: * }
109: * dateOfBirth [2] IMPLICIT GeneralizedTime
110: * }
111: * </pre>
112: *
113: * @return a DERObject
114: */
115: public DERObject toASN1Object() {
116: return declaration;
117: }
118:
119: public int getType() {
120: return declaration.getTagNo();
121: }
122:
123: /**
124: * @return notYoungerThan if that's what we are, -1 otherwise
125: */
126: public int notYoungerThan() {
127: if (declaration.getTagNo() != 0) {
128: return -1;
129: }
130:
131: return DERInteger.getInstance(declaration, false).getValue()
132: .intValue();
133: }
134:
135: public ASN1Sequence fullAgeAtCountry() {
136: if (declaration.getTagNo() != 1) {
137: return null;
138: }
139:
140: return ASN1Sequence.getInstance(declaration, false);
141: }
142:
143: public DERGeneralizedTime getDateOfBirth() {
144: if (declaration.getTagNo() != 2) {
145: return null;
146: }
147:
148: return DERGeneralizedTime.getInstance(declaration, false);
149: }
150: }
|