APIdia Launch Partner
Top Description Inners Constructors Methods
sun.security.util

public final Class KeyUtil

extends Object
Class Inheritance
Imports
java.io.IOException, java.math.BigInteger, java.security.*, java.security.interfaces.*, java.security.spec.*, java.util.Arrays, java.util.function.Function, javax.crypto.SecretKey, javax.crypto.interfaces.DHKey, .DHPublicKey, javax.crypto.spec.DHParameterSpec, .DHPublicKeySpec, .PBEKeySpec, .SecretKeySpec, javax.security.auth.DestroyFailedException, jdk.internal.access.SharedSecrets, com.sun.crypto.provider.PBKDF2KeyImpl, sun.security.jca.JCAUtil, sun.security.pkcs.PKCS8Key, sun.security.x509.AlgorithmId, .X509Key

A utility class to get key length, validate keys, etc.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

the polished PreMasterSecret key in its "RAW" encoding format
checkTlsPreMasterSecretKey
(int
the version of the TLS protocol by which the client wishes to communicate during this session
clientVersion
,
int
the negotiated version of the TLS protocol which contains the lower of that suggested by the client in the client hello and the highest supported by the server.
serverVersion
,
SecureRandom random, byte[]
the encoded key in its "RAW" encoding format
encoded
,
boolean
true if encoded is incorrect according to previous checks
failure
)

Check the format of TLS PreMasterSecret.

public static void
clear(Object... list)

public static <T> T
clear(byte[] encoding, Function<byte[], T> op)

Executes op with encoding and then zeroes encoding in a finally block before returning or propagating an exception.

public static void
public static String
getAlgorithm(Key key)

If the key is a sub-algorithm of a larger group of algorithms, this method will return that sub-algorithm.

public static String

Returns:

AlgorithmID stored in the DER encoded bytes or null.
getAlgorithm
(byte[]
DER encoded bytes
encoded
)

With a given DER encoded bytes, read through and return the AlgorithmID stored if it can be found.

public static AlgorithmId

Returns:

AlgorithmID stored in the DER encoded bytes
getAlgorithmId
(byte[]
DER encoded bytes
encoded
)

With a given DER encoded bytes, read through and return the AlgorithmID stored if it can be found.

public static int

Returns:

the key size of the given key object in bits, or -1 if the key size is not accessible
getKeySize
(Key
the key object, cannot be null
key
)

Returns the key size of the given key object in bits.

public static final int

Returns:

the key size of the given cryptographic parameters in bits, or -1 if the key size is not accessible
getKeySize
(AlgorithmParameters
the cryptographic parameters, cannot be null
parameters
)

Returns the key size of the given cryptographic parameters in bits.

public static ObjectIdentifier

Returns:

the hash algorithm
hashAlgFromHSS
(PublicKey
the HSS/LMS public key
publicKey
)

Finds the hash algorithm from an HSS/LMS public key.

public static final boolean

Returns:

true if, and only if, the provider of the specified providerName is Oracle provider
isOracleJCEProvider
(String
the provider name
providerName
)

Returns whether the specified provider is Oracle provider or not.

public static boolean
public static PublicKey
newRawPublicKey(String algorithm, byte[] key)

public static PublicKey
newRawPublicKey(String algorithm, AlgorithmParameterSpec params, byte[] key)

public static byte[]
rawToX509(String pname, byte[] bytes)

public static byte[]
trimZeroes(byte[] b)

Trim leading (most significant) zeroes from the result.

public static final void
validate(Key
the key object, cannot be null
key
)

Returns whether the key is valid or not.

public static final void
validate(KeySpec
the key spec object, cannot be null
keySpec
)

Returns whether the key spec is valid or not.

private static void
validateDHPublicKey(DHPublicKey publicKey)

Returns whether the Diffie-Hellman public key is valid or not.

private static void
private static void
public static byte[]
x509ToRaw(byte[] bytes)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

KeyUtilback to summary
public KeyUtil()

Method Detail

checkTlsPreMasterSecretKeyback to summary
public static byte[] checkTlsPreMasterSecretKey(int clientVersion, int serverVersion, SecureRandom random, byte[] encoded, boolean failure)

Check the format of TLS PreMasterSecret.

To avoid vulnerabilities described by section 7.4.7.1, RFC 5246, treating incorrectly formatted message blocks and/or mismatched version numbers in a manner indistinguishable from correctly formatted RSA blocks. RFC 5246 describes the approach as:

1. Generate a string R of 48 random bytes

 2. Decrypt the message to recover the plaintext M

 3. If the PKCS#1 padding is not correct, or the length of message
    M is not exactly 48 bytes:
       pre_master_secret = R
    else If ClientHello.client_version <= TLS 1.0, and version
    number check is explicitly disabled:
       premaster secret = M
    else If M[0..1] != ClientHello.client_version:
       premaster secret = R
    else:
       premaster secret = M

