1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.pkcs;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Enumeration;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.BERSequence;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.BERTaggedObject;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DEREncodable;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERObject;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERObjectIdentifier;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERTaggedObject;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class ContentInfo
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    extends ASN1Encodable
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements PKCSObjectIdentifiers
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private DERObjectIdentifier contentType;
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private DEREncodable        content;
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static ContentInfo getInstance(
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof ContentInfo)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (ContentInfo)obj;
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else if (obj instanceof ASN1Sequence)
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new ContentInfo((ASN1Sequence)obj);
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
34c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ContentInfo(
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence  seq)
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Enumeration   e = seq.getObjects();
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        contentType = (DERObjectIdentifier)e.nextElement();
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (e.hasMoreElements())
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            content = ((DERTaggedObject)e.nextElement()).getObject();
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ContentInfo(
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DERObjectIdentifier contentType,
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable        content)
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.contentType = contentType;
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.content = content;
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERObjectIdentifier getContentType()
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return contentType;
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DEREncodable getContent()
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return content;
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * ContentInfo ::= SEQUENCE {
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *          contentType ContentType,
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *          content
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *          [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERObject toASN1Object()
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector  v = new ASN1EncodableVector();
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(contentType);
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (content != null)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(new BERTaggedObject(0, content));
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new BERSequence(v);
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
91