public class EncryptedContentInfo extends EncryptedContentInfoStream
EncryptedContentInfo
type.
The PKCS#7
Cryptographic Message Standard defines the EncryptedContentInfo
type for specifying the content type, the content encryption algorithm
and the encrypted content of an EnvelopedData
,
SignedAndEnvelopedData
, or EncryptedData
structure
(Version 1.5)::
EncryptedContentInfo ::= SEQUENCE { contentType ContentType, contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier, encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL }
EncryptedContent ::= OCTET STRING
This class provides several constructors and methods for creating an
EncryptedContentInfo
, encrypting its content (thereby optionally
creating a secret content-encryption key in accordance with the specified
content-encryption algorithm), and "re-decrypting" the encrypted content
again.
This class - as in common with all IAIK-JCE PKCS#7 implementations - provides mechanisms for encoding the inherent encrypted content data as indefinite primitive octet string instead of using the default primitive definite encoding scheme:
0x24 0x80 0x04 <blocksize> <first encrypted content block> 0x04 <blocksize> <second encrypted content block> 0x04 <blocksize> <third encrypted content block> ... 0x00 0x00instead of:
0x04 <length> <encrypted content>The indefinite constructed encoding scheme may be preferable when intending to be compatible to the encoding practice of some particular application (for instance some versions of Netscape Navigator).
setBlockSize
method of the parent
EncryptedContentInfoStream
class has to be used for defining the length of each primitive definite
encoded octet string component before actually performing the encoding by
means of the getEncoded
method, e.g.:
//create a EncryptedContentInfo for the data to be encrypted, supplied as byte array: byte[] data = ...; EncryptedContentInfo eci = new EncryptedContentInfo(ObjectID.pkcs7_data, data); //generate secret key and set up the cipher for encryption: SecretKey key = eci.setupCipher(AlgorithmID.des_EDE3_CBC); //optionally set the block size for splitting the encoding: eci.setBlockSize(1024); //transform the EncryptedContentInfo into an ASN1Object or immediately //perform the DER encoding: ASN1Object obj = eci.toASN1Object(); //respectively: byte[]encoding = eci.getEncoded();Note: in contrast to the equivalent stream supporting
EncryptedContentInfoStream
parent class, where the setupCipher
method only initializes the
cipher and the content encryption actually is done during the encoding by
piping the data through a cipher stream, in this class whole the content
encryption already is performed inside the setupCipher
method.
When parsing an already existing EncryptedContentInfo
object a
proper setupCipher
method has to be used for initializing the
cipher and decrypting the encrypted content:
// create an EncryptedContentInfo from the given EncryptedContentInfo // ASN1Object: // (if the EncryptedContentInfo is supplied as DER encoding first decode it to // an ASN1Objet) ASN1Object obj = DerCoder.decode(encoding); EncryptedContentInfo eci = new EncryptedContentInfo(obj); // setup the cipher with the right secret key and decrypt the encrypted content: eci.setupCipher(key); // get the recovered raw data: byte[] data = eci.getContent();
EnvelopedData
,
SignedAndEnvelopedData
,
EncryptedData
,
EncryptedContentInfoStream
Modifier | Constructor and Description |
---|---|
protected |
EncryptedContentInfo()
Default constructor.
|
|
EncryptedContentInfo(ASN1Object obj)
Creates an EncryptedContentInfo from an ASN1Object.
|
|
EncryptedContentInfo(java.io.InputStream is)
Creates a new EncryptedContentInfo where the DER encoded data is read from
the given InputStream.
|
|
EncryptedContentInfo(ObjectID contentType,
AlgorithmID contentEncAlg)
Creates an EncryptedContentInfo with given content type and
content-encryption algorithm ID.
|
|
EncryptedContentInfo(ObjectID contentType,
byte[] content)
Creates a new EncryptedContentInfo for the given content type.
|
Modifier and Type | Method and Description |
---|---|
void |
decode(ASN1Object obj)
Decodes the EncryptedContentInfo supplied as ASN1Object.
|
protected void |
decode(java.io.InputStream is)
Reads and decodes the EncryptedContentInfo from a DerInputStream.
|
byte[] |
getContent()
Returns the content.
|
byte[] |
getEncoded()
Returns the DER encoding of this EncryptedContentInfo in a byte array.
|
java.io.InputStream |
getInputStream()
Returns an InputStream for reading the content.
|
boolean |
hasContent()
Returns
true if there is a content. |
void |
setupCipher(AlgorithmID contentEA,
java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Setups the cipher and encrypts the content.
|
void |
setupCipher(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Uses the specified key and parameters for setting up the cipher and
decrypting the content.
|
void |
setVersion(int version)
Sets the version of this EncryptedContentInfo.
|
ASN1Object |
toASN1Object()
Returns this EncryptedContentInfo as ASN1Object.
|
getBlockSize, getContentEncryptionAlgorithm, getContentType, setBlockSize, setupCipher, setupCipher, setupCipher, toString, writeTo
protected EncryptedContentInfo()
public EncryptedContentInfo(ObjectID contentType, byte[] content)
contentType
- the PKCS#7 content typecontent
- the byte array holding the content data to encryptpublic EncryptedContentInfo(ObjectID contentType, AlgorithmID contentEncAlg)
contentType
- the PKCS#7 content type of the encrypted contentcontentEncAlg
- the algorithm used to encrypt the contentpublic EncryptedContentInfo(ASN1Object obj) throws PKCSParsingException
EncryptedContentInfo
object that may have been created by
calling toASN1Object
.
Use the EncryptedContentInfo(ObjectID contentType, byte[] content)
constructor for
supplying the content to be encrypted when creating an
EncryptedContentInfo
object.
obj
- the ASN1Object of ASN.1 type EncryptedContentInfo
PKCSParsingException
- if the ASN.1 object could not be parsedpublic EncryptedContentInfo(java.io.InputStream is) throws java.io.IOException, PKCSParsingException
is
- the InputStream holding a DER encoded PKCS#7 EncryptedContentInfo
objectjava.io.IOException
- if an I/O error occurs during reading from the InputStreamPKCSParsingException
- if an error occurs while parsing the objectpublic void decode(ASN1Object obj) throws PKCSParsingException
obj
- the PKCS#7 EncryptedContentInfo as ASN1ObjectPKCSParsingException
- if an error occurs while parsing the objectprotected void decode(java.io.InputStream is) throws java.io.IOException, PKCSParsingException
DerInputStream
, internally a
DerInputStream is created before parsing the data.decode
in class EncryptedContentInfoStream
is
- the InputStream holding a DER encoded PKCS#7 EncryptedContentInfo
objectjava.io.IOException
- if an I/O error occurs during reading from the InputStreamPKCSParsingException
- if an error occurs while parsing the objectpublic void setupCipher(AlgorithmID contentEA, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
EncryptedContentInfoStream
class, where the cipher only is initialized, in
this class this method already performs the content encryption.
Note: The supplied parameters are used for initializing the cipher. They may, for instance, constitute a initialization vector of type IvParameterSpec. In such cases, if the supplied contentEA algorithmID does not include parameters, an OCTET_STRING is created from the iv value and set as parameters for the contentEA algorithmID. However, if the contentEA algorithmID expects parameters of other ASN.1 representation than an OCTET_STRING constituting the IV, an application itself should take care for setting the parameters before supplying the algorithmID to this method. Imagine, for instance, RC2-CBC as used by S/MIME, where the parameters are encoded as SEQUENCE with two components having the OCTET_STRING iV as second component (see RFC 2311):
RC2-CBC parameter ::= SEQUENCE { rc2ParameterVersion INTEGER, iv OCTET STRING (8)}In such case an application may:
setupCipher
in class EncryptedContentInfoStream
contentEA
- the algorithm to use for encrypting the contentkey
- the key to useparams
- the parameters for the specified algorithmjava.security.NoSuchAlgorithmException
- if there is no implementation for the specified algorithmjava.security.InvalidKeyException
- if the key is inappropriate for the content-encryption
algorithmjava.security.InvalidAlgorithmParameterException
- if the provided parameters are not appropriate for the
algorithmpublic void setupCipher(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
EncryptedContentInfoStream
class, where the cipher only is initialized, in
this class this method already decrypts the encrypted content.
This method shall be used for initializing the cipher of an received
EncryptedContentInfo
object with key and parameters for
decrypting the encrypted content, particularly when the parameters of the
content encryption algorithmID do not constitute an OCTET_STRING that
represents IV parameters. The following example parses PBE parameters from
the algorithmID, and subsequently setups the cipher with key and derived
parameters:
// get the content encryption algorithm: AlgorithmID contentEA = eci.getContentEncryptionAlgorithm(); // get PBE parameters SEQUENCE seq = (SEQUENCE) contentEA.getParameter; OCTET_STRING oct = (OCTET_STRING) seq.getComponentAt(0); byte[] salt = (byte[]) oct.getValue(); INTEGER iteration_count = (INTEGER) seq.getComponentAt(1); int it = ((BigInteger) iteration_count.getValue()).intValue(); PBEParameterSpec params = new PBEParameterSpec(salt, it); // now setup the cipher eci.setupCipher(pbeKey, params); // get the content byte[] content = eci.getContent();This method may be used for setting up the cipher for an v1.6 EnvelopedData message, where the version number (1) indicates that the content encoding has been encrypted.
setupCipher
in class EncryptedContentInfoStream
key
- the (secret) key to decrypt the contentparams
- the algorithm parameters needed to decrypt the contentjava.security.NoSuchAlgorithmException
- if there is no implementation for the
content-encryption-algorithm to be usedjava.security.InvalidKeyException
- if the key is inappropriate for the content-encryption
algorithmjava.security.InvalidAlgorithmParameterException
- if the provided parameters are not appropriate for the created
cipherpublic void setVersion(int version)
Also an EncryptedContentInfo itself does not have a version number, when explicitly creating an EncryptedContentInfo for an EnvelopedData it might be necessary to decide whether to encrypt the content for a PKCS#7v1.5 EnvelopedData (default) or for a PKCS#7v1.6 EnvelopedData (the latter encrypting the content encoding).
The version number set by this method either may be 0 (indicating a PKCS#7v1.5 EnvelopedData) or 1 (indicating a PKCS#71.6 EnvelopedData); default is 0.
version
- the version; either 0 or 1public ASN1Object toASN1Object() throws PKCSException
EncryptedContentInfo
object using the
EncryptedContentInfo(ASN1Object
obj)
constructor.toASN1Object
in class EncryptedContentInfoStream
EncryptedContentInfo
as ASN1Object.PKCSException
public byte[] getContent()
The returned content depends on whether creating a new EncryptedContentInfo or parsing an existing one:
null
if there is
no contentpublic java.io.InputStream getInputStream()
The returned content depends on whether creating a new EncryptedContentInfo or parsing an existing one:
This method only overrides the corresponding getInputStream
method of the parent EncryptedContentInfoStream
class for returning the content of this
EncryptedContentInfo
object. There should be no real necessity
for using this method since the content immediately can be obtained by the
getContent
method. However, in contrast to the
equivalent getInputStream
method of the parent
EncryptedContentInfoStream
class, this method may be called
arbitrarily often; it only returns a ByteArrayInputStream that is
initialized with the content bytes.
getInputStream
in class EncryptedContentInfoStream
null
if there is
no contentpublic boolean hasContent()
true
if there is a content.hasContent
in class EncryptedContentInfoStream
true
if there is a contentpublic byte[] getEncoded() throws PKCSException
If the setBlockSize
method of the parent
EncryptedContentInfoStream
class has been utilized for defining a positive
blockSize
value, 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>
PKCSException