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 com.nimbusds.jose.CompressionAlgorithm;
022import com.nimbusds.jose.JOSEException;
023import com.nimbusds.jose.JWEHeader;
024import com.nimbusds.jose.util.DeflateUtils;
025import net.jcip.annotations.ThreadSafe;
026
027
028/**
029 * Deflate (RFC 1951) helper methods, intended for use by JWE encrypters and
030 * decrypters. This class is thread-safe.
031 *
032 * @author Vladimir Dzhuvinov
033 * @version 2014-07-08
034 */
035@ThreadSafe
036public class DeflateHelper {
037
038
039        /**
040         * Applies compression to the specified plain text if requested.
041         *
042         * @param jweHeader The JWE header. Must not be {@code null}.
043         * @param bytes     The plain text bytes. Must not be {@code null}.
044         *
045         * @return The bytes to encrypt.
046         *
047         * @throws JOSEException If compression failed or the requested 
048         *                       compression algorithm is not supported.
049         */
050        public static byte[] applyCompression(final JWEHeader jweHeader, final byte[] bytes)
051                throws JOSEException {
052
053                CompressionAlgorithm compressionAlg = jweHeader.getCompressionAlgorithm();
054
055                if (compressionAlg == null) {
056
057                        return bytes;
058
059                } else if (compressionAlg.equals(CompressionAlgorithm.DEF)) {
060
061                        try {
062                                return DeflateUtils.compress(bytes);
063
064                        } catch (Exception e) {
065
066                                throw new JOSEException("Couldn't compress plain text: " + e.getMessage(), e);
067                        }
068
069                } else {
070
071                        throw new JOSEException("Unsupported compression algorithm: " + compressionAlg);
072                }
073        }
074
075
076        /**
077         * Applies decompression to the specified plain text if requested.
078         *
079         * @param jweHeader The JWE header. Must not be {@code null}.
080         * @param bytes     The plain text bytes. Must not be {@code null}.
081         *
082         * @return The output bytes, decompressed if requested.
083         *
084         * @throws JOSEException If decompression failed or the requested 
085         *                       compression algorithm is not supported.
086         */
087        public static byte[] applyDecompression(final JWEHeader jweHeader, final byte[] bytes)
088                throws JOSEException {
089
090                CompressionAlgorithm compressionAlg = jweHeader.getCompressionAlgorithm();
091
092                if (compressionAlg == null) {
093
094                        return bytes;
095
096                } else if (compressionAlg.equals(CompressionAlgorithm.DEF)) {
097
098                        try {
099                                return DeflateUtils.decompress(bytes);
100
101                        } catch (Exception e) {
102
103                                throw new JOSEException("Couldn't decompress plain text: " + e.getMessage(), e);
104                        }
105
106                } else {
107
108                        throw new JOSEException("Unsupported compression algorithm: " + compressionAlg);
109                }
110        }
111}