1
package org.bouncycastle.asn1.cms;
3
import java.util.Enumeration;
5
import org.bouncycastle.asn1.ASN1Encodable;
6
import org.bouncycastle.asn1.ASN1EncodableVector;
7
import org.bouncycastle.asn1.ASN1Sequence;
8
import org.bouncycastle.asn1.ASN1Set;
9
import org.bouncycastle.asn1.ASN1TaggedObject;
10
import org.bouncycastle.asn1.BERSequence;
11
import org.bouncycastle.asn1.DERInteger;
12
import org.bouncycastle.asn1.DERObject;
13
import org.bouncycastle.asn1.DERTaggedObject;
15
public class EnvelopedData
18
private DERInteger version;
19
private OriginatorInfo originatorInfo;
20
private ASN1Set recipientInfos;
21
private EncryptedContentInfo encryptedContentInfo;
22
private ASN1Set unprotectedAttrs;
25
OriginatorInfo originatorInfo,
26
ASN1Set recipientInfos,
27
EncryptedContentInfo encryptedContentInfo,
28
ASN1Set unprotectedAttrs)
30
if (originatorInfo != null || unprotectedAttrs != null)
32
version = new DERInteger(2);
36
version = new DERInteger(0);
38
Enumeration e = recipientInfos.getObjects();
40
while (e.hasMoreElements())
42
RecipientInfo ri = RecipientInfo.getInstance(e.nextElement());
44
if (!ri.getVersion().equals(version))
46
version = new DERInteger(2);
52
this.originatorInfo = originatorInfo;
53
this.recipientInfos = recipientInfos;
54
this.encryptedContentInfo = encryptedContentInfo;
55
this.unprotectedAttrs = unprotectedAttrs;
63
version = (DERInteger)seq.getObjectAt(index++);
65
Object tmp = seq.getObjectAt(index++);
67
if (tmp instanceof ASN1TaggedObject)
69
originatorInfo = OriginatorInfo.getInstance((ASN1TaggedObject)tmp, false);
70
tmp = seq.getObjectAt(index++);
73
recipientInfos = ASN1Set.getInstance(tmp);
75
encryptedContentInfo = EncryptedContentInfo.getInstance(seq.getObjectAt(index++));
77
if(seq.size() > index)
79
unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(index), false);
84
* return an EnvelopedData object from a tagged object.
86
* @param obj the tagged object holding the object we want.
87
* @param explicit true if the object is meant to be explicitly
88
* tagged false otherwise.
89
* @exception IllegalArgumentException if the object held by the
90
* tagged object cannot be converted.
92
public static EnvelopedData getInstance(
96
return getInstance(ASN1Sequence.getInstance(obj, explicit));
100
* return an EnvelopedData object from the given object.
102
* @param obj the object we want converted.
103
* @exception IllegalArgumentException if the object cannot be converted.
105
public static EnvelopedData getInstance(
108
if (obj == null || obj instanceof EnvelopedData)
110
return (EnvelopedData)obj;
113
if (obj instanceof ASN1Sequence)
115
return new EnvelopedData((ASN1Sequence)obj);
118
throw new IllegalArgumentException("Invalid EnvelopedData: " + obj.getClass().getName());
121
public DERInteger getVersion()
126
public OriginatorInfo getOriginatorInfo()
128
return originatorInfo;
131
public ASN1Set getRecipientInfos()
133
return recipientInfos;
136
public EncryptedContentInfo getEncryptedContentInfo()
138
return encryptedContentInfo;
141
public ASN1Set getUnprotectedAttrs()
143
return unprotectedAttrs;
147
* Produce an object suitable for an ASN1OutputStream.
149
* EnvelopedData ::= SEQUENCE {
150
* version CMSVersion,
151
* originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
152
* recipientInfos RecipientInfos,
153
* encryptedContentInfo EncryptedContentInfo,
154
* unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
158
public DERObject toASN1Object()
160
ASN1EncodableVector v = new ASN1EncodableVector();
164
if (originatorInfo != null)
166
v.add(new DERTaggedObject(false, 0, originatorInfo));
169
v.add(recipientInfos);
170
v.add(encryptedContentInfo);
172
if (unprotectedAttrs != null)
174
v.add(new DERTaggedObject(false, 1, unprotectedAttrs));
177
return new BERSequence(v);