APIdia Launch Partner
Top Description Fields Constructors Methods
sun.security.x509

public Class X509Key

extends Object
implements PublicKey, DerEncoder
Class Inheritance
All Implemented Interfaces
sun.security.util.DerEncoder, java.security.PublicKey, java.security.AsymmetricKey, java.security.BinaryEncodable, java.security.Key, java.io.Serializable
Known Direct Subclasses
sun.security.ec.ECPublicKeyImpl, sun.security.ec.XDHPublicKeyImpl, sun.security.ec.ed.EdDSAPublicKeyImpl, sun.security.provider.DSAPublicKey, sun.security.provider.HSS.HSSPublicKey, sun.security.rsa.RSAPublicKeyImpl, sun.security.x509.NamedX509Key
Imports
java.io.*, java.util.Arrays, java.security.Key, .PublicKey, .KeyFactory, .Security, .Provider, .InvalidKeyException, .NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException, .X509EncodedKeySpec, sun.security.util.HexDumpEncoder, sun.security.util.*

Holds an X.509 key, for example a public key found in an X.509 certificate. Includes a description of the algorithm to be used with the key; these keys normally are used as "SubjectPublicKeyInfo".

While this class can represent any kind of X.509 key, it may be desirable to provide subclasses which understand how to parse keying data. For example, RSA public keys have two members, one for the public modulus and one for the prime exponent. If such a class is provided, it is used when parsing X.509 keys. If one is not provided, the key still parses correctly.

Author
David Brownell

Field Summary

Modifier and TypeField and Description
protected AlgorithmId
private transient BitArray
protected byte[]
private static final long
serialVersionUID

Hides java.security.PublicKey.serialVersionUID.

use serialVersionUID from JDK 1.1.

Constructor Summary

AccessConstructor and Description
public
X509Key()

Default constructor.

public

Method Summary

Modifier and TypeMethod and Description
pack-priv static PublicKey
public void
decode(DerValue
a DER-encoded X.509 SubjectPublicKeyInfo value
val
)

Initialize an X509Key object from a DerValue, obeying the X.509 SubjectPublicKeyInfo format.

public void
decode(byte[] encodedKey)

public final void
encode(DerOutputStream out)

Implements sun.security.util.DerEncoder.encode.

Encode SubjectPublicKeyInfo sequence on the DER output stream.

public byte[]
encode()

Returns the DER-encoded form of the key as a byte array.

pack-priv static void
public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public String
getAlgorithm()

Implements java.security.Key.getAlgorithm.

Returns the algorithm to be used with this key.

public AlgorithmId
getAlgorithmId()

Returns the algorithm ID to be used with this key.

public byte[]
getEncoded()

Implements java.security.Key.getEncoded.

Returns the DER-encoded form of the key as a byte array.

private byte[]
public String
getFormat()

Implements java.security.Key.getFormat.

Returns the format for this key: "X.509"

public BitArray

Returns:

a BitArray containing the key.
getKey
()

Gets the key.

public byte[]
public int
hashCode()

Overrides java.lang.Object.hashCode.

Calculates a hash code value for the object.

public static PublicKey
parse(DerValue
the DER-encoded SubjectPublicKeyInfo value
in
)

Construct X.509 subject public key from a DER value.

protected void
parseKeyBits()

Parse the key bits.

private void
readObject(ObjectInputStream stream)

Serialization read ...

protected void
setKey(BitArray key)

Sets the key in the BitArray form.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
writeObject(ObjectOutputStream stream)

Serialization write ...

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

algidback to summary
protected AlgorithmId algid
bitStringKeyback to summary
private transient BitArray bitStringKey
encodedKeyback to summary
protected byte[] encodedKey
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.PublicKey.serialVersionUID.

use serialVersionUID from JDK 1.1. for interoperability

Annotations
@Serial

Constructor Detail

X509Keyback to summary
public X509Key()

Default constructor. The key constructed must have its key and algorithm initialized before it may be used, for example by using decode.