Note that #2 should have completed before the call to this method.
Parameters
clientVersion:int

the version of the TLS protocol by which the client wishes to communicate during this session

serverVersion:int

the negotiated version of the TLS protocol which contains the lower of that suggested by the client in the client hello and the highest supported by the server.

encoded:byte[]

the encoded key in its "RAW" encoding format

failure:boolean

true if encoded is incorrect according to previous checks

Returns:byte[]

the polished PreMasterSecret key in its "RAW" encoding format

clearback to summary
public static void clear(Object... list)
clearback to summary
public static <T> T clear(byte[] encoding, Function<byte[], T> op)

Executes op with encoding and then zeroes encoding in a finally block before returning or propagating an exception. encoding is temporary sensitive data and is always wiped. Usage constraint: op must not return encoding itself, or any value backed by the same array. Otherwise, the returned data will already be zeroed when this method returns.

destroySecretKeysback to summary
public static void destroySecretKeys(SecretKey... keys)
getAlgorithmback to summary
public static String getAlgorithm(Key key)

If the key is a sub-algorithm of a larger group of algorithms, this method will return that sub-algorithm. For example, key.getAlgorithm() returns "EdDSA", but the underlying key may be "Ed448". For DisabledAlgorithmConstraints (DAC), this distinction is important. "EdDSA" means all curves for DAC, but when using it with KeyPairGenerator, "EdDSA" means "Ed25519".

getAlgorithmback to summary
public static String getAlgorithm(byte[] encoded) throws IOException

With a given DER encoded bytes, read through and return the AlgorithmID stored if it can be found. If none is found or there is an IOException, null is returned.

Parameters
encoded:byte[]

DER encoded bytes

Returns:String

AlgorithmID stored in the DER encoded bytes or null.

getAlgorithmIdback to summary
public static AlgorithmId getAlgorithmId(byte[] encoded) throws IOException

With a given DER encoded bytes, read through and return the AlgorithmID stored if it can be found.

Parameters
encoded:byte[]

DER encoded bytes

Returns:AlgorithmId

AlgorithmID stored in the DER encoded bytes

Exceptions
IOException:
if there was a DER or other parsing error
getKeySizeback to summary
public static int getKeySize(Key key)

Returns the key size of the given key object in bits.

Parameters
key:Key

the key object, cannot be null

Returns:int

the key size of the given key object in bits, or -1 if the key size is not accessible

getKeySizeback to summary
public static final int getKeySize(AlgorithmParameters parameters)

Returns the key size of the given cryptographic parameters in bits.

Parameters
parameters:AlgorithmParameters

the cryptographic parameters, cannot be null

Returns:int

the key size of the given cryptographic parameters in bits, or -1 if the key size is not accessible

hashAlgFromHSSback to summary
public static ObjectIdentifier hashAlgFromHSS(PublicKey publicKey) throws NoSuchAlgorithmException

Finds the hash algorithm from an HSS/LMS public key.

Parameters
publicKey:PublicKey

the HSS/LMS public key

Returns:ObjectIdentifier

the hash algorithm

Exceptions
NoSuchAlgorithmException:
if key is from an unknown configuration
isOracleJCEProviderback to summary
public static final boolean isOracleJCEProvider(String providerName)

Returns whether the specified provider is Oracle provider or not.

Parameters
providerName:String

the provider name

Returns:boolean

true if, and only if, the provider of the specified providerName is Oracle provider

isSupportedKeyAgreementOutputAlgorithmback to summary
public static boolean isSupportedKeyAgreementOutputAlgorithm(String alg)
newRawPublicKeyback to summary
public static PublicKey newRawPublicKey(String algorithm, byte[] key)
newRawPublicKeyback to summary
public static PublicKey newRawPublicKey(String algorithm, AlgorithmParameterSpec params, byte[] key)
rawToX509back to summary
public static byte[] rawToX509(String pname, byte[] bytes) throws NoSuchAlgorithmException
trimZeroesback to summary
public static byte[] trimZeroes(byte[] b)

Trim leading (most significant) zeroes from the result.

Exceptions
NullPointerException:
if b is null
validateback to summary
public static final void validate(Key key) throws InvalidKeyException

Returns whether the key is valid or not.

Note that this method is only apply to DHPublicKey at present.

Parameters
key:Key

the key object, cannot be null

Exceptions
InvalidKeyException:
if key is invalid
NullPointerException:
if key is null
validateback to summary
public static final void validate(KeySpec keySpec) throws InvalidKeyException

Returns whether the key spec is valid or not.

Note that this method is only apply to DHPublicKeySpec at present.

