001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2016, Connect2id Ltd. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.jose.crypto; 019 020 021import com.nimbusds.jose.*; 022import com.nimbusds.jose.crypto.impl.*; 023import com.nimbusds.jose.util.Base64URL; 024import com.nimbusds.jose.util.StandardCharset; 025import net.jcip.annotations.ThreadSafe; 026 027import javax.crypto.SecretKey; 028import java.util.Set; 029 030 031/** 032 * Password-based decrypter of {@link com.nimbusds.jose.JWEObject JWE objects}. 033 * Expects a password. 034 * 035 * <p>See RFC 7518 036 * <a href="https://tools.ietf.org/html/rfc7518#section-4.8">section 4.8</a> 037 * for more information. 038 * 039 * <p>This class is thread-safe. 040 * 041 * <p>Supports the following key management algorithms: 042 * 043 * <ul> 044 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS256_A128KW} 045 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS384_A192KW} 046 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS512_A256KW} 047 * </ul> 048 * 049 * <p>Supports the following content encryption algorithms: 050 * 051 * <ul> 052 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} 053 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} 054 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} 055 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM} 056 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM} 057 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM} 058 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED} 059 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED} 060 * <li>{@link com.nimbusds.jose.EncryptionMethod#XC20P} 061 * </ul> 062 * 063 * @author Vladimir Dzhuvinov 064 * @author Egor Puzanov 065 * @version 2024-09-10 066 */ 067@ThreadSafe 068public class PasswordBasedDecrypter extends PasswordBasedCryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware { 069 070 071 /** 072 * The maximum allowed iteration count (1 million). 073 */ 074 public static final int MAX_ALLOWED_ITERATION_COUNT = 1_000_000; 075 076 077 /** 078 * The critical header policy. 079 */ 080 private final CriticalHeaderParamsDeferral critPolicy = new CriticalHeaderParamsDeferral(); 081 082 083 /** 084 * Creates a new password-based decrypter. 085 * 086 * @param password The password bytes. Must not be empty or 087 * {@code null}. 088 */ 089 public PasswordBasedDecrypter(final byte[] password) { 090 091 super(password); 092 } 093 094 095 /** 096 * Creates a new password-based decrypter. 097 * 098 * @param password The password, as a UTF-8 encoded string. Must not be 099 * empty or {@code null}. 100 */ 101 public PasswordBasedDecrypter(final String password) { 102 103 super(password.getBytes(StandardCharset.UTF_8)); 104 } 105 106 107 @Override 108 public Set<String> getProcessedCriticalHeaderParams() { 109 110 return critPolicy.getProcessedCriticalHeaderParams(); 111 } 112 113 114 @Override 115 public Set<String> getDeferredCriticalHeaderParams() { 116 117 return critPolicy.getProcessedCriticalHeaderParams(); 118 } 119 120 121 /** 122 * Decrypts the specified cipher text of a {@link JWEObject JWE Object}. 123 * 124 * @param header The JSON Web Encryption (JWE) header. Must 125 * specify a supported JWE algorithm and method. 126 * Must not be {@code null}. 127 * @param encryptedKey The encrypted key, {@code null} if not required 128 * by the JWE algorithm. 129 * @param iv The initialisation vector, {@code null} if not 130 * required by the JWE algorithm. 131 * @param cipherText The cipher text to decrypt. Must not be 132 * {@code null}. 133 * @param authTag The authentication tag, {@code null} if not 134 * required. 135 * 136 * @return The clear text. 137 * 138 * @throws JOSEException If the JWE algorithm or method is not 139 * supported, if a critical header parameter is 140 * not supported or marked for deferral to the 141 * application, or if decryption failed for some 142 * other reason. 143 */ 144 @Deprecated 145 public byte[] decrypt(final JWEHeader header, 146 final Base64URL encryptedKey, 147 final Base64URL iv, 148 final Base64URL cipherText, 149 final Base64URL authTag) 150 throws JOSEException { 151 152 return decrypt(header, encryptedKey, iv, cipherText, authTag, AAD.compute(header)); 153 } 154 155 156 @Override 157 public byte[] decrypt(final JWEHeader header, 158 final Base64URL encryptedKey, 159 final Base64URL iv, 160 final Base64URL cipherText, 161 final Base64URL authTag, 162 final byte[] aad) 163 throws JOSEException { 164 165 // Validate required JWE parts 166 if (encryptedKey == null) { 167 throw new JOSEException("Missing JWE encrypted key"); 168 } 169 170 if (iv == null) { 171 throw new JOSEException("Missing JWE initialization vector (IV)"); 172 } 173 174 if (authTag == null) { 175 throw new JOSEException("Missing JWE authentication tag"); 176 } 177 178 if (header.getPBES2Salt() == null) { 179 throw new JOSEException("Missing JWE p2s header parameter"); 180 } 181 182 final byte[] salt = header.getPBES2Salt().decode(); 183 184 if (header.getPBES2Count() < 1) { 185 throw new JOSEException("Missing JWE p2c header parameter"); 186 } 187 188 final int iterationCount = header.getPBES2Count(); 189 190 if (iterationCount > MAX_ALLOWED_ITERATION_COUNT) { 191 throw new JOSEException("The JWE p2c header exceeds the maximum allowed " + MAX_ALLOWED_ITERATION_COUNT + " count"); 192 } 193 194 critPolicy.ensureHeaderPasses(header); 195 196 final JWEAlgorithm alg = JWEHeaderValidation.getAlgorithmAndEnsureNotNull(header); 197 final byte[] formattedSalt = PBKDF2.formatSalt(alg, salt); 198 final PRFParams prfParams = PRFParams.resolve(alg, getJCAContext().getMACProvider()); 199 final SecretKey psKey = PBKDF2.deriveKey(getPassword(), formattedSalt, iterationCount, prfParams, getJCAContext().getProvider()); 200 201 final SecretKey cek = AESKW.unwrapCEK(psKey, encryptedKey.decode(), getJCAContext().getKeyEncryptionProvider()); 202 203 return ContentCryptoProvider.decrypt(header, aad, encryptedKey, iv, cipherText, authTag, cek, getJCAContext()); 204 } 205}