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

SMIMEUtil

public class SMIMEUtil extends Object

Fields Summary
private static final int
BUF_SIZE
Constructors Summary
Methods Summary
public static org.bouncycastle.asn1.cms.IssuerAndSerialNumbercreateIssuerAndSerialNumberFor(java.security.cert.X509Certificate cert)
Return a CMS IssuerAndSerialNumber structure for the passed in X.509 certificate.

param
cert the X.509 certificate to get the issuer and serial number for.
return
an IssuerAndSerialNumber structure representing the certificate.

        try
        {
            return new IssuerAndSerialNumber(PrincipalUtil.getIssuerX509Principal(cert), cert.getSerialNumber());        
        }
        catch (Exception e)
        {
            throw new CertificateParsingException("exception extracting issuer and serial number: " + e);
        }
    
public static java.security.ProvidergetProvider(java.lang.String providerName)

        if (providerName != null)
        {
            Provider prov = Security.getProvider(providerName);

            if (prov != null)
            {
                return prov;
            }

            throw new NoSuchProviderException("provider " + providerName + " not found.");
        }

        return null;
    
static booleanisCanonicalisationRequired(javax.mail.internet.MimeBodyPart bodyPart, java.lang.String defaultContentTransferEncoding)

    
      
           
          
         
    
        String[]        cte = bodyPart.getHeader("Content-Transfer-Encoding");
        String          contentTransferEncoding;

        if (cte == null)
        {
            contentTransferEncoding = defaultContentTransferEncoding;
        }
        else
        {
            contentTransferEncoding = cte[0];
        }

        return !contentTransferEncoding.equalsIgnoreCase("binary");
    
static voidoutputBodyPart(java.io.OutputStream out, javax.mail.BodyPart bodyPart, java.lang.String defaultContentTransferEncoding)

        if (bodyPart instanceof MimeBodyPart)
        {
            MimeBodyPart    mimePart = (MimeBodyPart)bodyPart;
            String[]        cte = mimePart.getHeader("Content-Transfer-Encoding");
            String          contentTransferEncoding;

            if (mimePart.getContent() instanceof MimeMultipart)
            {
                MimeMultipart mp = (MimeMultipart)bodyPart.getContent();
                ContentType contentType = new ContentType(mp.getContentType());
                String boundary = "--" + contentType.getParameter("boundary");

                SMIMEUtil.LineOutputStream lOut = new SMIMEUtil.LineOutputStream(out);

                Enumeration headers = mimePart.getAllHeaderLines();
                while (headers.hasMoreElements())
                {
                    String header = (String)headers.nextElement();
                    lOut.writeln(header);
                }

                lOut.writeln();      // CRLF separator

                outputPreamble(lOut, mimePart, boundary);

                for (int i = 0; i < mp.getCount(); i++)
                {
                    lOut.writeln(boundary);
                    BodyPart part = mp.getBodyPart(i);
                    outputBodyPart(out, part, defaultContentTransferEncoding);
                    if (!(part.getContent() instanceof MimeMultipart))
                    {
                        lOut.writeln();       // CRLF terminator needed
                    }
                    else
                    {
                        outputPostamble(lOut, mimePart, boundary, part);
                    }
                }

                lOut.writeln(boundary + "--");

                return;
            }

            if (cte == null)
            {
                contentTransferEncoding = defaultContentTransferEncoding;
            }
            else
            {
                contentTransferEncoding = cte[0];
            }

            if (!contentTransferEncoding.equalsIgnoreCase("base64")
                   && !contentTransferEncoding.equalsIgnoreCase("quoted-printable"))
            {
                if (!contentTransferEncoding.equalsIgnoreCase("binary"))
                {
                    out = new CRLFOutputStream(out);
                }
                bodyPart.writeTo(out);
                out.flush();
                return;
            }

            boolean base64 = contentTransferEncoding.equalsIgnoreCase("base64");

            //
            // Write headers
            //
            LineOutputStream outLine = new LineOutputStream(out);
            for (Enumeration e = mimePart.getAllHeaderLines(); e.hasMoreElements();) 
            {
                String header = (String)e.nextElement();
                outLine.writeln(header);
            }

            outLine.writeln();
            outLine.flush();

            //
            // Write raw content, performing canonicalization
            //
            InputStream inRaw = mimePart.getRawInputStream();
            OutputStream outCRLF;

            if (base64)
            {
                outCRLF = new Base64CRLFOutputStream(out);
            }
            else
            {
                outCRLF = new CRLFOutputStream(out);
            }

            byte[]      buf = new byte[BUF_SIZE];

            int len;
            while ((len = inRaw.read(buf, 0, buf.length)) > 0)
            {

                outCRLF.write(buf, 0, len);
            }

            outCRLF.flush();
        }
        else
        {
            if (!defaultContentTransferEncoding.equalsIgnoreCase("binary"))
            {
                out = new CRLFOutputStream(out);
            }

            bodyPart.writeTo(out);

            out.flush();
        }
    
