IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

demo.cms.authEnvelopedData
Class AuthEnvelopedDataDemo

java.lang.Object
  extended by demo.cms.authEnvelopedData.AuthEnvelopedDataDemo

public class AuthEnvelopedDataDemo
extends java.lang.Object

Demonstrates the usage of class AuthEnvelopedDataStream and AuthEnvelopedData for authenticated encrypting data using the CMS type AuthEnvelopedData according to RFC 5083.

This demo uses the AES-CCM and AES-GCM authenticated encryption algorithms as specified by RFC 5084 and the ChaCha20-Poly1305 authenticated encryption algorithm as specified by RFC 8103. The demo creates an AuthEnvelopedData object and subsequently shows several ways that may be used for decrypting the content and verifying the message authentication code for some particular recipient.
Since AES-CCM and AES-GCM are not implemented by IAIK-JCE versions prior 3.17, this demo at least may require IAIK-JCE 3.17 as cryptographic service provider. ChaCha20-Poly1305 for CMS requires IAIK-JCE version 5.62 or later.

Keys and certificates are retrieved from the demo KeyStore ("cms.keystore") which has to be located in your current working directory and may be created by running the SetupCMSKeyStore program.

See Also:
AuthEnvelopedDataStream, AuthEnvelopedData, RecipientInfo, KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo

Constructor Summary
AuthEnvelopedDataDemo()
          Setup the demo certificate chains.
 
