Example usage for javax.mail.internet MimeMessage isMimeType

List of usage examples for javax.mail.internet MimeMessage isMimeType

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage isMimeType.

Prototype

@Override
public boolean isMimeType(String mimeType) throws MessagingException 

Source Link

Document

Is this Part of the specified MIME type?

Usage

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testOpaqueSignMultipleSigners() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    PrivateKeyEntry md5Entry = (PrivateKeyEntry) keyStore.getEntry("md5Hash", passwd);

    builder.addSigner(md5Entry.getPrivateKey(), (X509Certificate) md5Entry.getCertificate(),
            SMIMESigningAlgorithm.MD5WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(md5Entry.getCertificateChain()));

    builder.sign(SMIMESignMode.OPAQUE);/* w  w w .  j  a va  2s.  c o m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testOpaqueSignMultipleSigners.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);

    File opensslOutputFile = new File(tempDir, "testOpaqueSignMultipleSigners-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    // the message should contain the signed from, to and subject
    checkForEmbeddedHeaders(newMessage);
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testClearSignNoFromProtectedHeader() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "subject", "to");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);//  ww  w .j  a  va2 s .c  o  m

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testClearSignNoFromProtectedHeader.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);

    File opensslOutputFile = new File(tempDir, "testClearSignNoFromProtectedHeader-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    // the message should contain the subject
    assertEquals("normal message with attachment", newMessage.getHeader("subject", ","));
    assertEquals("<test@example.com>", message.getHeader("to", ","));
    assertNull(newMessage.getHeader("from"));
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testClearSignDefaultProtectedHeader() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message);

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.CLEAR);//from www  .j a  v  a 2  s  .  com

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testClearSignDefaultProtectedHeader.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);

    File opensslOutputFile = new File(tempDir, "testClearSignDefaultProtectedHeader-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    assertEquals("normal message with attachment", newMessage.getHeader("subject", ","));
    assertEquals("<test@example.com>", message.getHeader("to", ","));
    assertNull(newMessage.getHeader("from"));
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptSignedQuotedPrintableSoftBreaks() throws Exception {
    MimeMessage message = loadMessage("qp-soft-breaks-signed.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message);

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptSignedQuotedPrintableSoftBreaks.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFileSigned = new File(tempDir,
            "testEncryptSignedQuotedPrintableSoftBreaks-openssl-signed.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/signed"));

    File opensslOutputFile = new File(tempDir, "testEncryptSignedQuotedPrintableSoftBreaks-openssl.eml");

    verifyMessage(opensslOutputFileSigned, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptIssuerSerialRecipientId() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptIssuerSerialRecipientId.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);/*from  ww w.j a  v a  2s. c o m*/

    File opensslOutputFileSigned = new File(tempDir, "testEncryptIssuerSerialRecipientId-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForEmbeddedHeaders(newMessage);
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptMultipleRecipients() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    Enumeration<String> aliases = keyStore.aliases();

    int i = 15;//from  w w  w. j  a v a2  s .co m

    while (aliases.hasMoreElements() && i != 0) {
        String alias = aliases.nextElement();

        if (keyStore.isKeyEntry(alias)) {
            X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);

            builder.addRecipient(certificate, SMIMERecipientMode.ISSUER_SERIAL);

            i--;
        }
    }

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptMultipleRecipients.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);

    File opensslOutputFileSigned = new File(tempDir, "testEncryptMultipleRecipients-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForEmbeddedHeaders(newMessage);
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptIssuerSerialRecipientIdAES() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.AES128_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptIssuerSerialRecipientIdAES.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);/*from w w  w.  j  av  a2  s  .c om*/

    File opensslOutputFileSigned = new File(tempDir, "testEncryptIssuerSerialRecipientIdAES-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForEmbeddedHeaders(newMessage);
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptIssuerSerialRecipientIdRC2() throws Exception {
    MimeMessage message = loadMessage("normal-message-with-attach.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addRecipient(encryptionCertificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.RC2_CBC);

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testEncryptIssuerSerialRecipientIdRC2.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.Type.ENCRYPTED, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForSourceHeaders(newMessage);//from www.j  ava 2  s  .c  om

    File opensslOutputFileSigned = new File(tempDir, "testEncryptIssuerSerialRecipientIdRC2-openssl.eml");

    decryptMessage(file, privateKeyEntry.getPrivateKey(), opensslOutputFileSigned);

    newMessage = MailUtils.loadMessage(opensslOutputFileSigned);

    assertTrue(newMessage.isMimeType("multipart/mixed"));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    checkForEmbeddedHeaders(newMessage);
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testOpaqueSignSimpleTextMessage() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.OPAQUE);/*from   ww  w .ja  va 2s.  co m*/

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "test-simple-text-message-opaque-signed.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "test-simple-text-message-opaque-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testOpaqueSignDeprecatedHeaders() throws Exception {
    MimeMessage message = loadMessage("simple-text-message.eml");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    builder.setUseDeprecatedContentTypes(true);

    builder.addSigner(privateKeyEntry.getPrivateKey(), (X509Certificate) privateKeyEntry.getCertificate(),
            SMIMESigningAlgorithm.SHA1WITHRSA);

    builder.addCertificates(CertificateUtils.getX509Certificates(privateKeyEntry.getCertificateChain()));

    builder.sign(SMIMESignMode.OPAQUE);//  ww  w .  j av  a  2 s .  c  o  m

    MimeMessage newMessage = builder.buildMessage();

    File file = new File(tempDir, "testOpaqueSignDeprecatedHeaders.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    assertEquals(SMIMEHeader.DEPRECATED_ENCAPSULATED_SIGNED_CONTENT_TYPE, newMessage.getContentType());

    assertEquals(SMIMEHeader.Type.OPAQUE_SIGNED, SMIMEHeader.getSMIMEContentType(newMessage));

    File opensslOutputFile = new File(tempDir, "testOpaqueSignDeprecatedHeaders-openssl.eml");

    verifyMessage(file, rootCertificate, opensslOutputFile);

    newMessage = MailUtils.loadMessage(opensslOutputFile);

    assertTrue(newMessage.isMimeType("text/plain"));

    assertEquals("test@example.com", newMessage.getHeader("from", ","));
    assertEquals("test@example.com", newMessage.getHeader("to", ","));
    assertEquals("test simple message", newMessage.getHeader("subject", ","));

    assertEquals(SMIMEHeader.Type.NO_SMIME, SMIMEHeader.getSMIMEContentType(newMessage));

    String content = (String) newMessage.getContent();

    assertEquals("test", content.trim());
}