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
23package org.apache.harmony.security.x509;
24
25import org.apache.harmony.security.asn1.ASN1BitString;
26import org.apache.harmony.security.asn1.ASN1Sequence;
27import org.apache.harmony.security.asn1.ASN1Type;
28import org.apache.harmony.security.asn1.BerInputStream;
29import org.apache.harmony.security.asn1.BitString;
30import 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 */
48public final class CertificateList {
49    /** the value of tbsCertList field of the structure */
50    private final TBSCertList tbsCertList;
51    /** the value of signatureAlgorithm field of the structure */
52    private final AlgorithmIdentifier signatureAlgorithm;
53    /** the value of signatureValue field of the structure */
54    private final byte[] signatureValue;
55    /** the ASN.1 encoded form of CertList */
56    private byte[] encoding;
57
58    public CertificateList(TBSCertList tbsCertList,
59                       AlgorithmIdentifier signatureAlgorithm,
60                       byte[] signatureValue) {
61        this.tbsCertList = tbsCertList;
62        this.signatureAlgorithm = signatureAlgorithm;
63        this.signatureValue = new byte[signatureValue.length];
64        System.arraycopy(signatureValue, 0, this.signatureValue, 0,
65                                                    signatureValue.length);
66    }
67
68    private CertificateList(TBSCertList tbsCertList,
69                       AlgorithmIdentifier signatureAlgorithm,
70                       byte[] signatureValue, byte[] encoding) {
71        this(tbsCertList, signatureAlgorithm, signatureValue);
72        this.encoding = encoding;
73    }
74
75    /**
76     * Returns the value of tbsCertList field of the structure.
77     */
78    public TBSCertList getTbsCertList() {
79        return tbsCertList;
80    }
81
82    /**
83     * Returns the value of signatureValue field of the structure.
84     */
85    public byte[] getSignatureValue() {
86        byte[] result = new byte[signatureValue.length];
87        System.arraycopy(signatureValue, 0, result, 0, signatureValue.length);
88        return result;
89    }
90
91    @Override public String toString() {
92        StringBuilder result = new StringBuilder();
93        tbsCertList.dumpValue(result);
94        result.append("\nSignature Value:\n");
95        result.append(Array.toString(signatureValue, ""));
96        return result.toString();
97    }
98
99    /**
100     * Returns ASN.1 encoded form of this X.509 TBSCertList value.
101     */
102    public byte[] getEncoded() {
103        if (encoding == null) {
104            encoding = CertificateList.ASN1.encode(this);
105        }
106        return encoding;
107    }
108
109    /**
110     * X.509 CertList encoder/decoder.
111     */
112    public static final ASN1Sequence ASN1 =
113        new ASN1Sequence(new ASN1Type[]
114                {TBSCertList.ASN1, AlgorithmIdentifier.ASN1,
115                    ASN1BitString.getInstance()}) {
116
117        @Override protected Object getDecodedObject(BerInputStream in) {
118            Object[] values = (Object[]) in.content;
119            return new CertificateList(
120                    (TBSCertList) values[0],
121                    (AlgorithmIdentifier) values[1],
122                    ((BitString) values[2]).bytes, // FIXME keep as BitString object
123                    in.getEncoded()
124                    );
125        }
126
127        @Override protected void getValues(Object object, Object[] values) {
128            CertificateList certificateList = (CertificateList) object;
129            values[0] = certificateList.tbsCertList;
130            values[1] = certificateList.signatureAlgorithm;
131            values[2] = new BitString(certificateList.signatureValue, 0);
132        }
133    };
134}
135