001: package org.bouncycastle.cms;
002:
003: import org.bouncycastle.asn1.ASN1EncodableVector;
004: import org.bouncycastle.asn1.ASN1InputStream;
005: import org.bouncycastle.asn1.ASN1Object;
006: import org.bouncycastle.asn1.ASN1Set;
007: import org.bouncycastle.asn1.BERSet;
008: import org.bouncycastle.asn1.DEREncodable;
009: import org.bouncycastle.asn1.DERSet;
010: import org.bouncycastle.asn1.cms.ContentInfo;
011: import org.bouncycastle.asn1.x509.CertificateList;
012: import org.bouncycastle.asn1.x509.X509CertificateStructure;
013:
014: import java.io.ByteArrayOutputStream;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.security.cert.CRLException;
018: import java.security.cert.CertStore;
019: import java.security.cert.CertStoreException;
020: import java.security.cert.CertificateEncodingException;
021: import java.security.cert.X509CRL;
022: import java.security.cert.X509Certificate;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: class CMSUtils {
028: private static final Runtime RUNTIME = Runtime.getRuntime();
029:
030: static int getMaximumMemory() {
031: long maxMem = RUNTIME.maxMemory();
032:
033: if (maxMem > Integer.MAX_VALUE) {
034: return Integer.MAX_VALUE;
035: }
036:
037: return (int) maxMem;
038: }
039:
040: static ContentInfo readContentInfo(byte[] input)
041: throws CMSException {
042: // enforce limit checking as from a byte array
043: return readContentInfo(new ASN1InputStream(input));
044: }
045:
046: static ContentInfo readContentInfo(InputStream input)
047: throws CMSException {
048: // enforce some limit checking
049: return readContentInfo(new ASN1InputStream(input,
050: getMaximumMemory()));
051: }
052:
053: static List getCertificatesFromStore(CertStore certStore)
054: throws CertStoreException, CMSException {
055: List certs = new ArrayList();
056:
057: try {
058: for (Iterator it = certStore.getCertificates(null)
059: .iterator(); it.hasNext();) {
060: X509Certificate c = (X509Certificate) it.next();
061:
062: certs.add(X509CertificateStructure
063: .getInstance(ASN1Object.fromByteArray(c
064: .getEncoded())));
065: }
066:
067: return certs;
068: } catch (IllegalArgumentException e) {
069: throw new CMSException("error processing certs", e);
070: } catch (IOException e) {
071: throw new CMSException("error processing certs", e);
072: } catch (CertificateEncodingException e) {
073: throw new CMSException("error encoding certs", e);
074: }
075: }
076:
077: static List getCRLsFromStore(CertStore certStore)
078: throws CertStoreException, CMSException {
079: List crls = new ArrayList();
080:
081: try {
082: for (Iterator it = certStore.getCRLs(null).iterator(); it
083: .hasNext();) {
084: X509CRL c = (X509CRL) it.next();
085:
086: crls.add(CertificateList.getInstance(ASN1Object
087: .fromByteArray(c.getEncoded())));
088: }
089:
090: return crls;
091: } catch (IllegalArgumentException e) {
092: throw new CMSException("error processing crls", e);
093: } catch (IOException e) {
094: throw new CMSException("error processing crls", e);
095: } catch (CRLException e) {
096: throw new CMSException("error encoding crls", e);
097: }
098: }
099:
100: static ASN1Set createBerSetFromList(List derObjects) {
101: ASN1EncodableVector v = new ASN1EncodableVector();
102:
103: for (Iterator it = derObjects.iterator(); it.hasNext();) {
104: v.add((DEREncodable) it.next());
105: }
106:
107: return new BERSet(v);
108: }
109:
110: static ASN1Set createDerSetFromList(List derObjects) {
111: ASN1EncodableVector v = new ASN1EncodableVector();
112:
113: for (Iterator it = derObjects.iterator(); it.hasNext();) {
114: v.add((DEREncodable) it.next());
115: }
116:
117: return new DERSet(v);
118: }
119:
120: private static ContentInfo readContentInfo(ASN1InputStream in)
121: throws CMSException {
122: try {
123: return ContentInfo.getInstance(in.readObject());
124: } catch (IOException e) {
125: throw new CMSException("IOException reading content.", e);
126: } catch (ClassCastException e) {
127: throw new CMSException("Malformed content.", e);
128: } catch (IllegalArgumentException e) {
129: throw new CMSException("Malformed content.", e);
130: }
131: }
132:
133: public static byte[] streamToByteArray(InputStream in)
134: throws IOException {
135: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
136: int ch;
137:
138: while ((ch = in.read()) >= 0) {
139: bOut.write(ch);
140: }
141:
142: return bOut.toByteArray();
143: }
144: }
|