01: package org.bouncycastle.asn1.cms;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Sequence;
06: import org.bouncycastle.asn1.ASN1Set;
07: import org.bouncycastle.asn1.BERSequence;
08: import org.bouncycastle.asn1.BERTaggedObject;
09: import org.bouncycastle.asn1.DERInteger;
10: import org.bouncycastle.asn1.DERObject;
11:
12: public class EncryptedData extends ASN1Encodable {
13: private DERInteger version;
14: private EncryptedContentInfo encryptedContentInfo;
15: private ASN1Set unprotectedAttrs;
16:
17: public static EncryptedData getInstance(Object o) {
18: if (o instanceof EncryptedData) {
19: return (EncryptedData) o;
20: }
21:
22: if (o instanceof ASN1Sequence) {
23: return new EncryptedData((ASN1Sequence) o);
24: }
25:
26: throw new IllegalArgumentException("Invalid EncryptedData: "
27: + o.getClass().getName());
28: }
29:
30: public EncryptedData(EncryptedContentInfo encInfo) {
31: this (encInfo, null);
32: }
33:
34: public EncryptedData(EncryptedContentInfo encInfo,
35: ASN1Set unprotectedAttrs) {
36: this .version = new DERInteger((unprotectedAttrs == null) ? 0
37: : 2);
38: this .encryptedContentInfo = encInfo;
39: this .unprotectedAttrs = unprotectedAttrs;
40: }
41:
42: private EncryptedData(ASN1Sequence seq) {
43: this .version = DERInteger.getInstance(seq.getObjectAt(0));
44: this .encryptedContentInfo = EncryptedContentInfo
45: .getInstance(seq.getObjectAt(1));
46:
47: if (seq.size() == 3) {
48: this .unprotectedAttrs = ASN1Set.getInstance(seq
49: .getObjectAt(2));
50: }
51: }
52:
53: public DERInteger getVersion() {
54: return version;
55: }
56:
57: public EncryptedContentInfo getEncryptedContentInfo() {
58: return encryptedContentInfo;
59: }
60:
61: public ASN1Set getUnprotectedAttrs() {
62: return unprotectedAttrs;
63: }
64:
65: /**
66: * <pre>
67: * EncryptedData ::= SEQUENCE {
68: * version CMSVersion,
69: * encryptedContentInfo EncryptedContentInfo,
70: * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL }
71: * </pre>
72: * @return a basic ASN.1 object representation.
73: */
74: public DERObject toASN1Object() {
75: ASN1EncodableVector v = new ASN1EncodableVector();
76:
77: v.add(version);
78: v.add(encryptedContentInfo);
79: if (unprotectedAttrs != null) {
80: v.add(new BERTaggedObject(false, 1, unprotectedAttrs));
81: }
82:
83: return new BERSequence(v);
84: }
85: }
|