BasicConstraints.java revision c37f4a04ef89e73a39a59f3c5a179af8c8ab5974
1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.asn1.ASN1Encodable;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERBoolean;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERInteger;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERObject;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.math.BigInteger;
13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class BasicConstraints
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    extends ASN1Encodable
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    // BEGIN android-changed
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DERBoolean  cA = DERBoolean.FALSE;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    // END android-changed
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DERInteger  pathLenConstraint = null;
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static BasicConstraints getInstance(
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(ASN1Sequence.getInstance(obj, explicit));
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static BasicConstraints getInstance(
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof BasicConstraints)
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (BasicConstraints)obj;
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
36c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
37c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (obj instanceof ASN1Sequence)
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new BasicConstraints((ASN1Sequence)obj);
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
42c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (obj instanceof X509Extension)
43c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
44c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return getInstance(X509Extension.convertValueToObject((X509Extension)obj));
45c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
46c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
47c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BasicConstraints(
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence   seq)
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() == 0)
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.cA = null;
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.pathLenConstraint = null;
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
60c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (seq.getObjectAt(0) instanceof DERBoolean)
61c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
62c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                this.cA = DERBoolean.getInstance(seq.getObjectAt(0));
63c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                this.cA = null;
67c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                this.pathLenConstraint = DERInteger.getInstance(seq.getObjectAt(0));
68c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (seq.size() > 1)
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (this.cA != null)
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    this.pathLenConstraint = DERInteger.getInstance(seq.getObjectAt(1));
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
76c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    throw new IllegalArgumentException("wrong sequence in constructor");
78c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @deprecated use one of the other two unambigous constructors.
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param cA
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param pathLenConstraint
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BasicConstraints(
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean cA,
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int     pathLenConstraint)
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (cA)
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // BEGIN android-changed
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.cA = DERBoolean.getInstance(cA);
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // END android-changed
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.pathLenConstraint = new DERInteger(pathLenConstraint);
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.cA = null;
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.pathLenConstraint = null;
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BasicConstraints(
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean cA)
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (cA)
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // BEGIN android-changed
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.cA = DERBoolean.TRUE;
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // END android-changed
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.cA = null;
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.pathLenConstraint = null;
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * create a cA=true object for the given path length constraint.
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param pathLenConstraint
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BasicConstraints(
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int     pathLenConstraint)
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // BEGIN android-changed
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.cA = DERBoolean.TRUE;
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // END android-changed
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.pathLenConstraint = new DERInteger(pathLenConstraint);
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public boolean isCA()
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return (cA != null) && cA.isTrue();
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getPathLenConstraint()
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (pathLenConstraint != null)
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return pathLenConstraint.getValue();
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return null;
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
154b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * BasicConstraints := SEQUENCE {
155b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *    cA                  BOOLEAN DEFAULT FALSE,
156b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *    pathLenConstraint   INTEGER (0..MAX) OPTIONAL
157b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * }
158b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
159b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
160b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERObject toASN1Object()
161b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
162b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector  v = new ASN1EncodableVector();
163b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
164b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (cA != null)
165b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
166b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(cA);
167c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
168c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
169c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (pathLenConstraint != null)  // yes some people actually do this when cA is false...
170c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
171c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            v.add(pathLenConstraint);
172b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
174b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
175b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
176b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
177b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String toString()
178b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
179b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (pathLenConstraint == null)
180b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
181b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (cA == null)
182b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
183b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return "BasicConstraints: isCa(false)";
184b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
185b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return "BasicConstraints: isCa(" + this.isCA() + ")";
186b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
187b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return "BasicConstraints: isCa(" + this.isCA() + "), pathLenConstraint = " + pathLenConstraint.getValue();
188b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
189b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
190