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.harmony.security.x509; 19 20 import java.io.IOException; 21 import java.math.BigInteger; 22 import org.apache.harmony.security.asn1.ASN1Boolean; 23 import org.apache.harmony.security.asn1.ASN1Integer; 24 import org.apache.harmony.security.asn1.ASN1Sequence; 25 import org.apache.harmony.security.asn1.ASN1Type; 26 import org.apache.harmony.security.asn1.BerInputStream; 27 28 /** 29 * Basic Constraints Extension (OID == 2.5.29.19). 30 * 31 * The ASN.1 definition for Basic Constraints Extension is: 32 * 33 * <pre> 34 * id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } 35 * 36 * BasicConstraints ::= SEQUENCE { 37 * ca BOOLEAN DEFAULT FALSE, 38 * pathLenConstraint INTEGER (0..MAX) OPTIONAL 39 * } 40 * </pre> 41 * (as specified in RFC 3280) 42 */ 43 public final class BasicConstraints extends ExtensionValue { 44 /** is CA */ 45 private boolean ca = false; 46 /** path len constraint */ 47 private int pathLenConstraint = Integer.MAX_VALUE; 48 49 /** 50 * Creates the extension object on the base of its encoded form. 51 */ 52 public BasicConstraints(byte[] encoding) throws IOException { 53 super(encoding); 54 Object[] values = (Object[]) ASN1.decode(encoding); 55 ca = (Boolean) values[0]; 56 if (values[1] != null) { 57 pathLenConstraint = new BigInteger((byte[]) values[1]).intValue(); 58 } 59 } 60 61 public int getPathLenConstraint() { 62 return pathLenConstraint; 63 } 64 65 /** 66 * Returns the encoded form of the object. 67 */ 68 public byte[] getEncoded() { 69 if (encoding == null) { 70 encoding = ASN1.encode(new Object[]{ca, BigInteger.valueOf(pathLenConstraint) }); 71 } 72 return encoding; 73 } 74 75 public void dumpValue(StringBuilder sb, String prefix) { 76 sb.append(prefix).append("BasicConstraints [\n").append(prefix) 77 .append(" CA: ").append(ca) 78 .append("\n ").append(prefix).append("pathLenConstraint: ") 79 .append(pathLenConstraint).append('\n').append(prefix) 80 .append("]\n"); 81 } 82 83 /** 84 * ASN.1 Encoder/Decoder. 85 */ 86 public static final ASN1Type ASN1 = new ASN1Sequence(new ASN1Type[] { 87 ASN1Boolean.getInstance(), ASN1Integer.getInstance() }) { 88 { 89 setDefault(Boolean.FALSE, 0); 90 setOptional(1); 91 } 92 93 public Object getDecodedObject(BerInputStream in) throws IOException { 94 return in.content; 95 } 96 97 protected void getValues(Object object, Object[] values) { 98 Object[] array = (Object[]) object; 99 values[0] = array[0]; 100 values[1] = ((BigInteger) array[1]).toByteArray(); 101 } 102 }; 103 } 104