public class PKCS7DecodeStream
extends java.io.FilterInputStream
PKCS7DecodeStream class decodes data that has been
signed and/or encrypted according to the PKCS#7 standard.
PKCS7DecodeStream verifies all signatures when the internal
stream is closed. Use this stream to read data written with
PKCS7EncodeStream.
close after you have finished reading from the stream.
| Modifier and Type | Field and Description |
|---|---|
static int |
CLEAR_SIGN
Operation constant for signing without including the data
|
static int |
ENCRYPT_ONLY
Operation constant for encrypting the data without signing it
|
static int |
EXPORT_CERTIFICATES
Operation constant for exporting certificates
|
static int |
SIGN_AND_ENCRYPT
Operation constant for signing and encrypting the data
|
static int |
SIGN_ONLY
Operation constant for signing the data without encrypting it
|
| Constructor and Description |
|---|
PKCS7DecodeStream(KeyAndCertificateSource keyAndCertificateSource,
java.io.InputStream inputStream)
Creates a new
PKCS7DecodeStream object to decrypt data, to
verify data, or to retrieve exported certificates. |
PKCS7DecodeStream(KeyAndCertificateSource keyAndCertificateSource,
java.io.InputStream data,
java.io.InputStream signature)
Creates a new
DecodeStream to decode and verify
clear signed data. |
PKCS7DecodeStream(User user,
java.io.InputStream inputStream)
Creates a new
PKCS7DecodeStream object to decrypt data, to
verify data, or to retrieve exported certificates. |
PKCS7DecodeStream(User user,
java.io.InputStream data,
java.io.InputStream signature)
Creates a new
DecodeStream to decode and verify
clear signed data. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns
1 if at least one byte can be read from this input
stream without blocking. |
void |
close()
Closes this input stream and releases any system resources associated
with the stream.
|
byte[] |
getDigest(int index)
Returns the digest from a signature that exists over the data
that was extracted during a verification operation.
|
AlgorithmID |
getDigestAlgorithm(int index)
Returns the message digest algorithm used to sign the data.
|
AlgorithmID |
getEncryptionAlgorithm()
Returns the algorithm used to encrypt the data.
|
CertificateSet |
getIncludedCertificates()
Returns the certificates included in the PKCS#7 message.
|
int |
getNumberOfSignatures()
Returns the number of signatures on the data.
|
int |
getOperation()
Returns the security operation that was performed on the data read from
this stream.
|
java.util.Iterator |
getRevocationWarnings()
Returns any revocation warnings that occurred during the verification
and validation of signed PKCS7 data.
|
X509Certificate |
getSignerCertificate(int index)
Returns the certificate that is used to verify the signature.
|
SignerInfo |
getSignerInfo(int index)
Returns the signer information of the signature indicated by
index. |
void |
mark(int readlimit)
mark() is not supported. |
boolean |
markSupported()
The
mark and reset methods are not supported. |
int |
read()
Reads the next byte of data from the input stream.
|
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes of data from the current input stream
into an array of bytes. |
void |
reset()
reset() is not supported. |
long |
skip(long n)
Skips over and discards
n bytes of data from the input
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 PKCS7DecodeStream(User user, java.io.InputStream inputStream) throws InvalidUserException, java.lang.NullPointerException, java.io.IOException, java.security.NoSuchAlgorithmException
PKCS7DecodeStream object to decrypt data, to
verify data, or to retrieve exported certificates.
The user must be logged in before your
application calls this constructor.
If the data is clear signed, you can pass this object as the
signature argument in
PKCS7DecodeStream(User, InputStream, InputStream).
user - the user trying to decrypt the data and verify the
certificatesinputStream - the underlying input stream from which PKCS7 encoded data is read.java.lang.NullPointerException - thrown if either user or data is
nulljava.io.IOException - if there is a problem reading from the stream.InvalidUserException - thrown if the user is not logged injava.security.NoSuchAlgorithmException - thrown if one of the algorithms in the PKCS#7 message is not
recognized or supported in the user's environmentNotARecipientException - thrown if the message was not encrypted for userFormatException - thrown if signature is not a signature without data,
or if the PKCS#7 or ASN.1 encoding is corruptpublic PKCS7DecodeStream(KeyAndCertificateSource keyAndCertificateSource, java.io.InputStream inputStream) throws java.lang.NullPointerException, java.io.IOException, java.security.NoSuchAlgorithmException
PKCS7DecodeStream object to decrypt data, to
verify data, or to retrieve exported certificates.
If the message is encrypted, keyAndCertificateSource must
contain a private key that is on the recipient list of the message to
be able to decrypt it. If it is not encrypted, no private keys are
required.
If the data is clear signed, you can pass this object as the
signature argument in
PKCS7DecodeStream(User, InputStream, InputStream).
keyAndCertificateSource - the source of the certificate verifier and private decryption
keys.inputStream - the underlying input stream from which PKCS7 encoded data is read.java.lang.NullPointerException - thrown if either keyAndCertificateSource or
data is nulljava.io.IOException - if there is a problem reading from the stream.java.security.NoSuchAlgorithmException - thrown if one of the algorithms in the PKCS#7 message is not
recognized or supported in the user's environmentNotARecipientException - thrown if the message was not encrypted for userFormatException - thrown if signature is not a signature without data,
or if the PKCS#7 or ASN.1 encoding is corruptpublic PKCS7DecodeStream(User user, java.io.InputStream data, java.io.InputStream signature) throws InvalidUserException, java.lang.NullPointerException, java.io.IOException, java.security.NoSuchAlgorithmException
DecodeStream to decode and verify
clear signed data.
The user must be logged in before your application can call
this constructor.
You can put the signature parameter into any input stream,
including a PKCS7DecodeStream.
user - the user trying to decrypt the data and verify the
certificatesdata - the underlying input stream from which data is read.signature - the input stream to read the signature fromjava.lang.NullPointerException - thrown if user, data, or
signature is nulljava.io.IOException - if there is a problem reading from the streams.InvalidUserException - thrown if the user is not logged onFormatException - thrown if signature is not a signature without data,
or if the PKCS#7 or ASN.1 encoding is corruptjava.security.NoSuchAlgorithmException - thrown if one of the algorithms in the PKCS#7 message is not
recognized or supported in the user's environmentpublic PKCS7DecodeStream(KeyAndCertificateSource keyAndCertificateSource, java.io.InputStream data, java.io.InputStream signature) throws java.lang.NullPointerException, java.io.IOException, java.security.NoSuchAlgorithmException
DecodeStream to decode and verify
clear signed data.
You can put the signature parameter into any input stream,
including a PKCS7DecodeStream.
keyAndCertificateSource - the source of the certificate verifier.data - the underlying input stream from which data is read.signature - the input stream to read the signature fromjava.lang.NullPointerException - thrown if user, data, or
signature is nulljava.io.IOException - if there is a problem reading from the streams.FormatException - thrown if signature is not a signature without data,
or if the PKCS#7 or ASN.1 encoding is corruptjava.security.NoSuchAlgorithmException - thrown if one of the algorithms in the PKCS#7 message is not
recognized or supported in the user's environmentpublic int getOperation()
The return value can be one of the following:
SIGN_AND_ENCRYPTENCRYPT_ONLYSIGN_ONLYEXPORT_CERTIFICATESCLEAR_SIGNpublic AlgorithmID getEncryptionAlgorithm()
If the data is not encrypted, this method returns null.
null if the data is not encryptedpublic int getNumberOfSignatures()
public AlgorithmID getDigestAlgorithm(int index)
The index specifies the signature for which the digest algorithm should
be returned. The first signature has index 0, the last has index
getNumberOfSignatures()-1.
index - the signature for which the digest algorithm should be returnednull
if the data is not signedjava.lang.IndexOutOfBoundsException - thrown if index < 0 or
index >= getNumberOfSignatures()public X509Certificate getSignerCertificate(int index)
The index specifies the signature for which the verification certificate
should be returned. The first certificate has index 0, the
last has index getNumberOfSignatures()-1.
index - the signature for which the verification certificate should be
returnednull if the data is not signedjava.lang.IndexOutOfBoundsException - thrown if index < 0 or
index >= getNumberOfSignatures()public SignerInfo getSignerInfo(int index)
index.
The first signature has index 0, the last has index
getNumberOfSignatures()-1.
index - the signature for which the signer info should be returnedjava.lang.IndexOutOfBoundsException - if index < 0 or
index >= getNumberOfSignatures()public CertificateSet getIncludedCertificates()
If the data is not signed, this method returns null.
If the data is signed, but no certificates are included, this method
returns an array with length 0.
public java.util.Iterator getRevocationWarnings()
When validating each signer certificate that was used to create a signature in the PKCS7 signed data, time-stamps must be considered. When, a signature is time-stamped, the time-stamp token is decoded, verified and validated, and then the signer's certificate is validated at the time specified in the time-stamp.
During the validation of the signer's certificate at the time specified in the time-stamp, a revocation warning can occur. This happens when the signer's certificate has been revoked, but was revoked after the time specified in the time-stamp. When this occurs, the signer's certificate can still be considered valid, depending on the policy surrounding time-stamping and the reason it was revoked. This causes a revocation warning to be created.
This API provides access to all the revocation warnings that occurred. It
returns an Iterator that contains Map.Entry
objects. Each Map.Entry object contains an Integer
key that represents the signer index in the signed data for which the
revocation warning occurred, and a RevocationWarningException
value that contains all the revocation warning information.
For each revocation warning, it is then up to the caller to decide whether the warning should be ignored, or acted upon.
Iterator over
Map.Entry objectspublic int read()
throws java.io.IOException
The value byte is returned as an int in the range
0 to 255. The value -1 is
returned when no byte is available because one of the following conditions
applies:
This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
When the end of a signed message is reached, all signatures and the
signers' certificates are validated. This may cause a Directory to
be contacted, which could cause this call to block for a long time
if the Directory is slow to respond. Eventually these exceptions would
be wrapped in a CertNotTrustedException.
See JNDIDirectory.getAttr(java.lang.String, java.lang.String)
for details on which exceptions may be thrown if the Directory is not responding.
read in class java.io.FilterInputStream-1 if the end of the
stream is reached.SignatureException - thrown if the signature is invalidNoSuchSignerException - thrown if a signer's certificate cannot be foundCertNotTrustedException - thrown if the signature is valid, but the signer's certificate
cannot be validatedInvalidUserException - thrown if the user is not logged injava.io.IOException - thrown if an I/O error occurspublic int read(byte[] b,
int off,
int len)
throws java.io.IOException
len bytes of data from the current input stream
into an array of bytes.
This method blocks until some input is available. When the end of a
signed message is reached, all signatures and the signers' certificates
are validated. This may cause a Directory to
be contacted, which could cause this call to block for a long time
if the Directory is slow to respond. Eventually these exceptions would
be wrapped in a CertNotTrustedException.
See JNDIDirectory.getAttr(java.lang.String, java.lang.String)
for details on which exceptions may be thrown if the Directory is not responding.
read in class java.io.FilterInputStreamb - the buffer into which the data is readoff - the start offset of the datalen - the maximum number of bytes read-1 if there is no more data because the end of the
stream has been reachedSignatureException - thrown if the signature is invalidNoSuchSignerException - if a signer's certificate cannot be foundCertNotTrustedException - thrown if the signature is valid, but the signer's certificate
cannot be validatedInvalidUserException - thrown if the user is not logged injava.io.IOException - thrown if an I/O error occurspublic long skip(long n)
throws java.io.IOException
n bytes of data from the input
stream.
The skip method might skip over a smaller number of bytes,
possibly 0. There are a number of reasons for this, but
the method returns the number of bytes actually skipped.
The skip() method calls read with an array of
size n, and returns the number of read bytes.
skip in class java.io.FilterInputStreamn - the number of bytes to be skippedSignatureException - thrown if the signature is invalidNoSuchSignerException - if a signer's certificate cannot be foundCertNotTrustedException - thrown if the signature is valid, but the signer's certificate
cannot be validatedjava.io.IOException - thrown if an I/O error occurspublic int available()
throws java.io.IOException
1 if at least one byte can be read from this input
stream without blocking.available in class java.io.FilterInputStream1 if at least one byte can be read from this input
stream without blockingjava.io.IOException - if an I/O error occurspublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.FilterInputStreamSignatureException - thrown if the signature is invalid and has not been
verified by read or skip yetjava.io.IOException - if an I/O error occurspublic void mark(int readlimit)
mark() is not supported. This method does nothing.mark in class java.io.FilterInputStreamreadlimit - ignoredpublic void reset()
throws java.io.IOException
reset() is not supported. This method always throws an
IOException.reset in class java.io.FilterInputStreamjava.io.IOException - alwayspublic boolean markSupported()
mark and reset methods are not supported.
Therefore, markSupported always returns false.markSupported in class java.io.FilterInputStreamfalsepublic byte[] getDigest(int index)
This is only accessible following a verification 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 verification, null is
returned.
The index specifies the signature from which the digest should
be returned. The first signature has index 0, the
last has index getNumberOfSignatures() - 1.
index - the index of the signaturejava.lang.ArrayIndexOutOfBoundsException - if index < 0 or
index >= getNumberOfSignatures()