public abstract class PublicKeyInfo extends java.lang.Object implements ASN1Type, java.security.PublicKey, java.lang.Cloneable
PublicKeyInfo
as used within X.509
certificates for representing the subject's public key in the
SubjectPublicKeyInfo
field.
The subject is the entity claiming for certification of its public key. The
subject's public key is of type subjectPublicKeyInfo
including a
BIT-STRING representation of the public key together with an identification
of the public-key algorithm being used, as defined in RFC 3280:
SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
where:
AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }
This class has to be extended by any class implementing some particular public key (e.g. RSAPublicKey, DSAPublicKey, DHPublicKey) for being used within the X.509 certificate environment.
This class provides a variety of methods for creating, parsing, decoding and encoding public key informations.
As an example, for creating an instance of
java.security.PublicKey
from an ASN1Object representation, or a
DER encoding, use the static getPublicKey(ASN1Object)
respectively getPublicKey(byte[])
methods, e.g.:
byte[] encoding = ...; PublicKey publicKey = PublicKeyInfo(encoding);
X509Certificate
,
RSAPublicKey
,
DSAPublicKey
,
DHPublicKey
,
AlgorithmID
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected AlgorithmID |
public_key_algorithm
The algorithm identifier of the public key algorithm belonging to the
particular public key implemeting this PublicKeyInfo.
|
Modifier | Constructor and Description |
---|---|
protected |
PublicKeyInfo()
Default Constructor for derived classes.
|
|
PublicKeyInfo(ASN1Object obj)
Creates a new PublicKeyInfo from an ASN1Object.
|
|
PublicKeyInfo(byte[] arr)
Creates a new PublicKeyInfo from a DER byte array.
|
|
PublicKeyInfo(java.io.InputStream is)
Creates a new PublicKeyInfo from an InputStream.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Return a clone of this private key object.
|
protected void |
createPublicKeyInfo()
Creates a
PublicKeyInfo data structure (X.509). |
void |
decode(ASN1Object obj)
Decodes the given ASN.1
PublicKeyInfo object for parsing the
internal structure. |
protected abstract void |
decode(byte[] publicKey)
Abstract method to be implemented for decoding a DER encoded public key.
|
protected abstract byte[] |
encode()
Abstract method to be implemented for DER encoding the particular public
key extending this PublicKeyInfo.
|
boolean |
equals(java.lang.Object obj)
Compares this public key to another public key.
|
abstract java.lang.String |
getAlgorithm()
Returns the name of the key algorithm.
|
AlgorithmID |
getAlgorithmID()
Returns the key algorithm id.
|
byte[] |
getEncoded()
Returns this PublicKeyInfo as a DER encoded ASN.1 data structure.
|
byte[] |
getFingerprint()
Returns a fingerprint of the public key.
|
java.lang.String |
getFormat()
Returns the name of the encoding format.
|
static java.security.PublicKey |
getPublicKey(ASN1Object publicKey)
Creates a PublicKey from an ASN1Object.
|
static java.security.PublicKey |
getPublicKey(ASN1Object publicKey,
java.security.Provider provider)
Creates a PublicKey of the given provider from an ASN1Object.
|
static java.security.PublicKey |
getPublicKey(ASN1Object publicKey,
java.lang.String providerName)
Creates a PublicKey of the given provider from an ASN1Object.
|
static java.security.PublicKey |
getPublicKey(byte[] publicKeyInfo)
Creates a PublicKey from a DER encoded byte array.
|
static java.security.PublicKey |
getPublicKey(byte[] publicKeyInfo,
java.security.Provider provider)
Creates a PublicKey of the given provider from a DER encoded byte array.
|
static java.security.PublicKey |
getPublicKey(byte[] publicKeyInfo,
java.lang.String providerName)
Creates a PublicKey of the given provider from a DER encoded byte array.
|
int |
hashCode()
Returns a hash code for this object.
|
ASN1Object |
toASN1Object()
Returns this PublicKeyInfo as ASN1Object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this public key.
|
void |
writeTo(java.io.OutputStream os)
Writes this public key to an output stream.
|
protected AlgorithmID public_key_algorithm
protected PublicKeyInfo()
public PublicKeyInfo(ASN1Object obj) throws java.security.InvalidKeyException
This constructor may be used for parsing an already existing
PublicKeyInfo
object, supplied as ASN1Object that may have
been created by calling toASN1Object
.
obj
- the PublicKeyInfo as ASN1Objectjava.security.InvalidKeyException
- if the data can not be parsedpublic PublicKeyInfo(java.io.InputStream is) throws java.security.InvalidKeyException, java.io.IOException
This constructor reads a DER or PEM encoded PublicKeyInfo which previously
may have been written with method writeTo(OutputStream)
.
This constructor cannot be used to read a serialized object.
is
- the input stream from where the encoded PublicKeyInfo shall be
readjava.security.InvalidKeyException
- if the data can not be parsedjava.io.IOException
- if an I/O error occurspublic PublicKeyInfo(byte[] arr) throws java.security.InvalidKeyException
This constructor may be used for parsing an already existing
PublicKeyInfo
ASN1 object, supplied as DER encoded byte array,
which may have been created by calling the getEncoded
method.
arr
- the array containing the encoded PublicKeyInfojava.security.InvalidKeyException
- if the data can not be parsedprotected abstract void decode(byte[] publicKey) throws java.security.InvalidKeyException
publicKey
- the public key as DER encoded ASN.1 objectjava.security.InvalidKeyException
- if something is wrong with the encoding of the keyprotected abstract byte[] encode()
public abstract java.lang.String getAlgorithm()
getAlgorithm
in interface java.security.Key
public AlgorithmID getAlgorithmID()
public java.lang.Object clone()
clone
in class java.lang.Object
public void decode(ASN1Object obj) throws CodingException
PublicKeyInfo
object for parsing the
internal structure.
This method implements the ASN1Type interface.
decode
in interface ASN1Type
obj
- the PublicKeyInfo as ASN1ObjectCodingException
- if the ASN1Object could not be parsedprotected void createPublicKeyInfo()
PublicKeyInfo
data structure (X.509).
This method is called by extending classes for creating a PublicKeyInfo. This method creates a PublicKeyInfo as an ASN.1 SEQUENCE object with components as specified in RFC 3280:
SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
public static java.security.PublicKey getPublicKey(ASN1Object publicKey) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic public key
is created allowing to get some
information about the key (algorithm, encoding).
publicKey
- the X.509 PublicKey as ASN1ObjectRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PublicKey getPublicKey(ASN1Object publicKey, 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 public key
is created allowing to get some
information about the key (algorithm, encoding).
publicKey
- the PublicKey as ASN1ObjectproviderName
- the name of the provider from which to get a PublicKey objectRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PublicKey getPublicKey(ASN1Object publicKey, 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 public key
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.
publicKey
- the PublicKey as ASN1Objectprovider
- the provider from which to get a PublicKey objectRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the ASN1Object cannot be parsedpublic static java.security.PublicKey getPublicKey(byte[] publicKeyInfo) throws java.security.InvalidKeyException
This method tries to instantiate a KeyFactory for the key algorithm. If no KeyFactory is
available a generic public key
is created allowing to get some
information about the key (algorithm, encoding).
publicKeyInfo
- the PublicKey as DER encoded byte arrayRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the publicKeyInfo cannot be parsedpublic static java.security.PublicKey getPublicKey(byte[] publicKeyInfo, 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 public key
is created allowing to get some
information about the key (algorithm, encoding).
publicKeyInfo
- the PublicKey as DER encoded byte arrayproviderName
- the name of the provider from which to get a PublicKey objectRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the publicKeyInfo cannot be parsedpublic static java.security.PublicKey getPublicKey(byte[] publicKeyInfo, 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 public key
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.
publicKeyInfo
- the PublicKey as DER encoded byte arrayprovider
- the provider from which to get a PublicKey objectRawPublicKey
object is returnedjava.security.InvalidKeyException
- if the publicKeyInfo 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 byte[] getFingerprint()
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