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