public abstract class PrivateKeyInfo extends java.lang.Object implements ASN1Type, java.security.PrivateKey, java.lang.Cloneable
A Private-key information according to PKCS#8 combines a private key (of a public-key algorithm) and some attributes containing additional key related informtation:
where:PrivateKeyInfo ::= SEQUENCE { version Version, privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, privateKey PrivateKey attributes [0] IMPLICIT Attributes OPTIONAL }
RFC 5958 enhances the PrivateKeyInfo syntax by introducing the OneAsymmetricKey type that basically is the same as PKCS#8 the PKCS#8 PrivateKeyInfo but adds theVersion ::= INTEGER -- Syntax Version Number PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier -- private key algorithm, e.g. rsaEncryption from PKCS#1 PrivateKey ::= OCTET STRING -- the value of the private key, e.g. a BER encoded RSAPrivateKey value Attributes ::= SET OF Attribute -- additional information
publicKey
field to the PrivateKeyInfo
structure (see RFC 5958):
Backwards compatibility is maintained by theOneAsymmetricKey ::= SEQUENCE { version Version, privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, privateKey PrivateKey, attributes [0] Attributes OPTIONAL, ..., [[2: publicKey [1] PublicKey OPTIONAL ]], ... } PrivateKeyInfo ::= OneAsymmetricKey
version
number
field. IAIK-JCE adds support for the OneAsymmetricKey type by simply
adding the publicKey
field to this PrivateKeyInfo class.
This allows to use the publicKey
enhancement by all PrivateKey
classes that are extended from PKCS#8 PrivateKeyInfo.
This class may be extended for every particular private key implementation wishing to support PKCS#8 PrivateKeyInfo/OneAsymmetricKey.
This class provides a variety of methods for creating, parsing, de- and encoding private key informations.
DHPrivateKey
,
DSAPrivateKey
,
RSAPrivateKey
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected AlgorithmID |
private_key_algorithm
Identifies the appertaining private-key algorithm.
|
Modifier | Constructor and Description |
---|---|
protected |
PrivateKeyInfo()
Default Constructor for derived classes.
|
|
PrivateKeyInfo(ASN1Object obj)
Creates a new PrivateKeyInfo from an ASN1Object.
|
|
PrivateKeyInfo(byte[] arr)
Creates a new PrivateKeyInfo from a byte array.
|
|
PrivateKeyInfo(java.io.InputStream is)
Creates a new PrivateKeyInfo from an InputStream.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Return a clone of this private key object.
|
protected void |
createPrivateKeyInfo()
Creates a PrivateKeyInfo ASN.1 data structure (PKCS#8).
|
void |
decode(ASN1Object obj)
Decodes the given ASN.1
PrivateKeyInfo object
for parsing the internal structure. |
protected abstract void |
decode(byte[] privateKey)
Abstract method to be implemented for decoding a DER encoded private key.
|
protected abstract byte[] |
encode()
Abstract method to be implemented for encoding this private key according
to DER.
|
boolean |
equals(java.lang.Object obj)
Compares this private key to another private key.
|
abstract java.lang.String |
getAlgorithm()
Returns the name of the algorithm.
|
AlgorithmID |
getAlgorithmID()
Returns the key algorithm id.
|
Attribute[] |
getAttributes()
Gets any included attributes.
|
byte[] |
getEncoded()
Returns this PrivateKeyInfo as a DER encoded ASN.1 data structure.
|
java.lang.String |
getFormat()
Returns the name of the encoding format..
|
static java.security.PrivateKey |
getPrivateKey(ASN1Object privateKeyInfo)
Creates a PrivateKey from an ASN1Object.
|
static java.security.PrivateKey |
getPrivateKey(ASN1Object privateKeyInfo,
java.security.Provider provider)
Creates a PrivateKey for the given provider from an ASN1Object.
|
static java.security.PrivateKey |
getPrivateKey(ASN1Object privateKeyInfo,
java.lang.String providerName)
Creates a PrivateKey for the given provider from an ASN1Object.
|
static java.security.PrivateKey |
getPrivateKey(byte[] privateKeyInfo)
Creates a PrivateKey from a DER encoded byte array in PKCS#8 PrivateKeyInfo format.
|
static java.security.PrivateKey |
getPrivateKey(byte[] privateKeyInfo,
java.security.Provider provider)
Creates a PrivateKey for the given provider from a DER encoded byte array in PKCS#8 PrivateKeyInfo format.
|
static java.security.PrivateKey |
getPrivateKey(byte[] privateKeyInfo,
java.lang.String providerName)
Creates a PrivateKey for the given provider from a DER encoded byte array in PKCS#8 PrivateKeyInfo format.
|
java.security.PublicKey |
getPubKey()
Gets the public key (if included).
|
int |
hashCode()
Returns a hash code for this object.
|
void |
setAttributes(Attribute[] attributes)
Sets any attributes to be included.
|
void |
setPubKey(java.security.PublicKey publicKey)
Sets the public key (if to be included).
|
ASN1Object |
toASN1Object()
Returns this PrivateKeyInfo as ASN1Object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this private key.
|
void |
writeTo(java.io.OutputStream os)
Writes this private key to an output stream.
|
protected AlgorithmID private_key_algorithm
protected PrivateKeyInfo()
public PrivateKeyInfo(ASN1Object obj) throws java.security.InvalidKeyException
This constructor may be used for parsing an
already existing PrivateKeyInfo
object, supplied as
ASN1Object that may have been created by calling
toASN1Object
.
obj
- the PrivateKeyInfo as ASN1Objectjava.security.InvalidKeyException
- if the data can not be parsedpublic PrivateKeyInfo(java.io.InputStream is) throws java.security.InvalidKeyException, java.io.IOException
is
- the input stream from where the encoded PrivateKeyInfo shall be readjava.security.InvalidKeyException
- if the data can not be parsedjava.io.IOException
- if an I/O error occurspublic PrivateKeyInfo(byte[] arr) throws java.security.InvalidKeyException
This constructor may be used for parsing an
already existing PrivateKeyInfo
object, supplied
as DER encoded ASN.1 structure, which may have been created
by calling the getEncoded
method of this class.
arr
- the array containing the encoded PrivateKeyInfojava.security.InvalidKeyException
- if the data can not be parsedprotected abstract void decode(byte[] privateKey) throws java.security.InvalidKeyException
privateKey
- the private key as DER encoded byte arrayjava.security.InvalidKeyException
- if the does not match to the implemented
public-key algorithmprotected abstract byte[] encode()
public abstract java.lang.String getAlgorithm()
getAlgorithm
in interface java.security.Key
public AlgorithmID getAlgorithmID()
public void setAttributes(Attribute[] attributes)
attributes
- the attributespublic Attribute[] getAttributes()
null
if
no attributes are includedpublic void setPubKey(java.security.PublicKey publicKey) throws java.security.InvalidKeyException
publicKey
- the public keyjava.security.InvalidKeyException
- if the public key algorithm is
not appropriate for this private keypublic java.security.PublicKey getPubKey()
public java.lang.Object clone()
clone
in class java.lang.Object
public void decode(ASN1Object obj) throws CodingException
PrivateKeyInfo
object
for parsing the internal structure.
This method implements the ASN1Type interface and internally is called when
creating a PKCS#8 PrivateKeyInfo
object from an
already existing PrivateKeyInfo
object, supplied
as ASN1Object or DER encoded ASN1Object.
decode
in interface ASN1Type
obj
- the PrivateKeyInfo as ASN1ObjectCodingException
- if the ASN1Object could not be parsedprotected void createPrivateKeyInfo()
This method is called by extending classes for creating a PrivateKeyInfo. This method creates a PrivateKeyInfo as an ASN.1 SEQUENCE object with components as specified in the PKCS#8 Private-Key Information Syntax Standard:
public static java.security.PrivateKey getPrivateKey(ASN1Object privateKeyInfo) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
privateKeyInfo
- the PrivateKeyInfo as ASN1ObjectRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PrivateKey getPrivateKey(ASN1Object privateKeyInfo, java.lang.String providerName) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
privateKeyInfo
- the PrivateKeyInfo as ASN1ObjectproviderName
- the name of the provider from which to get a PrivateKey objectRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PrivateKey getPrivateKey(ASN1Object privateKeyInfo, java.security.Provider provider) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
If Provider object based JCA/JCE KeyFactory engine instantiation
is not available the Java VM in use (<1.4), this method tries to instantiate an implementation
based on the provider name (if the Provider is installed within the Security Provider
framework). I.e. if method KeyFactory.getInstance(algorithm,provider)
is not available method KeyFactory.getInstance(algorithm,provider.getName())
is tried.
privateKeyInfo
- the PrivateKeyInfo as ASN1Objectprovider
- the provider from which to get a PrivateKey objectRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PrivateKey getPrivateKey(byte[] privateKeyInfo) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
privateKeyInfo
- the PrivateKey as DER encoded byte arrayRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the privateKey cannot be parsedpublic static java.security.PrivateKey getPrivateKey(byte[] privateKeyInfo, java.lang.String providerName) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
privateKeyInfo
- the PrivateKey as DER encoded byte arrayproviderName
- the name of the provider from which to get a PrivateKey objectRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the privateKey cannot be parsedpublic static java.security.PrivateKey getPrivateKey(byte[] privateKeyInfo, java.security.Provider provider) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic PrivateKey
is created allowing to get some
information about the key (algorithm, encoding).
If Provider object based JCA/JCE KeyFactory engine instantiation
is not available the Java VM in use (<1.4), this method tries to instantiate an implementation
based on the provider name (if the Provider is installed within the Security Provider
framework). I.e. if method KeyFactory.getInstance(algorithm,provider)
is not available method KeyFactory.getInstance(algorithm,provider.getName())
is tried.
privateKeyInfo
- the PrivateKey as DER encoded byte arrayprovider
- the provider from which to get a PrivateKey objectRawPrivateKey
object is returnedjava.security.InvalidKeyException
- if the privateKey cannot be parsedpublic ASN1Object toASN1Object()
toASN1Object
in interface ASN1Type
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public byte[] getEncoded()
getEncoded
in interface java.security.Key
public java.lang.String getFormat()
getFormat
in interface java.security.Key
public void writeTo(java.io.OutputStream os) throws java.io.IOException
os
- the output streamjava.io.IOException
- if an I/O error occurspublic java.lang.String toString()
toString
in class java.lang.Object