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.util.Base64URL;
026import com.nimbusds.jose.util.StandardCharset;
027import net.jcip.annotations.ThreadSafe;
028
029
030/**
031 * Password-based decrypter of {@link com.nimbusds.jose.JWEObject JWE objects}.
032 * Expects a password.
033 *
034 * <p>See RFC 7518
035 * <a href="https://tools.ietf.org/html/rfc7518#section-4.8">section 4.8</a>
036 * for more information.
037 *
038 * <p>This class is thread-safe.
039 *
040 * <p>Supports the following key management algorithms:
041 *
042 * <ul>
043 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS256_A128KW}
044 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS384_A192KW}
045 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS512_A256KW}
046 * </ul>
047 *
048 * <p>Supports the following content encryption algorithms:
049 *
050 * <ul>
051 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256}
052 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384}
053 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512}
054 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM}
055 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM}
056 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM}
057 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED}
058 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED}
059 * </ul>
060 *
061 * @author Vladimir Dzhuvinov
062 * @version 2016-07-26
063 */
064@ThreadSafe
065public class PasswordBasedDecrypter extends PasswordBasedCryptoProvider implements JWEDecrypter, CriticalHeaderParamsAware {
066
067
068        /**
069         * The critical header policy.
070         */
071        private final CriticalHeaderParamsDeferral critPolicy = new CriticalHeaderParamsDeferral();
072
073
074        /**
075         * Creates a new password-based decrypter.
076         *
077         * @param password The password bytes. Must not be empty or
078         *                 {@code null}.
079         */
080        public PasswordBasedDecrypter(final byte[] password) {
081
082                super(password);
083        }
084
085
086        /**
087         * Creates a new password-based decrypter.
088         *
089         * @param password The password, as a UTF-8 encoded string. Must not be
090         *                 empty or {@code null}.
091         */
092        public PasswordBasedDecrypter(final String password) {
093
094                super(password.getBytes(StandardCharset.UTF_8));
095        }
096
097
098        @Override
099        public Set<String> getProcessedCriticalHeaderParams() {
100
101                return critPolicy.getProcessedCriticalHeaderParams();
102        }
103
104
105        @Override
106        public Set<String> getDeferredCriticalHeaderParams() {
107
108                return critPolicy.getProcessedCriticalHeaderParams();
109        }
110
111
112        @Override
113        public byte[] decrypt(final JWEHeader header,
114                              final Base64URL encryptedKey,
115                              final Base64URL iv,
116                              final Base64URL cipherText,
117                              final Base64URL authTag)
118                throws JOSEException {
119
120                // Validate required JWE parts
121                if (encryptedKey == null) {
122                        throw new JOSEException("Missing JWE encrypted key");
123                }
124
125                if (iv == null) {
126                        throw new JOSEException("Missing JWE initialization vector (IV)");
127                }
128
129                if (authTag == null) {
130                        throw new JOSEException("Missing JWE authentication tag");
131                }
132
133                if (header.getPBES2Salt() == null) {
134                        throw new JOSEException("Missing JWE \"p2s\" header parameter");
135                }
136
137                final byte[] salt = header.getPBES2Salt().decode();
138
139                if (header.getPBES2Count() < 1) {
140                        throw new JOSEException("Missing JWE \"p2c\" header parameter");
141                }
142
143                final int iterationCount = header.getPBES2Count();
144
145                critPolicy.ensureHeaderPasses(header);
146
147                final JWEAlgorithm alg = header.getAlgorithm();
148                final byte[] formattedSalt = PBKDF2.formatSalt(alg, salt);
149                final PRFParams prfParams = PRFParams.resolve(alg, getJCAContext().getMACProvider());
150                final SecretKey psKey = PBKDF2.deriveKey(getPassword(), formattedSalt, iterationCount, prfParams);
151
152                final SecretKey cek = AESKW.unwrapCEK(psKey, encryptedKey.decode(), getJCAContext().getKeyEncryptionProvider());
153
154                return ContentCryptoProvider.decrypt(header, encryptedKey, iv, cipherText, authTag, cek, getJCAContext());
155        }
156}