001: package org.bouncycastle.mail.smime.examples;
002:
003: import java.io.FileInputStream;
004: import java.security.cert.CertStore;
005: import java.security.cert.X509Certificate;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.Properties;
009:
010: import javax.mail.BodyPart;
011: import javax.mail.Multipart;
012: import javax.mail.Session;
013: import javax.mail.internet.MimeBodyPart;
014: import javax.mail.internet.MimeMessage;
015: import javax.mail.internet.MimeMultipart;
016:
017: import org.bouncycastle.cms.SignerInformation;
018: import org.bouncycastle.cms.SignerInformationStore;
019: import org.bouncycastle.mail.smime.SMIMESigned;
020:
021: /**
022: * a simple example that reads a basic SMIME signed mail file.
023: */
024: public class ReadSignedMail {
025: /**
026: * verify the signature (assuming the cert is contained in the message)
027: */
028: private static void verify(SMIMESigned s) throws Exception {
029: //
030: // extract the information to verify the signatures.
031: //
032:
033: //
034: // certificates and crls passed in the signature
035: //
036: CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
037:
038: //
039: // SignerInfo blocks which contain the signatures
040: //
041: SignerInformationStore signers = s.getSignerInfos();
042:
043: Collection c = signers.getSigners();
044: Iterator it = c.iterator();
045:
046: //
047: // check each signer
048: //
049: while (it.hasNext()) {
050: SignerInformation signer = (SignerInformation) it.next();
051: Collection certCollection = certs.getCertificates(signer
052: .getSID());
053:
054: Iterator certIt = certCollection.iterator();
055: X509Certificate cert = (X509Certificate) certIt.next();
056:
057: //
058: // verify that the sig is correct and that it was generated
059: // when the certificate was current
060: //
061: if (signer.verify(cert, "BC")) {
062: System.out.println("signature verified");
063: } else {
064: System.out.println("signature failed!");
065: }
066: }
067: }
068:
069: public static void main(String[] args) throws Exception {
070: //
071: // Get a Session object with the default properties.
072: //
073: Properties props = System.getProperties();
074:
075: Session session = Session.getDefaultInstance(props, null);
076:
077: MimeMessage msg = new MimeMessage(session, new FileInputStream(
078: "signed.message"));
079:
080: //
081: // make sure this was a multipart/signed message - there should be
082: // two parts as we have one part for the content that was signed and
083: // one part for the actual signature.
084: //
085: if (msg.isMimeType("multipart/signed")) {
086: SMIMESigned s = new SMIMESigned((MimeMultipart) msg
087: .getContent());
088:
089: //
090: // extract the content
091: //
092: MimeBodyPart content = s.getContent();
093:
094: System.out.println("Content:");
095:
096: Object cont = content.getContent();
097:
098: if (cont instanceof String) {
099: System.out.println((String) cont);
100: } else if (cont instanceof Multipart) {
101: Multipart mp = (Multipart) cont;
102: int count = mp.getCount();
103: for (int i = 0; i < count; i++) {
104: BodyPart m = mp.getBodyPart(i);
105: Object part = m.getContent();
106:
107: System.out.println("Part " + i);
108: System.out.println("---------------------------");
109:
110: if (part instanceof String) {
111: System.out.println((String) part);
112: } else {
113: System.out.println("can't print...");
114: }
115: }
116: }
117:
118: System.out.println("Status:");
119:
120: verify(s);
121: } else if (msg.isMimeType("application/pkcs7-mime")
122: || msg.isMimeType("application/x-pkcs7-mime")) {
123: //
124: // in this case the content is wrapped in the signature block.
125: //
126: SMIMESigned s = new SMIMESigned(msg);
127:
128: //
129: // extract the content
130: //
131: MimeBodyPart content = s.getContent();
132:
133: System.out.println("Content:");
134:
135: Object cont = content.getContent();
136:
137: if (cont instanceof String) {
138: System.out.println((String) cont);
139: }
140:
141: System.out.println("Status:");
142:
143: verify(s);
144: } else {
145: System.err.println("Not a signed message!");
146: }
147: }
148: }
|