001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import java.math.BigInteger;
021    
022    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
023    import org.apache.geronimo.util.asn1.ASN1Sequence;
024    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
025    import org.apache.geronimo.util.asn1.DERBoolean;
026    import org.apache.geronimo.util.asn1.ASN1Encodable;
027    import org.apache.geronimo.util.asn1.DERInteger;
028    import org.apache.geronimo.util.asn1.DERObject;
029    import org.apache.geronimo.util.asn1.DERSequence;
030    
031    public class BasicConstraints
032        extends ASN1Encodable
033    {
034        DERBoolean  cA = new DERBoolean(false);
035        DERInteger  pathLenConstraint = null;
036    
037        public static BasicConstraints getInstance(
038            ASN1TaggedObject obj,
039            boolean          explicit)
040        {
041            return getInstance(ASN1Sequence.getInstance(obj, explicit));
042        }
043    
044        public static BasicConstraints getInstance(
045            Object  obj)
046        {
047            if (obj == null || obj instanceof BasicConstraints)
048            {
049                return (BasicConstraints)obj;
050            }
051            else if (obj instanceof ASN1Sequence)
052            {
053                return new BasicConstraints((ASN1Sequence)obj);
054            }
055    
056            throw new IllegalArgumentException("unknown object in factory");
057        }
058    
059        public BasicConstraints(
060            ASN1Sequence   seq)
061        {
062            if (seq.size() == 0)
063            {
064                this.cA = null;
065                this.pathLenConstraint = null;
066            }
067            else
068            {
069                this.cA = (DERBoolean)seq.getObjectAt(0);
070                if (seq.size() > 1)
071                {
072                    this.pathLenConstraint = (DERInteger)seq.getObjectAt(1);
073                }
074            }
075        }
076    
077        /**
078         * @deprecated use one of the other two unambigous constructors.
079         * @param cA
080         * @param pathLenConstraint
081         */
082        public BasicConstraints(
083            boolean cA,
084            int     pathLenConstraint)
085        {
086            if (cA )
087            {
088                this.cA = new DERBoolean(cA);
089                this.pathLenConstraint = new DERInteger(pathLenConstraint);
090            }
091            else
092            {
093                this.cA = null;
094                this.pathLenConstraint = null;
095            }
096        }
097    
098        public BasicConstraints(
099            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 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 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    }