Method Summary
 byte[] createAuthEnvelopedData(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
          Creates a CMS AuthEnvelopedData message.
 byte[] createAuthEnvelopedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
          Creates a CMS AuthEnvelopedDataStream message.
 iaik.cms.RecipientInfo[] createRecipients()
          Creates the RecipientInfos.
 byte[] getAuthEnvelopedData(byte[] enc, java.security.Key key, int recipientInfoIndex)
          Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its index into the recipientInfos field and verifies the message authentication code.
 byte[] getAuthEnvelopedData(byte[] enc, java.security.Key key, iaik.cms.KeyIdentifier recipientID)
          Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by recipient identifier.
 byte[] getAuthEnvelopedData(byte[] enc, java.security.Key key, iaik.x509.X509Certificate recipientCert, byte[] kekID)
          Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its recipient certificate or keyID.
 byte[] getAuthEnvelopedDataStream(byte[] encoding, java.security.Key key, int recipientInfoIndex)
          Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its index into the recipientInfos field and verifies the message authentication code.
 byte[] getAuthEnvelopedDataStream(byte[] encoding, java.security.Key key, iaik.cms.KeyIdentifier recipientID)
          Decrypts the encrypted content of the given EnvelopedData object for the recipient identified by recipient identifier and verifies the message authentication code.
 byte[] getAuthEnvelopedDataStream(byte[] encoding, java.security.Key key, iaik.x509.X509Certificate recipientCert, byte[] kekID)
          Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its recipient certificate or kekID and verifies the message authentication code.
static void main(java.lang.String[] argv)
          Main method.
 void parseAuthEnvelopedDataWithRecipientCertOrKEKId(boolean stream, byte[] encodedAuthEnvelopedData)
          Parses an AuthEnvelopedData and decrypts the content for all test recipients using their recipient certificate (for RecipientInfos of type KeyTransRecipientInfo or KeyAgreeRecipientInfo) or key id (for RecipientInfos of type KEKRecipientInfo) for identifying the recipient.
 void parseAuthEnvelopedDataWithRecipientIdentifier(boolean stream, byte[] encodedAuthEnvelopedData)
          Parses an AuthEnvelopedData and decrypts the content for all test recipients using their recipient identifiers for identifying the recipient.
 void parseAuthEnvelopedDataWithRecipientInfoIndex(boolean stream, byte[] encodedAuthEnvelopedData)
          Parses an AuthEnvelopedData and decrypts the content for all test recipients using the index into the recipientInfos field for identifying the recipient.
 void start()
          Starts the test.
 void start(iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
          Starts the test for the given content-authenticated encryption algorithm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AuthEnvelopedDataDemo

public AuthEnvelopedDataDemo()
                      throws java.io.IOException
Setup the demo certificate chains. Keys and certificates are retrieved from the demo KeyStore ("cms.keystore") file which has to be located in your current working directory and may be created by running SetupCMSKeyStore.

Throws:
java.io.IOException - if an file read error occurs
Method Detail

createAuthEnvelopedDataStream

public byte[] createAuthEnvelopedDataStream(byte[] message,
                                            iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
                                     throws iaik.cms.CMSException,
                                            java.io.IOException
Creates a CMS AuthEnvelopedDataStream message.

Parameters:
message - the message to be authenticated-enveloped, as byte representation
contentAuthEncAlg - the id of the content-authenticated encryption algorithm
Returns:
the BER encoding of the AuthEnvelopedData object just created
Throws:
iaik.cms.CMSException - if the AuthEnvelopedData object cannot be created
java.io.IOException - if an I/O error occurs

getAuthEnvelopedDataStream

public byte[] getAuthEnvelopedDataStream(byte[] encoding,
                                         java.security.Key key,
                                         int recipientInfoIndex)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its index into the recipientInfos field and verifies the message authentication code.

This way of decrypting the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo), but requires to know at what index of the recipientInfo field the RecipientInfo for the particular recipient in mind can be found. If the recipient in mind uses a RecipientInfo of type KeyAgreeRecipientInfo some processing overhead may take place because a KeyAgreeRecipientInfo may contain encrypted content-encryption keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted content encryption key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.

Parameters:
encoding - the AuthEnvelopedData object as DER encoded byte array
key - the key to decrypt the message
recipientInfoIndex - the index into the RecipientInfo array to which the specified key belongs
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered or MAC verification fails
java.io.IOException - if a stream read/write error occurs

getAuthEnvelopedDataStream

public byte[] getAuthEnvelopedDataStream(byte[] encoding,
                                         java.security.Key key,
                                         iaik.cms.KeyIdentifier recipientID)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted content of the given EnvelopedData object for the recipient identified by recipient identifier and verifies the message authentication code.

This way of decrypting the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo). The recipient in mind is identified by its recipient identifier.

Parameters:
encoding - the AuthEnvelopedData object as BER encoded byte array
key - the key to decrypt the message
recipientID - the recipient identifier uniquely identifying the key of the recipient
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if a stream read/write error occurs

getAuthEnvelopedDataStream

public byte[] getAuthEnvelopedDataStream(byte[] encoding,
                                         java.security.Key key,
                                         iaik.x509.X509Certificate recipientCert,
                                         byte[] kekID)
                                  throws iaik.cms.CMSException,
                                         java.io.IOException
Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its recipient certificate or kekID and verifies the message authentication code.

Since recipient certificates only may be used for for RecipientInfos of type KeyTransRecipientInfo or KeyAgreeRecipientInfo, a key id has to be supplied for decrypting the content for a recipient using a KEKRecipientInfo.

Parameters:
encoding - the AuthEnvelopedData object as BER encoded byte array
key - the key to decrypt the message
recipientCert - the certificate of the recipient having a RecipientInfo of type KeyTransRecipientInfo or KeyAgreeRecipientInfo
kekID - the kekID identifying the recipient key when using a RecipientInfo of type KEKRecipientInfo
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if a stream read/write error occurs

createAuthEnvelopedData

public byte[] createAuthEnvelopedData(byte[] message,
                                      iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
                               throws iaik.cms.CMSException
Creates a CMS AuthEnvelopedData message.

Parameters:
message - the message to be enveloped, as byte representation
contentAuthEncAlg - the id of the content-authenticated encryption algorithm
Returns:
the encoded AuthEnvelopedData, as byte array
Throws:
iaik.cms.CMSException - if the AuthEnvelopedData object cannot be created

getAuthEnvelopedData

public byte[] getAuthEnvelopedData(byte[] enc,
                                   java.security.Key key,
                                   int recipientInfoIndex)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its index into the recipientInfos field and verifies the message authentication code.

This way of decrypting the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo), but requires to know at what index of the recipientInfo field the RecipientInfo for the particular recipient in mind can be found. If the recipient in mind uses a RecipientInfo of type KeyAgreeRecipientInfo some processing overhead may take place because a KeyAgreeRecipientInfo may contain encrypted content-encryption keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted content encryption key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.

Parameters:
enc - the encoded AuthEnvelopedData
key - the key to decrypt the message
recipientInfoIndex - the index into the RecipientInfo array to which the specified key belongs
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if an I/O error occurs

getAuthEnvelopedData

public byte[] getAuthEnvelopedData(byte[] enc,
                                   java.security.Key key,
                                   iaik.cms.KeyIdentifier recipientID)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by recipient identifier.

This way of decrypting the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo). The recipient in mind is identified by its recipient identifier.