static voidoutputPostamble(org.bouncycastle.mail.smime.SMIMEUtil$LineOutputStream lOut, javax.mail.BodyPart parent, java.lang.String parentBoundary, javax.mail.BodyPart part)

        InputStream in;

        try
        {
            in = ((MimeBodyPart)parent).getRawInputStream();
        }
        catch (MessagingException e)
        {
            return;   // no underlying content rely on default generation
        }


        MimeMultipart multipart = (MimeMultipart)part.getContent();
        ContentType contentType = new ContentType(multipart.getContentType());
        String boundary = "--" + contentType.getParameter("boundary");
        int count = multipart.getCount() + 1;
        String line;
        while (count != 0 && (line = readLine(in)) != null)
        {
            if (line.startsWith(boundary))
            {
                count--;
            }
        }

        while ((line = readLine(in)) != null)
        {
            if (line.startsWith(parentBoundary))
            {
                break;
            }
            lOut.writeln(line);
        }

        in.close();
    
static voidoutputPreamble(org.bouncycastle.mail.smime.SMIMEUtil$LineOutputStream lOut, javax.mail.internet.MimeBodyPart part, java.lang.String boundary)
internal preamble is generally included in signatures, while this is technically wrong, if we find internal preamble we include it by default.

        InputStream in;

        try
        {
            in = part.getRawInputStream();
        }
        catch (MessagingException e)
        {
            return;   // no underlying content rely on default generation
        }

        String line;

        while ((line = readLine(in)) != null)
        {
            if (line.equals(boundary))
            {
                break;
            }

            lOut.writeln(line);
        }

        in.close();

        if (line == null)
        {
            throw new MessagingException("no boundary found");
        }
    
private static java.lang.StringreadLine(java.io.InputStream in)

        StringBuffer b = new StringBuffer();

        int ch;
        while ((ch = in.read()) >= 0 && ch != '\n")
        {
            if (ch != '\r")
            {
                b.append((char)ch);
            }
        }

        if (ch < 0)
        {
            return null;
        }
        
        return b.toString();
    
public static org.bouncycastle.mail.smime.util.FileBackedMimeBodyParttoMimeBodyPart(org.bouncycastle.cms.CMSTypedStream content)
return a file backed MimeBodyPart described in {@link CMSTypedStream} content.

        try
        {
            return toMimeBodyPart(content, File.createTempFile("bcMail", ".mime"));
        }
        catch (IOException e)
        {
            throw new SMIMEException("IOException creating tmp file:" + e.getMessage(), e);
        }
    
public static org.bouncycastle.mail.smime.util.FileBackedMimeBodyParttoMimeBodyPart(org.bouncycastle.cms.CMSTypedStream content, java.io.File file)
Return a file based MimeBodyPart represented by content and backed by the file represented by file.

param
content content stream containing body part.
param
file file to store the decoded body part in.
return
the decoded body part.
throws
SMIMEException

        try
        {
            return new FileBackedMimeBodyPart(content.getContentStream(), file);
        }
        catch (IOException e)
        {
            throw new SMIMEException("can't save content to file: " + e, e);
        }
        catch (MessagingException e)
        {
            throw new SMIMEException("can't create part: " + e, e);
        }
    
public static javax.mail.internet.MimeBodyParttoMimeBodyPart(byte[] content)
return the MimeBodyPart described in the raw bytes provided in content

        return toMimeBodyPart(new ByteArrayInputStream(content));
    
public static javax.mail.internet.MimeBodyParttoMimeBodyPart(java.io.InputStream content)
return the MimeBodyPart described in the input stream content

        try
        {
            return new MimeBodyPart(content);
        }
        catch (MessagingException e)
        {
            throw new SMIMEException("exception creating body part.", e);
        }
    
static org.bouncycastle.mail.smime.util.FileBackedMimeBodyParttoWriteOnceBodyPart(org.bouncycastle.cms.CMSTypedStream content)

        try
        {
            return new WriteOnceFileBackedMimeBodyPart(content.getContentStream(), File.createTempFile("bcMail", ".mime"));
        }
        catch (IOException e)
        {
            throw new SMIMEException("IOException creating tmp file:" + e.getMessage(), e);
        }
        catch (MessagingException e)
        {
            throw new SMIMEException("can't create part: " + e, e);
        }