public class EncryptedMailDemo extends PKCS11Demo
SecurityProvider
feature of the CMS implementation of the IAIK-CMS toolkit.
To run this demo the following packages are required:
iaik_cms.jar
iaik_jce(_full).jar
(IAIK-JCE Core Crypto Library).
iaikPkcs11Provider.jar
(IAIK PKCS#11 Provider).
iaikPkcs11Wrapper.jar
(IAIK PKCS#11 Wrapper).
pkcs11wrapper.dll
for Windows, libpkcs11wrapper.so
for Unix); contained in the IAIK PKCS#11 Wrapper library.
iaik_eccelerate.jar
(IAIK ECC Library, if you want to use Elliptic Curve Cryptography).
iaik_cms.jar
, iaik_cms_demo.jar
, iaik_jce(full).jar
,
iaikPkcs11Wrapper.jar
and iaikPkcs11Provider.jar
(and
iaik_eccelerate.jar
, mail.jar
, activation.jar
) have to be put
into the classpath, the shared library (pkcs11wrapper.dll
or libpkcs11wrapper.so
)
has to be in your system library search path or in your VM library path, e.g. (on Windows,
assuming that all jar files are located in a lib sub-directory and the dll is in a lib/win64
sub-directory):
java -Djava.library.path=lib/win64 -cp lib/iaik_jce.jar;lib/iaikPkcs11Wrapper.jar;lib/iaikPkcs11Provider.jar;lib/iaik_cms.jar;lib/iaik_cms_demo.jar;lib/mail.jar;lib/activation.jar demo.pkcs11.EncryptedMailDemo <pkcs11Module>.dll
Modifier and Type | Field and Description |
---|---|
protected iaik.x509.X509Certificate |
certificate_
The certificate of the recipient.
|
protected java.security.PrivateKey |
privateKey_
The private key of the recipient.
|
protected java.lang.String |
recipient_
The email address of the recipient.
|
protected java.lang.String |
sender_
The email address of the sender.
|
iaikPkcs11Provider_, iaikSoftwareProvider_, moduleName_, tokenKeyStore_, userPin_
Constructor and Description |
---|
EncryptedMailDemo(java.lang.String moduleName,
char[] userPin)
Creates a EncryptedMailDemo object for the given module name.
|
Modifier and Type | Method and Description |
---|---|
protected jakarta.mail.internet.MimeMessage |
createEncryptedMessage(jakarta.mail.Session session,
jakarta.activation.DataHandler dataHandler)
Creates an encrypted message.
|
void |
getKeyAndCertificate()
This method gets the key store of the PKCS#11 provider and searches for a
certificate and corresponding private key entry that can en/decrypt the data.
|
static void |
main(java.lang.String[] args)
This is the main method that is called by the JVM during startup.
|
void |
start()
Starts the demo.
|
getKeyStore, init
protected java.security.PrivateKey privateKey_
protected iaik.x509.X509Certificate certificate_
protected java.lang.String sender_
protected java.lang.String recipient_
public EncryptedMailDemo(java.lang.String moduleName, char[] userPin)
moduleName
- the name of the moduleuserPin
- the user-pin (password) for the TokenKeyStore
(may be null
to pou-up a dialog asking for the pin)public void getKeyAndCertificate() throws java.security.GeneralSecurityException, java.io.IOException, iaik.cms.CMSException
privateKey_
and certificate_
member variables. Usually you only will have the smartcard on the decryption
side (i.e. the sender will get the certificate by other means to use it
for encrypting the message), however, for simplicity (and since we do not know
which certificate/card you are actually will use for running the demo) we
get both, key and certificate from the card.java.security.GeneralSecurityException
- If anything with the provider fails.java.io.IOException
- If loading the key store fails.iaik.cms.CMSException
protected jakarta.mail.internet.MimeMessage createEncryptedMessage(jakarta.mail.Session session, jakarta.activation.DataHandler dataHandler) throws jakarta.mail.MessagingException
session
- the mail sessiondataHandler
- the content of the message to be encryptedjakarta.mail.MessagingException
- if an error occurs when creating the messagepublic void start()
public static void main(java.lang.String[] args)
args
- These are the command line arguments.