|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object
|
+--iaik.pkcs.pkcs7.EncryptedContentInfoStream
|
+--iaik.pkcs.pkcs7.EncryptedContentInfo
This class implements the PKCS#7 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 mechanims 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 0x00
instead of:
0x04 <length> <encrypted content>The indefinte constrcuted 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| Constructor Summary | |
protected |
EncryptedContentInfo()
Default constructor. |
|
EncryptedContentInfo(ASN1Object obj)
Creates an EncryptedContentInfo from an ASN1Object. |
|
EncryptedContentInfo(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. |
| Method Summary | |
void |
decode(ASN1Object obj)
Decodes the EncryptedContentInfo supplied as ASN1Object. |
protected void |
decode(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. |
InputStream |
getInputStream()
Returns an InputStream for reading the content. |
boolean |
hasContent()
Returns true if there is a content. |
void |
setupCipher(AlgorithmID contentEA,
Key key,
AlgorithmParameterSpec params)
Setups the cipher and encrypts the content. |
void |
setupCipher(Key key,
AlgorithmParameterSpec params)
Uses the specified key and paramters 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. |
| Methods inherited from class iaik.pkcs.pkcs7.EncryptedContentInfoStream |
getBlockSize, getContentEncryptionAlgorithm, getContentType, setBlockSize, setupCipher, setupCipher, setupCipher, toString, writeTo |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
protected EncryptedContentInfo()
public EncryptedContentInfo(ObjectID contentType,
byte[] content)
contentType - the PKCS#7 content typecontent - the byte array holding the content data to encrypt
public EncryptedContentInfo(ObjectID contentType,
AlgorithmID contentEncAlg)
contentType - the PKCS#7 content type of the encrypted contentcontentEncAlg - the algorithm used to encrypt the content
public 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 EncryptedContentInfoPKCSParsingException - if the ASN.1 object could not be parsed
public EncryptedContentInfo(InputStream is)
throws IOException,
PKCSParsingException
is - the InputStream holding a DER encoded PKCS#7 EncryptedContentInfo objectIOException - if an I/O error occurs during reading from the InputStreamPKCSParsingException - if an error occurs while parsing the object| Method Detail |
public void decode(ASN1Object obj)
throws PKCSParsingException
obj - the PKCS#7 EncryptedContentInfo as ASN1ObjectPKCSParsingException - if an error occurs while parsing the object
protected void decode(InputStream is)
throws IOException,
PKCSParsingException
DerInputStream,
internally a DerInputStream is created before parsing the data.decode in class EncryptedContentInfoStreamis - the InputStream holding a DER encoded PKCS#7 EncryptedContentInfo objectIOException - if an I/O error occurs during reading from the InputStreamPKCSParsingException - if an error occurs while parsing the object
public void setupCipher(AlgorithmID contentEA,
Key key,
AlgorithmParameterSpec params)
throws NoSuchAlgorithmException,
InvalidKeyException,
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 EncryptedContentInfoStreamcontentEA - the algorithm to use for encrypting the contentkey - the key to useparams - the parameters for the specified algorithmNoSuchAlgorithmException - if there is no implementation for the specified algorithmInvalidKeyException - if the key is inappropriate for the content-encryption algorithmInvalidAlgorithmParameterException - if the provided parameters are not appropriate for the algorithm
public void setupCipher(Key key,
AlgorithmParameterSpec params)
throws NoSuchAlgorithmException,
InvalidKeyException,
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 EncryptedContentInfoStreamkey - the (secret) key to decrypt the contentparams - the algorithm parameters needed to decrypt the contentversion - the version; either 0 or 1NoSuchAlgorithmException - if there is no implementation for the content-encryption-algorithm to be usedInvalidKeyException - if the key is inappropriate for the content-encryption algorithmInvalidAlgorithmParameterException - 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 expilitly 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 1
public ASN1Object toASN1Object()
throws PKCSException
EncryptedContentInfo object using the
EncryptedContentInfo(ASN1Object obj) constructor.toASN1Object in class EncryptedContentInfoStreamEncryptedContentInfo as ASN1Object.public byte[] getContent()
The returned content depends on whether creating a new EncryptedContentInfo or parsing an existing one:
null if there is no contentpublic 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
arbitrarly often; it only returns a ByteArrayInputStream that is initialized with
the content bytes.
getInputStream in class EncryptedContentInfoStreamnull if there is no contentpublic boolean hasContent()
true if there is a content.hasContent in class EncryptedContentInfoStreamtrue if there is a content
public 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 0x00
Otherwise, whole the encrypted content is encoded
as definite primitive octet string:
0x04 <length> <encrypted content>
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK