KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > BasicConstraints


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22
23 import java.math.BigInteger JavaDoc;
24
25 import com.maverick.crypto.asn1.ASN1EncodableVector;
26 import com.maverick.crypto.asn1.ASN1Sequence;
27 import com.maverick.crypto.asn1.ASN1TaggedObject;
28 import com.maverick.crypto.asn1.DERBoolean;
29 import com.maverick.crypto.asn1.ASN1Encodable;
30 import com.maverick.crypto.asn1.DERInteger;
31 import com.maverick.crypto.asn1.DERObject;
32 import com.maverick.crypto.asn1.DERSequence;
33
34 public class BasicConstraints
35     extends ASN1Encodable
36 {
37     DERBoolean cA = new DERBoolean(false);
38     DERInteger pathLenConstraint = null;
39
40     public static BasicConstraints getInstance(
41         ASN1TaggedObject obj,
42         boolean explicit)
43     {
44         return getInstance(ASN1Sequence.getInstance(obj, explicit));
45     }
46
47     public static BasicConstraints getInstance(
48         Object JavaDoc obj)
49     {
50         if (obj == null || obj instanceof BasicConstraints)
51         {
52             return (BasicConstraints)obj;
53         }
54         else if (obj instanceof ASN1Sequence)
55         {
56             return new BasicConstraints((ASN1Sequence)obj);
57         }
58
59         throw new IllegalArgumentException JavaDoc("unknown object in factory");
60     }
61
62     public BasicConstraints(
63         ASN1Sequence seq)
64     {
65         if (seq.size() == 0)
66         {
67             this.cA = null;
68             this.pathLenConstraint = null;
69         }
70         else
71         {
72             this.cA = (DERBoolean)seq.getObjectAt(0);
73             if (seq.size() > 1)
74             {
75                 this.pathLenConstraint = (DERInteger)seq.getObjectAt(1);
76             }
77         }
78     }
79
80     /**
81      * @deprecated use one of the other two unambigous constructors.
82      * @param cA
83      * @param pathLenConstraint
84      */

85     public BasicConstraints(
86         boolean cA,
87         int pathLenConstraint)
88     {
89         if (cA )
90         {
91             this.cA = new DERBoolean(cA);
92             this.pathLenConstraint = new DERInteger(pathLenConstraint);
93         }
94         else
95         {
96             this.cA = null;
97             this.pathLenConstraint = null;
98         }
99     }
100
101     public BasicConstraints(
102         boolean cA)
103     {
104         if (cA)
105         {
106             this.cA = new DERBoolean(true);
107         }
108         else
109         {
110             this.cA = null;
111         }
112         this.pathLenConstraint = null;
113     }
114
115     /**
116      * create a cA=true object for the given path length constraint.
117      *
118      * @param pathLenConstraint
119      */

120     public BasicConstraints(
121         int pathLenConstraint)
122     {
123         this.cA = new DERBoolean(true);
124         this.pathLenConstraint = new DERInteger(pathLenConstraint);
125     }
126
127     public boolean isCA()
128     {
129         return (cA != null) && cA.isTrue();
130     }
131
132     public BigInteger JavaDoc getPathLenConstraint()
133     {
134         if (pathLenConstraint != null)
135         {
136             return pathLenConstraint.getValue();
137         }
138
139         return null;
140     }
141
142     /**
143      * Produce an object suitable for an ASN1OutputStream.
144      * <pre>
145      * BasicConstraints := SEQUENCE {
146      * cA BOOLEAN DEFAULT FALSE,
147      * pathLenConstraint INTEGER (0..MAX) OPTIONAL
148      * }
149      * </pre>
150      */

151     public DERObject toASN1Object()
152     {
153         ASN1EncodableVector v = new ASN1EncodableVector();
154
155         if (cA != null)
156         {
157             v.add(cA);
158
159             if (pathLenConstraint != null)
160             {
161                 v.add(pathLenConstraint);
162             }
163         }
164
165         return new DERSequence(v);
166     }
167
168     public String JavaDoc toString()
169     {
170         if (pathLenConstraint == null)
171         {
172             if (cA == null)
173             {
174                 return "BasicConstraints: isCa(false)";
175             }
176             return "BasicConstraints: isCa(" + this.isCA() + ")";
177         }
178         return "BasicConstraints: isCa(" + this.isCA() + "), pathLenConstraint = " + pathLenConstraint.getValue();
179     }
180 }
181
Popular Tags