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 javax.crypto.SecretKey;
022import javax.crypto.spec.SecretKeySpec;
023
024import net.jcip.annotations.ThreadSafe;
025
026import com.nimbusds.jose.*;
027import com.nimbusds.jose.jwk.OctetSequenceKey;
028import com.nimbusds.jose.util.Base64URL;
029import com.nimbusds.jose.util.ByteUtils;
030
031
032/**
033 * Direct encrypter of {@link com.nimbusds.jose.JWEObject JWE objects} with a
034 * shared symmetric key.
035 *
036 * <p>See RFC 7518
037 * <a href="https://tools.ietf.org/html/rfc7518#section-4.5">section 4.5</a>
038 * for more information.</p>
039 *
040 * <p>This class is thread-safe.
041 *
042 * <p>Supports the following key management algorithms:
043 *
044 * <ul>
045 *     <li>{@link com.nimbusds.jose.JWEAlgorithm#DIR}
046 * </ul>
047 *
048 * <p>Supports the following content encryption algorithms:
049 *
050 * <ul>
051 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} (requires 256 bit key)
052 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} (requires 384 bit key)
053 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} (requires 512 bit key)
054 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM} (requires 128 bit key)
055 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM} (requires 192 bit key)
056 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM} (requires 256 bit key)
057 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED} (requires 256 bit key)
058 *     <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED} (requires 512 bit key)
059 * </ul>
060 *
061 * @author Vladimir Dzhuvinov
062 * @version 2017-06-01
063 */
064@ThreadSafe
065public class DirectEncrypter extends DirectCryptoProvider implements JWEEncrypter {
066
067
068        /**
069         * Creates a new direct encrypter.
070         *
071         * @param key The symmetric key. Its algorithm must be "AES". Must be
072         *            128 bits (16 bytes), 192 bits (24 bytes), 256 bits (32
073         *            bytes), 384 bits (48 bytes) or 512 bits (64 bytes) long.
074         *            Must not be {@code null}.
075         *
076         * @throws KeyLengthException If the symmetric key length is not
077         *                            compatible.
078         */
079        public DirectEncrypter(final SecretKey key)
080                throws KeyLengthException {
081
082                super(key);
083        }
084
085
086        /**
087         * Creates a new direct encrypter.
088         *
089         * @param keyBytes The symmetric key, as a byte array. Must be 128 bits
090         *                 (16 bytes), 192 bits (24 bytes), 256 bits (32
091         *                 bytes), 384 bits (48 bytes) or 512 bits (64 bytes)
092         *                 long. Must not be {@code null}.
093         *
094         * @throws KeyLengthException If the symmetric key length is not
095         *                            compatible.
096         */
097        public DirectEncrypter(final byte[] keyBytes)
098                throws KeyLengthException {
099
100                this(new SecretKeySpec(keyBytes, "AES"));
101        }
102
103
104        /**
105         * Creates a new direct encrypter.
106         *
107         * @param octJWK The symmetric key, as a JWK. Must be 128 bits (16
108         *               bytes), 192 bits (24 bytes), 256 bits (32 bytes), 384
109         *               bits (48 bytes) or 512 bits (64 bytes) long. Must not
110         *               be {@code null}.
111         *
112         * @throws KeyLengthException If the symmetric key length is not
113         *                            compatible.
114         */
115        public DirectEncrypter(final OctetSequenceKey octJWK)
116                throws KeyLengthException {
117
118                this(octJWK.toSecretKey("AES"));
119        }
120
121
122        @Override
123        public JWECryptoParts encrypt(final JWEHeader header, final byte[] clearText)
124                throws JOSEException {
125
126                JWEAlgorithm alg = header.getAlgorithm();
127
128                if (! alg.equals(JWEAlgorithm.DIR)) {
129                        throw new JOSEException(AlgorithmSupportMessage.unsupportedJWEAlgorithm(alg, SUPPORTED_ALGORITHMS));
130                }
131
132                // Check key length matches encryption method
133                EncryptionMethod enc = header.getEncryptionMethod();
134
135                if (enc.cekBitLength() != ByteUtils.safeBitLength(getKey().getEncoded())) {
136                        throw new KeyLengthException(enc.cekBitLength(), enc);
137                }
138
139                final Base64URL encryptedKey = null; // The second JWE part
140
141                return ContentCryptoProvider.encrypt(header, clearText, getKey(), encryptedKey, getJCAContext());
142        }
143}