~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to jdk1.3/org/bouncycastle/cms/CMSUtils.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cms;
 
2
 
 
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;
 
20
 
 
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;
 
42
 
 
43
class CMSUtils
 
44
{
 
45
    private static final Runtime RUNTIME = Runtime.getRuntime();
 
46
    
 
47
    static int getMaximumMemory()
 
48
    {
 
49
            return Integer.MAX_VALUE;
 
50
    }
 
51
    
 
52
    static ContentInfo readContentInfo(
 
53
        byte[] input)
 
54
        throws CMSException
 
55
    {
 
56
        // enforce limit checking as from a byte array
 
57
        return readContentInfo(new ASN1InputStream(input));
 
58
    }
 
59
 
 
60
    static ContentInfo readContentInfo(
 
61
        InputStream input)
 
62
        throws CMSException
 
63
    {
 
64
        // enforce some limit checking
 
65
        return readContentInfo(new ASN1InputStream(input, getMaximumMemory()));
 
66
    } 
 
67
 
 
68
    static List getCertificatesFromStore(CertStore certStore)
 
69
        throws CertStoreException, CMSException
 
70
    {
 
71
        List certs = new ArrayList();
 
72
 
 
73
        try
 
74
        {
 
75
            for (Iterator it = certStore.getCertificates(null).iterator(); it.hasNext();)
 
76
            {
 
77
                X509Certificate c = (X509Certificate)it.next();
 
78
 
 
79
                certs.add(X509CertificateStructure.getInstance(
 
80
                                                       ASN1Object.fromByteArray(c.getEncoded())));
 
81
            }
 
82
 
 
83
            return certs;
 
84
        }
 
85
        catch (IllegalArgumentException e)
 
86
        {
 
87
            throw new CMSException("error processing certs", e);
 
88
        }
 
89
        catch (IOException e)
 
90
        {
 
91
            throw new CMSException("error processing certs", e);
 
92
        }
 
93
        catch (CertificateEncodingException e)
 
94
        {
 
95
            throw new CMSException("error encoding certs", e);
 
96
        }
 
97
    }
 
98
 
 
99
    static List getCertificatesFromStore(Store certStore)
 
100
        throws CMSException
 
101
    {
 
102
        List certs = new ArrayList();
 
103
 
 
104
        try
 
105
        {
 
106
            for (Iterator it = certStore.getMatches(null).iterator(); it.hasNext();)
 
107
            {
 
108
                X509CertificateHolder c = (X509CertificateHolder)it.next();
 
109
 
 
110
                certs.add(c.toASN1Structure());
 
111
            }
 
112
 
 
113
            return certs;
 
114
        }
 
115
        catch (ClassCastException e)
 
116
        {
 
117
            throw new CMSException("error processing certs", e);
 
118
        }
 
119
    }
 
120
 
 
121
    static List getAttributeCertificatesFromStore(Store attrStore)
 
122
        throws CMSException
 
123
    {
 
124
        List certs = new ArrayList();
 
125
 
 
126
        try
 
127
        {
 
128
            for (Iterator it = attrStore.getMatches(null).iterator(); it.hasNext();)
 
129
            {
 
130
                X509AttributeCertificateHolder attrCert = (X509AttributeCertificateHolder)it.next();
 
131
 
 
132
                certs.add(new DERTaggedObject(false, 2, attrCert.toASN1Structure()));
 
133
            }
 
134
 
 
135
            return certs;
 
136
        }
 
137
        catch (ClassCastException e)
 
138
        {
 
139
            throw new CMSException("error processing certs", e);
 
140
        }
 
141
    }
 
142
 
 
143
    static List getCRLsFromStore(CertStore certStore)
 
144
        throws CertStoreException, CMSException
 
145
    {
 
146
        List crls = new ArrayList();
 
147
 
 
148
        try
 
149
        {
 
150
            for (Iterator it = certStore.getCRLs(null).iterator(); it.hasNext();)
 
151
            {
 
152
                X509CRL c = (X509CRL)it.next();
 
153
 
 
154
                crls.add(CertificateList.getInstance(ASN1Object.fromByteArray(c.getEncoded())));
 
155
            }
 
156
 
 
157
            return crls;
 
158
        }
 
159
        catch (IllegalArgumentException e)
 
160
        {
 
161
            throw new CMSException("error processing crls", e);
 
162
        }
 
163
        catch (IOException e)
 
164
        {
 
165
            throw new CMSException("error processing crls", e);
 
166
        }
 
167
        catch (CRLException e)
 
168
        {
 
169
            throw new CMSException("error encoding crls", e);
 
170
        }
 
171
    }
 
172
 
 
173
    static List getCRLsFromStore(Store crlStore)
 
174
        throws CMSException
 
175
    {
 
176
        List certs = new ArrayList();
 
177
 
 
178
        try
 
179
        {
 
180
            for (Iterator it = crlStore.getMatches(null).iterator(); it.hasNext();)
 
181
            {
 
182
                X509CRLHolder c = (X509CRLHolder)it.next();
 
183
 
 
184
                certs.add(c.toASN1Structure());
 
185
            }
 
186
 
 
187
            return certs;
 
188
        }
 
189
        catch (ClassCastException e)
 
190
        {
 
191
            throw new CMSException("error processing certs", e);
 
192
        }
 
193
    }
 
194
 
 
195
    static ASN1Set createBerSetFromList(List derObjects)
 
196
    {
 
197
        ASN1EncodableVector v = new ASN1EncodableVector();
 
198
 
 
199
        for (Iterator it = derObjects.iterator(); it.hasNext();)
 
200
        {
 
201
            v.add((DEREncodable)it.next());
 
202
        }
 
203
 
 
204
        return new BERSet(v);
 
205
    }
 
206
 
 
207
    static ASN1Set createDerSetFromList(List derObjects)
 
208
    {
 
209
        ASN1EncodableVector v = new ASN1EncodableVector();
 
210
 
 
211
        for (Iterator it = derObjects.iterator(); it.hasNext();)
 
212
        {
 
213
            v.add((DEREncodable)it.next());
 
214
        }
 
215
 
 
216
        return new DERSet(v);
 
217
    }
 
218
 
 
219
    static OutputStream createBEROctetOutputStream(OutputStream s,
 
220
            int tagNo, boolean isExplicit, int bufferSize) throws IOException
 
221
    {
 
222
        BEROctetStringGenerator octGen = new BEROctetStringGenerator(s, tagNo, isExplicit);
 
223
 
 
224
        if (bufferSize != 0)
 
225
        {
 
226
            return octGen.getOctetOutputStream(new byte[bufferSize]);
 
227
        }
 
228
 
 
229
        return octGen.getOctetOutputStream();
 
230
    }
 
231
 
 
232
    static TBSCertificateStructure getTBSCertificateStructure(
 
233
        X509Certificate cert)
 
234
    {
 
235
        try
 
236
        {
 
237
            return TBSCertificateStructure.getInstance(
 
238
                ASN1Object.fromByteArray(cert.getTBSCertificate()));
 
239
        }
 
240
        catch (Exception e)
 
241
        {
 
242
            throw new IllegalArgumentException(
 
243
                "can't extract TBS structure from this cert");
 
244
        }
 
245
    }
 
246
 
 
247
    static IssuerAndSerialNumber getIssuerAndSerialNumber(X509Certificate cert)
 
248
    {
 
249
        TBSCertificateStructure tbsCert = getTBSCertificateStructure(cert);
 
250
        return new IssuerAndSerialNumber(tbsCert.getIssuer(), tbsCert.getSerialNumber().getValue());
 
251
    }
 
252
 
 
253
    private static ContentInfo readContentInfo(
 
254
        ASN1InputStream in)
 
255
        throws CMSException
 
256
    {
 
257
        try
 
258
        {
 
259
            return ContentInfo.getInstance(in.readObject());
 
260
        }
 
261
        catch (IOException e)
 
262
        {
 
263
            throw new CMSException("IOException reading content.", e);
 
264
        }
 
265
        catch (ClassCastException e)
 
266
        {
 
267
            throw new CMSException("Malformed content.", e);
 
268
        }
 
269
        catch (IllegalArgumentException e)
 
270
        {
 
271
            throw new CMSException("Malformed content.", e);
 
272
        }
 
273
    }
 
274
    
 
275
    public static byte[] streamToByteArray(
 
276
        InputStream in) 
 
277
        throws IOException
 
278
    {
 
279
        return Streams.readAll(in);
 
280
    }
 
281
 
 
282
    public static byte[] streamToByteArray(
 
283
        InputStream in,
 
284
        int         limit)
 
285
        throws IOException
 
286
    {
 
287
        return Streams.readAllLimited(in, limit);
 
288
    }
 
289
 
 
290
    public static Provider getProvider(String providerName)
 
291
        throws NoSuchProviderException
 
292
    {
 
293
        if (providerName != null)
 
294
        {
 
295
            Provider prov = Security.getProvider(providerName);
 
296
 
 
297
            if (prov != null)
 
298
            {
 
299
                return prov;
 
300
            }
 
301
 
 
302
            throw new NoSuchProviderException("provider " + providerName + " not found.");
 
303
        }
 
304
 
 
305
        return null; 
 
306
    }
 
307
 
 
308
    static InputStream attachDigestsToInputStream(Collection digests, InputStream s)
 
309
    {
 
310
        InputStream result = s;
 
311
        Iterator it = digests.iterator();
 
312
        while (it.hasNext())
 
313
        {
 
314
            MessageDigest digest = (MessageDigest)it.next();
 
315
            result = new TeeInputStream(result, new DigOutputStream(digest));
 
316
        }
 
317
        return result;
 
318
    }
 
319
 
 
320
    static OutputStream attachDigestsToOutputStream(Collection digests, OutputStream s)
 
321
    {
 
322
        OutputStream result = s;
 
323
        Iterator it = digests.iterator();
 
324
        while (it.hasNext())
 
325
        {
 
326
            MessageDigest digest = (MessageDigest)it.next();
 
327
            result = getSafeTeeOutputStream(result, new DigOutputStream(digest));
 
328
        }
 
329
        return result;
 
330
    }
 
331
 
 
332
    static OutputStream attachSignersToOutputStream(Collection signers, OutputStream s)
 
333
    {
 
334
        OutputStream result = s;
 
335
        Iterator it = signers.iterator();
 
336
        while (it.hasNext())
 
337
        {
 
338
            SignerInfoGenerator signerGen = (SignerInfoGenerator)it.next();
 
339
            result = getSafeTeeOutputStream(result, signerGen.getCalculatingOutputStream());
 
340
        }
 
341
        return result;
 
342
    }
 
343
 
 
344
    static OutputStream getSafeOutputStream(OutputStream s)
 
345
    {
 
346
        return s == null ? new NullOutputStream() : s;
 
347
    }
 
348
 
 
349
    static OutputStream getSafeTeeOutputStream(OutputStream s1,
 
350
            OutputStream s2)
 
351
    {
 
352
        return s1 == null ? getSafeOutputStream(s2)
 
353
                : s2 == null ? getSafeOutputStream(s1) : new TeeOutputStream(
 
354
                        s1, s2);
 
355
    }
 
356
}