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

SMIMESigned

public class SMIMESigned extends org.bouncycastle.cms.CMSSignedData
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 SMIMESigned(MimeMultipart message)
base constructor using a defaultContentTransferEncoding of 7bit

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);
    
        super(new CMSProcessableBodyPartInbound(message.getBodyPart(0)), getInputStream(message.getBodyPart(1)));

        this.message = message;
        this.content = (MimeBodyPart)message.getBodyPart(0);
    
public SMIMESigned(MimeMultipart message, String defaultContentTransferEncoding)
base constructor with settable contentTransferEncoding

param
message the signed message
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.

        super(new CMSProcessableBodyPartInbound(message.getBodyPart(0), defaultContentTransferEncoding), getInputStream(message.getBodyPart(1)));

        this.message = message;
        this.content = (MimeBodyPart)message.getBodyPart(0);
    
public SMIMESigned(Part message)
base constructor for a signed message with encapsulated content.

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;

        CMSProcessable  cont = this.getSignedContent();

        if (cont != null)
        {
            byte[]  contBytes = (byte[])cont.getContent();
    
            this.content = SMIMEUtil.toMimeBodyPart(contBytes);
        }
    
Methods Summary
public javax.mail.internet.MimeBodyPartgetContent()
return the content that was signed.

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

param
session
return
a MimeMessage holding the content.
throws
MessagingException

        Object content = getSignedContent().getContent();
        byte[] contentBytes = null;

        if (content instanceof byte[])
        {
            contentBytes = (byte[])content;
        }
        else if (content instanceof MimePart)
        {
            MimePart part = (MimePart)content;
            ByteArrayOutputStream out;
            
            if (part.getSize() > 0)
            {
                out = new ByteArrayOutputStream(part.getSize());
            }
            else
            {
                out = new ByteArrayOutputStream();
            }
            
            part.writeTo(out);
            contentBytes = out.toByteArray();
        }
        else
        {
            String type = "<null>";
            if (content != null)
            {
                type = content.getClass().getName();
            }

            throw new MessagingException(
                "Could not transfrom content of type "
                    + type
                    + " into MimeMessage.");
        }

        if (contentBytes != null)
        {
            ByteArrayInputStream in = new ByteArrayInputStream(contentBytes);

            return new MimeMessage(session, in);
        }

        return null;
    
public java.lang.ObjectgetContentWithSignature()
return the content that was signed - 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);
        }