IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

demo.cms.pkcs11
Class IaikPkcs11SecurityProvider

java.lang.Object
  extended by iaik.cms.SecurityProvider
      extended by iaik.cms.IaikProvider
          extended by demo.cms.pkcs11.IaikPkcs11SecurityProvider

public class IaikPkcs11SecurityProvider
extends iaik.cms.IaikProvider

This class implements a SecurityProvider for the IAIK-CMS toolkit. This SecurityProvider can handle IAIKPKCS11Key objects and is thus suitable for use with the PKCS#11 provider. The demos in this package use this class to get the IAIK-CMS library to use a PKCS#11 module instead of pure software crypto.

To install this security provider call:

 IAIKPkcs11 iaikPkcs11Provider = ...;
 IaikPkcs11SecurityProvider pkcs11CmsSecurityProvider = new IaikPkcs11SecurityProvider(iaikPkcs11Provider);
 SecurityProvider.setSecurityProvider(pkcs11CmsSecurityProvider);
 


Field Summary
protected  iaik.pkcs.pkcs11.provider.IAIKPkcs11 iaikPkcs11Provider_
          Reference to the installed PKCS#11 provider instance.
 
Fields inherited from class iaik.cms.IaikProvider
ALG_SIGNATURE_RAWRSA, ALG_SIGNATURE_RAWRSASSA_PKCS1_V15
 
Fields inherited from class iaik.cms.SecurityProvider
ALG_CIPHER_RSA, ALG_CIPHER_RSA_DECRYPT, ALG_CIPHER_RSA_ENCRYPT, ALG_CIPHER_RSA_SIGN, ALG_CIPHER_RSA_VERIFY, ALG_DIGEST_MD5, ALG_DIGEST_SHA, ALG_HMAC_MD5, ALG_HMAC_SHA, ALG_KEYEX_DH, ALG_KEYEX_ESDH, ALG_KEYEX_SSDH, ALG_SIGNATURE_RAWDSA, ALG_SIGNATURE_RAWECDSA, ALG_SIGNATURE_RAWECDSA_PLAIN, ALG_SIGNATURE_RAWRSAPSS, ALG_SIGNATURE_SHADSA, CIPHER_DECRYPT, CIPHER_ENCRYPT, CIPHER_NONE, CIPHER_UNWRAP, CIPHER_WRAP, COMPRESS, DECOMPRESS, IMPLEMENTATION_NAME_DSA, IMPLEMENTATION_NAME_ECDSA, IMPLEMENTATION_NAME_ECDSA_PLAIN, IMPLEMENTATION_NAME_PBKDF2, IMPLEMENTATION_NAME_PWRI_KEK, IMPLEMENTATION_NAME_RSA, IMPLEMENTATION_NAME_RSA_OAEP, IMPLEMENTATION_NAME_RSA_PSS, provider_, providerName_, random_, SIGNATURE_NONE, SIGNATURE_SIGN, SIGNATURE_VERIFY
 
Constructor Summary
IaikPkcs11SecurityProvider(iaik.pkcs.pkcs11.provider.IAIKPkcs11 iaikPkcs11Provider)
          The given PKCS#11 provider instance must already be installed in the JCA framework.
 
Method Summary
 byte[] calculateSignatureFromHash(iaik.asn1.structures.AlgorithmID signatureAlgorithm, iaik.asn1.structures.AlgorithmID digestAlgorithm, java.security.PrivateKey privateKey, byte[] digest)
          Calculates the signature value for a CMS SignerInfo over the given digest value with the given algorithm using the supplied private key.
 byte[] calculateSignatureFromSignedAttributes(iaik.asn1.structures.AlgorithmID signatureAlgorithm, iaik.asn1.structures.AlgorithmID digestAlgorithm, java.security.PrivateKey privateKey, byte[] signedAttributes)
          Calculates the signature value for a CMS SignerInfo over the given signed attributes with the given algorithm using the supplied private key.
 javax.crypto.SecretKey decryptKey(byte[] encryptedKey, iaik.asn1.structures.AlgorithmID kea, java.security.PrivateKey recipientKey, java.lang.String cekAlgorithmName)
          Decrypts the given encrypted content encryption key for a KeyTransRecipientInfo.
 java.security.Signature getSignature(iaik.asn1.structures.AlgorithmID algorithm, int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec paramSpec)
          This method returns the desired Signature object.
 java.security.Signature getSignature(java.lang.String algorithm, int mode, java.security.Key key)
          This method returns the desired Signature object which uses the PKCS#11 provider if the key is a PKCS#11 key.
 
