1
package org.bouncycastle.cms;
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.OutputStream;
6
import java.security.MessageDigest;
7
import java.security.NoSuchProviderException;
8
import java.security.Provider;
9
import java.security.Security;
10
import java.security.cert.CRLException;
11
import org.bouncycastle.jce.cert.CertStore;
12
import org.bouncycastle.jce.cert.CertStoreException;
13
import java.security.cert.CertificateEncodingException;
14
import java.security.cert.X509CRL;
15
import java.security.cert.X509Certificate;
16
import java.util.ArrayList;
17
import java.util.Collection;
18
import java.util.Iterator;
19
import java.util.List;
21
import org.bouncycastle.asn1.ASN1EncodableVector;
22
import org.bouncycastle.asn1.ASN1InputStream;
23
import org.bouncycastle.asn1.ASN1Object;
24
import org.bouncycastle.asn1.ASN1Set;
25
import org.bouncycastle.asn1.BEROctetStringGenerator;
26
import org.bouncycastle.asn1.BERSet;
27
import org.bouncycastle.asn1.DEREncodable;
28
import org.bouncycastle.asn1.DERSet;
29
import org.bouncycastle.asn1.DERTaggedObject;
30
import org.bouncycastle.asn1.cms.ContentInfo;
31
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
32
import org.bouncycastle.asn1.x509.CertificateList;
33
import org.bouncycastle.asn1.x509.TBSCertificateStructure;
34
import org.bouncycastle.asn1.x509.X509CertificateStructure;
35
import org.bouncycastle.cert.X509AttributeCertificateHolder;
36
import org.bouncycastle.cert.X509CRLHolder;
37
import org.bouncycastle.cert.X509CertificateHolder;
38
import org.bouncycastle.util.Store;
39
import org.bouncycastle.util.io.Streams;
40
import org.bouncycastle.util.io.TeeInputStream;
41
import org.bouncycastle.util.io.TeeOutputStream;
45
private static final Runtime RUNTIME = Runtime.getRuntime();
47
static int getMaximumMemory()
49
return Integer.MAX_VALUE;
52
static ContentInfo readContentInfo(
56
// enforce limit checking as from a byte array
57
return readContentInfo(new ASN1InputStream(input));
60
static ContentInfo readContentInfo(
64
// enforce some limit checking
65
return readContentInfo(new ASN1InputStream(input, getMaximumMemory()));
68
static List getCertificatesFromStore(CertStore certStore)
69
throws CertStoreException, CMSException
71
List certs = new ArrayList();
75
for (Iterator it = certStore.getCertificates(null).iterator(); it.hasNext();)
77
X509Certificate c = (X509Certificate)it.next();
79
certs.add(X509CertificateStructure.getInstance(
80
ASN1Object.fromByteArray(c.getEncoded())));
85
catch (IllegalArgumentException e)
87
throw new CMSException("error processing certs", e);
91
throw new CMSException("error processing certs", e);
93
catch (CertificateEncodingException e)
95
throw new CMSException("error encoding certs", e);
99
static List getCertificatesFromStore(Store certStore)
102
List certs = new ArrayList();
106
for (Iterator it = certStore.getMatches(null).iterator(); it.hasNext();)
108
X509CertificateHolder c = (X509CertificateHolder)it.next();
110
certs.add(c.toASN1Structure());
115
catch (ClassCastException e)
117
throw new CMSException("error processing certs", e);
121
static List getAttributeCertificatesFromStore(Store attrStore)
124
List certs = new ArrayList();
128
for (Iterator it = attrStore.getMatches(null).iterator(); it.hasNext();)
130
X509AttributeCertificateHolder attrCert = (X509AttributeCertificateHolder)it.next();
132
certs.add(new DERTaggedObject(false, 2, attrCert.toASN1Structure()));
137
catch (ClassCastException e)
139
throw new CMSException("error processing certs", e);
143
static List getCRLsFromStore(CertStore certStore)
144
throws CertStoreException, CMSException
146
List crls = new ArrayList();
150
for (Iterator it = certStore.getCRLs(null).iterator(); it.hasNext();)
152
X509CRL c = (X509CRL)it.next();
154
crls.add(CertificateList.getInstance(ASN1Object.fromByteArray(c.getEncoded())));
159
catch (IllegalArgumentException e)
161
throw new CMSException("error processing crls", e);
163
catch (IOException e)
165
throw new CMSException("error processing crls", e);
167
catch (CRLException e)
169
throw new CMSException("error encoding crls", e);
173
static List getCRLsFromStore(Store crlStore)
176
List certs = new ArrayList();
180
for (Iterator it = crlStore.getMatches(null).iterator(); it.hasNext();)
182
X509CRLHolder c = (X509CRLHolder)it.next();
184
certs.add(c.toASN1Structure());
189
catch (ClassCastException e)
191
throw new CMSException("error processing certs", e);
195
static ASN1Set createBerSetFromList(List derObjects)
197
ASN1EncodableVector v = new ASN1EncodableVector();
199
for (Iterator it = derObjects.iterator(); it.hasNext();)
201
v.add((DEREncodable)it.next());
204
return new BERSet(v);
207
static ASN1Set createDerSetFromList(List derObjects)
209
ASN1EncodableVector v = new ASN1EncodableVector();
211
for (Iterator it = derObjects.iterator(); it.hasNext();)
213
v.add((DEREncodable)it.next());
216
return new DERSet(v);
219
static OutputStream createBEROctetOutputStream(OutputStream s,
220
int tagNo, boolean isExplicit, int bufferSize) throws IOException
222
BEROctetStringGenerator octGen = new BEROctetStringGenerator(s, tagNo, isExplicit);
226
return octGen.getOctetOutputStream(new byte[bufferSize]);
229
return octGen.getOctetOutputStream();
232
static TBSCertificateStructure getTBSCertificateStructure(
233
X509Certificate cert)
237
return TBSCertificateStructure.getInstance(
238
ASN1Object.fromByteArray(cert.getTBSCertificate()));
242
throw new IllegalArgumentException(
243
"can't extract TBS structure from this cert");
247
static IssuerAndSerialNumber getIssuerAndSerialNumber(X509Certificate cert)
249
TBSCertificateStructure tbsCert = getTBSCertificateStructure(cert);
250
return new IssuerAndSerialNumber(tbsCert.getIssuer(), tbsCert.getSerialNumber().getValue());
253
private static ContentInfo readContentInfo(
259
return ContentInfo.getInstance(in.readObject());
261
catch (IOException e)
263
throw new CMSException("IOException reading content.", e);
265
catch (ClassCastException e)
267
throw new CMSException("Malformed content.", e);
269
catch (IllegalArgumentException e)
271
throw new CMSException("Malformed content.", e);
275
public static byte[] streamToByteArray(
279
return Streams.readAll(in);
282
public static byte[] streamToByteArray(
287
return Streams.readAllLimited(in, limit);
290
public static Provider getProvider(String providerName)
291
throws NoSuchProviderException
293
if (providerName != null)
295
Provider prov = Security.getProvider(providerName);
302
throw new NoSuchProviderException("provider " + providerName + " not found.");
308
static InputStream attachDigestsToInputStream(Collection digests, InputStream s)
310
InputStream result = s;
311
Iterator it = digests.iterator();
314
MessageDigest digest = (MessageDigest)it.next();
315
result = new TeeInputStream(result, new DigOutputStream(digest));
320
static OutputStream attachDigestsToOutputStream(Collection digests, OutputStream s)
322
OutputStream result = s;
323
Iterator it = digests.iterator();
326
MessageDigest digest = (MessageDigest)it.next();
327
result = getSafeTeeOutputStream(result, new DigOutputStream(digest));
332
static OutputStream attachSignersToOutputStream(Collection signers, OutputStream s)
334
OutputStream result = s;
335
Iterator it = signers.iterator();
338
SignerInfoGenerator signerGen = (SignerInfoGenerator)it.next();
339
result = getSafeTeeOutputStream(result, signerGen.getCalculatingOutputStream());
344
static OutputStream getSafeOutputStream(OutputStream s)
346
return s == null ? new NullOutputStream() : s;
349
static OutputStream getSafeTeeOutputStream(OutputStream s1,
352
return s1 == null ? getSafeOutputStream(s2)
353
: s2 == null ? getSafeOutputStream(s1) : new TeeOutputStream(