2014-10-10 19:44:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Jan Dittberner
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-10-12 14:23:08 +02:00
|
|
|
package info.dittberner.bcsmime_demo;
|
2014-10-10 19:44:24 +02:00
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
import org.bouncycastle.asn1.x500.X500Name;
|
2014-10-12 14:23:08 +02:00
|
|
|
import org.bouncycastle.asn1.x509.*;
|
|
|
|
import org.bouncycastle.cert.CertIOException;
|
2014-10-10 19:44:24 +02:00
|
|
|
import org.bouncycastle.cert.X509CertificateHolder;
|
|
|
|
import org.bouncycastle.cert.X509v3CertificateBuilder;
|
|
|
|
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
|
|
|
|
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
|
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
|
import org.bouncycastle.operator.ContentSigner;
|
2014-10-12 14:23:08 +02:00
|
|
|
import org.bouncycastle.operator.OperatorCreationException;
|
2014-10-10 19:44:24 +02:00
|
|
|
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
|
|
|
|
2014-10-12 14:23:08 +02:00
|
|
|
import javax.mail.BodyPart;
|
|
|
|
import javax.mail.Message.RecipientType;
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.Multipart;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeBodyPart;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.internet.MimeMultipart;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.KeyPairGenerator;
|
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.security.cert.Certificate;
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.util.Date;
|
|
|
|
|
2014-10-10 19:44:24 +02:00
|
|
|
/**
|
|
|
|
* Test Encryption and Decryption.
|
2014-10-12 14:23:08 +02:00
|
|
|
*
|
2014-10-10 19:44:24 +02:00
|
|
|
* @author Jan Dittberner <<a href=
|
|
|
|
* "mailto:jan.dittberner@t-systems.com>jan.dittberner@t-systems.com</a>
|
|
|
|
* & g t ;
|
|
|
|
*/
|
|
|
|
public class EncryptDecryptTest extends TestCase {
|
2014-10-12 14:23:08 +02:00
|
|
|
String[][] testEntries = new String[][]{
|
|
|
|
new String[]{"test1", "testrecpt1@example.org", "Test Recipient 1"},
|
|
|
|
new String[]{"test2", "testrecpt2@example.org", "Test Recipient 2"}
|
|
|
|
};
|
2014-10-10 19:44:24 +02:00
|
|
|
private KeyStore keystore;
|
|
|
|
|
2014-10-12 14:23:08 +02:00
|
|
|
private class KeyEntryData {
|
|
|
|
private final X509CertificateHolder certificateHolder;
|
|
|
|
KeyPair keyPair;
|
|
|
|
|
|
|
|
private KeyEntryData(KeyPairGenerator kpg, String address) throws CertIOException, OperatorCreationException {
|
|
|
|
this.keyPair = kpg.generateKeyPair();
|
|
|
|
|
|
|
|
X500Name issuer = new X500Name(
|
|
|
|
String.format("CN=Test Recipient,emailAddress=%s", address));
|
|
|
|
//noinspection UnnecessaryLocalVariable
|
|
|
|
X500Name subject = issuer;
|
|
|
|
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
|
|
|
|
issuer, BigInteger.valueOf(System.currentTimeMillis()),
|
|
|
|
new Date(System.currentTimeMillis() - 50000), new Date(
|
|
|
|
System.currentTimeMillis() + 50000), subject,
|
|
|
|
keyPair.getPublic());
|
|
|
|
certificateBuilder.addExtension(Extension.basicConstraints, true,
|
|
|
|
new BasicConstraints(true));
|
|
|
|
certificateBuilder.addExtension(Extension.keyUsage, true,
|
|
|
|
new KeyUsage(KeyUsage.digitalSignature
|
|
|
|
| KeyUsage.keyEncipherment));
|
|
|
|
certificateBuilder.addExtension(Extension.extendedKeyUsage, true,
|
|
|
|
new ExtendedKeyUsage(KeyPurposeId.id_kp_emailProtection));
|
|
|
|
certificateBuilder.addExtension(Extension.subjectAlternativeName,
|
|
|
|
false, new GeneralNames(new GeneralName(
|
|
|
|
GeneralName.rfc822Name, address)));
|
|
|
|
|
|
|
|
ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSA")
|
|
|
|
.build(keyPair.getPrivate());
|
|
|
|
this.certificateHolder = certificateBuilder.build(signer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Certificate getCertificate() throws CertificateException, CertIOException, OperatorCreationException {
|
|
|
|
return (new JcaX509CertificateConverter()).getCertificate(certificateHolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 19:44:24 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2014-10-12 14:23:08 +02:00
|
|
|
*
|
2014-10-10 19:44:24 +02:00
|
|
|
* @see junit.framework.TestCase#setUp()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.keystore == null) {
|
|
|
|
this.keystore = KeyStore.getInstance("JKS");
|
|
|
|
keystore.load(null, null);
|
|
|
|
|
|
|
|
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
|
|
|
|
kpg.initialize(2048);
|
|
|
|
|
2014-10-12 14:23:08 +02:00
|
|
|
for (String[] entry : testEntries) {
|
|
|
|
KeyEntryData keyEntryData = new KeyEntryData(kpg, entry[1]);
|
|
|
|
keystore.setKeyEntry(entry[0], keyEntryData.keyPair.getPrivate(), "changeit".toCharArray(), new Certificate[]{keyEntryData.getCertificate()});
|
|
|
|
}
|
2014-10-10 19:44:24 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test of {@link SMIMEEncrypt} and {@link SMIMEDecrypt}.
|
|
|
|
*/
|
|
|
|
public void testEncryptDecryptMail() throws Exception {
|
|
|
|
MimeMessage message = getNewMultipartMessage();
|
|
|
|
assertNotNull(message);
|
|
|
|
|
|
|
|
SMIMEEncrypt encrypt = new SMIMEEncrypt(keystore);
|
|
|
|
MimeMessage encrypted = encrypt.encryptMessage(message);
|
|
|
|
assertNotNull(encrypted);
|
|
|
|
encrypted.writeTo(System.err);
|
|
|
|
|
|
|
|
SMIMEDecrypt decrypt = new SMIMEDecrypt(keystore);
|
|
|
|
MimeMessage decrypted = decrypt.decryptMessage(encrypted);
|
|
|
|
assertNotNull(decrypted);
|
|
|
|
decrypted.writeTo(System.err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-12 14:23:08 +02:00
|
|
|
* Creates a new MimeMessage with one body part.
|
|
|
|
*
|
2014-10-10 19:44:24 +02:00
|
|
|
* @return MimeMessage instance
|
2014-10-12 14:23:08 +02:00
|
|
|
* @throws MessagingException on error creating the message
|
2014-10-10 19:44:24 +02:00
|
|
|
*/
|
|
|
|
private MimeMessage getNewMultipartMessage() throws MessagingException,
|
|
|
|
IOException {
|
|
|
|
MimeMessage message = new MimeMessage(Session.getDefaultInstance(System
|
|
|
|
.getProperties()));
|
|
|
|
message.setFrom(new InternetAddress("testsender@example.org",
|
|
|
|
"Test Sender"));
|
2014-10-12 14:23:08 +02:00
|
|
|
for (String[] entry : testEntries) {
|
|
|
|
message.addRecipient(RecipientType.TO, new InternetAddress(entry[1], entry[2]));
|
|
|
|
}
|
2014-10-10 19:44:24 +02:00
|
|
|
message.setSubject("Test subject");
|
|
|
|
Multipart multipart = new MimeMultipart();
|
2014-10-12 14:23:08 +02:00
|
|
|
BodyPart textPart = new MimeBodyPart();
|
|
|
|
textPart.setText("Das ist ein Text");
|
|
|
|
multipart.addBodyPart(textPart);
|
2014-10-10 19:44:24 +02:00
|
|
|
message.setContent(multipart);
|
|
|
|
return message;
|
|
|
|
}
|
2014-10-12 14:23:08 +02:00
|
|
|
}
|