S/MIME
S/MIME (Secure / Multipurpose Internet Mail Extensions) is a standard for public key encryption and signing of e-mail encapsulated in MIME.
Java Libraries
There are several Java libraries for S/MIME encryption:
ISNetworks S/MIME (link did not work last time I was trying to locate it),
CMS-S/MIME,
JSMIME,
JavaMail-Crypto etc. But JavaMail-Crypto library is the easiest in use with
Java Mail. It uses
Bouncy Castle libraries (the
bcprov-jdk14-139.jar (BouncyCastle JCE provider) and the
bcmail-jdk14-139.jar (BouncyCastle S/MIME implementation) files).
Code Examples for Encryption and Signing
How to encrypt email message using JavaMail-Crypto example:
public MimeMessage encrypt(Session session, MimeMessage mimeMessage) throws Exception {
// Getting of the S/MIME EncryptionUtilities.
EncryptionUtils encUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME);
// Loading of the S/MIME keystore from the file (stored as resource).
char[] keystorePass = "keystore pass".toCharArray();
EncryptionKeyManager encKeyManager = encUtils.createKeyManager();
encKeyManager.loadPublicKeystore(
getClass().getResourceAsStream("/keystore.p12"),
keystorePass);
// Getting of the S/MIME public key for encryption.
Key publicKey = encKeyManager.getPublicKey("Key Alias");
// Encrypting the message.
return encUtils.encryptMessage(session, mimeMessage, publicKey);
}
How to sign email message using JavaMail-Crypto example:
public MimeMessage sign(Session session, MimeMessage mimeMessage) throws Exception {
// Getting of the S/MIME EncryptionUtilities.
EncryptionUtils encUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME);
// Loading of the S/MIME keystore from the file (stored as resource).
char[] keystorePass = "keystore pass".toCharArray();
EncryptionKeyManager encKeyManager = encUtils.createKeyManager();
encKeyManager.loadPrivateKeystore(
getClass().getResourceAsStream("/keystore.p12"), keystorePass);
// Getting of the S/MIME private key for signing.
Key privateKey = encKeyManager.getPrivateKey("Key Alias", keystorePass);
// Signing the message.
return encUtils.signMessage(session, mimeMessage, privateKey);
}
Source Code
You can download source code from
here.
Troubleshooting
To run this code you will need to install
Unlimited Strength Jurisdiction Policy Files for your JDK: http://java.sun.com/j2se/1.4.2/download.html. If it is not installed you will have one of the following exceptions:
"java.lang.SecurityException: Unsupported keysize or algorithm parameters"
or
"java.security.InvalidKeyException: Illegal key size"
Email Client Setup
To read email messages encrypted with S/MIME encryption standard you will need to import your PKCS12 certificate into the email client you use. If you use
Mozilla Thunderbird email client you should do following:
Tools -> Options -> Advanced -> Certificates -> View Certificates -> Your Certificates -> Import
and select your keystore.p12 PKCS12 certificate file. Use your keystore password to import PKCS12 certificate.
After performing this steps you will be able to read messages encrypted by your certificate.
Certificate Generation
PKCS12, Personal Information Exchange Syntax Standard, certificates can be used for things such as email signing and file signing. They are different from other certificates in that rather than being only the public or private certificate, they are a combination of both plus the root certificate. This means the person they are made for only has to worry with one file.
Certificate generation using OpenSSL
To generate PKCS12 certificate using
OpenSSL follow the steps from the "
Creating PKCS12 Certificates" article.
Certificate generation using Thawte
There is ability to generate certificate using
Thawte service:
https://www.thawte.com/secure-email/personal-email-certificates/index.html?click=main-nav-products-email