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 certificate. 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  *  Certificate  ::=  SEQUENCE  {
     42  *      tbsCertificate       TBSCertificate,
     43  *      signatureAlgorithm   AlgorithmIdentifier,
     44  *      signatureValue       BIT STRING
     45  *  }
     46  * </pre>
     47  */
     48 public class Certificate {
     49 
     50     // the value of tbsCertificate field of the structure
     51     private final TBSCertificate tbsCertificate;
     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 Certificate
     57     private byte[] encoding;
     58 
     59     /**
     60      * TODO
     61      * @param   tbsCertificate: TBSCertificate
     62      * @param   signatureAlgorithm: AlgorithmIdentifier
     63      * @param   signatureValue: byte[]
     64      */
     65     public Certificate(TBSCertificate tbsCertificate,
     66                        AlgorithmIdentifier signatureAlgorithm,
     67                        byte[] signatureValue) {
     68         this.tbsCertificate = tbsCertificate;
     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   tbsCertificate: TBSCertificate
     78     // @param   signatureAlgorithm: AlgorithmIdentifier
     79     // @param   signatureValue: byte[]
     80     // @param   encoding:   byte[]
     81     //
     82     private Certificate(TBSCertificate tbsCertificate,
     83                        AlgorithmIdentifier signatureAlgorithm,
     84                        byte[] signatureValue, byte[] encoding) {
     85         this(tbsCertificate, signatureAlgorithm, signatureValue);
     86         this.encoding = encoding;
     87     }
     88 
     89     /**
     90      * Returns the value of tbsCertificate field of the structure.
     91      * @return  tbsCertificate
     92      */
     93     public TBSCertificate getTbsCertificate() {
     94         return tbsCertificate;
     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 buffer = new StringBuffer();
    117         buffer.append("X.509 Certificate:\n[\n");
    118         tbsCertificate.dumpValue(buffer);
    119         buffer.append("\n  Algorithm: [");
    120         signatureAlgorithm.dumpValue(buffer);
    121         buffer.append(']');
    122         buffer.append("\n  Signature Value:\n");
    123         buffer.append(Array.toString(signatureValue, ""));
    124         buffer.append(']');
    125         return buffer.toString();
    126     }
    127 
    128     /**
    129      * Returns ASN.1 encoded form of this X.509 TBSCertificate value.
    130      * @return a byte array containing ASN.1 encode form.
    131      */
    132     public byte[] getEncoded() {
    133         if (encoding == null) {
    134             encoding = Certificate.ASN1.encode(this);
    135         }
    136         return encoding;
    137     }
    138 
    139     /**
    140      * X.509 Certificate encoder/decoder.
    141      */
    142     public static final ASN1Sequence ASN1 =
    143         new ASN1Sequence(new ASN1Type[]
    144                 {TBSCertificate.ASN1, AlgorithmIdentifier.ASN1, ASN1BitString.getInstance()}) {
    145 
    146         protected Object getDecodedObject(BerInputStream in) {
    147             Object[] values = (Object[]) in.content;
    148             return new Certificate(
    149                     (TBSCertificate) values[0],
    150                     (AlgorithmIdentifier) values[1],
    151                     ((BitString) values[2]).bytes, // FIXME keep as BitString object
    152                     in.getEncoded()
    153                     );
    154         }
    155 
    156         protected void getValues(Object object, Object[] values) {
    157 
    158             Certificate cert = (Certificate) object;
    159 
    160             values[0] = cert.tbsCertificate;
    161             values[1] = cert.signatureAlgorithm;
    162             values[2] = new BitString(cert.signatureValue, 0);
    163         }
    164     };
    165 }
    166 
    167