1
package org.bouncycastle.mail.smime.examples;
3
import java.security.cert.X509Certificate;
4
import java.util.Collection;
5
import java.util.Iterator;
6
import java.util.Properties;
8
import javax.mail.Session;
9
import javax.mail.internet.MimeMessage;
10
import javax.mail.internet.MimeMultipart;
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;
23
* a simple example that reads a basic SMIME signed mail file.
25
public class ReadLargeSignedMail
27
private static final String BC = BouncyCastleProvider.PROVIDER_NAME;
30
* verify the signature (assuming the cert is contained in the message)
32
private static void verify(
37
// extract the information to verify the signatures.
41
// certificates and crls passed in the signature - this must happen before
44
Store certs = s.getCertificates();
47
// SignerInfo blocks which contain the signatures
49
SignerInformationStore signers = s.getSignerInfos();
51
Collection c = signers.getSigners();
52
Iterator it = c.iterator();
59
SignerInformation signer = (SignerInformation)it.next();
60
Collection certCollection = certs.getMatches(signer.getSID());
62
Iterator certIt = certCollection.iterator();
63
X509Certificate cert = new JcaX509CertificateConverter().setProvider(BC).getCertificate((X509CertificateHolder)certIt.next());
67
// verify that the sig is correct and that it was generated
68
// when the certificate was current
70
if (signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(cert)))
72
System.out.println("signature verified");
76
System.out.println("signature failed!");
81
public static void main(
86
// Get a Session object with the default properties.
88
Properties props = System.getProperties();
90
Session session = Session.getDefaultInstance(props, null);
92
MimeMessage msg = new MimeMessage(session, new SharedFileInputStream("signed.message"));
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.
99
if (msg.isMimeType("multipart/signed"))
101
SMIMESignedParser s = new SMIMESignedParser(
102
(MimeMultipart)msg.getContent());
104
System.out.println("Status:");
108
else if (msg.isMimeType("application/pkcs7-mime"))
111
// in this case the content is wrapped in the signature block.
113
SMIMESignedParser s = new SMIMESignedParser(msg);
115
System.out.println("Status:");
121
System.err.println("Not a signed message!");