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 /**
     19 * @author Alexander Y. Kleymenov
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.x509;
     24 
     25 import org.apache.harmony.security.asn1.ASN1BitString;
     26 import org.apache.harmony.security.asn1.ASN1Sequence;
     27 import org.apache.harmony.security.asn1.ASN1Type;
     28 import org.apache.harmony.security.asn1.BerInputStream;
     29 import org.apache.harmony.security.asn1.BitString;
     30 import org.apache.harmony.security.utils.Array;
     31 
     32 /**
     33  * The class encapsulates the ASN.1 DER encoding/decoding work
     34  * with the X.509 CRL. Its ASN notation is as follows
     35  * (as specified in RFC 3280 -
     36  *  Internet X.509 Public Key Infrastructure.
     37  *  Certificate and Certificate Revocation List (CRL) Profile.
     38  *  http://www.ietf.org/rfc/rfc3280.txt):
     39  *
     40  * <pre>
     41  *  CertificateList  ::=  SEQUENCE  {
     42  *       tbsCertList          TBSCertList,
     43  *       signatureAlgorithm   AlgorithmIdentifier,
     44  *       signatureValue       BIT STRING
     45  *  }
     46  * </pre>
     47  */
     48 public class CertificateList {
     49 
     50     // the value of tbsCertList field of the structure
     51     private final TBSCertList tbsCertList;
     52     // the value of signatureAlgorithm field of the structure
     53     private final AlgorithmIdentifier signatureAlgorithm;
     54     // the value of signatureValue field of the structure
     55     private final byte[] signatureValue;
     56     // the ASN.1 encoded form of CertList
     57     private byte[] encoding;
     58 
     59     /**
     60      * TODO
     61      * @param   tbsCertList: TBSCertList
     62      * @param   signatureAlgorithm: AlgorithmIdentifier
     63      * @param   signatureValue: byte[]
     64      */
     65     public CertificateList(TBSCertList tbsCertList,
     66                        AlgorithmIdentifier signatureAlgorithm,
     67                        byte[] signatureValue) {
     68         this.tbsCertList = tbsCertList;
     69         this.signatureAlgorithm = signatureAlgorithm;
     70         this.signatureValue = new byte[signatureValue.length];
     71         System.arraycopy(signatureValue, 0, this.signatureValue, 0,
     72                                                     signatureValue.length);
     73     }
     74 
     75     //
     76     // TODO
     77     // @param   tbsCertList: TBSCertList
     78     // @param   signatureAlgorithm: AlgorithmIdentifier
     79     // @param   signatureValue: byte[]
     80     // @param   encoding:   byte[]
     81     //
     82     private CertificateList(TBSCertList tbsCertList,
     83                        AlgorithmIdentifier signatureAlgorithm,
     84                        byte[] signatureValue, byte[] encoding) {
     85         this(tbsCertList, signatureAlgorithm, signatureValue);
     86         this.encoding = encoding;
     87     }
     88 
     89     /**
     90      * Returns the value of tbsCertList field of the structure.
     91      * @return  tbsCertList
     92      */
     93     public TBSCertList getTbsCertList() {
     94         return tbsCertList;
     95     }
     96 
     97     /**
     98      * Returns the value of signatureAlgorithm field of the structure.
     99      * @return  signatureAlgorithm
    100      */
    101     public AlgorithmIdentifier getSignatureAlgorithm() {
    102         return signatureAlgorithm;
    103     }
    104 
    105     /**
    106      * Returns the value of signatureValue field of the structure.
    107      * @return  signatureValue
    108      */
    109     public byte[] getSignatureValue() {
    110         byte[] result = new byte[signatureValue.length];
    111         System.arraycopy(signatureValue, 0, result, 0, signatureValue.length);
    112         return result;
    113     }
    114 
    115     public String toString() {
    116         StringBuffer res = new StringBuffer();
    117         tbsCertList.dumpValue(res);
    118         res.append("\nSignature Value:\n");
    119         res.append(Array.toString(signatureValue, ""));
    120         return res.toString();
    121     }
    122 
    123     /**
    124      * Returns ASN.1 encoded form of this X.509 TBSCertList value.
    125      * @return a byte array containing ASN.1 encode form.
    126      */
    127     public byte[] getEncoded() {
    128         if (encoding == null) {
    129             encoding = CertificateList.ASN1.encode(this);
    130         }
    131         return encoding;
    132     }
    133 
    134     /**
    135      * X.509 CertList encoder/decoder.
    136      */
    137     public static final ASN1Sequence ASN1 =
    138         new ASN1Sequence(new ASN1Type[]
    139                 {TBSCertList.ASN1, AlgorithmIdentifier.ASN1,
    140                     ASN1BitString.getInstance()}) {
    141 
    142         protected Object getDecodedObject(BerInputStream in) {
    143             Object[] values = (Object[]) in.content;
    144             return new CertificateList(
    145                     (TBSCertList) values[0],
    146                     (AlgorithmIdentifier) values[1],
    147                     ((BitString) values[2]).bytes, // FIXME keep as BitString object
    148                     in.getEncoded()
    149                     );
    150         }
    151 
    152         protected void getValues(Object object, Object[] values) {
    153 
    154             CertificateList certlist = (CertificateList) object;
    155 
    156             values[0] = certlist.tbsCertList;
    157             values[1] = certlist.signatureAlgorithm;
    158             values[2] = new BitString(certlist.signatureValue, 0);
    159         }
    160     };
    161 }
    162 
    163