public class EnvelopedDataDemoAEAD extends java.lang.Object
EnvelopedDataStream
,
EnvelopedData
and EnvelopedDataOutputStream
for
AEAD encrypting data using the CMS type EnvelopedData according to
RFC 5652.
Usually CMS uses AEAD cipher modes like GCM with the AuthEnvelopedData
type. However,
technically -- when appending the mac value to the cipher text -- it is also possible to use
AEAD ciphers with the EnvelopedData
type as shown in this demo.
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.
This demo requires Java 7 or later.
EnvelopedDataStream
,
EnvelopedData
,
RecipientInfo
,
KeyTransRecipientInfo
,
KeyAgreeRecipientInfo
,
KEKRecipientInfo
Constructor and Description |
---|
EnvelopedDataDemoAEAD()
Setup the demo certificate chains.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
createEnvelopedData(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Creates a CMS
EnvelopedData message. |
byte[] |
createEnvelopedDataOutputStream(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Uses the
EnvelopedDataOutputStream implementation to create an
EnvelopedData message. |
byte[] |
createEnvelopedDataStream(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Creates a CMS
EnvelopedDataStream message. |
iaik.cms.RecipientInfo[] |
createRecipients()
Creates the RecipientInfos.
|
byte[] |
getEnvelopedData(byte[] enc,
java.security.Key key,
int recipientInfoIndex)
Decrypts the encrypted content of the given
EnvelopedData object for
the recipient identified by its index into the recipientInfos field and verifies
the message authentication code. |
byte[] |
getEnvelopedData(byte[] enc,
java.security.Key key,
iaik.cms.KeyIdentifier recipientID)
Decrypts the encrypted content of the given
EnvelopedData object for
the recipient identified by recipient identifier. |
byte[] |
getEnvelopedData(byte[] enc,
java.security.Key key,
iaik.x509.X509Certificate recipientCert,
byte[] kekID)
Decrypts the encrypted content of the given
EnvelopedData object for
the recipient identified by its recipient certificate or keyID. |
byte[] |
getEnvelopedDataStream(byte[] encoding,
java.security.Key key,
int recipientInfoIndex)
Decrypts the encrypted content of the given
EnvelopedData object for
the recipient identified by its index into the recipientInfos field and verifies
the message authentication code. |
byte[] |
getEnvelopedDataStream(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[] |
getEnvelopedDataStream(byte[] encoding,
java.security.Key key,
iaik.x509.X509Certificate recipientCert,
byte[] kekID)
Decrypts the encrypted content of the given
EnvelopedData 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 |
parseEnvelopedDataWithRecipientCertOrKEKId(boolean stream,
byte[] encodedEnvelopedData)
Parses an EnvelopedData 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 |
parseEnvelopedDataWithRecipientIdentifier(boolean stream,
byte[] encodedEnvelopedData)
Parses an EnvelopedData and decrypts the content for all test recipients
using their recipient identifiers for identifying the recipient.
|
void |
parseEnvelopedDataWithRecipientInfoIndex(boolean stream,
byte[] encodedEnvelopedData)
Parses an EnvelopedData 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.
|
public EnvelopedDataDemoAEAD() throws java.io.IOException
SetupCMSKeyStore
.java.io.IOException
- if an file read error occurspublic byte[] createEnvelopedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException, java.io.IOException
EnvelopedDataStream
message.message
- the message to be authenticated-enveloped, as byte representationcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmEnvelopedData
object just creatediaik.cms.CMSException
- if the EnvelopedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] createEnvelopedDataOutputStream(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException, java.io.IOException
EnvelopedDataOutputStream
implementation to create an
EnvelopedData message.message
- the message to be authenticated-enveloped, as byte representationcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmEnvelopedData
object just creatediaik.cms.CMSException
- if the EnvelopedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getEnvelopedDataStream(byte[] encoding, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
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.
encoding
- the EnvelopedData
object as DER encoded byte arraykey
- the key to decrypt the messagerecipientInfoIndex
- the index into the RecipientInfo
array
to which the specified key belongsiaik.cms.CMSException
- if the message cannot be recovered or MAC verification failsjava.io.IOException
- if a stream read/write error occurspublic byte[] getEnvelopedDataStream(byte[] encoding, java.security.Key key, iaik.cms.KeyIdentifier recipientID) throws iaik.cms.CMSException, java.io.IOException
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.
encoding
- the EnvelopedData
object as BER encoded byte arraykey
- the key to decrypt the messagerecipientID
- the recipient identifier uniquely identifying the key of the
recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if a stream read/write error occurspublic byte[] getEnvelopedDataStream(byte[] encoding, java.security.Key key, iaik.x509.X509Certificate recipientCert, byte[] kekID) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
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.
encoding
- the EnvelopedData
object as BER encoded byte arraykey
- the key to decrypt the messagerecipientCert
- the certificate of the recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfokekID
- the kekID identifying the recipient key when using a RecipientInfo
of type KEKRecipientInfoiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if a stream read/write error occurspublic byte[] createEnvelopedData(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException
EnvelopedData
message.message
- the message to be enveloped, as byte representationcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmEnvelopedData
, as byte arrayiaik.cms.CMSException
- if the EnvelopedData
object cannot
be createdpublic byte[] getEnvelopedData(byte[] enc, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
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.
enc
- the encoded EnvelopedData
key
- the key to decrypt the messagerecipientInfoIndex
- the index into the RecipientInfo
array
to which the specified key belongsiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] getEnvelopedData(byte[] enc, java.security.Key key, iaik.cms.KeyIdentifier recipientID) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
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.
enc
- the BER encoded EnvelopedData
ASN.1 objectkey
- the key to decrypt the messagerecipientID
- the recipient identifier uniquely identifying the key of the
recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] getEnvelopedData(byte[] enc, java.security.Key key, iaik.x509.X509Certificate recipientCert, byte[] kekID) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
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.
enc
- the BER encoded EnvelopedData
ASN.1 objectkey
- the key to decrypt the messagerecipientCert
- the certificate of the recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfokekID
- the kekID identifying the recipient key when using a RecipientInfo
of type KEKRecipientInfoiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
public iaik.cms.RecipientInfo[] createRecipients() throws iaik.cms.CMSException
iaik.cms.CMSException
- if an error occurs when creating the recipient infospublic void parseEnvelopedDataWithRecipientInfoIndex(boolean stream, byte[] encodedEnvelopedData) throws java.lang.Exception
stream
- whether to use EnvelopedDataStream or EnvelopedDataencodedEnvelopedData
- the encoded EnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void parseEnvelopedDataWithRecipientIdentifier(boolean stream, byte[] encodedEnvelopedData) throws java.lang.Exception
stream
- whether to use EnvelopedDataStream or EnvelopedDataencodedEnvelopedData
- the encoded EnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void parseEnvelopedDataWithRecipientCertOrKEKId(boolean stream, byte[] encodedEnvelopedData) throws java.lang.Exception
stream
- whether to use EnvelopedDataStream or EnvelopedDataencodedEnvelopedData
- the encoded EnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void start()
public void start(iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
contentAuthEncAlg
- the id of the content-authenticated encryption algorithmpublic static void main(java.lang.String[] argv) throws java.lang.Exception
java.io.IOException
- if an I/O error occurs when reading required keys
and certificates from filesjava.lang.Exception