~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/mail/smime/examples/ReadLargeSignedMail.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.mail.smime.examples;
 
2
 
 
3
import java.security.cert.X509Certificate;
 
4
import java.util.Collection;
 
5
import java.util.Iterator;
 
6
import java.util.Properties;
 
7
 
 
8
import javax.mail.Session;
 
9
import javax.mail.internet.MimeMessage;
 
10
import javax.mail.internet.MimeMultipart;
 
11
 
 
12
import org.bouncycastle.cert.X509CertificateHolder;
 
13
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
 
14
import org.bouncycastle.cms.SignerInformation;
 
15
import org.bouncycastle.cms.SignerInformationStore;
 
16
import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder;
 
17
import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
18
import org.bouncycastle.mail.smime.SMIMESignedParser;
 
19
import org.bouncycastle.mail.smime.util.SharedFileInputStream;
 
20
import org.bouncycastle.util.Store;
 
21
 
 
22
/**
 
23
 * a simple example that reads a basic SMIME signed mail file.
 
24
 */
 
25
public class ReadLargeSignedMail
 
26
{
 
27
    private static final String BC = BouncyCastleProvider.PROVIDER_NAME;
 
28
 
 
29
    /**
 
30
     * verify the signature (assuming the cert is contained in the message)
 
31
     */
 
32
    private static void verify(
 
33
        SMIMESignedParser s)
 
34
        throws Exception
 
35
    {
 
36
        //
 
37
        // extract the information to verify the signatures.
 
38
        //
 
39
 
 
40
        //
 
41
        // certificates and crls passed in the signature - this must happen before
 
42
        // s.getSignerInfos()
 
43
        //
 
44
        Store certs = s.getCertificates();
 
45
 
 
46
        //
 
47
        // SignerInfo blocks which contain the signatures
 
48
        //
 
49
        SignerInformationStore  signers = s.getSignerInfos();
 
50
 
 
51
        Collection              c = signers.getSigners();
 
52
        Iterator                it = c.iterator();
 
53
 
 
54
        //
 
55
        // check each signer
 
56
        //
 
57
        while (it.hasNext())
 
58
        {
 
59
            SignerInformation   signer = (SignerInformation)it.next();
 
60
            Collection          certCollection = certs.getMatches(signer.getSID());
 
61
 
 
62
            Iterator        certIt = certCollection.iterator();
 
63
            X509Certificate cert = new JcaX509CertificateConverter().setProvider(BC).getCertificate((X509CertificateHolder)certIt.next());
 
64
 
 
65
 
 
66
            //
 
67
            // verify that the sig is correct and that it was generated
 
68
            // when the certificate was current
 
69
            //
 
70
            if (signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(cert)))
 
71
            {
 
72
                System.out.println("signature verified");
 
73
            }
 
74
            else
 
75
            {
 
76
                System.out.println("signature failed!");
 
77
            }
 
78
        }
 
79
    }
 
80
 
 
81
    public static void main(
 
82
        String[]    args)
 
83
        throws Exception
 
84
    {
 
85
        //
 
86
        // Get a Session object with the default properties.
 
87
        //         
 
88
        Properties props = System.getProperties();
 
89
 
 
90
        Session session = Session.getDefaultInstance(props, null);
 
91
 
 
92
        MimeMessage msg = new MimeMessage(session, new SharedFileInputStream("signed.message"));
 
93
 
 
94
        //
 
95
        // make sure this was a multipart/signed message - there should be
 
96
        // two parts as we have one part for the content that was signed and
 
97
        // one part for the actual signature.
 
98
        //
 
99
        if (msg.isMimeType("multipart/signed"))
 
100
        {
 
101
            SMIMESignedParser             s = new SMIMESignedParser(
 
102
                                            (MimeMultipart)msg.getContent());
 
103
 
 
104
            System.out.println("Status:");
 
105
 
 
106
            verify(s);
 
107
        }
 
108
        else if (msg.isMimeType("application/pkcs7-mime"))
 
109
        {
 
110
            //
 
111
            // in this case the content is wrapped in the signature block.
 
112
            //
 
113
            SMIMESignedParser       s = new SMIMESignedParser(msg);
 
114
 
 
115
            System.out.println("Status:");
 
116
 
 
117
            verify(s);
 
118
        }
 
119
        else
 
120
        {
 
121
            System.err.println("Not a signed message!");
 
122
        }
 
123
    }
 
124
}