Methods inherited from class iaik.cms.IaikProvider
checkDomainParameters, createSharedKeyEncryptionKey, deriveKey, generateAEADParamSpec, generateAEADParamSpec, generateKey, generateKeyAgreementKeyPair, getAlgorithmParameterSpec, getASN1OriginatorPublicKey, getPBEKey, getSecureRandom, setAEADMac, setIv, turnOffIAIKProviderVersionCheck, unwrapKey, verifySignatureFromHash, verifySignatureFromSignedAttributes, wrapKey
 
Methods inherited from class iaik.cms.SecurityProvider
calculateMac, calculateSharedSecret, compress, convertCipherMode, decryptKey, encryptKey, generateGCMParamSpec, generateKey, getAlgorithmParameters, getAlgorithmParameters, getAlgorithmParameters, getAuthCipherEngine, getAuthCipherEngine, getByteArrayAuthCipherEngine, getByteArrayAuthCipherEngine, getByteArrayCipherEngine, getByteArrayCipherEngine, getCipher, getCipher, getCipher, getCipher, getEllipticCurveParameterSpec, getHash, getInputStreamAuthCipherEngine, getInputStreamAuthCipherEngine, getInputStreamCipherEngine, getInputStreamCipherEngine, getInputStreamCompressEngine, getInputStreamHashEngine, getInputStreamMacEngine, getKeyAgreement, getKeyAlgorithmID, getKeyFactory, getKeyGenerator, getKeyGenerator, getKeyGenerator, getKeyLength, getKeyLength, getKeyLength, getKeyPairGenerator, getKeyStore, getMac, getMac, getMaskGenerationAlgorithm, getMessageDigest, getMessageDigest, getMicAlgs, getOriginatorPublicKey, getOutputStreamCompressEngine, getOutputStreamHashEngine, getOutputStreamMacEngine, getProviderName, getSecretKeyFactory, getSecretKeyFactory, getSecurityProvider, getSignature, getSignature, getSignatureParameters, setSecureRandom, setSecurityProvider, setSignatureParameters, validateDHPublicKey, validateKeyAgreementKey
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iaikPkcs11Provider_

protected iaik.pkcs.pkcs11.provider.IAIKPkcs11 iaikPkcs11Provider_
Reference to the installed PKCS#11 provider instance.

Constructor Detail

IaikPkcs11SecurityProvider

public IaikPkcs11SecurityProvider(iaik.pkcs.pkcs11.provider.IAIKPkcs11 iaikPkcs11Provider)
The given PKCS#11 provider instance must already be installed in the JCA framework.

Parameters:
iaikPkcs11Provider - The PKCS#11 provider instance to use in this CMS security provider.
Method Detail

calculateSignatureFromHash

public byte[] calculateSignatureFromHash(iaik.asn1.structures.AlgorithmID signatureAlgorithm,
                                         iaik.asn1.structures.AlgorithmID digestAlgorithm,
                                         java.security.PrivateKey privateKey,
                                         byte[] digest)
                                  throws java.security.NoSuchAlgorithmException,
                                         java.security.InvalidKeyException,
                                         java.security.SignatureException
Calculates the signature value for a CMS SignerInfo over the given digest value with the given algorithm using the supplied private key.

