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

public Class PKCS8Key

extends Object
implements PrivateKey, InternalPrivateKey
Class Inheritance
All Implemented Interfaces
sun.security.util.InternalPrivateKey, java.security.PrivateKey, javax.security.auth.Destroyable, java.security.AsymmetricKey, java.security.BinaryEncodable, java.security.Key, java.io.Serializable
Known Direct Subclasses
sun.security.provider.DSAPrivateKey, sun.security.rsa.RSAPrivateCrtKeyImpl, sun.security.rsa.RSAPrivateKeyImpl, sun.security.ec.ECPrivateKeyImpl, sun.security.ec.XDHPrivateKeyImpl, sun.security.ec.ed.EdDSAPrivateKeyImpl, sun.security.pkcs.NamedPKCS8Key
Imports
jdk.internal.access.SharedSecrets, sun.security.ec.ECPrivateKeyImpl, sun.security.util.*, sun.security.x509.AlgorithmId, .X509Key, java.io.IOException, .ObjectInputStream, java.security.*, java.security.spec.InvalidKeySpecException, .PKCS8EncodedKeySpec, java.util.Arrays

Holds a PKCS#8 key, for example a private key According to https://tools.ietf.org/html/rfc5958: OneAsymmetricKey ::= SEQUENCE { version Version, privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, privateKey PrivateKey, attributes [0] Attributes OPTIONAL, ..., [[2: publicKey [1] PublicKey OPTIONAL ]], ... } We support this format but do not parse attributes.

Field Summary

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

Hides java.security.PrivateKey.serialVersionUID.

use serialVersionUID from JDK 1.1.
public static final int
public static final int
private int

Constructor Summary

AccessConstructor and Description
protected
PKCS8Key()

Default constructor.

public
PKCS8Key(byte[] input)

Another constructor.

public
PKCS8Key(byte[] publicEncoding, byte[] privateEncoding)

Constructor that takes both public and private encodings.

Method Summary

Modifier and TypeMethod and Description
public void
private void
decode(DerValue val)

Method for decoding PKCS8 v1 and v2 formats.

public boolean

Returns:

true if this key has the same encoding as the object argument; false otherwise.
equals
(Object
the object with which to compare
object
)

Overrides java.lang.Object.equals.

Compares two private keys.

private byte[]
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, or null if an encoding error occurs.

public static byte[]

Returns:

DER-encoded byte array
getEncoded
(byte[]
DER-encoded PublicKey, this may be null.
pubKeyEncoded
,
byte[]
DER-encoded PrivateKey
privKeyEncoded
)

With a given encoded Public and Private key, generate and return a PKCS8v2 DER-encoded byte[].

private synchronized byte[]

Returns:

the encoding
getEncodedInternal
()

DER-encodes this key as a byte array stored inside this object and return it.

public String
getFormat()

Implements java.security.Key.getFormat.

Returns the format for this key: "PKCS#8"

public byte[]
public int
public int
hashCode()

Overrides java.lang.Object.hashCode.

Calculates a hash code value for this object.

public boolean
public static PrivateKey
parseKey(byte[]
the DER-encoded SubjectPublicKeyInfo value
encoded
)

Construct PKCS#8 subject public key from a DER encoding.

public static PrivateKey
parseKey(byte[] encoded, Provider provider)

private void
readObject(ObjectInputStream stream)

We used to serialize a PKCS8Key as itself (instead of a KeyRep).

protected Object
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

algidback to summary
protected AlgorithmId algid
attributesback to summary
private byte[] attributes
encodedKeyback to summary
protected byte[] encodedKey
privKeyMaterialback to summary
protected byte[] privKeyMaterial
pubKeyEncodedback to summary
protected byte[] pubKeyEncoded
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.PrivateKey.serialVersionUID.

use serialVersionUID from JDK 1.1. for interoperability

Annotations
@Serial
V1back to summary
public static final int V1
V2back to summary
public static final int V2
versionback to summary
private int version

Constructor Detail

PKCS8Keyback to summary
protected PKCS8Key()

Default constructor. Constructors in subclasses that create a new key from its components require this. These constructors must initialize algid and privKeyMaterial.

PKCS8Keyback to summary
public PKCS8Key(byte[] input) throws InvalidKeyException

Another constructor. Constructors in subclasses that create a new key from an encoded byte array require this. We do not assign this encoding to encodedKey directly. This method is also used by parseKey to create a raw key.

PKCS8Keyback to summary
public PKCS8Key(byte[] publicEncoding, byte[] privateEncoding) throws InvalidKeyException

Constructor that takes both public and private encodings. If the private key includes a public key encoding (like an EC key in SEC1v2 format), and a specified public key matches it, the existing encoding is reused rather than recreated.

Method Detail

clearback to summary
public void clear()
decodeback to summary
private void decode(DerValue val) throws InvalidKeyException

Method for decoding PKCS8 v1 and v2 formats. Decoded values are stored in this class, key material remains in DER format for algorithm subclasses to decode.

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Compares two private keys. This returns false if the object with which to compare is not of type Key. Otherwise, the encoding of this key object is compared with the encoding of the given key object.

Parameters
object:Object

the object with which to compare

Returns:boolean

true if this key has the same encoding as the object argument; false otherwise.

Annotations
@Override
generateEncodingback to summary
private byte[] generateEncoding() throws IOException
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, or null if an encoding error occurs.

getEncodedback to summary
public static byte[] getEncoded(byte[] pubKeyEncoded, byte[] privKeyEncoded) throws IOException

With a given encoded Public and Private key, generate and return a PKCS8v2 DER-encoded byte[].

Parameters
pubKeyEncoded:byte[]

DER-encoded PublicKey, this may be null.

privKeyEncoded:byte[]

DER-encoded PrivateKey

Returns:byte[]

DER-encoded byte array

Exceptions
IOException:
thrown on encoding failure
getEncodedInternalback to summary
private synchronized byte[] getEncodedInternal()

DER-encodes this key as a byte array stored inside this object and return it.

Returns:byte[]

the encoding

getFormatback to summary
public String getFormat()

Implements java.security.Key.getFormat.

Returns the format for this key: "PKCS#8"

getPubKeyEncodedback to summary
public byte[] getPubKeyEncoded()
getVersionback to summary
public int getVersion()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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

Annotations
@Override
hasPublicKeyback to summary
public boolean hasPublicKey()
parseKeyback to summary
public static PrivateKey parseKey(byte[] encoded) throws InvalidKeyException

Construct PKCS#8 subject public key from a DER encoding. If a security provider supports the key algorithm with a specific class, a PrivateKey from the provider is returned. Otherwise, a raw PKCS8Key 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
encoded:byte[]

the DER-encoded SubjectPublicKeyInfo value

Exceptions
InvalidKeyException:
on data format errors
parseKeyback to summary
public static PrivateKey parseKey(byte[] encoded, Provider provider) throws InvalidKeyException
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException

We used to serialize a PKCS8Key as itself (instead of a KeyRep).

Annotations
@Serial
writeReplaceback to summary
protected Object writeReplace() throws ObjectStreamException
Annotations
@Serial