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