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