1package org.bouncycastle.asn1.pkcs;
2
3import java.util.Enumeration;
4
5import org.bouncycastle.asn1.ASN1Encodable;
6import org.bouncycastle.asn1.ASN1EncodableVector;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.ASN1TaggedObject;
12import org.bouncycastle.asn1.BERSequence;
13import org.bouncycastle.asn1.BERTaggedObject;
14import org.bouncycastle.asn1.DLSequence;
15
16public class ContentInfo
17    extends ASN1Object
18    implements PKCSObjectIdentifiers
19{
20    private ASN1ObjectIdentifier contentType;
21    private ASN1Encodable content;
22    private boolean       isBer = true;
23
24    public static ContentInfo getInstance(
25        Object  obj)
26    {
27        if (obj instanceof ContentInfo)
28        {
29            return (ContentInfo)obj;
30        }
31
32        if (obj != null)
33        {
34            return new ContentInfo(ASN1Sequence.getInstance(obj));
35        }
36
37        return null;
38    }
39
40    private ContentInfo(
41        ASN1Sequence  seq)
42    {
43        Enumeration   e = seq.getObjects();
44
45        contentType = (ASN1ObjectIdentifier)e.nextElement();
46
47        if (e.hasMoreElements())
48        {
49            content = ((ASN1TaggedObject)e.nextElement()).getObject();
50        }
51
52        isBer = seq instanceof BERSequence;
53    }
54
55    public ContentInfo(
56        ASN1ObjectIdentifier contentType,
57        ASN1Encodable content)
58    {
59        this.contentType = contentType;
60        this.content = content;
61    }
62
63    public ASN1ObjectIdentifier getContentType()
64    {
65        return contentType;
66    }
67
68    public ASN1Encodable getContent()
69    {
70        return content;
71    }
72
73    /**
74     * Produce an object suitable for an ASN1OutputStream.
75     * <pre>
76     * ContentInfo ::= SEQUENCE {
77     *          contentType ContentType,
78     *          content
79     *          [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
80     * </pre>
81     */
82    public ASN1Primitive toASN1Primitive()
83    {
84        ASN1EncodableVector v = new ASN1EncodableVector();
85
86        v.add(contentType);
87
88        if (content != null)
89        {
90            v.add(new BERTaggedObject(true, 0, content));
91        }
92
93        if (isBer)
94        {
95            return new BERSequence(v);
96        }
97        else
98        {
99            return new DLSequence(v);
100        }
101    }
102}
103