Package com.nimbusds.jose.jwk
Class JWK
java.lang.Object
com.nimbusds.jose.jwk.JWK
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ECKey,OctetKeyPair,OctetSequenceKey,RSAKey
The base abstract class for JSON Web Keys (JWKs). It serialises to a JSON
object.
The following JSON object members are common to all JWK types:
kty(required)use(optional)key_ops(optional)kid(optional)x5u(optional)x5t(optional)x5t#S256(optional)x5c(optional)exp(optional)nbf(optional)iat(optional)getKeyStore()
Example JWK (of the Elliptic Curve type):
{
"kty" : "EC",
"crv" : "P-256",
"x" : "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
"y" : "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
"use" : "enc",
"kid" : "1"
}
- Version:
- 2022-12-26
- Author:
- Vladimir Dzhuvinov, Justin Richer, Stefan Larsson
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJWK(KeyType kty, KeyUse use, Set<KeyOperation> ops, Algorithm alg, String kid, URI x5u, Base64URL x5t, Base64URL x5t256, List<Base64> x5c, KeyStore ks) Deprecated.protectedJWK(KeyType kty, KeyUse use, Set<KeyOperation> ops, Algorithm alg, String kid, URI x5u, Base64URL x5t, Base64URL x5t256, List<Base64> x5c, Date exp, Date nbf, Date iat, KeyStore ks) Creates a new JSON Web Key (JWK). -
Method Summary
Modifier and TypeMethodDescriptionComputes the SHA-256 thumbprint of this JWK.computeThumbprint(String hashAlg) Computes the thumbprint of this JWK using the specified hash algorithm.Computes the SHA-256 thumbprint URI of this JWK.booleanGets the intended JOSE algorithm (alg) for this JWK.Gets the expiration time (exp) if this JWK.Gets the issued-at (iat) time of this JWK.getKeyID()Gets the ID (kid) of this JWK.Gets the operations (key_ops) for this JWK.Returns a reference to the underlying key store.Gets the type (kty) of this JWK.Gets the use (use) of this JWK.Gets the not-before (nbf) of this JWK.Gets the parsed X.509 certificate chain (x5c) of this JWK.abstract LinkedHashMap<String,?> Returns the required JWK parameters.Gets the X.509 certificate chain (x5c) of this JWK.Gets the X.509 certificate SHA-256 thumbprint (x5t#S256) of this JWK.Deprecated.Gets the X.509 certificate URL (x5u) of this JWK.inthashCode()abstract booleanReturnstrueif this JWK contains private or sensitive (non-public) parameters.static JWKLoads a JWK from the specified JCE key store.static JWKParses a JWK from the specified JSON object string representation.static JWKparse(X509Certificate cert) static JWKParses a JWK from the specified JSON object representation.static JWKparseFromPEMEncodedObjects(String pemEncodedObjects) Parses an RSA or EC JWK from the specified string of one or more PEM-encoded object(s): X.509 certificate (PEM header: BEGIN CERTIFICATE) PKCS#1 RSAPublicKey (PEM header: BEGIN RSA PUBLIC KEY) X.509 SubjectPublicKeyInfo (PEM header: BEGIN PUBLIC KEY) PKCS#1 RSAPrivateKey (PEM header: BEGIN RSA PRIVATE KEY) PKCS#8 PrivateKeyInfo (PEM header: BEGIN PRIVATE KEY) matching pair of the abovestatic JWKparseFromPEMEncodedX509Cert(String pemEncodedCert) abstract intsize()Returns the size of this JWK.toECKey()Casts this JWK to an EC JWK.Returns a JSON object representation of this JWK.Returns the JSON object string representation of this JWK.Casts this JWK to an octet key pair JWK.Casts this JWK to an octet sequence JWK.abstract JWKCreates a copy of this JWK with all private or sensitive parameters removed.toRSAKey()Casts this JWK to an RSA JWK.toString()
-
Field Details
-
MIME_TYPE
The MIME type of JWK objects:application/jwk+json; charset=UTF-8- See Also:
-
-
Constructor Details
-
JWK
@Deprecated protected JWK(KeyType kty, KeyUse use, Set<KeyOperation> ops, Algorithm alg, String kid, URI x5u, Base64URL x5t, Base64URL x5t256, List<Base64> x5c, KeyStore ks) Deprecated.Creates a new JSON Web Key (JWK).- Parameters:
kty- The key type. Must not benull.use- The key use,nullif not specified or if the key is intended for signing as well as encryption.ops- The key operations,nullif not specified.alg- The intended JOSE algorithm for the key,nullif not specified.kid- The key ID,nullif not specified.x5u- The X.509 certificate URL,nullif not specified.x5t- The X.509 certificate thumbprint,nullif not specified.x5t256- The X.509 certificate SHA-256 thumbprint,nullif not specified.x5c- The X.509 certificate chain,nullif not specified.ks- Reference to the underlying key store,nullif none.
-
JWK
protected JWK(KeyType kty, KeyUse use, Set<KeyOperation> ops, Algorithm alg, String kid, URI x5u, Base64URL x5t, Base64URL x5t256, List<Base64> x5c, Date exp, Date nbf, Date iat, KeyStore ks) Creates a new JSON Web Key (JWK).- Parameters:
kty- The key type. Must not benull.use- The key use,nullif not specified or if the key is intended for signing as well as encryption.ops- The key operations,nullif not specified.alg- The intended JOSE algorithm for the key,nullif not specified.kid- The key ID,nullif not specified.x5u- The X.509 certificate URL,nullif not specified.x5t- The X.509 certificate thumbprint,nullif not specified.x5t256- The X.509 certificate SHA-256 thumbprint,nullif not specified.x5c- The X.509 certificate chain,nullif not specified.exp- The key expiration time,nullif not specified.nbf- The key not-before time,nullif not specified.iat- The key issued-at time,nullif not specified.ks- Reference to the underlying key store,nullif none.
-
-
Method Details
-
getKeyType
Gets the type (kty) of this JWK.- Returns:
- The key type.
-
getKeyUse
Gets the use (use) of this JWK.- Returns:
- The key use,
nullif not specified or if the key is intended for signing as well as encryption.
-
getKeyOperations
Gets the operations (key_ops) for this JWK.- Returns:
- The key operations,
nullif not specified.
-
getAlgorithm
Gets the intended JOSE algorithm (alg) for this JWK.- Returns:
- The intended JOSE algorithm,
nullif not specified.
-
getKeyID
Gets the ID (kid) of this JWK. The key ID can be used to match a specific key. This can be used, for instance, to choose a key within aJWKSetduring key rollover. The key ID may also correspond to a JWS/JWEkidheader parameter value.- Returns:
- The key ID,
nullif not specified.
-
getX509CertURL
Gets the X.509 certificate URL (x5u) of this JWK.- Returns:
- The X.509 certificate URL,
nullif not specified.
-
getX509CertThumbprint
Deprecated.Gets the X.509 certificate SHA-1 thumbprint (x5t) of this JWK.- Returns:
- The X.509 certificate SHA-1 thumbprint,
nullif not specified.
-
getX509CertSHA256Thumbprint
Gets the X.509 certificate SHA-256 thumbprint (x5t#S256) of this JWK.- Returns:
- The X.509 certificate SHA-256 thumbprint,
nullif not specified.
-
getX509CertChain
Gets the X.509 certificate chain (x5c) of this JWK.- Returns:
- The X.509 certificate chain as a unmodifiable list,
nullif not specified.
-
getParsedX509CertChain
Gets the parsed X.509 certificate chain (x5c) of this JWK.- Returns:
- The X.509 certificate chain as a unmodifiable list,
nullif not specified.
-
getExpirationTime
Gets the expiration time (exp) if this JWK.- Returns:
- The expiration time,
nullif not specified.
-
getNotBeforeTime
Gets the not-before (nbf) of this JWK.- Returns:
- The not-before time,
nullif not specified.
-
getIssueTime
Gets the issued-at (iat) time of this JWK.- Returns:
- The issued-at time,
nullif not specified.
-
getKeyStore
Returns a reference to the underlying key store.- Returns:
- The underlying key store,
nullif none.
-
getRequiredParams
Returns the required JWK parameters. Intended as input for JWK thumbprint computation. See RFC 7638 for more information.- Returns:
- The required JWK parameters, sorted alphanumerically by key name and ready for JSON serialisation.
-
computeThumbprint
Computes the SHA-256 thumbprint of this JWK. See RFC 7638 for more information.- Returns:
- The SHA-256 thumbprint.
- Throws:
JOSEException- If the SHA-256 hash algorithm is not supported.
-
computeThumbprint
Computes the thumbprint of this JWK using the specified hash algorithm. See RFC 7638 for more information.- Parameters:
hashAlg- The hash algorithm. Must not benull.- Returns:
- The SHA-256 thumbprint.
- Throws:
JOSEException- If the hash algorithm is not supported.
-
computeThumbprintURI
Computes the SHA-256 thumbprint URI of this JWK. See RFC 7638 and draft-ietf-oauth-jwk-thumbprint-uri for more information.- Returns:
- The SHA-256 thumbprint URI.
- Throws:
JOSEException- If the SHA-256 hash algorithm is not supported.
-
isPrivate
Returnstrueif this JWK contains private or sensitive (non-public) parameters.- Returns:
trueif this JWK contains private parameters, elsefalse.
-
toPublicJWK
Creates a copy of this JWK with all private or sensitive parameters removed.- Returns:
- The newly created public JWK, or
nullif none can be created.
-
size
Returns the size of this JWK.- Returns:
- The JWK size, in bits.
-
toRSAKey
Casts this JWK to an RSA JWK.- Returns:
- The RSA JWK.
-
toECKey
Casts this JWK to an EC JWK.- Returns:
- The EC JWK.
-
toOctetSequenceKey
Casts this JWK to an octet sequence JWK.- Returns:
- The octet sequence JWK.
-
toOctetKeyPair
Casts this JWK to an octet key pair JWK.- Returns:
- The octet key pair JWK.
-
toJSONObject
Returns a JSON object representation of this JWK. This method is intended to be called from extending classes.Example:
{ "kty" : "RSA", "use" : "sig", "kid" : "fd28e025-8d24-48bc-a51a-e2ffc8bc274b" }- Returns:
- The JSON object representation.
-
toJSONString
Returns the JSON object string representation of this JWK.- Returns:
- The JSON object string representation.
-
toString
-
parse
Parses a JWK from the specified JSON object string representation. The JWK must be anECKey, anRSAKey, or aOctetSequenceKey.- Parameters:
s- The JSON object string to parse. Must not benull.- Returns:
- The JWK.
- Throws:
ParseException- If the string couldn't be parsed to a supported JWK.
-
parse
Parses a JWK from the specified JSON object representation. The JWK must be anECKey, anRSAKey, or aOctetSequenceKey.- Parameters:
jsonObject- The JSON object to parse. Must not benull.- Returns:
- The JWK.
- Throws:
ParseException- If the JSON object couldn't be parsed to a supported JWK.
-
parse
Parses a publicRSAorEC JWKfrom the specified X.509 certificate. Requires BouncyCastle.Important: The X.509 certificate is not validated!
Sets the following JWK parameters:
- For an EC key the curve is obtained from the subject public key info algorithm parameters.
- The JWK use inferred by
KeyUse.from(java.security.cert.X509Certificate). - The JWK ID from the X.509 serial number (in base 10).
- The JWK X.509 certificate chain (this certificate only).
- The JWK X.509 certificate SHA-256 thumbprint.
- Parameters:
cert- The X.509 certificate. Must not benull.- Returns:
- The public RSA or EC JWK.
- Throws:
JOSEException- If parsing failed.
-
parseFromPEMEncodedX509Cert
Parses a publicRSAorEC JWKfrom the specified PEM-encoded X.509 certificate. Requires BouncyCastle.Important: The X.509 certificate is not validated!
Sets the following JWK parameters:
- For an EC key the curve is obtained from the subject public key info algorithm parameters.
- The JWK use inferred by
KeyUse.from(java.security.cert.X509Certificate). - The JWK ID from the X.509 serial number (in base 10).
- The JWK X.509 certificate chain (this certificate only).
- The JWK X.509 certificate SHA-256 thumbprint.
- Parameters:
pemEncodedCert- The PEM-encoded X.509 certificate. Must not benull.- Returns:
- The public RSA or EC JWK.
- Throws:
JOSEException- If parsing failed.
-
load
public static JWK load(KeyStore keyStore, String alias, char[] pin) throws KeyStoreException, JOSEException Loads a JWK from the specified JCE key store. The JWK can be a public / privateRSA key, a public / privateEC key, or asecret key. Requires BouncyCastle.Important: The X.509 certificate is not validated!
- Parameters:
keyStore- The key store. Must not benull.alias- The alias. Must not benull.pin- The pin to unlock the private key if any, empty ornullif not required.- Returns:
- The public / private RSA or EC JWK, or secret JWK, or
nullif no key with the specified alias was found. - Throws:
KeyStoreException- On a key store exception.JOSEException- If RSA or EC key loading failed.
-
parseFromPEMEncodedObjects
Parses an RSA or EC JWK from the specified string of one or more PEM-encoded object(s):- X.509 certificate (PEM header: BEGIN CERTIFICATE)
- PKCS#1 RSAPublicKey (PEM header: BEGIN RSA PUBLIC KEY)
- X.509 SubjectPublicKeyInfo (PEM header: BEGIN PUBLIC KEY)
- PKCS#1 RSAPrivateKey (PEM header: BEGIN RSA PRIVATE KEY)
- PKCS#8 PrivateKeyInfo (PEM header: BEGIN PRIVATE KEY)
- matching pair of the above
Requires BouncyCastle.
- Parameters:
pemEncodedObjects- The string of PEM-encoded object(s).- Returns:
- The public / (private) RSA or EC JWK.
- Throws:
JOSEException- If RSA or EC key parsing failed.
-
equals
-
hashCode
-