public class CMSDemo extends java.lang.Object
All keys and certificates are read from a keystore created by the SetupCMSKeyStore program.
This class demonstrates how to wrap the several CMS types into ContentInfos:
Additionally, a SignedAndEncryptedData test is performed, which is a sequential combination of signed and enveloped data content types.
All sub-tests use the same proceeding: A test message is properly processed to give the requested content type object, which subsequently is encoded to be "sent" to some recipient, who parses it for the inherent structures.
Constructor and Description |
---|
CMSDemo()
Setup the demo certificate chains.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
createAuthenticatedData(byte[] message,
boolean includeAuthAttrs,
int mode)
Creates a CMS
AuthenticatedData for the given message message. |
byte[] |
createAuthenticatedDataStream(byte[] message,
boolean includeAuthAttrs,
int mode)
Creates a CMS
AuthenticatedDataStream for the given message message. |
byte[] |
createData(byte[] message)
Creates a CMS
Data object. |
byte[] |
createDataStream(byte[] message)
Creates a CMS
Data object and wraps it into a ContentInfo. |
byte[] |
createDigestedData(byte[] message,
int mode)
Creates a CMS
DigestedData object. |
byte[] |
createDigestedDataStream(byte[] message,
int mode)
Creates a CMS
DigestedData object. |
byte[] |
createEncryptedData(byte[] message,
iaik.asn1.structures.AlgorithmID pbeAlgorithm,
char[] password)
Creates a CMS
EncryptedData message. |
byte[] |
createEncryptedDataStream(byte[] message,
iaik.asn1.structures.AlgorithmID pbeAlgorithm,
char[] password)
Creates a CMS
EncryptedDataStream message. |
byte[] |
createEnvelopedData(byte[] message)
Creates a CMS
EnvelopedData message and wraps it into a ContentInfo. |
byte[] |
createEnvelopedDataStream(byte[] message)
Creates a CMS
EnvelopedData and wraps it into a ContentInfo. |
byte[] |
createSignedAndEncryptedData(byte[] message)
Creates a SignedAndEncrypted (i.e. sequential combination of
SignedData and EnvelopedData ) object. |
byte[] |
createSignedAndEncryptedDataStream(byte[] message)
Creates a SignedAndEncrypted (i.e. sequential combination of
SignedData and EnvelopedData ). |
byte[] |
createSignedData(byte[] message,
int mode)
Creates a CMS
SignedData object and wraps it into a ContentInfo. |
byte[] |
createSignedDataStream(byte[] message,
int mode)
Creates a CMS
SignedData object ans wraps it into a ContentInfo. |
byte[] |
getAuthenticatedData(byte[] encoding,
byte[] message,
java.security.PrivateKey key,
int recipientInfoIndex)
Decrypts the encrypted MAC key for the recipient identified by its index
into the recipientInfos field and uses the MAC key to verify
the authenticated data.
|
byte[] |
getAuthenticatedDataStream(byte[] encoding,
byte[] message,
java.security.PrivateKey key,
int recipientInfoIndex)
Decrypts the encrypted MAC key for the recipient identified by its index
into the recipientInfos field and uses the MAC key to verify
the authenticated data.
|
byte[] |
getData(byte[] encoding)
Parses a CMS
Data object. |
byte[] |
getDataStream(byte[] data)
Parses a CMS
Data object. |
byte[] |
getDigestedData(byte[] encoding,
byte[] message)
Parses a CMS
DigestedData object and verifies the hash value. |
byte[] |
getDigestedDataStream(byte[] digestedData,
byte[] message)
Parses a CMS
DigestedData object and verifies the hash. |
byte[] |
getEncryptedData(byte[] encoding,
char[] password)
Decrypts the PBE-encrypted content of the given
EncryptedData object
using the specified password and returns the decrypted (= original) message. |
byte[] |
getEncryptedDataStream(byte[] encoding,
char[] password)
Decrypts the PBE-encrypted content of the given
EncryptedData object
using the specified password and returns the decrypted (= original) message. |
byte[] |
getEnvelopedData(byte[] encoding,
java.security.PrivateKey privateKey,
int recipientInfoIndex)
Decrypts the encrypted content of the given
EnvelopedData object for the
specified recipient and returns the decrypted (= original) message. |
byte[] |
getEnvelopedDataStream(byte[] encoding,
java.security.PrivateKey privateKey,
int recipientInfoIndex)
Decrypts the encrypted content of the given EnvelopedData object for the
specified recipient and returns the decrypted (= original) message.
|
byte[] |
getSignedAndEncryptedData(byte[] encoding)
Recovers the original message and verifies the signature.
|
byte[] |
getSignedAndEncryptedDataStream(byte[] in)
Recovers the original message and verifies the signature.
|
byte[] |
getSignedData(byte[] encoding,
byte[] message)
Parses a CMS
SignedData object and verifies the signatures
for all participated signers. |
byte[] |
getSignedDataStream(byte[] signedData,
byte[] message)
Parses a CMS
SignedData object and verifies the signatures
for all participated signers. |
static void |
main(java.lang.String[] argv)
Starts the CMS content type implementation tests.
|
void |
start()
Tests the CMS content type implementations
Data , EnvelopedData ,
SignedData , DigestedData , EncryptedData . |
public CMSDemo() throws java.io.IOException
java.io.IOException
- if an file read error occurspublic byte[] createDataStream(byte[] message) throws iaik.cms.CMSException, java.io.IOException
Data
object and wraps it into a ContentInfo.
message
- the message to be sent, as byte representationData
object just creatediaik.cms.CMSException
- if the Data
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getDataStream(byte[] data) throws iaik.cms.CMSException, java.io.IOException
Data
object.data
- the encoded ContentInfo holding the Data
iaik.cms.CMSException
- if an parsing exception occursjava.io.IOException
- if an I/O error occurspublic byte[] createEnvelopedDataStream(byte[] message) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
and wraps it into a ContentInfo.message
- the message to be enveloped, as byte representationiaik.cms.CMSException
- if the EnvelopedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getEnvelopedDataStream(byte[] encoding, java.security.PrivateKey privateKey, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
encoding
- the encoded ContentInfo containing an EnvelopedData objectprivateKey
- the private key to decrypt the messagerecipientInfoIndex
- the index into the RecipientInfo
array
to which the specified private key belongsiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] createSignedDataStream(byte[] message, int mode) throws iaik.cms.CMSException, java.io.IOException
SignedData
object ans wraps it into a ContentInfo.
message
- the message to be signed, as byte representationmode
- the mode indicating whether to include the content
(SignedDataStream.IMPLICIT) or not (SignedDataStream.EXPLICIT)SignedData
object just creatediaik.cms.CMSException
- if the SignedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getSignedDataStream(byte[] signedData, byte[] message) throws iaik.cms.CMSException, java.io.IOException
SignedData
object and verifies the signatures
for all participated signers.signedData
- the ContentInfo with inherent SignedData, as BER encoded byte arraymessage
- the the message which was transmitted out-of-band (explicit signed)null
if there
is no message included into the supplied SignedData
objectiaik.cms.CMSException
- if any signature does not verifyjava.io.IOException
- if an I/O error occurspublic byte[] createSignedAndEncryptedDataStream(byte[] message) throws iaik.cms.CMSException, java.io.IOException
SignedData
and EnvelopedData
).message
- the message to be signed and encrypted, as byte representationiaik.cms.CMSException
- if the the SignedData
or
EnvelopedData
object cannot be createdjava.io.IOException
- if an I/O error occurspublic byte[] getSignedAndEncryptedDataStream(byte[] in) throws iaik.cms.CMSException, java.io.IOException
in
- the encoded CMS objectiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] createDigestedDataStream(byte[] message, int mode) throws iaik.cms.CMSException, java.io.IOException
DigestedData
object.
message
- the message to be digested, as byte representationiaik.cms.CMSException
- if the DigestedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getDigestedDataStream(byte[] digestedData, byte[] message) throws iaik.cms.CMSException, java.io.IOException
DigestedData
object and verifies the hash.digestedData
- the encoded ContentInfo holding a DigestedData objectmessage
- the the message which was transmitted out-of-bandnull
if there
is no message included into the supplied DigestedData
objectiaik.cms.CMSException
- if any signature does not verifyjava.io.IOException
- if an I/O error occurspublic byte[] createEncryptedDataStream(byte[] message, iaik.asn1.structures.AlgorithmID pbeAlgorithm, char[] password) throws iaik.cms.CMSException, java.io.IOException
EncryptedDataStream
message.
The supplied content is PBE-encrypted using the specified password.
message
- the message to be encrypted, as byte representationpbeAlgorithm
- the PBE algorithm to be usedpassword
- the passwordEncryptedData
object just creatediaik.cms.CMSException
- if the EncryptedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getEncryptedDataStream(byte[] encoding, char[] password) throws iaik.cms.CMSException, java.io.IOException
EncryptedData
object
using the specified password and returns the decrypted (= original) message.encoding
- the encoded ContentInfo holding an EncryptedData
objectpassword
- the password to decrypt the messageiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] createAuthenticatedDataStream(byte[] message, boolean includeAuthAttrs, int mode) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedDataStream
for the given message message.
Attention: This AuthenticatedData demo uses RSA as key management technique.
In practice (see RFC 5652) a key management technique that provides data origin
authentication should be used like, for instance, Static-Static Diffie-Hellman when
both the originator and recipient public keys are bound to appropriate identities
in X.509 certificates, see, for instance, AuthenticatedDataDemo
.
message
- the message to be authenticated, as byte representationincludeAuthAttrs
- whether to include authenticated attributesmode
- the mode indicating whether to include the content
(AuthenticatedDataStream.IMPLICIT) or not (AuthenticatedDataStream.EXPLICIT)AuthenticatedData
object, wrapped in a ContentInfoiaik.cms.CMSException
- if the AuthenticatedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getAuthenticatedDataStream(byte[] encoding, byte[] message, java.security.PrivateKey key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
This way of decrypting the MAC key and verifying the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo), but requires to know at what index of the recipientInfos 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 mac keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted mac key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.
Attention: This AuthenticatedData demo uses RSA as key management technique.
In practice (see RFC 5652) a key management technique that provides data origin
authentication should be used like, for instance, Static-Static Diffie-Hellman when
both the originator and recipient public keys are bound to appropriate identities
in X.509 certificates, see, for instance, AuthenticatedDataDemo
.
encoding
- the BER encoded ContentInfo holding the AuthenticatedData
objectmessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the mac keyrecipientInfoIndex
- the index of the right RecipientInfo
to
which the given key belongsiaik.cms.CMSException
- if the authenticated data cannot be verifiedjava.io.IOException
- if a stream read/write error occurspublic byte[] createData(byte[] message) throws iaik.cms.CMSException
Data
object.
message
- the message to be sent, as byte representationData
object just creatediaik.cms.CMSException
- if the Data
object cannot
be createdpublic byte[] getData(byte[] encoding) throws iaik.cms.CMSException, java.io.IOException
Data
object.encoding
- the DER encoded ContentInfo holding with inherent Data
iaik.cms.CMSException
- if an parsing exception occursjava.io.IOException
- if an I/O related error occurspublic byte[] createEnvelopedData(byte[] message) throws iaik.cms.CMSException
EnvelopedData
message and wraps it into a ContentInfo.
message
- the message to be enveloped, as byte representationiaik.cms.CMSException
- if the EnvelopedData
object cannot
be createdpublic byte[] getEnvelopedData(byte[] encoding, java.security.PrivateKey privateKey, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
EnvelopedData
object for the
specified recipient and returns the decrypted (= original) message.encoding
- the DER encoded ContentInfo holding an EnvelopedDataprivateKey
- the private key to decrypt the messagerecipientInfoIndex
- the index into the RecipientInfo
array
to which the specified private key belongsiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
public byte[] createSignedData(byte[] message, int mode) throws iaik.cms.CMSException
SignedData
object and wraps it into a ContentInfo.message
- the message to be signed, as byte representationmode
- the mode indicating whether to include the content
(SignedDataStream.IMPLICIT) or not (SignedDataStream.EXPLICIT)SignedData
object just creatediaik.cms.CMSException
- if the SignedData
object cannot
be createdpublic byte[] getSignedData(byte[] encoding, byte[] message) throws iaik.cms.CMSException, java.io.IOException
SignedData
object and verifies the signatures
for all participated signers.encoding
- the ContentInfo with inherent SignedData
object, as DER encodingmessage
- the the message which was transmitted out-of-band (explicit signed)null
if there
is no message included into the supplied SignedData
objectiaik.cms.CMSException
- if any signature does not verifyjava.io.IOException
- if an I/O error occurspublic byte[] createSignedAndEncryptedData(byte[] message) throws iaik.cms.CMSException
SignedData
and EnvelopedData
) object.message
- the message to be signed and encrypted, as byte representationiaik.cms.CMSException
- if the the SignedData
or
EnvelopedData
object cannot be createdpublic byte[] getSignedAndEncryptedData(byte[] encoding) throws iaik.cms.CMSException, java.io.IOException
encoding
- the DER encoded ContentInfo holding a SignedAndEnryptedData objectiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] createDigestedData(byte[] message, int mode) throws iaik.cms.CMSException
DigestedData
object.
message
- the message to be digested, as byte representationDigestedData
wrapped into a ContentInfo, as DER encodingiaik.cms.CMSException
- if the DigestedData
object cannot
be createdpublic byte[] getDigestedData(byte[] encoding, byte[] message) throws iaik.cms.CMSException, java.io.IOException
DigestedData
object and verifies the hash value.encoding
- the ContentInfo holding a DigestedData
, as DER encodingmessage
- the the message which was transmitted out-of-band (explicit digested)iaik.cms.CMSException
- if some parsing exception occursjava.io.IOException
- if an I/O error occurspublic byte[] createEncryptedData(byte[] message, iaik.asn1.structures.AlgorithmID pbeAlgorithm, char[] password) throws iaik.cms.CMSException
EncryptedData
message.
The supplied content is PBE-encrypted using the specified password.
message
- the message to be encrypted, as byte representationpbeAlgorithm
- the PBE algorithm to be usedpassword
- the passwordEncryptedData
object wrapped into a ContentInfo, as DER encodingiaik.cms.CMSException
- if the EncryptedData
object cannot
be createdpublic byte[] getEncryptedData(byte[] encoding, char[] password) throws iaik.cms.CMSException, java.io.IOException
EncryptedData
object
using the specified password and returns the decrypted (= original) message.encoding
- the DER encoded ContentInfo holding the EncryptedData
objectpassword
- the password to decrypt the messageiaik.cms.CMSException
- if the message cannot be recoveredjava.io.IOException
- if an I/O error occurspublic byte[] createAuthenticatedData(byte[] message, boolean includeAuthAttrs, int mode) throws iaik.cms.CMSException, java.io.IOException
AuthenticatedData
for the given message message.
Attention: This AuthenticatedData demo uses RSA as key management technique.
In practice (see RFC 5652) a key management technique that provides data origin
authentication should be used like, for instance, Static-Static Diffie-Hellman when
both the originator and recipient public keys are bound to appropriate identities
in X.509 certificates, see, for instance, AuthenticatedDataDemo
.
message
- the message to be authenticated, as byte representationincludeAuthAttrs
- whether to include authenticated attributesmode
- the mode indicating whether to include the content
(AuthenticatedData.IMPLICIT) or not (AuthenticatedDatam.EXPLICIT)AuthenticatedData
object, wrapped in a ContentInfoiaik.cms.CMSException
- if the AuthenticatedData
object cannot
be createdjava.io.IOException
- if an I/O error occurspublic byte[] getAuthenticatedData(byte[] encoding, byte[] message, java.security.PrivateKey key, int recipientInfoIndex) throws iaik.cms.CMSException, java.io.IOException
This way of decrypting the MAC key and verifying the content may be used for any type of RecipientInfo (KeyTransRecipientInfo, KeyAgreeRecipientInfo, KEKRecipientInfo), but requires to know at what index of the recipientInfos 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 mac keys for more than only one recipient; since the recipientInfoIndex only specifies the RecipientInfo but not the encrypted mac key -- if there are more than only one -- repeated decryption runs may be required as long as the decryption process completes successfully.
Attention: This AuthenticatedData demo uses RSA as key management technique.
In practice (see RFC 5652) a key management technique that provides data origin
authentication should be used like, for instance, Static-Static Diffie-Hellman when
both the originator and recipient public keys are bound to appropriate identities
in X.509 certificates, see, for instance, AuthenticatedDataDemo
.
encoding
- the DER encoded ContentInfo holding the AuthenticatedData
objectmessage
- the content message, if transmitted by other means (explicit mode)key
- the key to decrypt the mac keyrecipientInfoIndex
- the index of the right RecipientInfo
to
which the given key belongsiaik.cms.CMSException
- if the authenticated data cannot be verifiedjava.io.IOException
- if a IO read/write error occurspublic void start()
Data
, EnvelopedData
,
SignedData
, DigestedData
, EncryptedData
.
An additional SignedAndEncryptedData test sequentially combines
signed and enveloped data.public static void main(java.lang.String[] argv) throws java.lang.Exception
java.lang.Exception
- if some error occurs