Parameters:
enc - the BER encoded AuthEnvelopedData ASN.1 object
key - the key to decrypt the message
recipientID - the recipient identifier uniquely identifying the key of the recipient
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException - if an I/O error occurs

getAuthEnvelopedData

public byte[] getAuthEnvelopedData(byte[] enc,
                                   java.security.Key key,
                                   iaik.x509.X509Certificate recipientCert,
                                   byte[] kekID)
                            throws iaik.cms.CMSException,
                                   java.io.IOException
Decrypts the encrypted content of the given AuthEnvelopedData object for the recipient identified by its recipient certificate or keyID.

Since recipient certificates only may be used for for RecipientInfos of type KeyTransRecipientInfo or KeyAgreeRecipientInfo, a key id has to be supplied for decrypting the content for a recipient using a KEKRecipientInfo.

Parameters:
enc - the BER encoded AuthEnvelopedData ASN.1 object
key - the key to decrypt the message
recipientCert - the certificate of the recipient having a RecipientInfo of type KeyTransRecipientInfo or KeyAgreeRecipientInfo
kekID - the kekID identifying the recipient key when using a RecipientInfo of type KEKRecipientInfo
Returns:
the recovered message, as byte array
Throws:
iaik.cms.CMSException - if the message cannot be recovered
java.io.IOException

createRecipients

public iaik.cms.RecipientInfo[] createRecipients()
                                          throws iaik.cms.CMSException
Creates the RecipientInfos.

Returns:
the RecipientInfos created, two KeyTransRecipientInfos, one KeyAgreeRecipientInfo (for two recipients with same domain parameters), and one KEKRecipientInfo
Throws:
iaik.cms.CMSException - if an error occurs when creating the recipient infos

parseAuthEnvelopedDataWithRecipientInfoIndex

public void parseAuthEnvelopedDataWithRecipientInfoIndex(boolean stream,
                                                         byte[] encodedAuthEnvelopedData)
                                                  throws java.lang.Exception
Parses an AuthEnvelopedData and decrypts the content for all test recipients using the index into the recipientInfos field for identifying the recipient.

Parameters:
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedData
encodedAuthEnvelopedData - the encoded AuthEnvelopedData object
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

parseAuthEnvelopedDataWithRecipientIdentifier

public void parseAuthEnvelopedDataWithRecipientIdentifier(boolean stream,
                                                          byte[] encodedAuthEnvelopedData)
                                                   throws java.lang.Exception
Parses an AuthEnvelopedData and decrypts the content for all test recipients using their recipient identifiers for identifying the recipient.

Parameters:
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedData
encodedAuthEnvelopedData - the encoded AuthEnvelopedData object
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

parseAuthEnvelopedDataWithRecipientCertOrKEKId

public void parseAuthEnvelopedDataWithRecipientCertOrKEKId(boolean stream,
                                                           byte[] encodedAuthEnvelopedData)
                                                    throws java.lang.Exception
Parses an AuthEnvelopedData and decrypts the content for all test recipients using their recipient certificate (for RecipientInfos of type KeyTransRecipientInfo or KeyAgreeRecipientInfo) or key id (for RecipientInfos of type KEKRecipientInfo) for identifying the recipient.

Parameters:
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedData
encodedAuthEnvelopedData - the encoded AuthEnvelopedData object
Throws:
java.lang.Exception - if some error occurs during decoding/decryption

start

public void start()
Starts the test.


start

public void start(iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Starts the test for the given content-authenticated encryption algorithm.

Parameters:
contentAuthEncAlg - the id of the content-authenticated encryption algorithm

main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Main method.

Throws:
java.io.IOException - if an I/O error occurs when reading required keys and certificates from files
java.lang.Exception

IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

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