X509Keyback to summary
public X509Key(AlgorithmId algid, BitArray key)
Annotations
@SuppressWarnings:this-escape

Method Detail

buildX509Keyback to summary
pack-priv static PublicKey buildX509Key(AlgorithmId algid, BitArray key) throws IOException, InvalidKeyException
decodeback to summary
public void decode(DerValue val) throws InvalidKeyException

Initialize an X509Key object from a DerValue, obeying the X.509 SubjectPublicKeyInfo format. That is, the data is a sequence consisting of an algorithm ID and a bit string which holds the key. (That bit string is often used to encapsulate another DER encoded sequence.)

Subclasses should not normally redefine this method; they should instead provide a parseKeyBits method to parse any fields inside the key member.

The exception to this rule is that since private keys need not be encoded using the X.509 SubjectPublicKeyInfo format, private keys may override this method, encode, and of course getFormat.

Parameters
val:DerValue

a DER-encoded X.509 SubjectPublicKeyInfo value

Exceptions
InvalidKeyException:
on parsing errors.
decodeback to summary
public void decode(byte[] encodedKey) throws InvalidKeyException
encodeback to summary
public final void encode(DerOutputStream out)

Implements sun.security.util.DerEncoder.encode.

Encode SubjectPublicKeyInfo sequence on the DER output stream.

Annotations
@Override
encodeback to summary
public byte[] encode()

Returns the DER-encoded form of the key as a byte array.

encodeback to summary
pack-priv static void encode(DerOutputStream out, AlgorithmId algid, BitArray key)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getAlgorithmback to summary
public String getAlgorithm()

Implements java.security.Key.getAlgorithm.

Returns the algorithm to be used with this key.

getAlgorithmIdback to summary
public AlgorithmId getAlgorithmId()

Returns the algorithm ID to be used with this key.

getEncodedback to summary
public byte[] getEncoded()

Implements java.security.Key.getEncoded.

Returns the DER-encoded form of the key as a byte array.

getEncodedInternalback to summary
private byte[] getEncodedInternal()
getFormatback to summary
public String getFormat()

Implements java.security.Key.getFormat.

Returns the format for this key: "X.509"

getKeyback to summary
public BitArray getKey()

Gets the key. The key may or may not be byte aligned.

Returns:BitArray

a BitArray containing the key.

getKeyAsBytesback to summary
public byte[] getKeyAsBytes()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Calculates a hash code value for the object. Objects which are equal will also have the same hashcode.

Annotations
@Override
parseback to summary
public static PublicKey parse(DerValue in) throws IOException

Construct X.509 subject public key from a DER value. If the runtime environment is configured with a specific class for this kind of key, a subclass is returned. Otherwise, a generic X509Key object is returned.

This mechanism guarantees that keys (and algorithms) may be freely manipulated and transferred, without risk of losing information. Also, when a key (or algorithm) needs some special handling, that specific need can be accommodated.

Parameters
in:DerValue

the DER-encoded SubjectPublicKeyInfo value

Exceptions
IOException:
on data format errors
parseKeyBitsback to summary
protected void parseKeyBits() throws InvalidKeyException

Parse the key bits. This may be redefined by subclasses to take advantage of structure within the key. For example, RSA public keys encapsulate two unsigned integers (modulus and exponent) as DER values within the key bits; Diffie-Hellman and DSS/DSA keys encapsulate a single unsigned integer.

This function is called when creating X.509 SubjectPublicKeyInfo values using the X509Key member functions, such as parse and decode.

Exceptions
InvalidKeyException:
on invalid key encodings.
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException

Serialization read ... X.509 keys serialize as themselves, and they're parsed when they get read back.

Annotations
@Serial
setKeyback to summary
protected void setKey(BitArray key)

Sets the key in the BitArray form.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

writeObjectback to summary
private void writeObject(ObjectOutputStream stream) throws IOException

Serialization write ... X.509 keys serialize as themselves, and they're parsed when they get read back.

Annotations
@Serial