1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.geronimo.util.asn1.x509; 20 21 import java.math.BigInteger; 22 23 import org.apache.geronimo.util.asn1.ASN1EncodableVector; 24 import org.apache.geronimo.util.asn1.ASN1Sequence; 25 import org.apache.geronimo.util.asn1.ASN1TaggedObject; 26 import org.apache.geronimo.util.asn1.DERBoolean; 27 import org.apache.geronimo.util.asn1.ASN1Encodable; 28 import org.apache.geronimo.util.asn1.DERInteger; 29 import org.apache.geronimo.util.asn1.DERObject; 30 import org.apache.geronimo.util.asn1.DERSequence; 31 32 public class BasicConstraints 33 extends ASN1Encodable 34 { 35 DERBoolean cA = new DERBoolean(false); 36 DERInteger pathLenConstraint = null; 37 38 public static BasicConstraints getInstance( 39 ASN1TaggedObject obj, 40 boolean explicit) 41 { 42 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 43 } 44 45 public static BasicConstraints getInstance( 46 Object obj) 47 { 48 if (obj == null || obj instanceof BasicConstraints) 49 { 50 return (BasicConstraints)obj; 51 } 52 else if (obj instanceof ASN1Sequence) 53 { 54 return new BasicConstraints((ASN1Sequence)obj); 55 } 56 57 throw new IllegalArgumentException("unknown object in factory"); 58 } 59 60 public BasicConstraints( 61 ASN1Sequence seq) 62 { 63 if (seq.size() == 0) 64 { 65 this.cA = null; 66 this.pathLenConstraint = null; 67 } 68 else 69 { 70 this.cA = (DERBoolean)seq.getObjectAt(0); 71 if (seq.size() > 1) 72 { 73 this.pathLenConstraint = (DERInteger)seq.getObjectAt(1); 74 } 75 } 76 } 77 78 /** 79 * @deprecated use one of the other two unambigous constructors. 80 * @param cA 81 * @param pathLenConstraint 82 */ 83 public BasicConstraints( 84 boolean cA, 85 int pathLenConstraint) 86 { 87 if (cA ) 88 { 89 this.cA = new DERBoolean(cA); 90 this.pathLenConstraint = new DERInteger(pathLenConstraint); 91 } 92 else 93 { 94 this.cA = null; 95 this.pathLenConstraint = null; 96 } 97 } 98 99 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 }