SignedData.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.asn1.pkcs;
2
3import java.util.Enumeration;
4
5import org.bouncycastle.asn1.ASN1EncodableVector;
6import org.bouncycastle.asn1.ASN1Integer;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1Primitive;
9import org.bouncycastle.asn1.ASN1Sequence;
10import org.bouncycastle.asn1.ASN1Set;
11import org.bouncycastle.asn1.ASN1TaggedObject;
12import org.bouncycastle.asn1.BERSequence;
13import org.bouncycastle.asn1.DERTaggedObject;
14
15/**
16 * a PKCS#7 signed data object.
17 */
18public class SignedData
19    extends ASN1Object
20    implements PKCSObjectIdentifiers
21{
22    private ASN1Integer              version;
23    private ASN1Set                 digestAlgorithms;
24    private ContentInfo             contentInfo;
25    private ASN1Set                 certificates;
26    private ASN1Set                 crls;
27    private ASN1Set                 signerInfos;
28
29    public static SignedData getInstance(
30        Object  o)
31    {
32        if (o instanceof SignedData)
33        {
34            return (SignedData)o;
35        }
36        else if (o != null)
37        {
38            return new SignedData(ASN1Sequence.getInstance(o));
39        }
40
41        return null;
42    }
43
44    public SignedData(
45        ASN1Integer        _version,
46        ASN1Set           _digestAlgorithms,
47        ContentInfo       _contentInfo,
48        ASN1Set           _certificates,
49        ASN1Set           _crls,
50        ASN1Set           _signerInfos)
51    {
52        version          = _version;
53        digestAlgorithms = _digestAlgorithms;
54        contentInfo      = _contentInfo;
55        certificates     = _certificates;
56        crls             = _crls;
57        signerInfos      = _signerInfos;
58    }
59
60    public SignedData(
61        ASN1Sequence seq)
62    {
63        Enumeration     e = seq.getObjects();
64
65        version = (ASN1Integer)e.nextElement();
66        digestAlgorithms = ((ASN1Set)e.nextElement());
67        contentInfo = ContentInfo.getInstance(e.nextElement());
68
69        while (e.hasMoreElements())
70        {
71            ASN1Primitive o = (ASN1Primitive)e.nextElement();
72
73            //
74            // an interesting feature of SignedData is that there appear to be varying implementations...
75            // for the moment we ignore anything which doesn't fit.
76            //
77            if (o instanceof ASN1TaggedObject)
78            {
79                ASN1TaggedObject tagged = (ASN1TaggedObject)o;
80
81                switch (tagged.getTagNo())
82                {
83                case 0:
84                    certificates = ASN1Set.getInstance(tagged, false);
85                    break;
86                case 1:
87                    crls = ASN1Set.getInstance(tagged, false);
88                    break;
89                default:
90                    throw new IllegalArgumentException("unknown tag value " + tagged.getTagNo());
91                }
92            }
93            else
94            {
95                signerInfos = (ASN1Set)o;
96            }
97        }
98    }
99
100    public ASN1Integer getVersion()
101    {
102        return version;
103    }
104
105    public ASN1Set getDigestAlgorithms()
106    {
107        return digestAlgorithms;
108    }
109
110    public ContentInfo getContentInfo()
111    {
112        return contentInfo;
113    }
114
115    public ASN1Set getCertificates()
116    {
117        return certificates;
118    }
119
120    public ASN1Set getCRLs()
121    {
122        return crls;
123    }
124
125    public ASN1Set getSignerInfos()
126    {
127        return signerInfos;
128    }
129
130    /**
131     * Produce an object suitable for an ASN1OutputStream.
132     * <pre>
133     *  SignedData ::= SEQUENCE {
134     *      version Version,
135     *      digestAlgorithms DigestAlgorithmIdentifiers,
136     *      contentInfo ContentInfo,
137     *      certificates
138     *          [0] IMPLICIT ExtendedCertificatesAndCertificates
139     *                   OPTIONAL,
140     *      crls
141     *          [1] IMPLICIT CertificateRevocationLists OPTIONAL,
142     *      signerInfos SignerInfos }
143     * </pre>
144     */
145    public ASN1Primitive toASN1Primitive()
146    {
147        ASN1EncodableVector v = new ASN1EncodableVector();
148
149        v.add(version);
150        v.add(digestAlgorithms);
151        v.add(contentInfo);
152
153        if (certificates != null)
154        {
155            v.add(new DERTaggedObject(false, 0, certificates));
156        }
157
158        if (crls != null)
159        {
160            v.add(new DERTaggedObject(false, 1, crls));
161        }
162
163        v.add(signerInfos);
164
165        return new BERSequence(v);
166    }
167}
168