KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > BasicConstraints


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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

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

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

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