001package com.nimbusds.jose.crypto; 002 003 004import java.util.HashSet; 005import java.util.Set; 006import javax.crypto.SecretKey; 007 008import com.nimbusds.jose.EncryptionMethod; 009import com.nimbusds.jose.JOSEException; 010import com.nimbusds.jose.JWEAlgorithm; 011import com.nimbusds.jose.JWEDecrypter; 012import com.nimbusds.jose.ReadOnlyJWEHeader; 013import com.nimbusds.jose.util.Base64URL; 014import com.nimbusds.jose.util.StringUtils; 015 016 017/** 018 * Direct decrypter of {@link com.nimbusds.jose.JWEObject JWE objects} with a 019 * shared symmetric key. This class is thread-safe. 020 * 021 * <p>Supports the following JWE algorithms: 022 * 023 * <ul> 024 * <li>{@link com.nimbusds.jose.JWEAlgorithm#DIR} 025 * </ul> 026 * 027 * <p>Supports the following encryption methods: 028 * 029 * <ul> 030 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} 031 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} 032 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} 033 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM} 034 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM} 035 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM} 036 * </ul> 037 * 038 * <p>Accepts all {@link com.nimbusds.jose.JWEHeader#getRegisteredParameterNames 039 * registered JWE header parameters}. Use {@link #setAcceptedAlgorithms} and 040 * {@link #setAcceptedEncryptionMethods} to restrict the acceptable JWE 041 * algorithms and encryption methods. 042 * 043 * @author Vladimir Dzhuvinov 044 * @version $version$ (2014-04-22) 045 */ 046public class DirectDecrypter extends DirectCryptoProvider implements JWEDecrypter { 047 048 049 /** 050 * The accepted JWE algorithms. 051 */ 052 private Set<JWEAlgorithm> acceptedAlgs = 053 new HashSet<JWEAlgorithm>(supportedAlgorithms()); 054 055 056 /** 057 * The accepted encryption methods. 058 */ 059 private Set<EncryptionMethod> acceptedEncs = 060 new HashSet<EncryptionMethod>(supportedEncryptionMethods()); 061 062 063 /** 064 * The critical header parameter checker. 065 */ 066 private final CriticalHeaderParameterChecker critParamChecker = 067 new CriticalHeaderParameterChecker(); 068 069 070 /** 071 * Creates a new direct decrypter. 072 * 073 * @param key The shared symmetric key. Its algorithm must be "AES". 074 * Must be 128 bits (16 bytes), 256 bits (32 bytes) or 512 075 * bits (64 bytes) long. Must not be {@code null}. 076 * 077 * @throws JOSEException If the key length is unexpected. 078 */ 079 public DirectDecrypter(final SecretKey key) 080 throws JOSEException { 081 082 super(key); 083 } 084 085 086 /** 087 * Creates a new direct decrypter. 088 * 089 * @param keyBytes The shared symmetric key, as a byte array. Must be 090 * 128 bits (16 bytes), 256 bits (32 bytes) or 512 bits 091 * (64 bytes) long. Must not be {@code null}. 092 * 093 * @throws JOSEException If the key length is unexpected. 094 */ 095 public DirectDecrypter(final byte[] keyBytes) 096 throws JOSEException { 097 098 super(keyBytes); 099 } 100 101 102 @Override 103 public Set<JWEAlgorithm> getAcceptedAlgorithms() { 104 105 return acceptedAlgs; 106 } 107 108 109 @Override 110 public void setAcceptedAlgorithms(final Set<JWEAlgorithm> acceptedAlgs) { 111 112 if (acceptedAlgs == null) { 113 throw new IllegalArgumentException("The accepted JWE algorithms must not be null"); 114 } 115 116 if (! supportedAlgorithms().containsAll(acceptedAlgs)) { 117 throw new IllegalArgumentException("Unsupported JWE algorithm(s)"); 118 } 119 120 this.acceptedAlgs = acceptedAlgs; 121 } 122 123 124 @Override 125 public Set<EncryptionMethod> getAcceptedEncryptionMethods() { 126 127 return acceptedEncs; 128 } 129 130 131 @Override 132 public void setAcceptedEncryptionMethods(final Set<EncryptionMethod> acceptedEncs) { 133 134 if (acceptedEncs == null) 135 throw new IllegalArgumentException("The accepted encryption methods must not be null"); 136 137 if (!supportedEncryptionMethods().containsAll(acceptedEncs)) { 138 throw new IllegalArgumentException("Unsupported encryption method(s)"); 139 } 140 141 this.acceptedEncs = acceptedEncs; 142 } 143 144 145 @Override 146 public Set<String> getIgnoredCriticalHeaderParameters() { 147 148 return critParamChecker.getIgnoredCriticalHeaders(); 149 } 150 151 152 @Override 153 public void setIgnoredCriticalHeaderParameters(final Set<String> headers) { 154 155 critParamChecker.setIgnoredCriticalHeaders(headers); 156 } 157 158 159 @Override 160 public byte[] decrypt(final ReadOnlyJWEHeader header, 161 final Base64URL encryptedKey, 162 final Base64URL iv, 163 final Base64URL cipherText, 164 final Base64URL authTag) 165 throws JOSEException { 166 167 // Validate required JWE parts 168 if (encryptedKey != null) { 169 170 throw new JOSEException("Unexpected encrypted key, must be omitted"); 171 } 172 173 if (iv == null) { 174 175 throw new JOSEException("The initialization vector (IV) must not be null"); 176 } 177 178 if (authTag == null) { 179 180 throw new JOSEException("The authentication tag must not be null"); 181 } 182 183 184 JWEAlgorithm alg = header.getAlgorithm(); 185 186 if (! alg.equals(JWEAlgorithm.DIR)) { 187 188 throw new JOSEException("Unsupported algorithm, must be \"dir\""); 189 } 190 191 if (! critParamChecker.headerPasses(header)) { 192 193 throw new JOSEException("Unsupported critical header parameter"); 194 } 195 196 // Compose the AAD 197 byte[] aad = StringUtils.toByteArray(header.toBase64URL().toString()); 198 199 // Decrypt the cipher text according to the JWE enc 200 EncryptionMethod enc = header.getEncryptionMethod(); 201 202 byte[] plainText; 203 204 if (enc.equals(EncryptionMethod.A128CBC_HS256) || enc.equals(EncryptionMethod.A192CBC_HS384) || enc.equals(EncryptionMethod.A256CBC_HS512)) { 205 206 plainText = AESCBC.decryptAuthenticated(getKey(), iv.decode(), cipherText.decode(), aad, authTag.decode(), contentEncryptionProvider, macProvider); 207 208 } else if (enc.equals(EncryptionMethod.A128GCM) || enc.equals(EncryptionMethod.A192GCM) || enc.equals(EncryptionMethod.A256GCM)) { 209 210 plainText = AESGCM.decrypt(getKey(), iv.decode(), cipherText.decode(), aad, authTag.decode(), contentEncryptionProvider); 211 212 } else { 213 214 throw new JOSEException("Unsupported encryption method, must be A128CBC_HS256, A192CBC_HS384, A256CBC_HS512, A128GCM, A192GCM or A128GCM"); 215 } 216 217 218 // Apply decompression if requested 219 return DeflateHelper.applyDecompression(header, plainText); 220 } 221} 222