
com.nimbusds.jose.crypto.ECDHDecrypter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nimbus-jose-jwt Show documentation
Show all versions of nimbus-jose-jwt Show documentation
Java library for Javascript Object Signing and Encryption (JOSE) and
JSON Web Tokens (JWT)
package com.nimbusds.jose.crypto;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Set;
import javax.crypto.SecretKey;
import com.nimbusds.jose.*;
import com.nimbusds.jose.jwk.ECKey;
import com.nimbusds.jose.util.Base64URL;
/**
* Elliptic Curve Diffie-Hellman decrypter of
* {@link com.nimbusds.jose.JWEObject JWE objects}. This class is thread-safe.
*
* Supports the following key management algorithms:
*
*
* - {@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES}
*
- {@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A128KW}
*
- {@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A192KW}
*
- {@link com.nimbusds.jose.JWEAlgorithm#ECDH_ES_A256KW}
*
*
* Supports the following elliptic curves:
*
*
* - {@link com.nimbusds.jose.jwk.ECKey.Curve#P_256}
*
- {@link com.nimbusds.jose.jwk.ECKey.Curve#P_384}
*
- {@link com.nimbusds.jose.jwk.ECKey.Curve#P_521}
*
*
* Supports the following content encryption algorithms:
*
*
* - {@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256}
*
- {@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384}
*
- {@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512}
*
- {@link com.nimbusds.jose.EncryptionMethod#A128GCM}
*
- {@link com.nimbusds.jose.EncryptionMethod#A192GCM}
*
- {@link com.nimbusds.jose.EncryptionMethod#A256GCM}
*
- {@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED}
*
- {@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED}
*
*
* @author Vladimir Dzhuvinov
* @version 2015-06-08
*/
public class ECDHDecrypter extends ECDHCryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware {
/**
* The private EC key.
*/
private final ECPrivateKey privateKey;
/**
* The critical header policy.
*/
private final CriticalHeaderParamsDeferral critPolicy = new CriticalHeaderParamsDeferral();
/**
* Creates a new Elliptic Curve Diffie-Hellman decrypter.
*
* @param privateKey The private EC key. Must not be {@code null}.
*
* @throws JOSEException If the elliptic curve is not supported.
*/
public ECDHDecrypter(final ECPrivateKey privateKey)
throws JOSEException {
this(privateKey, null);
}
/**
* Creates a new Elliptic Curve Diffie-Hellman decrypter.
*
* @param ecJWK The EC JSON Web Key (JWK). Must contain a private
* part. Must not be {@code null}.
*
* @throws JOSEException If the elliptic curve is not supported.
*/
public ECDHDecrypter(final ECKey ecJWK)
throws JOSEException {
super(ecJWK.getCurve());
if (! ecJWK.isPrivate()) {
throw new JOSEException("The EC JWK doesn't contain a private part");
}
this.privateKey = ecJWK.toECPrivateKey();
}
/**
* Creates a new Elliptic Curve Diffie-Hellman decrypter.
*
* @param privateKey The private EC key. Must not be {@code null}.
* @param defCritHeaders The names of the critical header parameters
* that are deferred to the application for
* processing, empty set or {@code null} if none.
*
* @throws JOSEException If the elliptic curve is not supported.
*/
public ECDHDecrypter(final ECPrivateKey privateKey, final Set defCritHeaders)
throws JOSEException {
super(ECKey.Curve.forECParameterSpec(privateKey.getParams()));
critPolicy.setDeferredCriticalHeaderParams(defCritHeaders);
this.privateKey = privateKey;
}
/**
* Returns the private EC key.
*
* @return The private EC key.
*/
public ECPrivateKey getPrivateKey() {
return privateKey;
}
@Override
public Set getProcessedCriticalHeaderParams() {
return critPolicy.getProcessedCriticalHeaderParams();
}
@Override
public Set getDeferredCriticalHeaderParams() {
return critPolicy.getProcessedCriticalHeaderParams();
}
@Override
public byte[] decrypt(final JWEHeader header,
final Base64URL encryptedKey,
final Base64URL iv,
final Base64URL cipherText,
final Base64URL authTag)
throws JOSEException {
final JWEAlgorithm alg = header.getAlgorithm();
final ECDH.AlgorithmMode algMode = ECDH.resolveAlgorithmMode(alg);
critPolicy.ensureHeaderPasses(header);
// Get ephemeral EC key
ECKey ephemeralKey = header.getEphemeralPublicKey();
if (ephemeralKey == null) {
throw new JOSEException("Missing ephemeral public EC key \"epk\" JWE header parameter");
}
ECPublicKey ephemeralPublicKey = ephemeralKey.toECPublicKey();
// Derive 'Z'
SecretKey Z = ECDH.deriveSharedSecret(
ephemeralPublicKey,
privateKey,
getJCAContext().getKeyEncryptionProvider());
// Derive shared key via concat KDF
getConcatKDF().getJCAContext().setProvider(getJCAContext().getMACProvider()); // update before concat
SecretKey sharedKey = ECDH.deriveSharedKey(header, Z, getConcatKDF());
final SecretKey cek;
if (algMode.equals(ECDH.AlgorithmMode.DIRECT)) {
cek = sharedKey;
} else if (algMode.equals(ECDH.AlgorithmMode.KW)) {
if (encryptedKey == null) {
throw new JOSEException("Missing JWE encrypted key");
}
cek = AESKW.unwrapCEK(sharedKey, encryptedKey.decode(), getJCAContext().getKeyEncryptionProvider());
} else {
throw new JOSEException("Unexpected JWE ECDH algorithm mode: " + algMode);
}
return ContentCryptoProvider.decrypt(header, encryptedKey, iv, cipherText, authTag, cek, getJCAContext());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy