1e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrompackage org.bouncycastle.cms;
2e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
3e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.io.IOException;
4e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.io.InputStream;
5e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.io.OutputStream;
6e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.util.ArrayList;
7e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.util.Collection;
8e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.util.Iterator;
9e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport java.util.List;
10e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
11e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.ASN1Encodable;
12e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.ASN1EncodableVector;
13e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.ASN1InputStream;
14a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
15e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.ASN1Set;
16d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootimport org.bouncycastle.asn1.ASN1TaggedObject;
17e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.BEROctetStringGenerator;
18e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.BERSet;
19e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.DERSet;
20e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.DERTaggedObject;
21a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstromimport org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
22e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.asn1.cms.ContentInfo;
23a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom// BEGIN android-removed
24a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom// import org.bouncycastle.asn1.cms.OtherRevocationInfoFormat;
25a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom// import org.bouncycastle.asn1.ocsp.OCSPResponse;
26a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom// import org.bouncycastle.asn1.ocsp.OCSPResponseStatus;
27a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom// END android-removed
28e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.cert.X509AttributeCertificateHolder;
29e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.cert.X509CRLHolder;
30e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.cert.X509CertificateHolder;
31e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.operator.DigestCalculator;
32e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.util.Store;
33d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootimport org.bouncycastle.util.Strings;
34e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.util.io.Streams;
35e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.util.io.TeeInputStream;
36e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromimport org.bouncycastle.util.io.TeeOutputStream;
37e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
38e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstromclass CMSUtils
39e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom{
40e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static ContentInfo readContentInfo(
41e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        byte[] input)
42e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
43e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
44e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        // enforce limit checking as from a byte array
45e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return readContentInfo(new ASN1InputStream(input));
46e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
47e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
48e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static ContentInfo readContentInfo(
49e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        InputStream input)
50e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
51e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
52e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        // enforce some limit checking
53e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return readContentInfo(new ASN1InputStream(input));
54e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
55e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
56e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static List getCertificatesFromStore(Store certStore)
57e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
58e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
59e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        List certs = new ArrayList();
60e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
61e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        try
62e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
63e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            for (Iterator it = certStore.getMatches(null).iterator(); it.hasNext();)
64e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            {
65e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                X509CertificateHolder c = (X509CertificateHolder)it.next();
66e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
67e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                certs.add(c.toASN1Structure());
68e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            }
69e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
70e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            return certs;
71e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
72e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (ClassCastException e)
73e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
74e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("error processing certs", e);
75e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
76e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
77e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
78e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static List getAttributeCertificatesFromStore(Store attrStore)
79e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
80e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
81e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        List certs = new ArrayList();
82e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
83e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        try
84e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
85e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            for (Iterator it = attrStore.getMatches(null).iterator(); it.hasNext();)
86e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            {
87e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                X509AttributeCertificateHolder attrCert = (X509AttributeCertificateHolder)it.next();
88e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
89e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                certs.add(new DERTaggedObject(false, 2, attrCert.toASN1Structure()));
90e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            }
91e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
92e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            return certs;
93e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
94e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (ClassCastException e)
95e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
96e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("error processing certs", e);
97e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
98e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
99e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
100e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
101e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static List getCRLsFromStore(Store crlStore)
102e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
103e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
104d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        List crls = new ArrayList();
105e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
106e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        try
107e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
108e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            for (Iterator it = crlStore.getMatches(null).iterator(); it.hasNext();)
109e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            {
110d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                Object rev = it.next();
111e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
112d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                if (rev instanceof X509CRLHolder)
113d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                {
114d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                    X509CRLHolder c = (X509CRLHolder)rev;
115d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
116d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                    crls.add(c.toASN1Structure());
117d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                }
118d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                // BEGIN android-removed
119d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                // else if (rev instanceof OtherRevocationInfoFormat)
120d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                // {
121d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                //     OtherRevocationInfoFormat infoFormat = OtherRevocationInfoFormat.getInstance(rev);
122d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                //
123d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                //     validateInfoFormat(infoFormat);
124d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                //
125d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                //     crls.add(new DERTaggedObject(false, 1, infoFormat));
126d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                // }
127d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                // END android-removed
128d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                else if (rev instanceof ASN1TaggedObject)
129d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                {
130d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                    crls.add(rev);
131d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                }
132e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            }
133e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
134d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return crls;
135e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
136e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (ClassCastException e)
137e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
138e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("error processing certs", e);
139e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
140e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
141e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
142a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    // BEGIN android-removed
143d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    // private static void validateInfoFormat(OtherRevocationInfoFormat infoFormat)
144d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    // {
145d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //     if (CMSObjectIdentifiers.id_ri_ocsp_response.equals(infoFormat.getInfoFormat()))
146d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //     {
147d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         OCSPResponse resp = OCSPResponse.getInstance(infoFormat.getInfo());
148d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //
149d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         if (resp.getResponseStatus().getValue().intValue() != OCSPResponseStatus.SUCCESSFUL)
150d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         {
151d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //             throw new IllegalArgumentException("cannot add unsuccessful OCSP response to CMS SignedData");
152d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         }
153d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //     }
154d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    // }
155d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //
156a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    // static Collection getOthersFromStore(ASN1ObjectIdentifier otherRevocationInfoFormat, Store otherRevocationInfos)
157a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    // {
158a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //     List others = new ArrayList();
159a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //
160a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //     for (Iterator it = otherRevocationInfos.getMatches(null).iterator(); it.hasNext();)
161a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //     {
162a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //         ASN1Encodable info = (ASN1Encodable)it.next();
163d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         OtherRevocationInfoFormat infoFormat = new OtherRevocationInfoFormat(otherRevocationInfoFormat, info);
164d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         validateInfoFormat(infoFormat);
165a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //
166d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    //         others.add(new DERTaggedObject(false, 1, infoFormat));
167a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //     }
168a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //
169a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    //     return others;
170a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    // }
171a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    // END android-removed
172a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
173e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static ASN1Set createBerSetFromList(List derObjects)
174e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
175e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        ASN1EncodableVector v = new ASN1EncodableVector();
176e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
177e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        for (Iterator it = derObjects.iterator(); it.hasNext();)
178e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
179e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            v.add((ASN1Encodable)it.next());
180e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
181e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
182e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return new BERSet(v);
183e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
184e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
185e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static ASN1Set createDerSetFromList(List derObjects)
186e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
187e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        ASN1EncodableVector v = new ASN1EncodableVector();
188e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
189e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        for (Iterator it = derObjects.iterator(); it.hasNext();)
190e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
191e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            v.add((ASN1Encodable)it.next());
192e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
193e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
194e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return new DERSet(v);
195e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
196e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
197e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static OutputStream createBEROctetOutputStream(OutputStream s,
198e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            int tagNo, boolean isExplicit, int bufferSize) throws IOException
199e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
200e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        BEROctetStringGenerator octGen = new BEROctetStringGenerator(s, tagNo, isExplicit);
201e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
202e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        if (bufferSize != 0)
203e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
204e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            return octGen.getOctetOutputStream(new byte[bufferSize]);
205e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
206e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
207e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return octGen.getOctetOutputStream();
208e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
209e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
210e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    private static ContentInfo readContentInfo(
211e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        ASN1InputStream in)
212e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws CMSException
213e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
214e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        try
215e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
216e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            return ContentInfo.getInstance(in.readObject());
217e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
218e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (IOException e)
219e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
220e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("IOException reading content.", e);
221e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
222e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (ClassCastException e)
223e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
224e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("Malformed content.", e);
225e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
226e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        catch (IllegalArgumentException e)
227e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
228e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            throw new CMSException("Malformed content.", e);
229e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
230e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
231d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
232d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    static byte[] getPasswordBytes(int scheme, char[] password)
233d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    {
234d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        if (scheme == PasswordRecipient.PKCS5_SCHEME2)
235d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        {
236d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return PKCS5PasswordToBytes(password);
237d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        }
238d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
239d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        return PKCS5PasswordToUTF8Bytes(password);
240d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    }
241d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
242d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    /**
243d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * converts a password to a byte array according to the scheme in
244d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * PKCS5 (ascii, no padding)
245d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     *
246d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * @param password a character array representing the password.
247d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * @return a byte array representing the password.
248d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     */
249d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    private static byte[] PKCS5PasswordToBytes(
250d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        char[]  password)
251d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    {
252d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        if (password != null)
253d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        {
254d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            byte[]  bytes = new byte[password.length];
255d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
256d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            for (int i = 0; i != bytes.length; i++)
257d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            {
258d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root                bytes[i] = (byte)password[i];
259d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            }
260d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
261d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return bytes;
262d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        }
263d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        else
264d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        {
265d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return new byte[0];
266d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        }
267d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    }
268d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
269d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    /**
270d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * converts a password to a byte array according to the scheme in
271d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * PKCS5 (UTF-8, no padding)
272d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     *
273d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * @param password a character array representing the password.
274d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     * @return a byte array representing the password.
275d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root     */
276d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    private static byte[] PKCS5PasswordToUTF8Bytes(
277d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        char[]  password)
278d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    {
279d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        if (password != null)
280d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        {
281d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return Strings.toUTF8ByteArray(password);
282d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        }
283d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        else
284d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        {
285d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root            return new byte[0];
286d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root        }
287d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root    }
288d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root
289e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    public static byte[] streamToByteArray(
290e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        InputStream in)
291e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws IOException
292e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
293e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return Streams.readAll(in);
294e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
295e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
296e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    public static byte[] streamToByteArray(
297e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        InputStream in,
298e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        int         limit)
299e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        throws IOException
300e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
301e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return Streams.readAllLimited(in, limit);
302e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
303e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
304e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static InputStream attachDigestsToInputStream(Collection digests, InputStream s)
305e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
306e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        InputStream result = s;
307e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        Iterator it = digests.iterator();
308e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        while (it.hasNext())
309e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
310e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            DigestCalculator digest = (DigestCalculator)it.next();
311e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            result = new TeeInputStream(result, digest.getOutputStream());
312e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
313e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return result;
314e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
315e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
316e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static OutputStream attachSignersToOutputStream(Collection signers, OutputStream s)
317e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
318e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        OutputStream result = s;
319e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        Iterator it = signers.iterator();
320e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        while (it.hasNext())
321e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        {
322e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            SignerInfoGenerator signerGen = (SignerInfoGenerator)it.next();
323e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            result = getSafeTeeOutputStream(result, signerGen.getCalculatingOutputStream());
324e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        }
325e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return result;
326e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
327e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
328e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static OutputStream getSafeOutputStream(OutputStream s)
329e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
330e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return s == null ? new NullOutputStream() : s;
331e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
332e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom
333e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    static OutputStream getSafeTeeOutputStream(OutputStream s1,
334e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom            OutputStream s2)
335e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    {
336e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom        return s1 == null ? getSafeOutputStream(s2)
337e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                : s2 == null ? getSafeOutputStream(s1) : new TeeOutputStream(
338e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom                        s1, s2);
339e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom    }
340e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom}
341