|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object iaik.cms.AuthEnvelopedDataStream
public class AuthEnvelopedDataStream
This class represents the stream-supporting implementation of the CMS content
type AuthEnvelopedData
as defined in RFC 5083.
The AuthEnvelopedData
content type is identified by the following
object identifier (see RFC 5083):
id-ct-authEnvelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-9(9) smime(16) ct(1) 23 }which corresponds to the OID string "1.2.840.113549.1.9.16.1.23".
RFC 5083
specifies the AuthEnvelopedData
content type for use with
authenticated cipher modes like AES-CCM or AES-GCM (RFC 5084).
RFC 8103
specifies the AuthEnvelopedData
content type for use with
the ChaCha20-Poly1305 authenticated encryption algorithm (RFC 7539).
Using authenticated encryption the data is not only encrypted but also integrity
protected by a message authentication code. Similar to the EnvelopedData
and AuthenticatedData types content of any type may be authenticated
enveloped for any number of recipients in parallel. For each recipient, a
commonly at random generated content-authenticated-encryption key is
encrypted with the particular recipient key and - together with
recipient-specific information - collected into a RecipientInfo
value. Using a authenticated encryption method the content is authenticated
and encrypted with the content-authenticated-encryption key. All RecipientInfo
values are collected together with the authenticated encrypted content into an
AuthEnvelopedData
object to be sent to each intended recipients.
The AuthEnvelopedData
may also contain authenticated attributes
(which are authenticated but not encrypted) and unauthenticated attributes
(which are neither encrypted nor authenticated), (see RFC 5083:
AuthEnvelopedData ::= SEQUENCE { version CMSVersion, originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL, recipientInfos RecipientInfos, authEncryptedContentInfo EncryptedContentInfo, authAttrs [1] IMPLICIT AuthAttributes OPTIONAL, mac MessageAuthenticationCode, unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }The
recipientInfos
field is a non-empty collection of
per-recipient information. The authEncryptedContentInfo
field
specifies the type of the content being authenticated encrypted,
the content-authenticated-encryption algorithm (the same for all recipients)
used for authenticated encrypting the content, and the result of the
content authenticated encryption.
KeyTransRecipientInfo
for the public key of the recipient
certificate, identified by IssuerAndSerialNumber
.
KeyAgreeRecipientInfo
may transfer the encrypted content-authenticated-encryption
key to one or more recipient using the same key
agreement algorithm and domain parameters for that algorithm.
KEKRecipientInfo
using a CMS key wrap algorithm like
AES key wrap.
PassowrdRecipientInfo
using a CMS key wrap algorithm like PWRI-KEK (RFC 3211).
OtherRecipientInfo
.
originatorInfo
field may be used to include certificate/crl
information about the originator if required by the key management protocol.
The unauthAttrs
field may contain some attributes that are authenticated
but not encrypted, the unauthAttrs
may provide some attributes that are
not protected.
A recipient, when receiving the AuthEnvelopedData
message,
decrypts the corresponding encrypted content-authenticated-encryption key with his/her
key according the right key management protocol for subsequently decrypting the
encrypted content and verifying the mac value using the content-authenticated-encryption
key just recovered. The recipient key is referenced by proper KeyIdentifier
included in the corresponding RecipientInfo
object.
See RFC 5652 for more information.
When creating a new AuthEnvelopedDataStream
object for the data to be authenticated
enveloped the symmetric algorithm has to be specified to be used for content-authenticated-encryption.
After setting the recipients, the AuthEnvelopedDataStream object may be encoded and written to an
output stream by using a proper writeTo
method, e.g.:
AuthEnvelopedDataStream(InputStream is, AlgorithmID contentAuthEncAlg)
constructor. When using this constructor automatically a temporary symmetric key for
authenticated content encryption will be generated. If you want to use a
content-authenticated-encryption algorithm allowing keys of different size, you may specify
the key length by using the AuthEnvelopedDataStream(InputStream is, AlgorithmID contentAuthEncAlg, int keyLength)
constructor.
//the data to be auth-enveloped supplied from an input stream: InputStream dataStream = ...; //use AES-GCM mode for authenticated encrypting the content AuthEnvelopedDataStream authEnvelopedData = new AuthEnvelopedDataStream(dataStream, (AlgorithmID)AlgorithmID.aes256_GCM.clone());
RecipientInfo
object of requested type (KeyTransRecipientInfo
, KeyAgreeRecipientInfo
,
or KEKRecipientInfo
), and add all RecipientInfos
to the AuthEnvelopedDataStream object by calling the setRecipientInfos
method, e.g.
(assuming to use KeyTransRecipientInfos for two recipients with corresponding
certificates cert1
and cert2
):
RecipientInfo[] recipients = new RecipientInfo[2]; recipients[0] = new KeyTransRecipientInfo(cert1, (AlgorithmID)AlgorithmID.rsaEncryption.clone()); recipients[1] = new KeyTransRecipientInfo(cert2, (AlgorithmID)AlgorithmID.rsaEncryption.clone()); authEnvelopedData.setRecipientInfos(recipients);
writeTo
method for BER encoding the
AuthEnvelopedData object and writing it to an output stream. You optionally may specify
a particular block size for splitting the encoding of encrypted content. This step
also will perform generation and encryption of the symmetric content-authenticated-encryption
key for each participated recipient.
int blockSize = ...; OutputStream encodedStream = ...; authEnvelopedData.writeTo(encoded_stream, blockSize);respectively
authEnvelopedData.writeTo(encodedStream);
AuthEnvelopedDataStream(InputStream is)
constructor for parsing the internal structure. Before reading the recovered content by
means of the getInputStream
method, the cipher has to be
initialized for decryption and mac verification with the particular recipient key by
calling one of the following setupCipher
methods:
setupCipher(Key recipientKey, int recipientInfoIndex)
:
This method can be used for decrypting the encrypted content-authenticated-encryption key for
a RecipientInfo of any type; however, you must know the right index into the
recipientInfo
field. The recovered content-authenticated-encryption key is used
for setting up the cipher to decrypt the encrypted content and verify the message authentication
code.
setupCipher(Key recipientKey,
KeyIdentifier recipientKeyIdentifier)
: This method can be used for decrypting the
encrypted content-authenticated-encryption key for a RecipientInfo of any type; the recipient is
identified by its RecipientKeyIdentifier
.
The recovered content-authenticated-encryption key is used for setting up the cipher to decrypt
the encrypted content and verify the message authentication code.
setupCipher(Key recipientKey,
X509Certificate recipientCertificate)
: This method only can be used for decrypting
the encrypted content-authenticated-encryption key for a RecipientInfo of type KeyTransRecipientInfo
or KeyAgreeRecipientInfo
; the recipient is
identified by its public key certificate. The recovered content-authenticated-encryption key is used
for setting up the cipher to decrypt the encrypted content and verify the message authentication
code.
setupCipher(Key)
: In contrast to the three setupCipher
above you have to supply the already decrypted content-authenticated-encryption key when setting
up the cipher for content decryption and mac verification using this setupCipher
method.
KeyTransRecipientInfo
,
we can use any of the setupCipher
methods to setup the cipher for
decrypting the encrypted content-authenticated-encryption key, decrypting the encrypted content
and verifying the message authenitication code:
InputStream encodedStream = ...; AuthEnvelopedDataStream authEnvelopedData = new AuthEnvelopedDataStream(encodedStream);
EncryptedContentInfoStream eci = (EncryptedContentInfoStream)authEnvelopedData.getEncryptedContentInfo(); System.out.println("Content type: "+eci.getContentType().getName()); System.out.println("content authenticated encryption algorithm: "+eci.getContentEncryptionAlgorithm().getName());
RecipientInfo[] recipients = authEnvelopedData.getRecipientInfos(); System.out.println("Included RecipientInfos:"); for (int i=0; i < recipients.length; i++) { System.out.print("Recipient "+(i+1)+":"); System.out.println(recipients[i]); }
// the private key of recipient 1: Key privateKey = ...; //setup cipher for recipient 1: int recipientInfoIndex = 0; authEnvelopedData.setupCipher(privateKey, recipientInfoIndex);Unlike the non-stream supporting
AuthEnvelopedData
class where the encrypted-content decryption
already is performed inside the setupCipher
method, the cipher
will be only initialized for decryption in this class. The encrypted-content
decryption actually is done during reading the data obtained by calling the
getInputStream
method. So do not call
getInputStream
before setting up the cipher!
InputStream dataIs = authEnvelopedData.getInputStream(); byte[] buf = new byte[2048]; int r; while ((r = dataIs.read(buf)) > 0) { // do something useful }
Note: RFC 5084
specifies the usage of the AuthEnvelopedData
type for the
AES-CCM and AES-GCM authenticated encryption algorithms, both are supported
by this implementation when used with IAIK-JCE, version > 3.17.
RFC 8103
specifies the AuthEnvelopedData
content type for use with
the ChaCha20-Poly1305 authenticated encryption algorithm (RFC 7539).
ChaCha20-Poly1305 for CMS requires IAIK-JCE version 5.62 or later.
Unfortunately the structure of the AuthEnvelopedData
type does not
allow one-pass processing when parsing an AuthEnvelopedData
object
that uses AES-CCM or AES-GCM or ChaCha20-Poly1305 for authenticated content encryption since
they require to know the additional authenticated data (authenticated attributes) before processing the
content data. However, the authAttrs
field is located behind the
authEncryptedContentInfo
field. Thus the content must be read before
having access to the authenticated attributes which are needed as additional
authenticated data input the AEAD Cipher. For that reason a non-stream
EncryptedContentInfo
is used by this AuthenticatedDataStream
implementation for parsing the encrypted content of AuthEnvelopedData objects
that have AES-CCM, AES-GCM or ChaCha20Poly1305 as content-authenticated encryption algorithm.
AES-CCM furthermore needs to know the size of the to-be-auth-encrypted content in advance
when creating an AuthEnvelopedData object. If you know to data input length
(e.g. the size of a data file) you may set it by calling method setInputLength
, otherwise tha data has to be internally buffered before starting
the authentication-encryption process.
Note: This class does not provide a verifyMac
method. For
authenticated encryption modes like CCM and GCM Mac verification is part of
the decryption/verification procedure. When using some other content authentication
encryption method that follows a different verification strategy, be sure
to implement corresponding
authentication cipher engines
.
RecipientInfo
,
KeyTransRecipientInfo
,
KeyAgreeRecipientInfo
,
KEKRecipientInfo
,
PasswordRecipientInfo
,
OtherRecipientInfo
Field Summary | |
---|---|
protected EncryptedContentInfoStream |
encryptedContentInfo_
The EncryptedContentInfo for the encrypted content. |
static int |
EXPLICIT
Denotes a mode where the authenticated encrypted message is not transported within the AuthEnvelopedData (EncryptedContentInfo). |
static int |
IMPLICIT
Denotes a mode where the authenticated encrypted message is included in the AuthEnvelopedData (EncryptedContentInfo). |
Constructor Summary | |
---|---|
protected |
AuthEnvelopedDataStream()
Default constructor for dynamic object creation in ContentInfoStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is)
Creates a new AuthEnvelopedDataStream from a BER encoded AuthEnvelopedData object which is read from the given InputStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is,
AlgorithmID contentAuthEncAlg)
Creates a new AuthEnvelopedDataStream object where the content to be authenticated enveloped is read from the supplied InputStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is,
AlgorithmID contentAuthEncAlg,
int keyLength)
Creates a new AuthEnvelopedDataStream object where the content to be authenticated enveloped is read from the supplied InputStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is,
byte[] encodedAuthAttributes,
long inputLength)
Creates a new AuthEnvelopedDataStream from a BER encoded AuthEnvelopedData object which is read from the given InputStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is,
byte[] encodedAuthAttributes,
long inputLength,
SecurityProvider securityProvider)
Creates a new AuthEnvelopedDataStream from a BER encoded AuthEnvelopedData object which is read from the given InputStream. |
|
AuthEnvelopedDataStream(java.io.InputStream is,
SecurityProvider securityProvider)
Creates a new AuthEnvelopedDataStream from a BER encoded AuthEnvelopedData object which is read from the given InputStream. |
|
AuthEnvelopedDataStream(ObjectID contentType,
java.io.InputStream is,
AlgorithmID contentAuthEncAlg)
Creates a new AuthEnvelopedDataStream object to be authenticated enveloped is read from the supplied InputStream. |
|
AuthEnvelopedDataStream(ObjectID contentType,
java.io.InputStream is,
AlgorithmID contentAuthEncAlg,
int keyLength)
Creates a new AuthEnvelopedDataStream object where the content to be authenticated enveloped is read from the supplied InputStream. |
|
AuthEnvelopedDataStream(ObjectID contentType,
java.io.InputStream is,
AlgorithmID contentAuthEncAlg,
int keyLength,
SecurityProvider securityProvider)
Creates a new AuthEnvelopedDataStream object where the content to be authenticated enveloped is read from the supplied InputStream. |
|
AuthEnvelopedDataStream(RecipientInfo[] recipients,
EncryptedContentInfoStream encryptedContentInfo)
Constructs an AuthEnvelopedDataStream object with an already created EncryptedContentInfoStream. |
Method Summary | |
---|---|
void |
addRecipientInfo(RecipientInfo recipient)
Adds one recipient to the list of recipient infos. |
void |
decode(java.io.InputStream is)
Reads and decodes an BER encoded AuthEnvelopedData from a the given input stream. |
void |
decode(java.io.InputStream is,
boolean useStream)
Reads and decodes an BER encoded AuthEnvelopedData from a the given input stream. |
void |
encodeCalled(ASN1Object obj,
int id)
This method implements the EncodeListener interface. |
Attribute |
getAuthenticatedAttribute(ObjectID oid)
Returns the first authenticated attribute matching to the given ObjectID, if included in this AuthEnvelopedData object. |
Attribute[] |
getAuthenticatedAttributes()
Gets the authenticated attributes included in this AuthEnvelopedData. |
int |
getBlockSize()
Gets the block size defining the length of each definite primitive encoded octet string component. |
ObjectID |
getContentType()
Returns the content type this class implements. |
EncryptedContentInfoStream |
getEncryptedContentInfo()
Returns the EncryptedContentInfo included in this AuthEnvelopedDataStream object. |
long |
getInputLength()
Gets the length of input data. |
java.io.InputStream |
getInputStream()
Returns an InputStream from where the decrypted data can be read. |
byte[] |
getMac()
Gets the MAC value. |
int |
getMode()
Gets the mode of this AuthEnvelopedDataStream. |
OriginatorInfo |
getOriginatorInfo()
Gets the OriginatorInfo, if included. |
RecipientInfo |
getRecipientInfo(KeyIdentifier recipientIdentifier)
Returns the RecipientInfo belonging to the recipient identified by the given recipient identifier. |
RecipientInfo |
getRecipientInfo(X509Certificate recipientCertificate)
Returns the recipient info matching to the supplied recipient certificate. |
RecipientInfo[] |
getRecipientInfos()
Returns all the recipient infos included in this AuthEnvelopedData object. |
RecipientInfo[] |
getRecipientInfos(int type)
Returns all the recipient infos included in this AuthEnvelopedData object that have the specified type. |
SecurityProvider |
getSecurityProvider()
Gets the SecurityProvider installed for this EncryptedDataStream. |
Attribute |
getUnauthenticatedAttribute(ObjectID oid)
Returns the first unauthenticated attribute matching to the given ObjectID, if included in this AuthEnvelopedData object. |
Attribute[] |
getUnauthenticatedAttributes()
Gets the unauthenticated attributes included in this AuthEnvelopedData. |
int |
getVersion()
Returns the syntax version number. |
void |
notifyEOF()
This method implements the EOFListener interface for performing the final decoding of the attributes and mac field components. |
void |
setAuthenticatedAttributes(Attribute[] attributes)
Sets a set of authenticated attributes. |
void |
setBlockSize(int blockSize)
Sets the block size for defining the length of each definite primitive encoded octet string component. |
void |
setInputLength(long inputLength)
Sets the length of input data. |
void |
setInputStream(java.io.InputStream is)
Sets the input stream that supplies the content data to be authenticated encrypted. |
void |
setMac(byte[] mac)
Sets the MAC value (if calculated outside). |
void |
setMode(int mode)
Sets the mode for this AuthEnvelopedDataStream. |
void |
setOriginatorInfo(OriginatorInfo originatorInfo)
Sets the optional OriginatorInfo. |
void |
setRecipientInfos(RecipientInfo[] recipients)
Sets the recipient infos. |
void |
setSecurityProvider(SecurityProvider securityProvider)
Sets the SecurityProvider for this AuthEnvelopedDataStream. |
void |
setUnauthenticatedAttributes(Attribute[] attributes)
Sets a set of unauthenticated attributes. |
void |
setupCipher(java.security.Key key)
Uses the given symmetric key to setup the cipher for decrypting the content and verifying the message authentication code. |
javax.crypto.SecretKey |
setupCipher(java.security.Key recipientKey,
int recipientInfoIndex)
Uses the specified key for decrypting the content-authenticated-encryption key to setup the cipher for decrypting the encrypted content and verifying the mac of this AuthEnvelopedDataStream object for the requesting recipient, specified by
its recipientInfoIndex . |
javax.crypto.SecretKey |
setupCipher(java.security.Key recipientKey,
KeyIdentifier recipientIdentifier)
Uses the specified key for decrypting the content-authenticated-encryption key to setup the cipher for decrypting the encrypted content and verifying the mac of this AuthEnvelopedDataStream object for the requesting recipient, specified
by the given recipient identifier. |
javax.crypto.SecretKey |
setupCipher(java.security.Key recipientKey,
X509Certificate recipientCertificate)
Uses the specified key for decrypting the content-authenticated-encryption key to setup the cipher for decrypting the encrypted content and verifying the mac of this AuthEnvelopedDataStream object for the requesting recipient, specified by
the given recipient certificate. |
ASN1Object |
toASN1Object()
Returns this AuthEnvelopedDataStream as ASN1Object. |
protected ASN1Object |
toASN1Object(int blockSize)
Returns this AuthEnvelopedData as ASN1Object. |
java.lang.String |
toString()
Returns a string giving some information about this AuthEnvelopedData object. |
java.lang.String |
toString(boolean detailed)
Returns a string giving some - if requested - detailed information about this AuthEnvelopedData object. |
void |
writeTo(java.io.OutputStream os)
Writes this AuthEnvelopedData BER encoded to the supplied output stream. |
void |
writeTo(java.io.OutputStream os,
int blockSize)
Writes this AuthEnvelopedData BER encoded to the supplied output stream where a constructed OCTET STRING may be used for encoding the content. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int IMPLICIT
public static final int EXPLICIT
protected EncryptedContentInfoStream encryptedContentInfo_
Constructor Detail |
---|
protected AuthEnvelopedDataStream()
public AuthEnvelopedDataStream(java.io.InputStream is, AlgorithmID contentAuthEncAlg)
The content type is set to CMS Data
.
When using this constructor, automatically a temporary symmetric key for authenticated content
encryption will be generated. If you want to use a content-authenticated-encryption algorithm
allowing keys of different size, you may specify the key length by using the AuthEnvelopedDataStream(InputStream is, AlgorithmID contentAuthEncAlg, int keyLength)
constructor.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
is
- the InputStream supplying the data to be authenticated envelopedcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmpublic AuthEnvelopedDataStream(ObjectID contentType, java.io.InputStream is, AlgorithmID contentAuthEncAlg)
When using this constructor, automatically a temporary symmetric key for authenticated content
encryption will be generated. If you want to use a content-authenticated-encryption algorithm
allowing keys of different size, you may specify the key length by using the AuthEnvelopedDataStream(ObjectID contentType, InputStream is, AlgorithmID contentAuthEncAlg, int keyLength)
constructor.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
contentType
- the content type of the content to be authenticated envelopedis
- the InputStream containing the data to be authenticated envelopedcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmpublic AuthEnvelopedDataStream(java.io.InputStream is, AlgorithmID contentAuthEncAlg, int keyLength)
When using this constructor, automatically a symmetric key for authenticated content
encryption will be generated. If the specified content authenticated encryption algorithm
supports variable key lengths, a particular key length may be set by means of the
keyLength
parameter.
If no length is specified, the defined default key length will be used. If the
algorithm only works with keys of fixed-size length, the keyLength parameter
may be set to -1 or the AuthEnvelopedDataStream(InputStream is, AlgorithmID contentAuthEncAlg)
constructor may be used.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
is
- the InputStream containing the data to be authenticated envelopedcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmkeyLength
- the length of the content-authenticated-encryption key to be generated
(when using a content-authenticated-encryption algorithm that supports
variable key lengths)public AuthEnvelopedDataStream(ObjectID contentType, java.io.InputStream is, AlgorithmID contentAuthEncAlg, int keyLength)
When using this constructor, automatically a symmetric key for authenticated content
encryption is generated.
If the specified content authenticated encryption algorithm supports variable key lengths, a
particular key length may be set by means of the keyLength
parameter.
If no length is specified, the defined default key length will be used. If the
algorithm only works with keys of fixed-size length, the keyLength parameter
may be set to -1 or the AuthEnvelopedDataStream(ObjectID contentType, InputStream is, AlgorithmID contentAuthEncAlg)
constructor may be used.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
contentType
- the content type of the content to be authenticated envelopedis
- the InputStream containing the data to be authenticated envelopedcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmkeyLength
- the length of the content-authenticated-encryption key to be generated
(when using a content-authenticated-encryption algorithm that supports
variable key lengths)public AuthEnvelopedDataStream(ObjectID contentType, java.io.InputStream is, AlgorithmID contentAuthEncAlg, int keyLength, SecurityProvider securityProvider)
When using this constructor, automatically a symmetric key for authenticated
content encryption will be generated.
If the specified content authenticated encryption algorithm supports variable key lengths, a
particular key length may be set by means of the keyLength
parameter.
If no length is specified, the defined default key length will be used. If the
algorithm only works with keys of fixed-size length, the keyLength parameter
may be set to -1 or the AuthEnvelopedDataStream(ObjectID contentType, InputStream is, AlgorithmID contentAuthEncAlg)
constructor may be used.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
contentType
- the content type of the content to be authenticated envelopedis
- the InputStream containing the data to be authenticated envelopedcontentAuthEncAlg
- the id of the content-authenticated encryption algorithmkeyLength
- the length of the content-authenticated-encryption key to be generated
(when using a content-authenticated-encryption algorithm that supports
variable key lengths)securityProvider
- the security provider to be used; if null
the
default system-wide SecurityProvider is usedpublic AuthEnvelopedDataStream(RecipientInfo[] recipients, EncryptedContentInfoStream encryptedContentInfo)
The given array of RecipientInfo
specifies a collection of
per-recipient information, and the given EncryptedContentInfoStream
is responsible for authenticated content encryption.
recipients
- information about the recipientsencryptedContentInfo
- the EncryptedContentInfopublic AuthEnvelopedDataStream(java.io.InputStream is) throws CMSParsingException, java.io.IOException
is
- the InputStream supplying a BER encoded CMS AuthEnvelopedData object
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic AuthEnvelopedDataStream(java.io.InputStream is, SecurityProvider securityProvider) throws CMSParsingException, java.io.IOException
is
- the InputStream supplying a BER encoded CMS AuthEnvelopedData objectsecurityProvider
- the security provider to be used; if null
the
default system-wide SecurityProvider is used
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic AuthEnvelopedDataStream(java.io.InputStream is, byte[] encodedAuthAttributes, long inputLength) throws CMSParsingException, java.io.IOException
AEAD algorithm modes like GCM or CCM need to know the additional authenticated data
before processing (decrypting) the encrypted data. However, the authAttrs
field of an AuthEnvelopedData lies behind the authEncryptedContentInfo
field that holds the encrypted data, (see RFC 5083:
AuthEnvelopedData ::= SEQUENCE { version CMSVersion, originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL, recipientInfos RecipientInfos, authEncryptedContentInfo EncryptedContentInfo, authAttrs [1] IMPLICIT AuthAttributes OPTIONAL, mac MessageAuthenticationCode, unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }For that reason this implementation
by default
uses a non-stream EncryptedContentInfo
to parse whole the
encrypted and read the authAttrs
field before initializing the Cipher for
decryption. However, when no authenticated attributes have been used (or the authenticated
attributes are known in advance) the decryption process can be done stream based. For doing so
this constructor uses an EncryptedContentInfoStream
for
parsing the encrypted content. If authenticated attributes have been used, their encoding may
be specified.
EncryptedContentInfo
will be used to read the encrypted data
and determining its length before starting the decryption process.
is
- the InputStream supplying a BER encoded CMS AuthEnvelopedData objectencodedAuthAttributes
- the encoded authenticated attributes (or null
if no authenticated attributes have been used)inputLength
- the length of the input data (or -1 if not known)
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic AuthEnvelopedDataStream(java.io.InputStream is, byte[] encodedAuthAttributes, long inputLength, SecurityProvider securityProvider) throws CMSParsingException, java.io.IOException
AEAD algorithm modes like GCM or CCM need to know the additional authenticated data
before processing (decrypting) the encrypted data. However, the authAttrs
field of an AuthEnvelopedData lies behind the authEncryptedContentInfo
field that holds the encrypted data, (see RFC 5083:
AuthEnvelopedData ::= SEQUENCE { version CMSVersion, originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL, recipientInfos RecipientInfos, authEncryptedContentInfo EncryptedContentInfo, authAttrs [1] IMPLICIT AuthAttributes OPTIONAL, mac MessageAuthenticationCode, unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }For that reason this implementation
by default
uses a non-stream EncryptedContentInfo
to parse whole the
encrypted and read the authAttrs
field before initializing the Cipher for
decryption. However, when no authenticated attributes have been used (or the authenticated
attributes are known in advance) the decryption process can be done stream based. For doing so
this constructor uses an EncryptedContentInfoStream
for
parsing the encrypted content. If authenticated attributes have been used, their encoding may
be specified.
EncryptedContentInfo
will be used to read the encrypted data
and determining its length before starting the decryption process.
is
- the InputStream supplying a BER encoded CMS AuthEnvelopedData objectencodedAuthAttributes
- the encoded authenticated attributes (or null
if no authenticated attributes have been used)inputLength
- the length of the input data (or -1 if not known)securityProvider
- the security provider to be used; if null
the
default system-wide SecurityProvider is used
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectMethod Detail |
---|
public void setSecurityProvider(SecurityProvider securityProvider)
This method allows to explicitly set a SecurityProvider for this AuthEnvelopedDataStream. If no explicit SecurityProvider is set, the default system wide installed SecurityProvider will be used for the required cryptographic operations.
This class uses the following method(s) of the SecurityProvider
, which may be overriden by an application, if required:
getInputStreamAuthCipherEngine
methods to get an InputStreamAuthCipherEngine
for stream based authenticated content en/decryption as used by the inherent EncryptedContentInfoStream
getByteArrayAuthCipherEngine
methods to get a ByteArrayAuthCipherEngine
for authentictaed content en/decryption for non-stream AuthEnvelopedData
objects
generateKey
to generate the symmetric content-authenticated-encryption key as used by the
inherent EncryptedContentInfo
getAlgorithmParameters
to parse algorithm parameters from an AlgorithmID as used by the inherent EncryptedContentInfo
created
by
this class.
securityProvider
- the SecurityProvider to be setpublic SecurityProvider getSecurityProvider()
This class uses the following method(s) of the SecurityProvider
, which may be overriden by an application, if required:
getInputStreamAuthCipherEngine
methods to get an InputStreamAuthCipherEngine
for stream based authenticated content en/decryption as used by the inherent EncryptedContentInfoStream
getByteArrayAuthCipherEngine
methods to get a ByteArrayAuthCipherEngine
for authentictaed content en/decryption for non-stream AuthEnvelopedData
objects
generateKey
to generate the symmetric content-authenticated-encryption key as used by the
inherent EncryptedContentInfo
getAlgorithmParameters
to parse algorithm parameters from an AlgorithmID as used by the inherent EncryptedContentInfo
set
for this object,
the default system wide installed SecurityProvider will be used for the required cryptographic
operations. However, this method will return null
if it does not have its own
SecurityProvider.
null
if
this object does not have its own SecurityProviderpublic void setMode(int mode)
This method may be only called to set the mode to EXPLICIT
for creating a new AuthEnvelopedDataStream in EXPLICIT
mode where the
authenticated encrypted content shall not be included in the AuthEnvelopedData. In this case
the authenticated encrypted content has to be transmitted by other means. This method may not
be called in IMPLICIT
mode (default) where
the authenticated encrypted content is included in the AuthEnvelopedData. This method MUST
not be called when parsing an AuthEnvelopedData where the mode is automatically
detected and cannot be changed.
mode
- the mode, either IMPLICIT
(to include
the authenticated encrypted content (default) or
EXPLICIT
to not include it)
java.lang.IllegalArgumentException
- if the mode is not IMPLICIT
or EXPLICIT
; or if this
method is called when parsing an AuthEnvelopedDatapublic int getMode()
IMPLICIT
(to include
the authenticated encrypted content (default) or
EXPLICIT
to not include it)public void decode(java.io.InputStream is) throws java.io.IOException, CMSParsingException
decode
in interface ContentStream
is
- the InputStream holding a BER encoded AuthEnvelopedData object
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic void decode(java.io.InputStream is, boolean useStream) throws java.io.IOException, CMSParsingException
is
- the InputStream holding a BER encoded AuthEnvelopedData objectuseStream
- whether to try to use EncryptedContentInfoStream
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic ObjectID getContentType()
Use getEncryptedContentInfo().getContentType()
for getting the
type the authenticated encrypted content represents.
getContentType
in interface ContentStream
ObjectID.cms_authEnvelopedData
public void setOriginatorInfo(OriginatorInfo originatorInfo)
The originatorInfo may be set for including certificates and/or certificate revocation lists for the originator if required by the key management algorithm used.
originatorInfo
- the OriginatorInfo to be setpublic void setRecipientInfos(RecipientInfo[] recipients)
Any RecipientInfo
added supplies
recipient-specific information used for identifying the key of
the recipient to be used for authenticated en/decrypting the content.
recipients
- a collection of per-recipient informationRecipientInfo
,
KeyTransRecipientInfo
,
KeyAgreeRecipientInfo
,
KEKRecipientInfo
,
PasswordRecipientInfo
,
OtherRecipientInfo
public void addRecipientInfo(RecipientInfo recipient)
Any RecipientInfo
added supplies
recipient-specific information used for identifying the key of
the recipient to be used for authenticated en/decrypting the content.
recipient
- the RecipientInfo to be addedRecipientInfo
,
KeyTransRecipientInfo
,
KeyAgreeRecipientInfo
,
KEKRecipientInfo
,
PasswordRecipientInfo
,
OtherRecipientInfo
public void setAuthenticatedAttributes(Attribute[] attributes)
attributes
- the authenticated attributes to be setpublic void setUnauthenticatedAttributes(Attribute[] attributes)
attributes
- the unauthenticated attributes to be setpublic void setBlockSize(int blockSize)
blockSize
is smaller or equal to zero the
whole encrypted data is encoded as definite primitive octet string.
setBlockSize
in interface ContentStream
blockSize
- for defining the encoding scheme and setting the octet
string component length, if positivepublic int getBlockSize()
blockSize
is smaller or equal to zero the
whole data is encoded as definite primitive octet string.
getBlockSize
in interface ContentStream
public javax.crypto.SecretKey setupCipher(java.security.Key recipientKey, int recipientInfoIndex) throws CMSException, java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
AuthEnvelopedDataStream
object for the requesting recipient, specified by
its recipientInfoIndex
.
This method has to be called before reading the decrypted content.
method. So do not call method getInputStream
before
setting up the cipher!
Note that you have to know the right index into the recipientInfos
field when using this method for setting up the cipher for decryption. You may
search for the index by using one of the getRecipientInfo
methods
thereby identifying the recipient by its keyIdentifier
or -- if suitable for the key management algorithm used -- certificate
.
However, when having some recipient using a key agreement protocol the corresponding
RecipientInfo is of type KeyAgreeRecipientInfo
which may hold encrypted content-authenticated-encryption keys for more than only one recipients
using the same key agreement algorithm with same domain parameters. Since this
setupCipher
method only can get the KeyAgreeRecipientInfo with the given
index (but not search for the right recipient in the KeyAgreeRecipientInfo), it
will step through any recipient included in the KeyAgreeRecipientInfo trying to
decrypt the encrypted content-authenticated-encryption key with the supplied key. This may give
some overhead and it might be more appropriate to use another setupCipher
method
allowing to immediately identify the particular recipient in mind by its
#setupCipher(Key, KeyIdentifier) keyIdentifier} or certificate
.
recipientKey
- the key of the recipient to be used for decrypting
the encrypted content-authenticated-encryption key.recipientInfoIndex
- the index into the recipientInfos field
CMSException
- if there occurs an error while decrypting the content-authenticated-encryption key
or setting up the cipher for decrypting the content
java.security.NoSuchAlgorithmException
- if there is no implementation of the content-authenticated-encryption algorithm
java.security.InvalidKeyException
- if the specified key is not validpublic javax.crypto.SecretKey setupCipher(java.security.Key recipientKey, KeyIdentifier recipientIdentifier) throws CMSException, java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
AuthEnvelopedDataStream
object for the requesting recipient, specified
by the given recipient identifier.
This method has to be called before reading the decrypted content.
method. So do not call method getInputStream
before
setting up the cipher!
This setupCipher
can be used to setup the cipher for decryption for
any type of RecipientInfo. The supplied recipient identifier will be used for
searching for the right RecipientInfo in the recipientInfos
field.
recipientKey
- the key of the recipient to be used for decrypting
the encrypted content-authenticated-encryption key.recipientIdentifier
- specifies which RecipientInfo the given key belongs to
CMSException
- if there occurs an error while decrypting the content-authenticated-encryption key
or setting up the cipher for decrypting the content, or no RecipientInfo
for the requested recipient is included
java.security.NoSuchAlgorithmException
- if there is no implementation of the content-authenticated-encryption algorithm
java.security.InvalidKeyException
- if the specified key is not validpublic javax.crypto.SecretKey setupCipher(java.security.Key recipientKey, X509Certificate recipientCertificate) throws CMSException, java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
AuthEnvelopedDataStream
object for the requesting recipient, specified by
the given recipient certificate.
This method has to be called before reading the decrypted content.
method. So do not call method getInputStream
before
setting up the cipher!
Note that this method only can be used for decrypting the encrypted content
if the recipient in mind has a RecipientInfo of type KeyTransRecipientInfo
or KeyAgreeRecipientInfo
using a public
key from a certificate for its key management protocol.
recipientKey
- the private key of the recipient to be used for decrypting
the encrypted content-authenticated-encryption key.recipientCertificate
- the certificate of the recipient specifying which
RecipientInfo the recipient private key belongs to
CMSException
- if there occurs an error while decrypting the content-authenticated-encryption key
or setting up the cipher for decrypting the content, or no RecipientInfo
for the requested recipient is included
java.security.NoSuchAlgorithmException
- if there is no implementation of the content-authenticated-encryption algorithm
java.security.InvalidKeyException
- if the specified private key is not validpublic void setupCipher(java.security.Key key) throws CMSException, java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
The secret key supplied to this method has to be the already decrypted content authenticated encryption key.
This method has to be called before reading the decrypted content.
method. So do not call method getInputStream
before
setting up the cipher!
key
- the temporary symmetric key that has been used to encrypt the content,
and now is used for decrypting it again
CMSException
- if there occurs an error while setting up the cipher for decrypting the content
java.security.NoSuchAlgorithmException
- if there is no implementation for the content-authenticated-encryption algorithm
java.security.InvalidKeyException
- if the specified key is not validpublic java.io.InputStream getInputStream()
Attention! The stream only may be read once.
When having created a new AuthEnvelopedDataStream
object to
be encoded to a stream, this method should not be called at all, since the stream
automatically will be read during performing the encoding (which is done
when calling a writeTo
method).
When having decoded and parsed a received AuthEnvelopedDataStream
object
comimg from some stream, this method may be used for obtaining the raw (decrypted) data
after having setup the cipher for decrypting the content and verifying the mac.
public void setInputStream(java.io.InputStream is)
is
- the input stream holding the content data to authenticated and encryptpublic void setInputLength(long inputLength) throws java.lang.IllegalArgumentException
inputLength
- the length (number of bytes) of the input data
java.lang.IllegalArgumentException
- if the specified input length is not validpublic long getInputLength()
-1
if it has not been set
java.lang.IllegalArgumentException
- if the specified input length is not validsetInputLength(long)
public int getVersion()
public OriginatorInfo getOriginatorInfo()
The originatorInfo may be set for including certificates and/or certificate revocation lists if required by the key management algorithm used.
null
public RecipientInfo[] getRecipientInfos()
AuthEnvelopedData
object.
RecipientInfo
objects
included into this AuthEnvelopedData
public RecipientInfo[] getRecipientInfos(int type)
AuthEnvelopedData
object that have the specified type.
RecipientInfo
objects
included into this AuthEnvelopedData
that have the
specified type (e.g. all KeyTransRecipientInfos); the array may
be empty if no RecipientInfo with the requested type is includedpublic RecipientInfo getRecipientInfo(X509Certificate recipientCertificate)
This method may be used by a recipient for quering for the recipient info
that holds the content authenticated encryption key encrypted with the public key of the
given certificate.
Note that this method only can be used for searching for RecipientInfos
of type KeyTransRecipientInfo
or
KeyAgreeRecipientInfo
, but NOT
for a RecipientInfo of type KEKRecipientInfo
which does use a certificate.
null
if no recipient info belonging to the given
certificate can be foundpublic RecipientInfo getRecipientInfo(KeyIdentifier recipientIdentifier)
recipientIdentifier
- the recipient identifier identifying the
recipient in mind
null
if no recipient info belonging to the given
recipient identifier can be foundpublic EncryptedContentInfoStream getEncryptedContentInfo()
AuthEnvelopedDataStream
object.
public Attribute[] getAuthenticatedAttributes()
public Attribute getAuthenticatedAttribute(ObjectID oid)
null
if there is no attribute for the given OID.public byte[] getMac()
public void setMac(byte[] mac)
mac
- the MAC value.public Attribute[] getUnauthenticatedAttributes()
public Attribute getUnauthenticatedAttribute(ObjectID oid)
null
if there is no attribute for the given OID.public ASN1Object toASN1Object() throws CMSException
toASN1Object
in interface ContentStream
AuthEnvelopedDataStream
as ASN1Object.
CMSException
- if the ASN1Object could not be createdprotected ASN1Object toASN1Object(int blockSize) throws CMSException
If
blockSize
- the block size defining the encoding scheme - and specifying the
length of each primitive encoded octet string component, if positive
CMSException
- if the ASN1Object could not be created
public void writeTo(java.io.OutputStream os) throws java.io.IOException
os
- the output stream to which this AuthEnvelopedData shall be written
java.io.IOException
public void writeTo(java.io.OutputStream os, int blockSize) throws java.io.IOException
When encoding the content data to the given stream it is piped through a cipher stream thereby performing the content authenticated encryption.
If the a positive blockSize
value is specified, the encrypted content
is encoded as indefinite constructed octet string being composed of a certain number
of definite primitive encoded octet strings of blockSize
length:
0x24 0x80 0x04 <blocksize> <first encrypted content block> 0x04 <blocksize> <second encrypted content block> 0x04 <blocksize> <third encrypted content block> ... 0x00 0x00Otherwise, whole the encrypted content is encoded as definite primitive octet string:
0x04 <length> <encrypted content>
os
- the output stream to which this SignedData shall be writtenblockSize
- the block size defining the encoding scheme - and specifying the
length of each primitive encoded octet string component, if positive
java.io.IOException
- if an error occurs during writing the objectpublic void encodeCalled(ASN1Object obj, int id) throws CodingException
This method shall not be called by an application!
encodeCalled
in interface EncodeListener
obj
- the AuthEnvelopedData SEQUENCEid
- the id identifying the SEQUENCE to be processed
CodingException
- if an error occurs when computing/signing
the message digestpublic void notifyEOF() throws java.io.IOException
notifyEOF
in interface EOFListener
java.io.IOException
- if an error occurs while parsing the streampublic java.lang.String toString()
AuthEnvelopedData
object.
toString
in class java.lang.Object
public java.lang.String toString(boolean detailed)
AuthEnvelopedData
object.
toString
in interface ContentStream
detailed
- - whether or not to give detailed information
|
This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |