|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.OutputStream iaik.cms.AuthEnvelopedDataOutputStream
public class AuthEnvelopedDataOutputStream
This is an OutputStream
implementation of the AuthEnvelopedData structure
as specified CMS.
This stream version will encode the authenticated encrypted content as a constructed
OCTET STRING. Each write
operation to this stream will result in an
OCTET STRING block within this constructed OCTET STRING provided that the input data
is at least as large as the block size of the employed (block) cipher.
Calling close()
will finish the authenticated encryption, i.e. it will do
the padding (if required) and finish writing the resulting AuthEnvelopedData strucure.
The recipient infos must be added
before the
application starts writing data to this stream. RecipientInfo
objects which
are added later will not be included.
The typical usage of this class looks like the following example for creating a CMS AuthEnvelopedData structure and writing it to an output stream:
// the certificate of the recipient X509Certificate recipientCert = ... // the input stream from which to read the data to be encrypted InputStream dataInputStream = ... // the output stream to which to write the AuthEnvelopedData OutputStream resultStream = ... // create enveloped data stream (in this sample we use AES-GCM as content-authenticated // encryption algorithm) AlgorithmID contentAuthEncAlg = (AlgorithmID)AlgorithmID.aes256_GCM.clone(); AuthEnvelopedDataOutputStream authEnvelopingStream = new AuthEnvelopedDataOutputStream(resultStream, contentAuthEncAlg); // use RSA for encrypting the content-authenticated encryption key RecipientInfo recipientInfo = new KeyTransRecipientInfo(recipientCert, (AlgorithmID)AlgorithmID.rsaEncryption.clone()); authEnvelopingStream.addRecipientInfo(recipientInfo); ... // add any further recipients (if required) ... // write in the data to be authenticated encrypted byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = dataInputStream.read(buffer)) != -1) { authEnvelopingStream.write(buffer, 0, bytesRead); } // closing the stream finishes authenticated encryption and closes the underlying stream authEnvelopingStream.close();If you want to encapsulate the AuthEnvelopedData into a ContentInfo you first must wrap a
ContentInfoOutputStream
around the final
output stream (the ContentInfoOutputStream has to write its headers to the
stream at first, thus it must be created at the "lowest" level):
ContentInfoOutputStream contentInfoStream = new ContentInfoOutputStream(ObjectID.cms_authEnvelopedData, resultStream); // now create AuthEnvelopedDataOutputStream for the ContentInfoStream: AuthEnvelopedDataOutputStream authEnvelopingStream = new AuthEnvelopedDataOutputStream(contentInfoStream, contentAuthEncAlg); // the further proceeding is same as above RecipientInfo recipientInfo = new KeyTransRecipientInfo(recipientCert, (AlgorithmID)AlgorithmID.rsaEncryption.clone()); authEnvelopingStream.addRecipientInfo(recipientInfo); // write in the data to be authenticated encrypted byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = dataInputStream.read(buffer)) != -1) { authEnvelopingStream.write(buffer, 0, bytesRead); } // closing the stream finishes authenticated encryption and closes the underlying stream authEnvelopingStream.close();Use class
AuthEnvelopedDataStream
to read in and parse
the encoded AuthEnvelopedData and decrypt the content.
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.
AES-CCM 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.
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.
RecipientInfo
,
KeyTransRecipientInfo
,
KeyAgreeRecipientInfo
,
KEKRecipientInfo
,
PasswordRecipientInfo
,
OtherRecipientInfo
,
AuthEnvelopedDataStream
Constructor Summary | |
---|---|
AuthEnvelopedDataOutputStream(ObjectID contentType,
java.io.OutputStream out,
AlgorithmID contentAuthEncAlg)
Creates a new AuthEnvelopedDataOutputStream object where the content to be authenticated enveloped is later written to the given output stream (e.g. |
|
AuthEnvelopedDataOutputStream(ObjectID contentType,
java.io.OutputStream out,
AlgorithmID contentAuthEncAlg,
int keyLength)
Creates a new AuthEnvelopedDataOutputStream object where the content to be authenticated enveloped is later written to the given output stream (e.g. |
|
AuthEnvelopedDataOutputStream(ObjectID contentType,
java.io.OutputStream out,
AlgorithmID contentAuthEncAlg,
int keyLength,
SecurityProvider securityProvider)
Creates a new AuthEnvelopedDataOutputStream object where the content to be authenticated enveloped is later written to the given output stream (e.g. |
|
AuthEnvelopedDataOutputStream(java.io.OutputStream out,
AlgorithmID contentAuthEncAlg)
Creates a new AuthEnvelopedDataOutputStream object where the content to be authenticated enveloped is later written to the given output stream (e.g. |
|
AuthEnvelopedDataOutputStream(java.io.OutputStream out,
AlgorithmID contentAuthEncAlg,
int keyLength)
Creates a new AuthEnvelopedDataOutputStream object where the content to be authenticated enveloped is later written to the given output stream (e.g. |
Method Summary | |
---|---|
void |
addRecipientInfo(RecipientInfo recipient)
Adds one recipient to the list of recipient infos. |
void |
close()
Finishes the encoding and writes any authenticated and unauthenticated attributes (if set) and the mac value to the underlying stream. |
void |
flush()
Flushes any internal data and calls flush of the underlying stream. |
SecurityProvider |
getSecurityProvider()
Gets the SecurityProvider installed for this EncryptedDataStream. |
int |
getVersion()
Returns the syntax version number. |
boolean |
isPassThroughClose()
Checks whether a call to close() will call close of the
underlying output stream |
void |
setAuthenticatedAttributes(Attribute[] attributes)
Sets a set of authenticated attributes. |
void |
setInputLength(long inputLength)
Sets the length of input data. |
void |
setOriginatorInfo(OriginatorInfo originatorInfo)
Sets the optional OriginatorInfo. |
void |
setPassThroughClose(boolean passThroughClose)
Setting this to true will cause close() to call
close of the underlying output stream. |
void |
setRecipientInfos(RecipientInfo[] recipients)
Sets the recipient infos. |
void |
setSecurityProvider(SecurityProvider securityProvider)
Sets the SecurityProvider for this AuthEnvelopedDataOutputStream. |
void |
setUnauthenticatedAttributes(Attribute[] attributes)
Sets a set of unauthenticated attributes. |
java.lang.String |
toString()
Returns a string giving some information about this EnvelopedData object. |
java.lang.String |
toString(boolean detailed)
Returns a string giving some - if requested - detailed information about this AuthEnvelopedData object. |
void |
write(byte[] b)
Authenticated encrypts, encodes and writes the given data to the output stream. |
void |
write(byte[] b,
int off,
int len)
Authenticated encrypts, encodes and writes the given data to the output stream. |
void |
write(int b)
Authenticated encrypts, encodes and writes the given data byte to the output stream. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public AuthEnvelopedDataOutputStream(java.io.OutputStream out, AlgorithmID contentAuthEncAlg)
write(byte[])
).
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 using a content-authenticated encryption algorithm allowing keys
of different size, you may specify the key length by using the
AuthEnvelopedDataOutputStream(OutputStream, AlgorithmID, int)
constructor.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
out
- the OutputStream receiving the authenticated enveloped datacontentAuthEncAlg
- the content-authenticated encryption algorithm for
authenticatedencrypting the contentpublic AuthEnvelopedDataOutputStream(ObjectID contentType, java.io.OutputStream out, AlgorithmID contentAuthEncAlg)
write(byte[])
).
When using this constructor, automatically a temporary symmetric key for authenticated content
encryption is generated. If using a content-authenticated encryption algorithm allowing keys
of different size, you may specify the key length by using the
AuthEnvelopedDataOutputStream(ObjectID, OutputStream, AlgorithmID, int)
constructor.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
contentType
- the content type of the content to be envelopedout
- the OutputStream receiving the authenticated enveloped datacontentAuthEncAlg
- the content-authenticated encryption algorithm for
authenticated encrypting the contentpublic AuthEnvelopedDataOutputStream(java.io.OutputStream out, AlgorithmID contentAuthEncAlg, int keyLength)
write(byte[])
).
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.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
out
- the OutputStream receiving the authenticated enveloped datacontentAuthEncAlg
- the content-authenticated encryption algorithm for
authenticated encrypting the contentkeyLength
- the length of the content-authenticated-encryption key to be generated
(when using a content-authenticated-encryption algorithm that supports
variable key lengths)public AuthEnvelopedDataOutputStream(ObjectID contentType, java.io.OutputStream out, AlgorithmID contentAuthEncAlg, int keyLength)
write(byte[])
).
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.
constructor may be used.
Note that the supplied contentAuthEncAlg
AlgorithmID internally is cloned.
contentType
- the content type of the content to be authenticated envelopedout
- the OutputStream receiving the authenticated enveloped datacontentAuthEncAlg
- the content-authenticated encryption algorithm
for authenticated encrypting the contentkeyLength
- the length of the content-authenticated-encryption key to be generated
(when using a content-authenticated-encryption algorithm that supports
variable key lengths)public AuthEnvelopedDataOutputStream(ObjectID contentType, java.io.OutputStream out, AlgorithmID contentAuthEncAlg, int keyLength, SecurityProvider securityProvider)
write(byte[])
).
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.
Note that the supplied contentAuthEncAlg
AlgorithmID is cloned internally.
contentType
- the content type of the content to be authenticated envelopedout
- the OutputStream receiving the authenticated enveloped datacontentAuthEncAlg
- the content-authenticated encryption algorithm for
authenticated encrypting the contentkeyLength
- 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 usedMethod Detail |
---|
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data to be encryptedoff
- The start offset in b
.len
- The number of bytes to write.
java.io.IOException
- If an I/O error occurs.public void write(byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data to be encrypted.
java.io.IOException
- If an I/O error occurs.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data byte to be encrypted.
java.io.IOException
- If an I/O error occurs.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- If flushing the stream fails.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in class java.io.OutputStream
java.io.IOException
- if an I/O error occurs when writing to the streampublic boolean isPassThroughClose()
close()
will call close
of the
underlying output stream
true
if a call to close()
will call
close
of the underlying output stream;
false
if a call to close()
will not close the
underlying stream.public void setPassThroughClose(boolean passThroughClose)
true
will cause close()
to call
close
of the underlying output stream. If false
,
a call to close()
will not close the underlying stream.
passThroughClose
- true
to pass through close()
calls. false
to not pass them through.public void setSecurityProvider(SecurityProvider securityProvider)
This method allows to explicitly set a SecurityProvider for this AuthEnvelopedDataOutputStream. 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:
getAuthCipherEngine
to get an AuthCipher engine for authenticated encrypting the content
generateKey
to generate the symmetric content-authenticated encryption key
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:
getAuthCipherEngine
to get an AuthCipher engine for authenticated encrypting the content
generateKey
to generate the symmetric content-authenticated encryption key
null
if it does not have its own
SecurityProvider.
null
if
this object does not have its own SecurityProviderpublic 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 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 int getVersion()
public java.lang.String toString()
EnvelopedData
object.
toString
in class java.lang.Object
public java.lang.String toString(boolean detailed)
AuthEnvelopedData
object.
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 |