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

SMIMEGenerator

public class SMIMEGenerator extends Object
super class of the various generators.

Fields Summary
private static Map
BASE_CIPHER_NAMES
protected boolean
useBase64
protected String
encoding
Constructors Summary
protected SMIMEGenerator()
base constructor

  // default sets base64

           
     
    
    
Methods Summary
private javax.crypto.KeyGeneratorcreateKeyGenerator(java.lang.String algName, java.security.Provider provider)

        if (provider != null)
        {
            return KeyGenerator.getInstance(algName, provider);
        }
        else
        {
            return KeyGenerator.getInstance(algName);
        }
    
protected javax.crypto.KeyGeneratorcreateSymmetricKeyGenerator(java.lang.String encryptionOID, java.security.Provider provider)

        try
        {
            return createKeyGenerator(encryptionOID, provider);
        }
        catch (NoSuchAlgorithmException e)
        {
            try
            {
                String algName = (String)BASE_CIPHER_NAMES.get(encryptionOID);
                if (algName != null)
                {
                    return createKeyGenerator(algName, provider);
                }
            }
            catch (NoSuchAlgorithmException ex)
            {
                // ignore
            }
            if (provider != null)
            {
                return createSymmetricKeyGenerator(encryptionOID, null);
            }
            throw e;
        }
    
private voidextractHeaders(javax.mail.internet.MimeBodyPart content, javax.mail.internet.MimeMessage message)

        Enumeration e = message.getAllHeaders();

        while (e.hasMoreElements())
        {
            Header hdr =(Header)e.nextElement();

            content.setHeader(hdr.getName(), hdr.getValue());
        }
    
protected javax.mail.internet.MimeBodyPartmakeContentBodyPart(javax.mail.internet.MimeBodyPart content)
Make sure we have a valid content body part - setting the headers with defaults if neccessary.

        //
        // add the headers to the body part - if they are missing, in
        // the event they have already been set the content settings override
        // any defaults that might be set.
        //
        try
        {
            MimeMessage     msg = new MimeMessage((Session)null);

            Enumeration     e = content.getAllHeaders();

            msg.setDataHandler(content.getDataHandler());

            while (e.hasMoreElements())
            {
                Header  hdr =(Header)e.nextElement();

                msg.setHeader(hdr.getName(), hdr.getValue());
            }

            msg.saveChanges();

            //
            // we do this to make sure at least the default headers are
            // set in the body part.
            //
            e = msg.getAllHeaders();

            while (e.hasMoreElements())
            {
                Header  hdr =(Header)e.nextElement();

                if (Strings.toLowerCase(hdr.getName()).startsWith("content-"))
                {
                    content.setHeader(hdr.getName(), hdr.getValue());
                }
            }
        }
        catch (MessagingException e)
        {
            throw new SMIMEException("exception saving message state.", e);
        }

        return content;
    
protected javax.mail.internet.MimeBodyPartmakeContentBodyPart(javax.mail.internet.MimeMessage message)
extract an appropriate body part from the passed in MimeMessage

        MimeBodyPart    content = new MimeBodyPart();

        //
        // add the headers to the body part.
        //
        try
        {
            message.removeHeader("Message-Id");
            message.removeHeader("Mime-Version");

            // JavaMail has a habit of reparsing some content types, if the bodypart is
            // a multipart it might be signed, we rebuild the body part using the raw input stream for the message.
            try
            {
                if (message.getContent() instanceof Multipart)
                {
                    content.setContent(message.getRawInputStream(), message.getContentType());

                    extractHeaders(content, message);
                    
                    return content;
                }
            }
            catch (MessagingException e)
            {
                // fall back to usual method below
            }
       
            content.setContent(message.getContent(), message.getContentType());

            extractHeaders(content, message);
        }
        catch (MessagingException e)
        {
            throw new SMIMEException("exception saving message state.", e);
        }
        catch (IOException e)
        {
            throw new SMIMEException("exception getting message content.", e);
        }

        return content;
    
public voidsetContentTransferEncoding(java.lang.String encoding)
set the content-transfer-encoding for the signature.

        this.encoding = encoding;
        this.useBase64 = Strings.toLowerCase(encoding).equals("base64");