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 java.util.Set;
022import javax.crypto.SecretKey;
023
024import com.nimbusds.jose.*;
025import com.nimbusds.jose.crypto.impl.*;
026import com.nimbusds.jose.util.Base64URL;
027import com.nimbusds.jose.util.StandardCharset;
028import net.jcip.annotations.ThreadSafe;
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 * </ul>
061 *
062 * @author Vladimir Dzhuvinov
063 * @version 2016-07-26
064 */
065@ThreadSafe
066public class PasswordBasedDecrypter extends PasswordBasedCryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware {
067
068
069        /**
070         * The critical header policy.
071         */
072        private final CriticalHeaderParamsDeferral critPolicy = new CriticalHeaderParamsDeferral();
073
074
075        /**
076         * Creates a new password-based decrypter.
077         *
078         * @param password The password bytes. Must not be empty or
079         *                 {@code null}.
080         */
081        public PasswordBasedDecrypter(final byte[] password) {
082
083                super(password);
084        }
085
086
087        /**
088         * Creates a new password-based decrypter.
089         *
090         * @param password The password, as a UTF-8 encoded string. Must not be
091         *                 empty or {@code null}.
092         */
093        public PasswordBasedDecrypter(final String password) {
094
095                super(password.getBytes(StandardCharset.UTF_8));
096        }
097
098
099        @Override
100        public Set<String> getProcessedCriticalHeaderParams() {
101
102                return critPolicy.getProcessedCriticalHeaderParams();
103        }
104
105
106        @Override
107        public Set<String> getDeferredCriticalHeaderParams() {
108
109                return critPolicy.getProcessedCriticalHeaderParams();
110        }
111
112
113        @Override
114        public byte[] decrypt(final JWEHeader header,
115                              final Base64URL encryptedKey,
116                              final Base64URL iv,
117                              final Base64URL cipherText,
118                              final Base64URL authTag)
119                throws JOSEException {
120
121                // Validate required JWE parts
122                if (encryptedKey == null) {
123                        throw new JOSEException("Missing JWE encrypted key");
124                }
125
126                if (iv == null) {
127                        throw new JOSEException("Missing JWE initialization vector (IV)");
128                }
129
130                if (authTag == null) {
131                        throw new JOSEException("Missing JWE authentication tag");
132                }
133
134                if (header.getPBES2Salt() == null) {
135                        throw new JOSEException("Missing JWE \"p2s\" header parameter");
136                }
137
138                final byte[] salt = header.getPBES2Salt().decode();
139
140                if (header.getPBES2Count() < 1) {
141                        throw new JOSEException("Missing JWE \"p2c\" header parameter");
142                }
143
144                final int iterationCount = header.getPBES2Count();
145
146                critPolicy.ensureHeaderPasses(header);
147
148                final JWEAlgorithm alg = header.getAlgorithm();
149                final byte[] formattedSalt = PBKDF2.formatSalt(alg, salt);
150                final PRFParams prfParams = PRFParams.resolve(alg, getJCAContext().getMACProvider());
151                final SecretKey psKey = PBKDF2.deriveKey(getPassword(), formattedSalt, iterationCount, prfParams);
152
153                final SecretKey cek = AESKW.unwrapCEK(psKey, encryptedKey.decode(), getJCAContext().getKeyEncryptionProvider());
154
155                return ContentCryptoProvider.decrypt(header, encryptedKey, iv, cipherText, authTag, cek, getJCAContext());
156        }
157}