public class PKCS7EncodeStream
extends java.io.FilterOutputStream
PKCS7EncodeStream class performs data encryption and
signing operations according to the PKCS#7 standard.
Use PKCS7DecodeStream to read data written with
PKCS7EncodeStream.
setBlockSize method
to specify a non-zero block size in bytes.
| Modifier and Type | Class and Description |
|---|---|
class |
PKCS7EncodeStream.ConsumerInputStream
Represents the buffer of a producer/consumer scheme as an input stream from
which the consumer reads.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
CLEAR_SIGN
Indicator for clear signing data; signing without including the data in
the structure.
|
static int |
ENCRYPT_ONLY
Indicator for encrypting data without signing it.
|
static int |
EXPORT_CERTIFICATES
Indicator for exporting certificates (data cannot be written).
|
static int |
SIGN_AND_ENCRYPT
Indicator for signing and encrypting data.
|
static int |
SIGN_ONLY
Indicator for signing data without encrypting it.
|
| Constructor and Description |
|---|
PKCS7EncodeStream(KeyAndCertificateSource keyAndCertSource,
java.io.OutputStream out,
int operation)
Creates a new
PKCS7EncodeStream object to encrypt and/or
sign data. |
PKCS7EncodeStream(KeyAndCertificateSource keyAndCertSource,
java.io.OutputStream out,
java.io.OutputStream signature)
Creates a new
PKCS7EncodeStream to clear sign data. |
PKCS7EncodeStream(User user,
java.io.OutputStream out,
int operation)
Creates a new
PKCS7EncodeStream object to encrypt and/or
sign data. |
PKCS7EncodeStream(User user,
java.io.OutputStream out,
java.io.OutputStream signature)
Creates a new
PKCS7EncodeStream to clear sign data. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this output stream and releases any system resources associated
with the stream.
|
void |
flush()
Flushes this output stream forcing any buffered output bytes to be
written to the stream.
|
Attribute[] |
getAuthenticatedAttributes()
This method returns the set of authenticated attributes
in the PKCS7 Message.
|
byte[] |
getDigest()
Returns the digest that was calculated over the data during a signing
operation.
|
AlgorithmID |
getDigestAlgorithm()
Returns the message digest algorithm used to sign the data.
|
AlgorithmID |
getEncryptionAlgorithm()
Returns the algorithm used to encrypt the data during the PKCS7 encode
operation.
|
CertificateSet |
getIncludedCertificates()
Returns the certificates that are to be included in the PKCS#7 message.
|
int |
getOperation()
Returns the security operation that is to be performed on the data
written to this stream.
|
CertificateSet |
getRecipients()
Returns the set of certificates for the public keys used to encrypt the
data.
|
void |
requestTimeStamp(TimeStampClient timeStampClient)
Requests that the signature be time-stamped.
|
void |
setAuthenticatedAttributes(Attribute[] attributes)
This method sets additional authenticated attributes to be included in this
PKCS7 Message.
|
void |
setBlockSize(int blockSize)
Sets the size of the data parts in the underlying ASN.1 structure.
|
void |
setDigestAlgorithm(AlgorithmID digest)
Specifies the message digest algorithm to use for signing the data.
|
void |
setEncryptionAlgorithm(AlgorithmID encryptionAlg)
Specifies the encryption algorithm to be used during an PKCS7 encode
operation that involves data being encrypted.
|
void |
setEncryptionAlgorithm(AlgorithmID encryptionAlg,
int keyLength)
Specifies the encryption algorithm and encryption key length to be used
during an PKCS7 encode operation that involves data being encrypted.
|
CertificateSet |
setIncludedCertificates(CertificateSet certs)
Specifies the certificates that are included in the PKCS#7 message.
|
void |
setOAEPParams(java.security.spec.AlgorithmParameterSpec params)
Sets Optimal Asymmetric Encryption Padding (OAEP) parameters.
|
CertificateSet |
setRecipients(CertificateSet recipients)
Specifies the public keys with which to encrypt the message.
|
void |
setTrustedRecipients(CertificateSet recipients)
Specifies the validated public keys with which to encrypt the message.
|
void |
write(byte[] b)
Signs and encrypts, or just encrypts,
b.length bytes and writes them to
the output stream. |
void |
write(byte[] b,
int off,
int len)
Signs and encrypts, or just encrypts,
len bytes from the specified byte
array. |
void |
write(int b)
Signs and encrypts, or just encrypts, the specified byte and writes it
to the output stream.
|
public static final int SIGN_AND_ENCRYPT
public static final int ENCRYPT_ONLY
public static final int SIGN_ONLY
public static final int EXPORT_CERTIFICATES
public static final int CLEAR_SIGN
public PKCS7EncodeStream(User user, java.io.OutputStream out, int operation) throws InvalidUserException, java.lang.IllegalArgumentException, java.lang.NullPointerException
PKCS7EncodeStream object to encrypt and/or
sign data.
The PKCS7EncodeStream object encrypts and/or signs data as it
writes it to the underlying output stream. The User must be
logged in before the application makes the call to this constructor.
To clear sign data and to write the signed data to an output stream, use
PKCS7EncodeStream(User, OutputStream, OutputStream).
Note that this constructor uses the default signing and encryption
keys from user. If user has extra key pairs
beyond the default that need to be used, use
PKCS7EncodeStream(KeyAndCertificateSource,OutputStream,int)
and set the keys to be used in the KeyAndCertificateSource
object.
user - the User object for which the data is to be signed
and/or encrypted. If the data is signed, it is signed with
the user's signing certificate.out - the underlying output stream to which the PKCS7 encoded data is
written.operation - the operation to perform. Use one of:
SIGN_AND_ENCRYPT = 1ENCRYPT_ONLY = 2SIGN_ONLY = 3EXPORT_CERTIFICATES = 4CLEAR_SIGN = 5java.lang.NullPointerException - thrown if user or data is
nulljava.lang.IllegalArgumentException - thrown if operation is a value other than one of
the values listed above.InvalidUserException - thrown if the user is not logged onpublic PKCS7EncodeStream(KeyAndCertificateSource keyAndCertSource, java.io.OutputStream out, int operation) throws java.lang.IllegalArgumentException, java.lang.NullPointerException
PKCS7EncodeStream object to encrypt and/or
sign data.
To clear sign data and to write the signed data to an output stream, use
PKCS7EncodeStream(User, OutputStream, OutputStream).
keyAndCertSource - the KeyAndCertSource object that supplies certificates
and private keys. If the data is to be signed, the signing information
must be set in this object. If the data is to be encrypted, the
encryption information must be set.out - the underlying output stream to which the PKCS7 encoded data is
written.operation - the operation to perform. Use one of:
SIGN_AND_ENCRYPT = 1ENCRYPT_ONLY = 2SIGN_ONLY = 3EXPORT_CERTIFICATES = 4CLEAR_SIGN = 5java.lang.NullPointerException - thrown if keyAndCertSource or data is
nulljava.lang.IllegalArgumentException - thrown if operation is a value other than one of
the values listed above, or if necessary key material is
not provided in keyAndCertSource.public PKCS7EncodeStream(User user, java.io.OutputStream out, java.io.OutputStream signature) throws InvalidUserException, java.lang.NullPointerException, java.lang.IllegalArgumentException
PKCS7EncodeStream to clear sign data.
The PKCS7EncodeStream object clear signs data while writing
the data to the underlying output stream. The user must be
logged in before the application calls this constructor.
Note that this constructor uses the default signing keys from
user. If user has extra signing key pairs
beyond the default that need to be used, use
PKCS7EncodeStream(KeyAndCertificateSource,OutputStream,OutputStream)
and set the keys to be used in the KeyAndCertificateSource
object.
To use other PKCS #7 operations or to export certificates, use
PKCS7EncodeStream(User, OutputStream, int).
user - the User object for which the data is to be clear
signed. If the data is signed, they are signed with this user's
signing certificate.out - the underlying output stream to which the data is written.
When out is null, the constructor
call is equivalent to
PKCS7EncodeStream(User, OutputStream, PKCS7EncodeStream.CLEAR_SIGN).signature - the stream to which the signature is writtenInvalidUserException - thrown if the user is not logged onjava.lang.NullPointerException - thrown if user or
signature is nulljava.lang.IllegalArgumentException - If user does not have a signing key.public PKCS7EncodeStream(KeyAndCertificateSource keyAndCertSource, java.io.OutputStream out, java.io.OutputStream signature) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
PKCS7EncodeStream to clear sign data.
The PKCS7EncodeStream object clear signs data while writing
the data to the underlying output stream.
To use other PKCS #7 operations or to export certificates, use
PKCS7EncodeStream(User, OutputStream, int).
keyAndCertSource - the KeyAndCertSource object that supplies the signing
private keys and verification certificate.out - the underlying output stream to which the data is written.
When out is null, the constructor
call is equivalent to
PKCS7EncodeStream(User, OutputStream, PKCS7EncodeStream.CLEAR_SIGN).signature - the stream to which the signature is writtenjava.lang.NullPointerException - thrown if keyAndCertSource or
signature is nulljava.lang.IllegalArgumentException - If keyAndCertSource does not contain a signing key.public int getOperation()
getOperation() returns one of the following security constants:
SIGN_AND_ENCRYPTENCRYPT_ONLYSIGN_ONLYEXPORT_CERTIFICATESCLEAR_SIGNpublic void setBlockSize(int blockSize)
throws PropertyChangeException
If blockSize is set to a value below 1 byte,
all the data is written in one block. The default block size is
0 bytes.
blockSize - the size of the individual blocks (in bytes) within the
constructed octet string of the PKCS#7 dataPropertyChangeException - thrown if the block size is changed to less than 1
after data has been written to the underlying streampublic void setOAEPParams(java.security.spec.AlgorithmParameterSpec params)
throws java.security.spec.InvalidParameterSpecException
The method tells the encoding stream to use RSA-ES-OAEP as the padding scheme for encrypting the symmetric key. For more information on the RSA-ES-OAEP padding scheme, see PKCS#1 v2.0.
This method must be called before any data is written to the stream, otherwise it will be ignored.
params - the cryptographic parametersjava.security.spec.InvalidParameterSpecExceptionpublic void setDigestAlgorithm(AlgorithmID digest) throws PropertyChangeException, java.lang.NullPointerException, java.security.NoSuchAlgorithmException
The default message digest algorithm is SHA1. The algorithm
is irrelevant if the data is not signed.
Possible choices for the digest algorithm are:
AlgorithmID.sha1 - if a DSA, ECDSA, or RSA signing key is being used.AlgorithmID.sha256 - if an RSA signing key is being used.AlgorithmID.sha384 - if an RSA signing key is being used.AlgorithmID.sha512 - if an RSA signing key is being used.AlgorithmID.md5 - if an RSA signing key is being used.digest - the algorithm to use for hashing the datajava.lang.NullPointerException - thrown if digest is nulljava.security.NoSuchAlgorithmException - thrown if digest is not recognized as a message
digest algorithm in the user's environment, or if the algorithm
specified is not a valid choice of digest algorithm.PropertyChangeException - thrown if data has already been written to the streampublic AlgorithmID getDigestAlgorithm()
If the data is not signed, this method returns null.
null if
the data is not signedpublic void setEncryptionAlgorithm(AlgorithmID encryptionAlg) throws PropertyChangeException, java.security.NoSuchAlgorithmException
The default encryption algorithm is CAST5/CBC/PKCS5Padding,
with no parameters. The encryption algorithm is irrelevant if the data
is not being encrypted during the PKCS7 encode operation.
encryptionAlg - the encryption algorithm to be used for encrypting data during
the PKCS7 encode operationjava.security.NoSuchAlgorithmException - thrown if a Cipher implementation or a KeyGenerator implementation
for the encryption algorithm is not provided by any of the installed
JCA providersPropertyChangeException - thrown if the encode operation has already begun (data has been
written to the stream)public void setEncryptionAlgorithm(AlgorithmID encryptionAlg, int keyLength) throws PropertyChangeException, java.security.NoSuchAlgorithmException
The key length parameter will be ignored if the encryption algorithm
identifier itself indicates a key length. Some algorithm identifiers
implicitly indicate a key length (AlgorithmID.aes_128_CBC)
while others can indicate the key length in the parameters that are
included in the algorithm identifier (AlgorithmID.cast5_CBC
containing a CAST128ParameterSpec).
If the key length is not indicated by the encryption algorithm identifier, but the key length paraemter contains a value that is invalid for the encryption algorithm, it will be ignored and the default key length for the algorithm will be used instead.
The default encryption algorithm is CAST5/CBC/PKCS5Padding,
with no parameters. The encryption algorithm is irrelevant if the data
is not being encrypted during the PKCS7 encode operation.
encryptionAlg - the encryption algorithm to be used for encrypting data during
the PKCS7 encode operationkeyLength - the requested key length of the encryption key in bitsjava.security.NoSuchAlgorithmException - thrown if a Cipher implementation or a KeyGenerator implementation
for the encryption algorithm is not provided by any of the installed
JCA providersPropertyChangeException - thrown if the encode operation has already begun (data has been
written to the stream)public AlgorithmID getEncryptionAlgorithm()
If PKCS7 encode operation does not involve the data being encrypted,
null is returned.
public void setAuthenticatedAttributes(Attribute[] attributes)
ObjectID.contentType authenticated attribute as well as the
ObjectID.signingTime authenticated attribute set to the current time.attributes - an Array list of Authenticated attributes to add to
this PKCS7 messagepublic Attribute[] getAuthenticatedAttributes()
setAuthenticatedAttributes(Attribute[])public CertificateSet setRecipients(CertificateSet recipients) throws PropertyChangeException
Recipients that were set by previous calls to setRecipients
or setTrustedRecipients
are discarded. The user specified in the constructor is always a recipient
and does not have to be included in the recipients list. If no recipient
is specified, the message is encrypted only for the originator (that is, the
user specified in the constructor).
If, for some reason, one or more certificates in the recipients
set fails validation, and is therefore not acceptable as an encryption
certificate, this method returns a set of all rejected certificates.
These certificates are removed from recipients.
The default certificate is the user's encryption certificate specified in the constructor.
This method is irrelevant if the data is not encrypted.
recipients - the encryption certificates of those users who are able to
decrypt the data. Rejected certificates are removed from this
set.null
if all recipients' certificates are accepted.PropertyChangeException - if data has been written to the stream alreadyCertVerifierpublic void setTrustedRecipients(CertificateSet recipients) throws PropertyChangeException
Recipients that were set by previous calls to setRecipients
or setTrustedRecipients
are discarded. The user specified in the constructor is always a recipient
and does not have to be included in the recipients list. If no recipient
is specified, the message is encrypted only for the originator (that is, the
user specified in the constructor).
The method assumes that all the certificates are valid, and does not do the
validation checking. To validate the certificates, use setRecipients.
The default certificate is the user's encryption certificate specified in the constructor.
This method is irrelevant if the data is not encrypted.
recipients - the validated encryption certificates of those users who are able to
decrypt the data.PropertyChangeException - if data has been written to the stream alreadypublic CertificateSet getRecipients()
This list always includes the user specified in the constructor.
null if the
data is not encrypted.public CertificateSet setIncludedCertificates(CertificateSet certs) throws PropertyChangeException
If one or more certificates fails validation and is therefore not
acceptable as an included certificate, this method returns a set of
all rejected certificates. These certificates are removed from
certs.
By default, the user's verification and encryption certificates and the user's CA's verification certificate are included with the message.
If the operation is ENCRYPT_ONLY, no certificates are
included and this method is irrelevant.
certs - a set of the certificates included in the PKCS#7 messagenull
if all certificates are acceptedPropertyChangeException - if data has been written to the stream alreadyCertVerifierpublic CertificateSet getIncludedCertificates()
null if the
operation is ENCRYPT_ONLYpublic void write(int b)
throws java.io.IOException
If the operation is EXPORT_CERTIFICATES, this
method returns without doing anything.
write in class java.io.FilterOutputStreamb - the byte to sign and/or encryptjava.io.IOException - thrown if an I/O error occurs in the underlying streampublic void write(byte[] b)
throws java.io.IOException
b.length bytes and writes them to
the output stream.
This method calls write(byte, int, int) with the arguments:
b, 0, and b.length. If the operation
is EXPORT_CERTIFICATES, this method returns without doing anything.
write in class java.io.FilterOutputStreamb - the data to be signed and/or encryptedjava.io.IOException - thrown if an I/O error occurs in the underlying streampublic void write(byte[] b,
int off,
int len)
throws java.io.IOException
len bytes from the specified byte
array.
The method starts at offset off and writes to the output
stream. If the operation is EXPORT_CERTIFICATES, this method
returns without doing anything.
write in class java.io.FilterOutputStreamb - the data to be signed and/or encryptedoff - the start offset in the datalen - the number of bytes to sign and/or encryptjava.io.IOException - thrown if an I/O error occurs in the underlying streampublic void flush()
throws java.io.IOException
flush in interface java.io.Flushableflush in class java.io.FilterOutputStreamjava.io.IOException - thrown if an I/O error occurs in the underlying streampublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.FilterOutputStreamjava.io.IOException - thrown if an I/O error occurs in the underlying streampublic void requestTimeStamp(TimeStampClient timeStampClient)
For operations that involve signing (SIGN_AND_ENCRYPT,
SIGN_ONLY, or CLEAR_SIGN), when the SignerInfo
structure this stream contains is encoded to an ASN1 object, a time-stamp
will automatically be requested from the TimeStamp Authority indicated,
and added to the SignerInfo structure as an unsigned attribute.
A time-stamp will only be requested when timeStampClient is
not set to null.
PKCS7EncodeStream encoding is done, which occurs
during a close operation. Thus, if the time-stamp client that was
used to request that the signature be time-stamped is modified
(hash algorithm changed, TSA policy changed, ...) before the close
operation, these changes will apply to the time-stamp that is
requested during the encoding.timeStampClient - the time-stamp client that will be used to request a time-stamp for
the signature value from a TimeStamp Authoritypublic byte[] getDigest()
This is only accessible following a successful signing operation,
and only after the stream has been closed. If the stream has not
been closed, an error occurred during the P7 operation, or the P7
operation did not involve signing, null is returned.