Each SignerInfo included in a CMS SignedData object may calculate the signature value differently depending on the presence of signed attributes:

This method is called by class SignerInfo for calculating the signature when no signed attributes are present. Since the data to be signed may be of arbitrary size this method expects the already hashed data to only calculate the signature value on it (for instance, by doing the digest encrypting when using RSA for signing).

For that reason, when writing your own SecurityProvider and overriding this method, you will need some kind of RAW signature (respectively digest encryption) mechanism only expecting the already hashed data (e.g. a "RawDSA" signature engine when using DSA repectively a Cipher engine when using RSA).

If you want to override this method for use with smartcards, please be sure that your smartcard is able to do the signature (respectively digest encryption) operation only. However, if your smartcard requires to supply the whole data for doing the hash calcualtion itself, you may ensure that your SignerInfo contains signed attributes and override method calculateSignatureFromSignedAttributes for calculating the signature over the DER encoding of the signed attributes (thereby doing the hash computation, too).

Overrides:
calculateSignatureFromHash in class iaik.cms.IaikProvider
Parameters:
signatureAlgorithm - signatureAlgorithm the signature algorithm to be used, e.g. rsaEncryption, DSA
digestAlgorithm - the digest algorithm used for hash computation (e.g. SHA-1 or MD5); may be necessary for some signature schemes (e.g. to be included as a DigestInfo in a PKCS#1 RSA signature)
privateKey - the private key of the signer (i.e. the one supplied when creating a SignerInfo object; may be some kind of "dummy" key when used for smartcards
digest - the digest value over which the signature shall be calculated
Returns:
the signature value calculated from the given digest value
Throws:
java.security.NoSuchAlgorithmException - if any of the required algorithms is not supported
java.security.InvalidKeyException - if the key is not valid
java.security.SignatureException - if signature verification fails because of some crypto related error

calculateSignatureFromSignedAttributes

public byte[] calculateSignatureFromSignedAttributes(iaik.asn1.structures.AlgorithmID signatureAlgorithm,
                                                     iaik.asn1.structures.AlgorithmID digestAlgorithm,
                                                     java.security.PrivateKey privateKey,
                                                     byte[] signedAttributes)
                                              throws java.security.NoSuchAlgorithmException,
                                                     java.security.InvalidKeyException,
                                                     java.security.SignatureException
Calculates the signature value for a CMS SignerInfo over the given signed attributes with the given algorithm using the supplied private key.

Each SignerInfo included in a CMS SignedData object may calculate the signature value differently depending on the presence of signed attributes:

This method is called by class SignerInfo for calculating the signature when signed attributes are present.

When writing your own SecurityProvider and overriding this method, be aware that only the -- yet NOT hashed -- DER encoding of the signed attributes is supplied to this method. For that reason this method can be overriden for use with smartcards requiring to do the digest calculation theirselves: ensure that your SignerInfo contains signed attributes and override this method in a way to pass the given DER encoding of the signed attributes to your smartcard for doing the signature (and digest) calculation.

Since this method requires to calculate the digest value over the DER encoded signed attributes as part of the signature calculation, it uses a ordinary JCA Signature engine.

Overrides:
calculateSignatureFromSignedAttributes in class iaik.cms.IaikProvider
Parameters:
signatureAlgorithm - signatureAlgorithm the signature algorithm to be used, e.g. rsaEncryption, DSA
digestAlgorithm - the digest algorithm to be used for hash computation (e.g. SHA-1,..., SHA-512); may be necessary for some signature schemes (e.g. to be included as a DigestInfo in a PKCS#1 RSA signature)
privateKey - the private key of the signer (i.e. the one supplied when creating a SignerInfo object; may be some kind of "dummy" key when used for smartcards
signedAttributes - the DER encoding of the signed attributes over which the signature shall be calculated
Returns:
the signature value calculated from the given DER encoded signed attributes
Throws:
java.security.NoSuchAlgorithmException - if no Signature engine is available for the requested algorithm
java.security.InvalidKeyException - if the key is not valid
java.security.SignatureException - if signature calculation fails

getSignature

public java.security.Signature getSignature(java.lang.String algorithm,
                                            int mode,
                                            java.security.Key key)
                                     throws java.security.InvalidKeyException,
                                            java.security.NoSuchAlgorithmException
This method returns the desired Signature object which uses the PKCS#11 provider if the key is a PKCS#11 key. If the mode parameter is SIGNATURE_SIGN or SIGNATURE_VERIFY the signature object has to be initialized with the provided key in the respective mode.

Overrides:
getSignature in class iaik.cms.SecurityProvider
Parameters:
algorithm - the name of the Signature algorithm
mode - the mode indicating if the engine has to be initialized
key - the key for initializing the Signature engine
Returns:
the (if requested initialized) Signature engine
Throws:
java.security.InvalidKeyException - if the key is not valid
java.security.NoSuchAlgorithmException - if no Signature engine is available for the requested algorithm

getSignature

public java.security.Signature getSignature(iaik.asn1.structures.AlgorithmID algorithm,
                                            int mode,
                                            java.security.Key key,
                                            java.security.spec.AlgorithmParameterSpec paramSpec)
                                     throws java.security.InvalidKeyException,
                                            java.security.NoSuchAlgorithmException
This method returns the desired Signature object. If the mode parameter is SIGNATURE_SIGN or SIGNATURE_VERIFY the signature object is to be initialized with the provided key in the respective mode. If algorithm parameters are specified they are set for the Signature engine.

Overrides:
getSignature in class iaik.cms.SecurityProvider
Parameters:
algorithm - the AlgorithmID of the Signature algorithm
mode - the mode indicating if the engine has to be initialized
key - the key for initializing the Signature engine
paramSpec - any parameters to be set for the Signature engine, if not null
Returns:
the (if requested initialized) Signature engine
Throws:
java.security.InvalidKeyException - if the key is not valid
java.security.NoSuchAlgorithmException - if no Signature engine is available for the requested algorithm

decryptKey

public javax.crypto.SecretKey decryptKey(byte[] encryptedKey,
                                         iaik.asn1.structures.AlgorithmID kea,
                                         java.security.PrivateKey recipientKey,
                                         java.lang.String cekAlgorithmName)
                                  throws java.security.NoSuchAlgorithmException,
                                         java.security.InvalidKeyException,
                                         javax.crypto.NoSuchPaddingException,
                                         javax.crypto.BadPaddingException
Decrypts the given encrypted content encryption key for a KeyTransRecipientInfo.

CMS EnvelopedData uses the KeyTransRecipientInfo type for encrypting the secret content encryption key with the public key of the recipient. Currently in general RSA PKCS#1v1.5 is used for key transport. If rsaEncryption is requested as key encryption algorithm this method uses a RSA Cipher ("RSA/ECB/PKCS1Padding/Encrypt") for decrypting the encrypted content encryption key with the supplied private key of the recipient. If another algorithm than RSA is requested, this method throws a NoSuchAlgorithmException. An application wishing to support another algorithm may override this method.

Overrides:
decryptKey in class iaik.cms.IaikProvider
Parameters:
encryptedKey - the encrypted content encryption key to be decrypted
kea - the key encryption alglorithm to be used, e.g. rsaEncryption
recipientKey - the private key of the recipient to be used for decrypting the encrypted content encryption key
cekAlgorithmName - the name of the content encryption key (e.g. "DES") to be set for the SecretKey object created by this method
Returns:
the decrypted content encryption key
Throws:
java.security.NoSuchAlgorithmException - if the requested algorithm is not available
java.security.InvalidKeyException - if the decryption key is not valid
javax.crypto.NoSuchPaddingException - if the required padding scheme is not supported
javax.crypto.BadPaddingException - if an padding error occurs

IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

v6.1
(c) 2002 IAIK, (c) 2003 - 2025 SIC