001: package org.bouncycastle.cms;
002:
003: import org.bouncycastle.asn1.ASN1OutputStream;
004: import org.bouncycastle.asn1.ASN1Set;
005: import org.bouncycastle.asn1.DEREncodable;
006: import org.bouncycastle.asn1.cms.AttributeTable;
007: import org.bouncycastle.asn1.cms.ContentInfo;
008: import org.bouncycastle.asn1.cms.EncryptedContentInfo;
009: import org.bouncycastle.asn1.cms.EnvelopedData;
010: import org.bouncycastle.asn1.cms.KEKRecipientInfo;
011: import org.bouncycastle.asn1.cms.KeyAgreeRecipientInfo;
012: import org.bouncycastle.asn1.cms.KeyTransRecipientInfo;
013: import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
014: import org.bouncycastle.asn1.cms.RecipientInfo;
015: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.security.AlgorithmParameters;
022: import java.security.NoSuchProviderException;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: /**
027: * containing class for an CMS Enveloped Data object
028: */
029: public class CMSEnvelopedData {
030: RecipientInformationStore recipientInfoStore;
031: ContentInfo contentInfo;
032:
033: private AlgorithmIdentifier encAlg;
034: private ASN1Set unprotectedAttributes;
035: private AlgorithmIdentifier _encAlg;
036:
037: public CMSEnvelopedData(byte[] envelopedData) throws CMSException {
038: this (CMSUtils.readContentInfo(envelopedData));
039: }
040:
041: public CMSEnvelopedData(InputStream envelopedData)
042: throws CMSException {
043: this (CMSUtils.readContentInfo(envelopedData));
044: }
045:
046: public CMSEnvelopedData(ContentInfo contentInfo)
047: throws CMSException {
048: this .contentInfo = contentInfo;
049:
050: EnvelopedData envData = EnvelopedData.getInstance(contentInfo
051: .getContent());
052:
053: //
054: // read the encrypted content info
055: //
056: EncryptedContentInfo encInfo = envData
057: .getEncryptedContentInfo();
058:
059: this ._encAlg = encInfo.getContentEncryptionAlgorithm();
060:
061: //
062: // load the RecepientInfoStore
063: //
064: ASN1Set s = envData.getRecipientInfos();
065: List infos = new ArrayList();
066:
067: for (int i = 0; i != s.size(); i++) {
068: RecipientInfo info = RecipientInfo.getInstance(s
069: .getObjectAt(i));
070: Object type = info.getInfo();
071:
072: if (type instanceof KeyTransRecipientInfo) {
073: infos.add(new KeyTransRecipientInformation(
074: (KeyTransRecipientInfo) type, _encAlg,
075: new ByteArrayInputStream(encInfo
076: .getEncryptedContent().getOctets())));
077: } else if (type instanceof KEKRecipientInfo) {
078: infos.add(new KEKRecipientInformation(
079: (KEKRecipientInfo) type, _encAlg,
080: new ByteArrayInputStream(encInfo
081: .getEncryptedContent().getOctets())));
082: } else if (type instanceof KeyAgreeRecipientInfo) {
083: infos.add(new KeyAgreeRecipientInformation(
084: (KeyAgreeRecipientInfo) type, _encAlg,
085: new ByteArrayInputStream(encInfo
086: .getEncryptedContent().getOctets())));
087: } else if (type instanceof PasswordRecipientInfo) {
088: infos.add(new PasswordRecipientInformation(
089: (PasswordRecipientInfo) type, _encAlg,
090: new ByteArrayInputStream(encInfo
091: .getEncryptedContent().getOctets())));
092: }
093: }
094:
095: this .encAlg = envData.getEncryptedContentInfo()
096: .getContentEncryptionAlgorithm();
097: this .recipientInfoStore = new RecipientInformationStore(infos);
098: this .unprotectedAttributes = envData.getUnprotectedAttrs();
099: }
100:
101: private byte[] encodeObj(DEREncodable obj) throws IOException {
102: if (obj != null) {
103: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
104: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
105:
106: aOut.writeObject(obj);
107:
108: return bOut.toByteArray();
109: }
110:
111: return null;
112: }
113:
114: /**
115: * return the object identifier for the content encryption algorithm.
116: */
117: public String getEncryptionAlgOID() {
118: return encAlg.getObjectId().getId();
119: }
120:
121: /**
122: * return the ASN.1 encoded encryption algorithm parameters, or null if
123: * there aren't any.
124: */
125: public byte[] getEncryptionAlgParams() {
126: try {
127: return encodeObj(encAlg.getParameters());
128: } catch (Exception e) {
129: throw new RuntimeException(
130: "exception getting encryption parameters " + e);
131: }
132: }
133:
134: /**
135: * Return an AlgorithmParameters object giving the encryption parameters
136: * used to encrypt the message content.
137: *
138: * @param provider the provider to generate the parameters for.
139: * @return the parameters object, null if there is not one.
140: * @throws CMSException if the algorithm cannot be found, or the parameters can't be parsed.
141: * @throws NoSuchProviderException if the provider cannot be found.
142: */
143: public AlgorithmParameters getEncryptionAlgorithmParameters(
144: String provider) throws CMSException,
145: NoSuchProviderException {
146: return CMSEnvelopedHelper.INSTANCE
147: .getEncryptionAlgorithmParameters(
148: getEncryptionAlgOID(),
149: getEncryptionAlgParams(), provider);
150: }
151:
152: /**
153: * return a store of the intended recipients for this message
154: */
155: public RecipientInformationStore getRecipientInfos() {
156: return recipientInfoStore;
157: }
158:
159: /**
160: * return a table of the unprotected attributes indexed by
161: * the OID of the attribute.
162: */
163: public AttributeTable getUnprotectedAttributes() {
164: if (unprotectedAttributes == null) {
165: return null;
166: }
167:
168: return new AttributeTable(unprotectedAttributes);
169: }
170:
171: /**
172: * return the ASN.1 encoded representation of this object.
173: */
174: public byte[] getEncoded() throws IOException {
175: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
176: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
177:
178: aOut.writeObject(contentInfo);
179:
180: return bOut.toByteArray();
181: }
182: }
|