Home | History | Annotate | Download | only in x509
      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 boolean getCa() {
     62         return ca;
     63     }
     64 
     65     public int getPathLenConstraint() {
     66         return pathLenConstraint;
     67     }
     68 
     69     /**
     70      * Returns the encoded form of the object.
     71      */
     72     public byte[] getEncoded() {
     73         if (encoding == null) {
     74             encoding = ASN1.encode(new Object[]{ca, BigInteger.valueOf(pathLenConstraint) });
     75         }
     76         return encoding;
     77     }
     78 
     79     public void dumpValue(StringBuilder sb, String prefix) {
     80         sb.append(prefix).append("BasicConstraints [\n").append(prefix)
     81             .append("  CA: ").append(ca)
     82             .append("\n  ").append(prefix).append("pathLenConstraint: ")
     83             .append(pathLenConstraint).append('\n').append(prefix)
     84             .append("]\n");
     85     }
     86 
     87     /**
     88      * ASN.1 Encoder/Decoder.
     89      */
     90     public static final ASN1Type ASN1 = new ASN1Sequence(new ASN1Type[] {
     91             ASN1Boolean.getInstance(), ASN1Integer.getInstance() }) {
     92         {
     93             setDefault(Boolean.FALSE, 0);
     94             setOptional(1);
     95         }
     96 
     97         public Object getDecodedObject(BerInputStream in) throws IOException {
     98             return in.content;
     99         }
    100 
    101         protected void getValues(Object object, Object[] values) {
    102             Object[] array = (Object[]) object;
    103             values[0] = array[0];
    104             values[1] = ((BigInteger) array[1]).toByteArray();
    105         }
    106     };
    107 }
    108