SignedData.java revision fd6bb3510c2f94d636f3572dcf5f7f4dcd1a2726
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 Boris Kuznetsov
20* @version $Revision$
21*/
22package org.apache.harmony.security.pkcs7;
23
24import java.util.List;
25
26import org.apache.harmony.security.asn1.ASN1Any;
27import org.apache.harmony.security.asn1.ASN1Implicit;
28import org.apache.harmony.security.asn1.ASN1Integer;
29import org.apache.harmony.security.asn1.ASN1Sequence;
30import org.apache.harmony.security.asn1.ASN1SetOf;
31import org.apache.harmony.security.asn1.ASN1Type;
32import org.apache.harmony.security.asn1.BerInputStream;
33import org.apache.harmony.security.x509.AlgorithmIdentifier;
34import org.apache.harmony.security.x509.Certificate;
35import org.apache.harmony.security.x509.CertificateList;
36
37
38/**
39 * As defined in PKCS #7: Cryptographic Message Syntax Standard
40 * (http://www.ietf.org/rfc/rfc2315.txt)
41 *
42 * SignedData ::= SEQUENCE {
43 *   version Version,
44 *   digestAlgorithms DigestAlgorithmIdentifiers,
45 *   contentInfo ContentInfo,
46 *   certificates
47 *     [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
48 *   crls
49 *     [1] IMPLICIT CertificateRevocationLists OPTIONAL,
50 *   signerInfos SignerInfos }
51 *
52 */
53
54public class SignedData {
55
56    private int version;
57
58    private List digestAlgorithms;
59    private ContentInfo contentInfo;
60    private List certificates;
61    private List crls;
62    private List signerInfos;
63
64    public SignedData(int version, List digestAlgorithms, ContentInfo contentInfo,
65            List certificates, List crls, List signerInfos) {
66        this.version = version;
67        this.digestAlgorithms = digestAlgorithms;
68        this.contentInfo = contentInfo;
69        this.certificates = certificates;
70        this.crls = crls;
71        this.signerInfos = signerInfos;
72    }
73
74    public List getCertificates() {
75        return certificates;
76    }
77
78    public List getCRLs() {
79        return crls;
80    }
81
82    public List getSignerInfos() {
83        return signerInfos;
84    }
85
86    /**
87     * @return Returns the contentInfo.
88     */
89    public ContentInfo getContentInfo() {
90        return contentInfo;
91    }
92
93    /**
94     * @return Returns the digestAlgorithms.
95     */
96    public List getDigestAlgorithms() {
97        return digestAlgorithms;
98    }
99
100    /**
101     * @return Returns the version.
102     */
103    public int getVersion() {
104        return version;
105    }
106
107    public String toString() {
108        StringBuilder res = new StringBuilder();
109        res.append("---- SignedData:");
110        res.append("\nversion: ");
111        res.append(version);
112        res.append("\ndigestAlgorithms: ");
113        res.append(digestAlgorithms.toString());
114        res.append("\ncontentInfo: ");
115        res.append(contentInfo.toString());
116        res.append("\ncertificates: ");
117        if (certificates != null) {
118            res.append(certificates.toString());
119        }
120        res.append("\ncrls: ");
121        if (crls != null) {
122            res.append(crls.toString());
123        }
124        res.append("\nsignerInfos:\n");
125        res.append(signerInfos.toString());
126        res.append("\n---- SignedData End\n]");
127        return res.toString();
128    }
129
130    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
131            ASN1Integer.getInstance(),
132            new ASN1SetOf(AlgorithmIdentifier.ASN1),
133            ContentInfo.ASN1,
134            new ASN1Implicit(0, new ASN1SetOf(Certificate.ASN1)),
135            new ASN1Implicit(1, new ASN1SetOf(CertificateList.ASN1)),
136            new ASN1SetOf(SignerInfo.ASN1)
137            }) {
138        {
139            setOptional(3); // certificates is optional
140            setOptional(4); // crls is optional
141        }
142
143        protected void getValues(Object object, Object[] values) {
144            SignedData sd = (SignedData) object;
145            values[0] = new byte[] {(byte)sd.version};
146            values[1] = sd.digestAlgorithms;
147            values[2] = sd.contentInfo;
148            values[3] = sd.certificates;
149            values[4] = sd.crls;
150            values[5] = sd.signerInfos;
151        }
152
153        protected Object getDecodedObject(BerInputStream in) {
154            Object[] values = (Object[]) in.content;
155            return new SignedData(
156                        ASN1Integer.toIntValue(values[0]),
157                        (List) values[1],
158                        (ContentInfo) values[2],
159                        (List) values[3],
160                        (List) values[4],
161                        (List) values[5]
162                    );
163        }
164    };
165
166}
167