01: package org.bouncycastle.asn1.pkcs;
02:
03: import java.util.Enumeration;
04:
05: import org.bouncycastle.asn1.ASN1Encodable;
06: import org.bouncycastle.asn1.ASN1EncodableVector;
07: import org.bouncycastle.asn1.ASN1Sequence;
08: import org.bouncycastle.asn1.BERSequence;
09: import org.bouncycastle.asn1.BERTaggedObject;
10: import org.bouncycastle.asn1.DEREncodable;
11: import org.bouncycastle.asn1.DERObject;
12: import org.bouncycastle.asn1.DERObjectIdentifier;
13: import org.bouncycastle.asn1.DERTaggedObject;
14:
15: public class ContentInfo extends ASN1Encodable implements
16: PKCSObjectIdentifiers {
17: private DERObjectIdentifier contentType;
18: private DEREncodable content;
19:
20: public static ContentInfo getInstance(Object obj) {
21: if (obj instanceof ContentInfo) {
22: return (ContentInfo) obj;
23: } else if (obj instanceof ASN1Sequence) {
24: return new ContentInfo((ASN1Sequence) obj);
25: }
26:
27: throw new IllegalArgumentException("unknown object in factory");
28: }
29:
30: public ContentInfo(ASN1Sequence seq) {
31: Enumeration e = seq.getObjects();
32:
33: contentType = (DERObjectIdentifier) e.nextElement();
34:
35: if (e.hasMoreElements()) {
36: content = ((DERTaggedObject) e.nextElement()).getObject();
37: }
38: }
39:
40: public ContentInfo(DERObjectIdentifier contentType,
41: DEREncodable content) {
42: this .contentType = contentType;
43: this .content = content;
44: }
45:
46: public DERObjectIdentifier getContentType() {
47: return contentType;
48: }
49:
50: public DEREncodable getContent() {
51: return content;
52: }
53:
54: /**
55: * Produce an object suitable for an ASN1OutputStream.
56: * <pre>
57: * ContentInfo ::= SEQUENCE {
58: * contentType ContentType,
59: * content
60: * [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
61: * </pre>
62: */
63: public DERObject toASN1Object() {
64: ASN1EncodableVector v = new ASN1EncodableVector();
65:
66: v.add(contentType);
67:
68: if (content != null) {
69: v.add(new BERTaggedObject(0, content));
70: }
71:
72: return new BERSequence(v);
73: }
74: }
|