1    package org.bouncycastle.asn1.x509;
2    
3    import java.math.BigInteger;
4    
5    import org.bouncycastle.asn1.*;
6    
7    public class BasicConstraints
8        implements DEREncodable
9    {
10       DERBoolean  cA = new DERBoolean(false);
11       DERInteger  pathLenConstraint = null;
12   
13       public static BasicConstraints getInstance(
14           ASN1TaggedObject obj,
15           boolean          explicit)
16       {
17           return getInstance(ASN1Sequence.getInstance(obj, explicit));
18       }
19   
20       public static BasicConstraints getInstance(
21           Object  obj)
22       {
23           if (obj instanceof BasicConstraints)
24           {
25               return (BasicConstraints)obj;
26           }
27           else if (obj instanceof ASN1Sequence)
28           {
29               return new BasicConstraints((ASN1Sequence)obj);
30           }
31   
32           throw new IllegalArgumentException("unknown object in factory");
33       }
34           
35       public BasicConstraints(
36           ASN1Sequence   seq)
37       {
38           if (seq.size() != 0)
39           {
40               this.cA = (DERBoolean)seq.getObjectAt(0);
41               this.pathLenConstraint = (DERInteger)seq.getObjectAt(1);
42           }
43       }
44   
45       public BasicConstraints(
46           boolean cA,
47           int     pathLenConstraint)
48       {
49           this.cA = new DERBoolean(cA);
50           this.pathLenConstraint = new DERInteger(pathLenConstraint);
51       }
52   
53       public BasicConstraints(
54           boolean cA)
55       {
56           this.cA = new DERBoolean(cA);
57           this.pathLenConstraint = null;
58       }
59   
60       public boolean isCA()
61       {
62           return cA.isTrue();
63       }
64   
65       public BigInteger getPathLenConstraint()
66       {
67           if (pathLenConstraint != null)
68           {
69               return pathLenConstraint.getValue();
70           }
71   
72           return null;
73       }
74   
75       /**
76        * <pre>
77        * BasicConstraints := SEQUENCE {
78        *    cA                  BOOLEAN DEFAULT FALSE,
79        *    pathLenConstraint   INTEGER (0..MAX) OPTIONAL
80        * }
81        * </pre>
82        */
83       public DERObject getDERObject()
84       {
85           DERConstructedSequence  seq = new DERConstructedSequence();
86   
87           seq.addObject(cA);
88   
89           if (pathLenConstraint != null)
90           {
91               seq.addObject(pathLenConstraint);
92           }
93   
94           return seq;
95       }
96   
97       public String toString()
98       {
99           return "BasicConstraints: isCa(" + this.isCA() + "), pathLenConstraint = " + pathLenConstraint.getValue();
100      }
101  }
102