com.nimbusds.jose.crypto.DirectDecrypter 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.util.Set;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import net.jcip.annotations.ThreadSafe;
import com.nimbusds.jose.*;
import com.nimbusds.jose.jwk.OctetSequenceKey;
import com.nimbusds.jose.util.Base64URL;
/**
* Direct decrypter of {@link com.nimbusds.jose.JWEObject JWE objects} with a
* shared symmetric key. This class is thread-safe.
*
* Supports the following key management algorithms:
*
*
* - {@link com.nimbusds.jose.JWEAlgorithm#DIR}
*
*
* 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-29
*/
@ThreadSafe
public class DirectDecrypter extends DirectCryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware {
/**
* The critical header policy.
*/
private final CriticalHeaderParamsDeferral critPolicy = new CriticalHeaderParamsDeferral();
/**
* Creates a new direct decrypter.
*
* @param key The symmetric key. Its algorithm must be "AES". Must be
* 128 bits (16 bytes), 192 bits (24 bytes), 256 bits (32
* bytes), 384 bits (48 bytes) or 512 bits (64 bytes) long.
* Must not be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectDecrypter(final SecretKey key)
throws KeyLengthException {
super(key);
}
/**
* Creates a new direct decrypter.
*
* @param keyBytes The symmetric key, as a byte array. Must be 128 bits
* (16 bytes), 192 bits (24 bytes), 256 bits (32
* bytes), 384 bits (48 bytes) or 512 bits (64 bytes)
* long. Must not be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectDecrypter(final byte[] keyBytes)
throws KeyLengthException {
this(new SecretKeySpec(keyBytes, "AES"));
}
/**
* Creates a new direct decrypter.
*
* @param octJWK The symmetric key, as a JWK. Must be 128 bits (16
* bytes), 192 bits (24 bytes), 256 bits (32 bytes), 384
* bits (48 bytes) or 512 bits (64 bytes) long. Must not
* be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectDecrypter(final OctetSequenceKey octJWK)
throws KeyLengthException {
this(octJWK.toSecretKey("AES"));
}
/**
* Creates a new direct decrypter.
*
* @param key The symmetric key. Its algorithm must be
* "AES". Must be 128 bits (16 bytes), 192 bits
* (24 bytes), 256 bits (32 bytes), 384 bits (48
* bytes) or 512 bits (64 bytes) long. 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 KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectDecrypter(final SecretKey key, final Set defCritHeaders)
throws KeyLengthException {
super(key);
critPolicy.setDeferredCriticalHeaderParams(defCritHeaders);
}
@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 {
// Validate required JWE parts
if (encryptedKey != null) {
throw new JOSEException("Unexpected present JWE encrypted key");
}
if (iv == null) {
throw new JOSEException("Unexpected present JWE initialization vector (IV)");
}
if (authTag == null) {
throw new JOSEException("Missing JWE authentication tag");
}
JWEAlgorithm alg = header.getAlgorithm();
if (! alg.equals(JWEAlgorithm.DIR)) {
throw new JOSEException(AlgorithmSupportMessage.unsupportedJWEAlgorithm(alg, SUPPORTED_ALGORITHMS));
}
critPolicy.ensureHeaderPasses(header);
return ContentCryptoProvider.decrypt(header, null, iv, cipherText, authTag, getKey(), getJCAContext());
}
}