FileDocCategorySizeDatePackage
SMIMESignedParser.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)11240Wed Oct 01 10:55:30 BST 2008org.bouncycastle.mail.smime

SMIMESignedParser

public class SMIMESignedParser extends org.bouncycastle.cms.CMSSignedDataParser
general class for handling a pkcs7-signature message.

A simple example of usage - note, in the example below the validity of the certificate isn't verified, just the fact that one of the certs matches the given signer...

CertStore certs = s.getCertificates("Collection", "BC");
SignerInformationStore signers = s.getSignerInfos();
Collection c = signers.getSigners();
Iterator it = c.iterator();

while (it.hasNext())
{
SignerInformation signer = (SignerInformation)it.next();
Collection certCollection = certs.getCertificates(signer.getSID());

Iterator certIt = certCollection.iterator();
X509Certificate cert = (X509Certificate)certIt.next();

if (signer.verify(cert.getPublicKey()))
{
verified++;
}
}

Note: if you are using this class with AS2 or some other protocol that does not use 7bit as the default content transfer encoding you will need to use the constructor that allows you to specify the default content transfer encoding, such as "binary".

Fields Summary
Object
message
MimeBodyPart
content
Constructors Summary
public SMIMESignedParser(MimeMultipart message)
base constructor using a defaultContentTransferEncoding of 7bit. A temporary backing file will be created for the signed data.

param
message signed message with signature.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
CMSException if some other problem occurs.

        MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();

        mc.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
        mc.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
        mc.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
        mc.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
        mc.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
        
        CommandMap.setDefaultCommandMap(mc);
    
        this(message, getTmpFile());
    
public SMIMESignedParser(MimeMultipart message, File backingFile)
base constructor using a defaultContentTransferEncoding of 7bit and a specified backing file.

param
message signed message with signature.
param
backingFile the temporary file to use to back the signed data.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
CMSException if some other problem occurs.

        this(message, "7bit", backingFile);
    
public SMIMESignedParser(MimeMultipart message, String defaultContentTransferEncoding)
base constructor with settable contentTransferEncoding. A temporary backing file will be created to contain the signed data.

param
message the signed message with signature.
param
defaultContentTransferEncoding new default to use.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
CMSException if some other problem occurs.

        this(message, defaultContentTransferEncoding, getTmpFile());
    
public SMIMESignedParser(MimeMultipart message, String defaultContentTransferEncoding, File backingFile)
base constructor with settable contentTransferEncoding and a specified backing file.

param
message the signed message with signature.
param
defaultContentTransferEncoding new default to use.
param
backingFile the temporary file to use to back the signed data.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
CMSException if some other problem occurs.

        super(getSignedInputStream(message.getBodyPart(0), defaultContentTransferEncoding, backingFile), getInputStream(message.getBodyPart(1)));

        this.message = message;
        this.content = (MimeBodyPart)message.getBodyPart(0);

        drainContent();
    
public SMIMESignedParser(Part message)
base constructor for a signed message with encapsulated content.

Note: in this case the encapsulated MimeBody part will only be suitable for a single writeTo - once writeTo has been called the file containing the body part will be deleted. If writeTo is not called the file will be left in the temp directory.

param
message the message containing the encapsulated signed data.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
SMIMEException if the body part encapsulated in the message cannot be extracted.
exception
CMSException if some other problem occurs.

        super(getInputStream(message));

        this.message = message;

        CMSTypedStream  cont = this.getSignedContent();

        if (cont != null)
        {
            this.content = SMIMEUtil.toWriteOnceBodyPart(cont);
        }
    
public SMIMESignedParser(Part message, File file)
Constructor for a signed message with encapsulated content. The encapsulated content, if it exists, is written to the file represented by the File object passed in.

param
message the Part containing the signed content.
param
file the file the encapsulated part is to be written to after it has been decoded.
exception
MessagingException on an error extracting the signature or otherwise processing the message.
exception
SMIMEException if the body part encapsulated in the message cannot be extracted.
exception
CMSException if some other problem occurs.

        super(getInputStream(message));

        this.message = message;

        CMSTypedStream  cont = this.getSignedContent();

        if (cont != null)
        {
            this.content = SMIMEUtil.toMimeBodyPart(cont, file);
        }
    
Methods Summary
private voiddrainContent()

        try
        {
            this.getSignedContent().drain();
        }
        catch (IOException e)
        {
            throw new CMSException("unable to read content for verification: " + e, e);
        }
    
public javax.mail.internet.MimeBodyPartgetContent()
return the content that was signed.

return
the signed body part in this message.

        return content;
    
public javax.mail.internet.MimeMessagegetContentAsMimeMessage(javax.mail.Session session)
Return the content that was signed as a mime message.

param
session the session to base the MimeMessage around.
return
a MimeMessage holding the content.
throws
MessagingException if there is an issue creating the MimeMessage.
throws
IOException if there is an issue reading the content.

        if (message instanceof MimeMultipart)
        {
            BodyPart    bp = ((MimeMultipart)message).getBodyPart(0);
            return new MimeMessage(session, bp.getInputStream());
        }
        else
        {
            return new MimeMessage(session, getSignedContent().getContentStream());
        }
    
public java.lang.ObjectgetContentWithSignature()
return the content that was signed with its signature attached.

return
depending on whether this was unencapsulated or not it will return a MimeMultipart or a MimeBodyPart

        return message;
    
private static java.io.InputStreamgetInputStream(javax.mail.Part bodyPart)

        try
        {
            if (bodyPart.isMimeType("multipart/signed"))
            {
                throw new MessagingException("attempt to create signed data object from multipart content - use MimeMultipart constructor.");
            }
            
            return bodyPart.getInputStream();
        }
        catch (IOException e)
        {
            throw new MessagingException("can't extract input stream: " + e);
        }
    
private static org.bouncycastle.cms.CMSTypedStreamgetSignedInputStream(javax.mail.BodyPart bodyPart, java.lang.String defaultContentTransferEncoding, java.io.File backingFile)

        try
        {
            OutputStream   out = new BufferedOutputStream(new FileOutputStream(backingFile));

            SMIMEUtil.outputBodyPart(out, bodyPart, defaultContentTransferEncoding);

            out.close();

            InputStream in = new TemporaryFileInputStream(backingFile);

            return new CMSTypedStream(in);
        }
        catch (IOException e)
        {
            throw new MessagingException("can't extract input stream: " + e);
        }
    
private static java.io.FilegetTmpFile()

        try
        {
            return File.createTempFile("bcMail", ".mime");
        }
        catch (IOException e)
        {
            throw new MessagingException("can't extract input stream: " + e);
        }