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: */package org.apache.geronimo.crypto.asn1.x509;
017:
018: import java.math.BigInteger;
019:
020: import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
021: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
022: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
023: import org.apache.geronimo.crypto.asn1.DERBoolean;
024: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
025: import org.apache.geronimo.crypto.asn1.DERInteger;
026: import org.apache.geronimo.crypto.asn1.DERObject;
027: import org.apache.geronimo.crypto.asn1.DERSequence;
028:
029: public class BasicConstraints extends ASN1Encodable {
030: DERBoolean cA = new DERBoolean(false);
031: DERInteger pathLenConstraint = null;
032:
033: public static BasicConstraints getInstance(ASN1TaggedObject obj,
034: boolean explicit) {
035: return getInstance(ASN1Sequence.getInstance(obj, explicit));
036: }
037:
038: public static BasicConstraints getInstance(Object obj) {
039: if (obj == null || obj instanceof BasicConstraints) {
040: return (BasicConstraints) obj;
041: } else if (obj instanceof ASN1Sequence) {
042: return new BasicConstraints((ASN1Sequence) obj);
043: }
044:
045: throw new IllegalArgumentException("unknown object in factory");
046: }
047:
048: public BasicConstraints(ASN1Sequence seq) {
049: if (seq.size() == 0) {
050: this .cA = null;
051: this .pathLenConstraint = null;
052: } else {
053: this .cA = (DERBoolean) seq.getObjectAt(0);
054: if (seq.size() > 1) {
055: this .pathLenConstraint = (DERInteger) seq
056: .getObjectAt(1);
057: }
058: }
059: }
060:
061: /**
062: * @deprecated use one of the other two unambigous constructors.
063: * @param cA
064: * @param pathLenConstraint
065: */
066: public BasicConstraints(boolean cA, int pathLenConstraint) {
067: if (cA) {
068: this .cA = new DERBoolean(cA);
069: this .pathLenConstraint = new DERInteger(pathLenConstraint);
070: } else {
071: this .cA = null;
072: this .pathLenConstraint = null;
073: }
074: }
075:
076: public BasicConstraints(boolean cA) {
077: if (cA) {
078: this .cA = new DERBoolean(true);
079: } else {
080: this .cA = null;
081: }
082: this .pathLenConstraint = null;
083: }
084:
085: /**
086: * create a cA=true object for the given path length constraint.
087: *
088: * @param pathLenConstraint
089: */
090: public BasicConstraints(int pathLenConstraint) {
091: this .cA = new DERBoolean(true);
092: this .pathLenConstraint = new DERInteger(pathLenConstraint);
093: }
094:
095: public boolean isCA() {
096: return (cA != null) && cA.isTrue();
097: }
098:
099: public BigInteger getPathLenConstraint() {
100: if (pathLenConstraint != null) {
101: return pathLenConstraint.getValue();
102: }
103:
104: return null;
105: }
106:
107: /**
108: * Produce an object suitable for an ASN1OutputStream.
109: * <pre>
110: * BasicConstraints := SEQUENCE {
111: * cA BOOLEAN DEFAULT FALSE,
112: * pathLenConstraint INTEGER (0..MAX) OPTIONAL
113: * }
114: * </pre>
115: */
116: public DERObject toASN1Object() {
117: ASN1EncodableVector v = new ASN1EncodableVector();
118:
119: if (cA != null) {
120: v.add(cA);
121:
122: if (pathLenConstraint != null) {
123: v.add(pathLenConstraint);
124: }
125: }
126:
127: return new DERSequence(v);
128: }
129:
130: public String toString() {
131: if (pathLenConstraint == null) {
132: if (cA == null) {
133: return "BasicConstraints: isCa(false)";
134: }
135: return "BasicConstraints: isCa(" + this .isCA() + ")";
136: }
137: return "BasicConstraints: isCa(" + this .isCA()
138: + "), pathLenConstraint = "
139: + pathLenConstraint.getValue();
140: }
141: }
|