01: package org.bouncycastle.asn1.cms;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1OctetString;
06: import org.bouncycastle.asn1.ASN1Sequence;
07: import org.bouncycastle.asn1.ASN1TaggedObject;
08: import org.bouncycastle.asn1.BERSequence;
09: import org.bouncycastle.asn1.BERTaggedObject;
10: import org.bouncycastle.asn1.DERObject;
11: import org.bouncycastle.asn1.DERObjectIdentifier;
12: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
13:
14: public class EncryptedContentInfo extends ASN1Encodable {
15: private DERObjectIdentifier contentType;
16: private AlgorithmIdentifier contentEncryptionAlgorithm;
17: private ASN1OctetString encryptedContent;
18:
19: public EncryptedContentInfo(DERObjectIdentifier contentType,
20: AlgorithmIdentifier contentEncryptionAlgorithm,
21: ASN1OctetString encryptedContent) {
22: this .contentType = contentType;
23: this .contentEncryptionAlgorithm = contentEncryptionAlgorithm;
24: this .encryptedContent = encryptedContent;
25: }
26:
27: public EncryptedContentInfo(ASN1Sequence seq) {
28: contentType = (DERObjectIdentifier) seq.getObjectAt(0);
29: contentEncryptionAlgorithm = AlgorithmIdentifier
30: .getInstance(seq.getObjectAt(1));
31: if (seq.size() > 2) {
32: encryptedContent = ASN1OctetString.getInstance(
33: (ASN1TaggedObject) seq.getObjectAt(2), false);
34: }
35: }
36:
37: /**
38: * return an EncryptedContentInfo object from the given object.
39: *
40: * @param obj the object we want converted.
41: * @exception IllegalArgumentException if the object cannot be converted.
42: */
43: public static EncryptedContentInfo getInstance(Object obj) {
44: if (obj == null || obj instanceof EncryptedContentInfo) {
45: return (EncryptedContentInfo) obj;
46: }
47:
48: if (obj instanceof ASN1Sequence) {
49: return new EncryptedContentInfo((ASN1Sequence) obj);
50: }
51:
52: throw new IllegalArgumentException(
53: "Invalid EncryptedContentInfo: "
54: + obj.getClass().getName());
55: }
56:
57: public DERObjectIdentifier getContentType() {
58: return contentType;
59: }
60:
61: public AlgorithmIdentifier getContentEncryptionAlgorithm() {
62: return contentEncryptionAlgorithm;
63: }
64:
65: public ASN1OctetString getEncryptedContent() {
66: return encryptedContent;
67: }
68:
69: /**
70: * Produce an object suitable for an ASN1OutputStream.
71: * <pre>
72: * EncryptedContentInfo ::= SEQUENCE {
73: * contentType ContentType,
74: * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
75: * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
76: * }
77: * </pre>
78: */
79: public DERObject toASN1Object() {
80: ASN1EncodableVector v = new ASN1EncodableVector();
81:
82: v.add(contentType);
83: v.add(contentEncryptionAlgorithm);
84:
85: if (encryptedContent != null) {
86: v.add(new BERTaggedObject(false, 0, encryptedContent));
87: }
88:
89: return new BERSequence(v);
90: }
91: }
|