Parameters
keySpec:KeySpec

the key spec object, cannot be null

Exceptions
InvalidKeyException:
if keySpec is invalid
NullPointerException:
if keySpec is null
validateDHPublicKeyback to summary
private static void validateDHPublicKey(DHPublicKey publicKey) throws InvalidKeyException

Returns whether the Diffie-Hellman public key is valid or not. Per RFC 2631 and NIST SP800-56A, the following algorithm is used to validate Diffie-Hellman public keys: 1. Verify that y lies within the interval [2,p-1]. If it does not, the key is invalid. 2. Compute y^q mod p. If the result == 1, the key is valid. Otherwise, the key is invalid.

validateDHPublicKeyback to summary
private static void validateDHPublicKey(DHPublicKeySpec publicKeySpec) throws InvalidKeyException
validateDHPublicKeyback to summary
private static void validateDHPublicKey(BigInteger p, BigInteger g, BigInteger y) throws InvalidKeyException
x509ToRawback to summary
public static byte[] x509ToRaw(byte[] bytes) throws IOException
sun.security.util back to summary

private final Record KeyUtil.RawPublicKey

extends Record
implements PublicKey
Class Inheritance
All Implemented Interfaces
java.security.PublicKey, java.security.AsymmetricKey, java.security.BinaryEncodable, java.security.Key, java.io.Serializable
Record Components
algorithm:String 
params:AlgorithmParameterSpec 
data:byte[] 

Field Summary

Modifier and TypeField and Description
private final String
algorithm

Record Component accessed by algorithm().

private final byte[]
data

Record Component accessed by data().

private final AlgorithmParameterSpec
params

Record Component accessed by params().

Constructor Summary

AccessConstructor and Description
pack-priv
RawPublicKey(String algorithm, AlgorithmParameterSpec params, byte[] data)

Method Summary

Modifier and TypeMethod and Description
public String
algorithm()

Record Component getter of algorithm.

public byte[]
data()

Record Component getter of data.

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

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

public String
getAlgorithm()

Implements java.security.Key.getAlgorithm.

Returns the standard algorithm name for this key.

public byte[]
getEncoded()

Implements java.security.Key.getEncoded.

Returns the key in its primary encoding format, or null if this key does not support encoding.

public String
getFormat()

Implements java.security.Key.getFormat.

Returns the name of the primary encoding format of this key, or null if this key does not support encoding.

public AlgorithmParameterSpec
getParams()

Overrides default java.security.AsymmetricKey.getParams.

Returns the parameters associated with this key.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public AlgorithmParameterSpec
params()

Record Component getter of params.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

algorithmback to summary
private final String algorithm

Record Component accessed by algorithm().

databack to summary
private final byte[] data

Record Component accessed by data().

paramsback to summary
private final AlgorithmParameterSpec params

Record Component accessed by params().

Constructor Detail

RawPublicKeyback to summary
pack-priv RawPublicKey(String algorithm, AlgorithmParameterSpec params, byte[] data)

Method Detail

algorithmback to summary
public String algorithm()

Record Component getter of algorithm.

databack to summary
public byte[] data()

Record Component getter of data.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
getAlgorithmback to summary
public String getAlgorithm()

Implements java.security.Key.getAlgorithm.

Doc from java.security.Key.getAlgorithm.

Returns the standard algorithm name for this key. For example, "RSA" would indicate that this key is an RSA key. See the Key Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard key algorithm names.

Annotations
@Override
getEncodedback to summary
public byte[] getEncoded()

Implements java.security.Key.getEncoded.

Doc from java.security.Key.getEncoded.

Returns the key in its primary encoding format, or null if this key does not support encoding.

Annotations
@Override
getFormatback to summary
public String getFormat()

Implements java.security.Key.getFormat.

Doc from java.security.Key.getFormat.

Returns the name of the primary encoding format of this key, or null if this key does not support encoding. The primary encoding format is named in terms of the appropriate ASN.1 data format, if an ASN.1 specification for this key exists. For example, the name of the ASN.1 data format for public keys is SubjectPublicKeyInfo, as defined by the X.509 standard; in this case, the returned format is "X.509". Similarly, the name of the ASN.1 data format for private keys is PrivateKeyInfo, as defined by the PKCS #8 standard; in this case, the returned format is "PKCS#8".

Annotations
@Override
getParamsback to summary
public AlgorithmParameterSpec getParams()

Overrides default java.security.AsymmetricKey.getParams.

Doc from java.security.AsymmetricKey.getParams.

Returns the parameters associated with this key. The parameters are optional and may be either explicitly specified or implicitly created during key pair generation.

Annotations
@Override
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

paramsback to summary
public AlgorithmParameterSpec params()

Record Component getter of params.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.