public class EdDHAuthEnvelopedDataDemo extends java.lang.Object
AuthEnvelopedDataStream
,
AuthEnvelopedData
and AuthEnvelopedDataOutputStream
for authenticated encrypting data with the CMS content type AuthEnvelopedData using the
Elliptic Curve Diffie-Hellman (ECDH) key agreement algorithm with curve25519 and
curve448 according to RFC 5083
and RFC 8418.
This demo uses the AES-CCM and AES-GCM authenticated encryption algorithms
as specified by RFC 5084.
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.
Any keys/certificates required for this demo are read from a keystore
file "cmsecc.keystore" located in your current working directory. If
the keystore file does not exist you can create it by running the
SetupCMSEccKeyStore
program.
Additionally to iaik_cms.jar
you also must have
iaik_jce_(full).jar
(IAIK-JCE,
https://sic.tech/products/core-crypto-toolkits/jca-jce/),
and iaik_eccelarate.jar
(IAIK-ECCelerateTM,
https://sic.tech/products/core-crypto-toolkits/eccelerate/)
in your classpath.
AuthEnvelopedDataStream
,
AuthEnvelopedData
,
AuthEnvelopedDataOutputStream
,
RecipientInfo
,
KeyAgreeRecipientInfo
Constructor and Description |
---|
EdDHAuthEnvelopedDataDemo()
Setup the demo certificate chains.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
createAuthEnvelopedData(byte[] message,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Creates a CMS
AuthEnvelopedData message. |
byte[] |
createAuthEnvelopedDataOutputStream(byte[] message,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Creates a CMS
AuthEnvelopedData message using the
AuthEnvelopedDataOutputStream
class. |
byte[] |
createAuthEnvelopedDataStream(byte[] message,
iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Creates a CMS
AuthEnvelopedDataStream message. |
iaik.cms.RecipientInfo[] |
createRecipients(iaik.asn1.structures.AlgorithmID keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength)
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)
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)
Decrypts the encrypted content of the given
AuthEnvelopedData object for
the recipient identified by its recipient certificate and verifies the message
authentication code. |
static void |
main(java.lang.String[] argv)
Main method.
|
void |
parseAuthEnvelopedDataWithRecipientCert(boolean stream,
byte[] encodedAuthEnvelopedData)
Parses an AuthEnvelopedData and decrypts the content for all test recipients
using their recipient certificate 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 keyEA,
iaik.asn1.structures.AlgorithmID keyWrapAlg,
int kekLength,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
Starts the test for the given content-authenticated encryption algorithm.
|
public EdDHAuthEnvelopedDataDemo() throws java.io.IOException
SetupCMSKeyStore
.java.io.IOException
- if an file read error occurspublic byte[] createAuthEnvelopedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedDataStream
message.message
- the message to be authenticated-enveloped, as byte representationkeyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmAuthEnvelopedData
object just creatediaik.cms.CMSException
- if the AuthEnvelopedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] createAuthEnvelopedDataOutputStream(byte[] message, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData
message using the
AuthEnvelopedDataOutputStream
class.message
- the message to be authenticated-enveloped, as byte representationkeyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmAuthEnvelopedData
object just creatediaik.cms.CMSException
- if the AuthEnvelopedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getAuthEnvelopedDataStream(byte[] encoding, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
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.
encoding
- the AuthEnvelopedData
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[] getAuthEnvelopedDataStream(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 AuthEnvelopedData
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[] getAuthEnvelopedDataStream(byte[] encoding, java.security.Key key, iaik.x509.X509Certificate recipientCert) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData
object for
the recipient identified by its recipient certificate and verifies the message
authentication code.encoding
- the AuthEnvelopedData
object as BER encoded byte arraykey
- the key to decrypt the messagerecipientCert
- the certificate of the recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if a stream read/write error occurspublic byte[] createAuthEnvelopedData(byte[] message, iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength, iaik.asn1.structures.AlgorithmID contentAuthEncAlg) throws iaik.cms.CMSException
AuthEnvelopedData
message.message
- the message to be enveloped, as byte representationkeyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmAuthEnvelopedData
, as byte arrayiaik.cms.CMSException
- if the AuthEnvelopedData
object cannot
be createdpublic byte[] getAuthEnvelopedData(byte[] enc, java.security.Key key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
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.
enc
- the encoded AuthEnvelopedData
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[] getAuthEnvelopedData(byte[] enc, java.security.Key key, iaik.cms.KeyIdentifier recipientID) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData
object for
the recipient identified by recipient identifier.
enc
- the BER encoded AuthEnvelopedData
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[] getAuthEnvelopedData(byte[] enc, java.security.Key key, iaik.x509.X509Certificate recipientCert) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData
object for
the recipient identified by its recipient certificate or keyID.enc
- the BER encoded AuthEnvelopedData
ASN.1 objectkey
- the key to decrypt the messagerecipientCert
- the certificate of the recipientiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
public iaik.cms.RecipientInfo[] createRecipients(iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength) throws iaik.cms.CMSException
keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itiaik.cms.CMSException
- if an error occurs when creating the recipient infospublic void parseAuthEnvelopedDataWithRecipientInfoIndex(boolean stream, byte[] encodedAuthEnvelopedData) throws java.lang.Exception
stream
- whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData
- the encoded AuthEnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void parseAuthEnvelopedDataWithRecipientIdentifier(boolean stream, byte[] encodedAuthEnvelopedData) throws java.lang.Exception
stream
- whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData
- the encoded AuthEnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void parseAuthEnvelopedDataWithRecipientCert(boolean stream, byte[] encodedAuthEnvelopedData) throws java.lang.Exception
stream
- whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData
- the encoded AuthEnvelopedData objectjava.lang.Exception
- if some error occurs during decoding/decryptionpublic void start()
public void start(iaik.asn1.structures.AlgorithmID keyEA, iaik.asn1.structures.AlgorithmID keyWrapAlg, int kekLength, iaik.asn1.structures.AlgorithmID contentAuthEncAlg)
keyEA
- the key encryption (key agreement) algorithm used for creating
a shared key encryption key for encrypting the secret content
encryption key with itkeyWrapAlg
- the key wrap algorithm to be used for wrapping (encrypting)
the content encryption keykekLength
- the length of the key encryption key to be created for
encrypting the content encryption key with itcontentAuthEncAlg
- 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