public class EckaEGAuthEnvelopedDataDemo extends java.lang.Object
SignedDataStream and
SignedData, and AuthEnvelopedDataStream and
AuthEnvelopedData according to the BSI Technical
Recommendation
BSI TR-03109-1 for transmitting signed authenticated encrypted
data between Smart-Meter-Gateways and external market participants and the
Smart Meter Gateway Administrator.
This demo uses AES-GCM as specified by RFC 5084 and AES-CBC-CMAC as specified by BSI TR-03109-1 for authenticated encryption. The demo compressed the data, creates an AuthEnvelopedData object, packs it into a SignedData and subsequently shows several ways that may be used for decrypting the content for some particular recipient.
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,
SignedDataStream,
SignedData,
RecipientInfo,
KeyAgreeRecipientInfo| Constructor and Description |
|---|
EckaEGAuthEnvelopedDataDemo()
Setup the demo certificate chains.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
createAuthEnvelopedData(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg,
iaik.x509.X509Certificate recipient1Cert,
iaik.x509.X509Certificate recipient2Cert)
Creates a CMS
AuthEnvelopedData message. |
byte[] |
createAuthEnvelopedDataOutputStream(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg,
iaik.x509.X509Certificate recipient1Cert,
iaik.x509.X509Certificate recipient2Cert)
Creates a CMS
AuthEnvelopedData message using class
AuthEnvelopedDataOutputStream. |
byte[] |
createAuthEnvelopedDataStream(byte[] message,
iaik.asn1.structures.AlgorithmID contentAuthEncAlg,
iaik.x509.X509Certificate recipient1Cert,
iaik.x509.X509Certificate recipient2Cert)
Creates a CMS
AuthEnvelopedData message using class AuthEnvelopedDataStream. |
byte[] |
createCompressedData(byte[] message)
Creates a CMS
CompressedData object. |
byte[] |
createCompressedDataStream(byte[] message)
Creates a CMS
CompressedData object. |
iaik.cms.RecipientInfo[] |
createRecipients(iaik.asn1.structures.AlgorithmID contentAuthEncAlg,
iaik.x509.X509Certificate recipient1Cert,
iaik.x509.X509Certificate recipient2Cert)
Creates the RecipientInfos.
|
byte[] |
createSignedData(byte[] message,
int mode,
iaik.asn1.structures.AlgorithmID hashAlgorithm,
iaik.asn1.structures.AlgorithmID signatureAlgorithm,
java.security.PrivateKey signerKey,
iaik.x509.X509Certificate[] certificates)
Creates an ECDSA signed CMS
SignedData object and wraps it by a CMS
ContentInfo object. |
byte[] |
createSignedDataStream(byte[] message,
int mode,
iaik.asn1.structures.AlgorithmID hashAlgorithm,
iaik.asn1.structures.AlgorithmID signatureAlgorithm,
java.security.PrivateKey signerKey,
iaik.x509.X509Certificate[] certificates)
Creates an ECDSA signed CMS
SignedDataStream object and wraps it by a
CMS ContentInfoStream. |
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. |
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. |
byte[] |
getCompressedData(byte[] encoding)
Parses a CMS
CompressedData object. |
byte[] |
getCompressedDataStream(byte[] encoding)
Parses a CMS
CompressedData object. |
byte[] |
getSignedData(byte[] signedData,
byte[] message,
iaik.x509.X509Certificate[] certificates)
Parses a CMS
ContentInfo holding a SignedData
object and verifies the signature. |
byte[] |
getSignedDataStream(byte[] signedData,
byte[] message,
iaik.x509.X509Certificate[] certificates)
Parses a CMS
ContentInfo object holding a SignedData
object and verifies the signature. |
static void |
main(java.lang.String[] argv)
Main method.
|
void |
parseAuthEnvelopedDataWithRecipientCert(boolean stream,
byte[] encodedAuthEnvelopedData,
java.security.PrivateKey recipient1PrivateKey,
iaik.x509.X509Certificate recipient1Cert,
java.security.PrivateKey recipient2PrivateKey,
iaik.x509.X509Certificate recipient2Cert)
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,
java.security.PrivateKey recipient1PrivateKey,
iaik.x509.X509Certificate recipient1Cert,
java.security.PrivateKey recipient2PrivateKey,
iaik.x509.X509Certificate recipient2Cert)
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,
java.security.PrivateKey recipient1PrivateKey,
java.security.PrivateKey recipient2PrivateKey)
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,
java.security.PrivateKey signerPrivateKey,
iaik.x509.X509Certificate[] signerCertChain,
java.security.PrivateKey recipient1PrivateKey,
iaik.x509.X509Certificate recipient1Cert,
java.security.PrivateKey recipient2PrivateKey,
iaik.x509.X509Certificate recipient2Cert)
Starts the test for the given content-authenticated encryption algorithm.
|
public EckaEGAuthEnvelopedDataDemo() throws java.io.IOException
SetupCMSEccKeyStore.java.io.IOException - if an file read error occurspublic byte[] createAuthEnvelopedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID contentAuthEncAlg, iaik.x509.X509Certificate recipient1Cert, iaik.x509.X509Certificate recipient2Cert) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData message using class AuthEnvelopedDataStream.message - the message to be authenticated-enveloped, as byte representationcontentAuthEncAlg - the id of the content-authenticated encryption algorithmrecipient1Cert - the certificate of the first recipient (sender)recipient2Cert - the certificate of the second recipient *AuthEnvelopedData 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 contentAuthEncAlg, iaik.x509.X509Certificate recipient1Cert, iaik.x509.X509Certificate recipient2Cert) throws iaik.cms.CMSException, java.io.IOException
AuthEnvelopedData message using class
AuthEnvelopedDataOutputStream. The content data is
compressed inside this method.message - the message to be authenticated-enveloped, as byte representationcontentAuthEncAlg - the id of the content-authenticated encryption algorithmrecipient1Cert - the certificate of the first recipient (sender)recipient2Cert - the certificate of the second recipientAuthEnvelopedData 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 recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfoiaik.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 contentAuthEncAlg, iaik.x509.X509Certificate recipient1Cert, iaik.x509.X509Certificate recipient2Cert) throws iaik.cms.CMSException
AuthEnvelopedData message.message - the message to be enveloped, as byte representationcontentAuthEncAlg - the id of the content-authenticated encryption algorithmrecipient1Cert - the certificate of the first recipient (sender)recipient2Cert - the certificate of the second recipientAuthEnvelopedData, 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 AuthEnvelopedDatakey - 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.
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 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.enc - the BER encoded AuthEnvelopedData ASN.1 objectkey - the key to decrypt the messagerecipientCert - the certificate of the recipient having a RecipientInfo of
type KeyTransRecipientInfo or KeyAgreeRecipientInfoiaik.cms.CMSException - if the message cannot be recoveredjava.io.IOExceptionpublic iaik.cms.RecipientInfo[] createRecipients(iaik.asn1.structures.AlgorithmID contentAuthEncAlg, iaik.x509.X509Certificate recipient1Cert, iaik.x509.X509Certificate recipient2Cert) throws iaik.cms.CMSException
contentAuthEncAlg - the content encryption algorithmrecipient1Cert - the certificate of the first recipient (sender)recipient2Cert - the certificate of the second recipientiaik.cms.CMSException - if an error occurs when creating the recipient infospublic void parseAuthEnvelopedDataWithRecipientInfoIndex(boolean stream, byte[] encodedAuthEnvelopedData, java.security.PrivateKey recipient1PrivateKey, java.security.PrivateKey recipient2PrivateKey) throws java.lang.Exception
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData - the encoded AuthEnvelopedData objectrecipient1PrivateKey - the private key of the first recipient (sender)recipient2PrivateKey - the private key of the second recipientjava.lang.Exception - if some error occurs during decoding/decryptionpublic void parseAuthEnvelopedDataWithRecipientIdentifier(boolean stream, byte[] encodedAuthEnvelopedData, java.security.PrivateKey recipient1PrivateKey, iaik.x509.X509Certificate recipient1Cert, java.security.PrivateKey recipient2PrivateKey, iaik.x509.X509Certificate recipient2Cert) throws java.lang.Exception
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData - the encoded AuthEnvelopedData objectrecipient1PrivateKey - the private key of the first recipient (sender)recipient1Cert - the certificate of the first recipient (sender)recipient2PrivateKey - the private key of the second recipientrecipient2Cert - the certificate of the second recipientjava.lang.Exception - if some error occurs during decoding/decryptionpublic void parseAuthEnvelopedDataWithRecipientCert(boolean stream, byte[] encodedAuthEnvelopedData, java.security.PrivateKey recipient1PrivateKey, iaik.x509.X509Certificate recipient1Cert, java.security.PrivateKey recipient2PrivateKey, iaik.x509.X509Certificate recipient2Cert) throws java.lang.Exception
stream - whether to use AuthEnvelopedDataStream or AuthEnvelopedDataencodedAuthEnvelopedData - the encoded AuthEnvelopedData objectrecipient1PrivateKey - the private key of the first recipient (sender)recipient1Cert - the certificate of the first recipient (sender)recipient2PrivateKey - the private key of the second recipientrecipient2Cert - the certificate of the second recipientjava.lang.Exception - if some error occurs during decoding/decryptionpublic byte[] createSignedDataStream(byte[] message, int mode, iaik.asn1.structures.AlgorithmID hashAlgorithm, iaik.asn1.structures.AlgorithmID signatureAlgorithm, java.security.PrivateKey signerKey, iaik.x509.X509Certificate[] certificates) throws iaik.cms.CMSException, java.io.IOException
SignedDataStream object and wraps it by a
CMS ContentInfoStream.message - the message to be signed, as byte representationmode - the transmission mode, either IMPLICIT or EXPLICIThashAlgorithm - the hash algorithm to be usedsignatureAlgorithm - the signature algorithm to be usedsignerKey - the private key of the signercertificates - the certificate chain of the signerContentInfo object just creatediaik.cms.CMSException - if the SignedData, ContentInfo
object cannot be createdjava.io.IOException - if an I/O related error occurspublic byte[] getSignedDataStream(byte[] signedData, byte[] message, iaik.x509.X509Certificate[] certificates) throws iaik.cms.CMSException, java.io.IOException
ContentInfo object holding a SignedData
object and verifies the signature.signedData - the ContentInfo holding the SignedData
object as BER encoded byte arraymessage - the the message which was transmitted out-of-band (explicit signed)certificates - the certificate of the signer (used for alternative signature verification)iaik.cms.CMSException - if any signature does not verifyjava.io.IOException - if an I/O related error occurspublic byte[] createSignedData(byte[] message, int mode, iaik.asn1.structures.AlgorithmID hashAlgorithm, iaik.asn1.structures.AlgorithmID signatureAlgorithm, java.security.PrivateKey signerKey, iaik.x509.X509Certificate[] certificates) throws iaik.cms.CMSException, java.io.IOException
SignedData object and wraps it by a CMS
ContentInfo object.
message - the message to be signed, as byte representationmode - the mode, either SignedData.IMPLICIT or SignedData.EXPLICIThashAlgorithm - the hash algorithm to be usedsignatureAlgorithm - the signature algorithm to be usedsignerKey - the private key of the signercertificates - the certificate chain of the signerSignedData-ContentInfo objectiaik.cms.CMSException - if the SignedData-ContentInfo object cannot
be createdjava.io.IOException - if an I/O related error occurspublic byte[] getSignedData(byte[] signedData, byte[] message, iaik.x509.X509Certificate[] certificates) throws iaik.cms.CMSException, java.io.IOException
ContentInfo holding a SignedData
object and verifies the signature.signedData - the ContentInfo holding the SignedData
object as DER encoded byte arraymessage - the message which was transmitted out-of-band (explicit signed)certificates - the certificate of the signer (used for alternative signature verification)iaik.cms.CMSException - if any signature does not verifyjava.io.IOException - if an I/O related error occurspublic byte[] createCompressedDataStream(byte[] message) throws iaik.cms.CMSException, java.io.IOException, java.security.NoSuchAlgorithmException
CompressedData object.
message - the message to be compressed, as byte representationCompressedData object just creatediaik.cms.CMSException - if the CompressedData object cannot
be createdjava.io.IOException - if an I/O error occursjava.security.NoSuchAlgorithmException - if the compression algorithm is not supportedpublic byte[] getCompressedDataStream(byte[] encoding) throws iaik.cms.CMSException, java.io.IOException, java.security.NoSuchAlgorithmException
CompressedData object.encoding - the CompressedData object as BER encoded byte arrayiaik.cms.CMSException - if the CompressedData cannot be parsedjava.io.IOException - if an I/O error occursjava.security.NoSuchAlgorithmException - if the compression algorithm is not supportedpublic byte[] createCompressedData(byte[] message) throws iaik.cms.CMSException, java.io.IOException, java.security.NoSuchAlgorithmException
CompressedData object.
message - the message to be compressed, as byte representationCompressedDataiaik.cms.CMSException - if the CompressedData object cannot
be createdjava.io.IOException - if an I/O error occursjava.security.NoSuchAlgorithmException - if the compression algorithm is not supportedpublic byte[] getCompressedData(byte[] encoding) throws iaik.cms.CMSException, java.io.IOException, java.security.NoSuchAlgorithmException
CompressedData object.encoding - the DER encoded CompressedData objectiaik.cms.CMSException - if the CompressedData cannot be parsedjava.io.IOException - if an I/O error occursjava.security.NoSuchAlgorithmException - if the compression algorithm is not supportedpublic void start()
public void start(iaik.asn1.structures.AlgorithmID contentAuthEncAlg, java.security.PrivateKey signerPrivateKey, iaik.x509.X509Certificate[] signerCertChain, java.security.PrivateKey recipient1PrivateKey, iaik.x509.X509Certificate recipient1Cert, java.security.PrivateKey recipient2PrivateKey, iaik.x509.X509Certificate recipient2Cert)
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