001: package org.bouncycastle.asn1.cms;
002:
003: import java.util.Enumeration;
004:
005: import org.bouncycastle.asn1.ASN1Encodable;
006: import org.bouncycastle.asn1.ASN1EncodableVector;
007: import org.bouncycastle.asn1.ASN1Sequence;
008: import org.bouncycastle.asn1.ASN1Set;
009: import org.bouncycastle.asn1.ASN1TaggedObject;
010: import org.bouncycastle.asn1.BERSequence;
011: import org.bouncycastle.asn1.DERInteger;
012: import org.bouncycastle.asn1.DERObject;
013: import org.bouncycastle.asn1.DERTaggedObject;
014:
015: public class EnvelopedData extends ASN1Encodable {
016: private DERInteger version;
017: private OriginatorInfo originatorInfo;
018: private ASN1Set recipientInfos;
019: private EncryptedContentInfo encryptedContentInfo;
020: private ASN1Set unprotectedAttrs;
021:
022: public EnvelopedData(OriginatorInfo originatorInfo,
023: ASN1Set recipientInfos,
024: EncryptedContentInfo encryptedContentInfo,
025: ASN1Set unprotectedAttrs) {
026: if (originatorInfo != null || unprotectedAttrs != null) {
027: version = new DERInteger(2);
028: } else {
029: version = new DERInteger(0);
030:
031: Enumeration e = recipientInfos.getObjects();
032:
033: while (e.hasMoreElements()) {
034: RecipientInfo ri = RecipientInfo.getInstance(e
035: .nextElement());
036:
037: if (!ri.getVersion().equals(version)) {
038: version = new DERInteger(2);
039: break;
040: }
041: }
042: }
043:
044: this .originatorInfo = originatorInfo;
045: this .recipientInfos = recipientInfos;
046: this .encryptedContentInfo = encryptedContentInfo;
047: this .unprotectedAttrs = unprotectedAttrs;
048: }
049:
050: public EnvelopedData(ASN1Sequence seq) {
051: int index = 0;
052:
053: version = (DERInteger) seq.getObjectAt(index++);
054:
055: Object tmp = seq.getObjectAt(index++);
056:
057: if (tmp instanceof ASN1TaggedObject) {
058: originatorInfo = OriginatorInfo.getInstance(
059: (ASN1TaggedObject) tmp, false);
060: tmp = seq.getObjectAt(index++);
061: }
062:
063: recipientInfos = ASN1Set.getInstance(tmp);
064:
065: encryptedContentInfo = EncryptedContentInfo.getInstance(seq
066: .getObjectAt(index++));
067:
068: if (seq.size() > index) {
069: unprotectedAttrs = ASN1Set.getInstance(
070: (ASN1TaggedObject) seq.getObjectAt(index), false);
071: }
072: }
073:
074: /**
075: * return an EnvelopedData object from a tagged object.
076: *
077: * @param obj the tagged object holding the object we want.
078: * @param explicit true if the object is meant to be explicitly
079: * tagged false otherwise.
080: * @exception IllegalArgumentException if the object held by the
081: * tagged object cannot be converted.
082: */
083: public static EnvelopedData getInstance(ASN1TaggedObject obj,
084: boolean explicit) {
085: return getInstance(ASN1Sequence.getInstance(obj, explicit));
086: }
087:
088: /**
089: * return an EnvelopedData object from the given object.
090: *
091: * @param obj the object we want converted.
092: * @exception IllegalArgumentException if the object cannot be converted.
093: */
094: public static EnvelopedData getInstance(Object obj) {
095: if (obj == null || obj instanceof EnvelopedData) {
096: return (EnvelopedData) obj;
097: }
098:
099: if (obj instanceof ASN1Sequence) {
100: return new EnvelopedData((ASN1Sequence) obj);
101: }
102:
103: throw new IllegalArgumentException("Invalid EnvelopedData: "
104: + obj.getClass().getName());
105: }
106:
107: public DERInteger getVersion() {
108: return version;
109: }
110:
111: public OriginatorInfo getOriginatorInfo() {
112: return originatorInfo;
113: }
114:
115: public ASN1Set getRecipientInfos() {
116: return recipientInfos;
117: }
118:
119: public EncryptedContentInfo getEncryptedContentInfo() {
120: return encryptedContentInfo;
121: }
122:
123: public ASN1Set getUnprotectedAttrs() {
124: return unprotectedAttrs;
125: }
126:
127: /**
128: * Produce an object suitable for an ASN1OutputStream.
129: * <pre>
130: * EnvelopedData ::= SEQUENCE {
131: * version CMSVersion,
132: * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
133: * recipientInfos RecipientInfos,
134: * encryptedContentInfo EncryptedContentInfo,
135: * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
136: * }
137: * </pre>
138: */
139: public DERObject toASN1Object() {
140: ASN1EncodableVector v = new ASN1EncodableVector();
141:
142: v.add(version);
143:
144: if (originatorInfo != null) {
145: v.add(new DERTaggedObject(false, 0, originatorInfo));
146: }
147:
148: v.add(recipientInfos);
149: v.add(encryptedContentInfo);
150:
151: if (unprotectedAttrs != null) {
152: v.add(new DERTaggedObject(false, 1, unprotectedAttrs));
153: }
154:
155: return new BERSequence(v);
156: }
157: }
|