001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
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.impl;
019
020
021import java.util.Collections;
022import java.util.LinkedHashSet;
023import java.util.Set;
024
025import com.nimbusds.jose.EncryptionMethod;
026import com.nimbusds.jose.JWEAlgorithm;
027import com.nimbusds.jose.util.StandardCharset;
028
029
030/**
031 * The base abstract class for password-based encrypters and decrypters of
032 * {@link com.nimbusds.jose.JWEObject JWE objects}.
033 *
034 * <p>Supports the following key management algorithms:
035 *
036 * <ul>
037 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS256_A128KW}
038 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS384_A192KW}
039 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS512_A256KW}
040 * </ul>
041 *
042 * <p>Supports the following content encryption algorithms:
043 *
044 * <ul>
045 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256}
046 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384}
047 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512}
048 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM}
049 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM}
050 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM}
051 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED}
052 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED}
053 * </ul>
054 *
055 * @author Vladimir Dzhuvinov
056 * @version 2016-07-26
057 */
058public abstract class PasswordBasedCryptoProvider extends BaseJWEProvider {
059
060
061        /**
062         * The supported JWE algorithms by the password-based crypto provider
063         * class.
064         */
065        public static final Set<JWEAlgorithm> SUPPORTED_ALGORITHMS;
066
067
068        /**
069         * The supported encryption methods by the password-base crypto
070         * provider class.
071         */
072        public static final Set<EncryptionMethod> SUPPORTED_ENCRYPTION_METHODS = ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS;
073
074
075        static {
076                Set<JWEAlgorithm> algs = new LinkedHashSet<>();
077                algs.add(JWEAlgorithm.PBES2_HS256_A128KW);
078                algs.add(JWEAlgorithm.PBES2_HS384_A192KW);
079                algs.add(JWEAlgorithm.PBES2_HS512_A256KW);
080                SUPPORTED_ALGORITHMS = Collections.unmodifiableSet(algs);
081        }
082
083
084        /**
085         * The password.
086         */
087        private final byte[] password;
088
089
090        /**
091         * Creates a new password-based encryption / decryption provider.
092         *
093         * @param password The password bytes. Must not be empty or
094         *                 {@code null}.
095         */
096        protected PasswordBasedCryptoProvider(final byte[] password) {
097
098                super(SUPPORTED_ALGORITHMS, ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS);
099
100                if (password == null || password.length == 0) {
101                        throw new IllegalArgumentException("The password must not be null or empty");
102                }
103
104                this.password = password;
105        }
106
107
108        /**
109         * Returns the password.
110         *
111         * @return The password bytes.
112         */
113        public byte[] getPassword() {
114
115                return password;
116        }
117
118
119        /**
120         * Returns the password.
121         *
122         * @return The password as a UTF-8 encoded string.
123         */
124        public String getPasswordString() {
125
126                return new String(password, StandardCharset.UTF_8);
127        }
128}