001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1.x509;
020    
021    import java.math.BigInteger;
022    
023    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
024    import org.apache.geronimo.util.asn1.ASN1Sequence;
025    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
026    import org.apache.geronimo.util.asn1.DERBoolean;
027    import org.apache.geronimo.util.asn1.ASN1Encodable;
028    import org.apache.geronimo.util.asn1.DERInteger;
029    import org.apache.geronimo.util.asn1.DERObject;
030    import org.apache.geronimo.util.asn1.DERSequence;
031    
032    public class BasicConstraints
033        extends ASN1Encodable
034    {
035        DERBoolean  cA = new DERBoolean(false);
036        DERInteger  pathLenConstraint = null;
037    
038        public static BasicConstraints getInstance(
039            ASN1TaggedObject obj,
040            boolean          explicit)
041        {
042            return getInstance(ASN1Sequence.getInstance(obj, explicit));
043        }
044    
045        public static BasicConstraints getInstance(
046            Object  obj)
047        {
048            if (obj == null || obj instanceof BasicConstraints)
049            {
050                return (BasicConstraints)obj;
051            }
052            else if (obj instanceof ASN1Sequence)
053            {
054                return new BasicConstraints((ASN1Sequence)obj);
055            }
056    
057            throw new IllegalArgumentException("unknown object in factory");
058        }
059    
060        public BasicConstraints(
061            ASN1Sequence   seq)
062        {
063            if (seq.size() == 0)
064            {
065                this.cA = null;
066                this.pathLenConstraint = null;
067            }
068            else
069            {
070                this.cA = (DERBoolean)seq.getObjectAt(0);
071                if (seq.size() > 1)
072                {
073                    this.pathLenConstraint = (DERInteger)seq.getObjectAt(1);
074                }
075            }
076        }
077    
078        /**
079         * @deprecated use one of the other two unambigous constructors.
080         * @param cA
081         * @param pathLenConstraint
082         */
083        public BasicConstraints(
084            boolean cA,
085            int     pathLenConstraint)
086        {
087            if (cA )
088            {
089                this.cA = new DERBoolean(cA);
090                this.pathLenConstraint = new DERInteger(pathLenConstraint);
091            }
092            else
093            {
094                this.cA = null;
095                this.pathLenConstraint = null;
096            }
097        }
098    
099        public BasicConstraints(
100            boolean cA)
101        {
102            if (cA)
103            {
104                this.cA = new DERBoolean(true);
105            }
106            else
107            {
108                this.cA = null;
109            }
110            this.pathLenConstraint = null;
111        }
112    
113        /**
114         * create a cA=true object for the given path length constraint.
115         *
116         * @param pathLenConstraint
117         */
118        public BasicConstraints(
119            int     pathLenConstraint)
120        {
121            this.cA = new DERBoolean(true);
122            this.pathLenConstraint = new DERInteger(pathLenConstraint);
123        }
124    
125        public boolean isCA()
126        {
127            return (cA != null) && cA.isTrue();
128        }
129    
130        public BigInteger getPathLenConstraint()
131        {
132            if (pathLenConstraint != null)
133            {
134                return pathLenConstraint.getValue();
135            }
136    
137            return null;
138        }
139    
140        /**
141         * Produce an object suitable for an ASN1OutputStream.
142         * <pre>
143         * BasicConstraints := SEQUENCE {
144         *    cA                  BOOLEAN DEFAULT FALSE,
145         *    pathLenConstraint   INTEGER (0..MAX) OPTIONAL
146         * }
147         * </pre>
148         */
149        public DERObject toASN1Object()
150        {
151            ASN1EncodableVector  v = new ASN1EncodableVector();
152    
153            if (cA != null)
154            {
155                v.add(cA);
156    
157                if (pathLenConstraint != null)
158                {
159                    v.add(pathLenConstraint);
160                }
161            }
162    
163            return new DERSequence(v);
164        }
165    
166        public String toString()
167        {
168            if (pathLenConstraint == null)
169            {
170                if (cA == null)
171                {
172                    return "BasicConstraints: isCa(false)";
173                }
174                return "BasicConstraints: isCa(" + this.isCA() + ")";
175            }
176            return "BasicConstraints: isCa(" + this.isCA() + "), pathLenConstraint = " + pathLenConstraint.getValue();
177        }
178    }