Package com.nimbusds.jose.crypto
Class RSADecrypter
- java.lang.Object
-
- com.nimbusds.jose.crypto.impl.RSACryptoProvider
-
- com.nimbusds.jose.crypto.RSADecrypter
-
- All Implemented Interfaces:
CriticalHeaderParamsAware,JCAAware<JWEJCAContext>,JOSEProvider,JWEDecrypter,JWEProvider
@ThreadSafe public class RSADecrypter extends RSACryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware
RSA decrypter ofJWE objects. Expects a private RSA key.Decrypts the encrypted Content Encryption Key (CEK) with the private RSA key, and then uses the CEK along with the IV and authentication tag to decrypt the cipher text. See RFC 7518, sections 4.2 and 4.3 for more information.
This class is thread-safe.
Supports the following key management algorithms:
JWEAlgorithm.RSA_OAEP_256JWEAlgorithm.RSA_OAEP_384JWEAlgorithm.RSA_OAEP_512JWEAlgorithm.RSA_OAEP(deprecated)JWEAlgorithm.RSA1_5(deprecated)
Supports the following content encryption algorithms:
- Version:
- 2023-09-10
- Author:
- David Ortiz, Vladimir Dzhuvinov, Dimitar A. Stoikov, Egor Puzanov
-
-
Field Summary
-
Fields inherited from class com.nimbusds.jose.crypto.impl.RSACryptoProvider
SUPPORTED_ALGORITHMS, SUPPORTED_ENCRYPTION_METHODS
-
-
Constructor Summary
Constructors Constructor Description RSADecrypter(RSAKey rsaJWK)Creates a new RSA decrypter.RSADecrypter(PrivateKey privateKey)Creates a new RSA decrypter.RSADecrypter(PrivateKey privateKey, Set<String> defCritHeaders)Creates a new RSA decrypter.RSADecrypter(PrivateKey privateKey, Set<String> defCritHeaders, boolean allowWeakKey)Creates a new RSA decrypter.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description byte[]decrypt(JWEHeader header, Base64URL encryptedKey, Base64URL iv, Base64URL cipherText, Base64URL authTag)Deprecated.byte[]decrypt(JWEHeader header, Base64URL encryptedKey, Base64URL iv, Base64URL cipherText, Base64URL authTag, byte[] aad)Decrypts the specified cipher text of aJWE Object.protected SecretKeygetCEK(EncryptionMethod enc)Returns the content encryption key (CEK) to use.ExceptiongetCEKDecryptionException()Returns the Content Encryption Key (CEK) decryption exception if one was encountered during the lastdecrypt(com.nimbusds.jose.JWEHeader, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL)run.Set<String>getDeferredCriticalHeaderParams()Returns the names of the critical (crit) header parameters that are deferred to the application for processing and will be ignored by the JWS verifier / JWE decrypter.JWEJCAContextgetJCAContext()Returns the Java Cryptography Architecture (JCA) context.PrivateKeygetPrivateKey()Gets the private RSA key.Set<String>getProcessedCriticalHeaderParams()Returns the names of the critical (crit) header parameters that are understood and processed by the JWS verifier / JWE decrypter.protected booleanisCEKProvided()Returnstrueif a content encryption key (CEK) was provided at construction time.Set<EncryptionMethod>supportedEncryptionMethods()Returns the names of the supported encryption methods by the JWE provier.Set<JWEAlgorithm>supportedJWEAlgorithms()Returns the names of the supported algorithms by the JWE provider instance.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.nimbusds.jose.jca.JCAAware
getJCAContext
-
Methods inherited from interface com.nimbusds.jose.JWEProvider
supportedEncryptionMethods, supportedJWEAlgorithms
-
-
-
-
Constructor Detail
-
RSADecrypter
public RSADecrypter(PrivateKey privateKey)
Creates a new RSA decrypter. This constructor can also accept a private RSA key located in a PKCS#11 store that doesn't expose the private key parameters (such as a smart card or HSM).- Parameters:
privateKey- The private RSA key. Its algorithm must be "RSA" and its length at least 2048 bits. Note that the length of an RSA key in a PKCS#11 store cannot be checked. Must not benull.
-
RSADecrypter
public RSADecrypter(RSAKey rsaJWK) throws JOSEException
Creates a new RSA decrypter.- Parameters:
rsaJWK- The RSA JSON Web Key (JWK). Must contain or reference a private part. Its length must be at least 2048 bits. Note that the length of an RSA key in a PKCS#11 store cannot be checked. Must not benull.- Throws:
JOSEException- If the RSA JWK doesn't contain a private part or its extraction failed.
-
RSADecrypter
public RSADecrypter(PrivateKey privateKey, Set<String> defCritHeaders)
Creates a new RSA decrypter. This constructor can also accept a private RSA key located in a PKCS#11 store that doesn't expose the private key parameters (such as a smart card or HSM).- Parameters:
privateKey- The private RSA key. Its algorithm must be "RSA" and its length at least 2048 bits. Note that the length of an RSA key in a PKCS#11 store cannot be checked. Must not benull.defCritHeaders- The names of the critical header parameters that are deferred to the application for processing, empty set ornullif none.
-
RSADecrypter
public RSADecrypter(PrivateKey privateKey, Set<String> defCritHeaders, boolean allowWeakKey)
Creates a new RSA decrypter. This constructor can also accept a private RSA key located in a PKCS#11 store that doesn't expose the private key parameters (such as a smart card or HSM).- Parameters:
privateKey- The private RSA key. Its algorithm must be "RSA" and its length at least 2048 bits. Note that the length of an RSA key in a PKCS#11 store cannot be checked. Must not benull.defCritHeaders- The names of the critical header parameters that are deferred to the application for processing, empty set ornullif none.allowWeakKey-trueto allow an RSA key shorter than 2048 bits.
-
-
Method Detail
-
getPrivateKey
public PrivateKey getPrivateKey()
Gets the private RSA key.- Returns:
- The private RSA key. Casting to
RSAPrivateKeymay not be possible if the key is located in a PKCS#11 store that doesn't expose the private key parameters.
-
getProcessedCriticalHeaderParams
public Set<String> getProcessedCriticalHeaderParams()
Description copied from interface:CriticalHeaderParamsAwareReturns the names of the critical (crit) header parameters that are understood and processed by the JWS verifier / JWE decrypter.- Specified by:
getProcessedCriticalHeaderParamsin interfaceCriticalHeaderParamsAware- Returns:
- The names of the critical header parameters that are understood and processed, empty set if none.
-
getDeferredCriticalHeaderParams
public Set<String> getDeferredCriticalHeaderParams()
Description copied from interface:CriticalHeaderParamsAwareReturns the names of the critical (crit) header parameters that are deferred to the application for processing and will be ignored by the JWS verifier / JWE decrypter.- Specified by:
getDeferredCriticalHeaderParamsin interfaceCriticalHeaderParamsAware- Returns:
- The names of the critical header parameters that are deferred to the application for processing, empty set if none.
-
decrypt
@Deprecated public byte[] decrypt(JWEHeader header, Base64URL encryptedKey, Base64URL iv, Base64URL cipherText, Base64URL authTag) throws JOSEException
Deprecated.Decrypts the specified cipher text of aJWE Object.- Parameters:
header- The JSON Web Encryption (JWE) header. Must specify a supported JWE algorithm and method. Must not benull.encryptedKey- The encrypted key,nullif not required by the JWE algorithm.iv- The initialisation vector,nullif not required by the JWE algorithm.cipherText- The cipher text to decrypt. Must not benull.authTag- The authentication tag,nullif not required.- Returns:
- The clear text.
- Throws:
JOSEException- If the JWE algorithm or method is not supported, if a critical header parameter is not supported or marked for deferral to the application, or if decryption failed for some other reason.
-
decrypt
public byte[] decrypt(JWEHeader header, Base64URL encryptedKey, Base64URL iv, Base64URL cipherText, Base64URL authTag, byte[] aad) throws JOSEException
Description copied from interface:JWEDecrypterDecrypts the specified cipher text of aJWE Object.- Specified by:
decryptin interfaceJWEDecrypter- Parameters:
header- The JSON Web Encryption (JWE) header. Must specify a supported JWE algorithm and method. Must not benull.encryptedKey- The encrypted key,nullif not required by the JWE algorithm.iv- The initialisation vector,nullif not required by the JWE algorithm.cipherText- The cipher text to decrypt. Must not benull.authTag- The authentication tag,nullif not required.aad- The additional authenticated data. Must not benull.- Returns:
- The clear text.
- Throws:
JOSEException- If the JWE algorithm or method is not supported, if a critical header parameter is not supported or marked for deferral to the application, or if decryption failed for some other reason.
-
getCEKDecryptionException
public Exception getCEKDecryptionException()
Returns the Content Encryption Key (CEK) decryption exception if one was encountered during the lastdecrypt(com.nimbusds.jose.JWEHeader, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL, com.nimbusds.jose.util.Base64URL)run. Intended for logging and debugging purposes.- Returns:
- The recorded exception,
nullif none.
-
supportedJWEAlgorithms
public Set<JWEAlgorithm> supportedJWEAlgorithms()
Description copied from interface:JWEProviderReturns the names of the supported algorithms by the JWE provider instance. These correspond to thealgJWE header parameter.- Specified by:
supportedJWEAlgorithmsin interfaceJWEProvider- Returns:
- The supported JWE algorithms, empty set if none.
-
supportedEncryptionMethods
public Set<EncryptionMethod> supportedEncryptionMethods()
Description copied from interface:JWEProviderReturns the names of the supported encryption methods by the JWE provier. These correspond to theencJWE header parameter.- Specified by:
supportedEncryptionMethodsin interfaceJWEProvider- Returns:
- The supported encryption methods, empty set if none.
-
getJCAContext
public JWEJCAContext getJCAContext()
Description copied from interface:JCAAwareReturns the Java Cryptography Architecture (JCA) context. May be used to set a specific JCA security provider or secure random generator.- Specified by:
getJCAContextin interfaceJCAAware<JWEJCAContext>- Returns:
- The JCA context. Not
null.
-
isCEKProvided
protected boolean isCEKProvided()
Returnstrueif a content encryption key (CEK) was provided at construction time.- Returns:
trueif a CEK was provided at construction time,falseif CEKs will be internally generated.
-
getCEK
protected SecretKey getCEK(EncryptionMethod enc) throws JOSEException
Returns the content encryption key (CEK) to use. Unless a CEK was provided at construction time this will be a new internally generated CEK.- Parameters:
enc- The encryption method. Must not benull.- Returns:
- The content encryption key (CEK).
- Throws:
JOSEException- If an internal exception is encountered.
-
-