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;
019
020
021import net.jcip.annotations.Immutable;
022
023import com.nimbusds.jose.util.Base64URL;
024
025
026/**
027 * The cryptographic parts of a JSON Web Encryption (JWE) object.
028 *
029 * @author Vladimir Dzhuvinov
030 * @version 2021-09-30
031 */
032@Immutable
033public final class JWECryptoParts {
034
035
036        /**
037         * The modified JWE header (optional).
038         */
039        private final JWEHeader header;
040
041
042        /**
043         * The encrypted key (optional).
044         */
045        private final Base64URL encryptedKey;
046
047
048        /**
049         * The initialisation vector (optional).
050         */
051        private final Base64URL iv;
052
053
054        /**
055         * The cipher text.
056         */
057        private final Base64URL cipherText;
058
059
060        /**
061         * The authentication tag (optional).
062         */
063        private final Base64URL authenticationTag;
064
065
066        /**
067         * Creates a new cryptographic JWE parts instance.
068         *
069         * @param encryptedKey      The encrypted key, {@code null} if not
070         *                          required by the encryption algorithm.
071         * @param iv                The initialisation vector (IV), 
072         *                          {@code null} if not required by the 
073         *                          encryption algorithm.
074         * @param cipherText        The cipher text. Must not be {@code null}.
075         * @param authenticationTag The authentication tag, {@code null} if the
076         *                          JWE algorithm provides built-in integrity 
077         *                          check.
078         */
079        public JWECryptoParts(final Base64URL encryptedKey, 
080                              final Base64URL iv,
081                              final Base64URL cipherText, 
082                              final Base64URL authenticationTag) {
083
084                this(null, encryptedKey, iv, cipherText, authenticationTag);
085        }
086
087
088        /**
089         * Creates a new cryptographic JWE parts instance.
090         *
091         * @param header            The modified JWE header, {@code null} if
092         *                          not.
093         * @param encryptedKey      The encrypted key, {@code null} if not
094         *                          required by the encryption algorithm.
095         * @param iv                The initialisation vector (IV),
096         *                          {@code null} if not required by the
097         *                          encryption algorithm.
098         * @param cipherText        The cipher text. Must not be {@code null}.
099         * @param authenticationTag The authentication tag, {@code null} if the
100         *                          JWE algorithm provides built-in integrity
101         *                          check.
102         */
103        public JWECryptoParts(final JWEHeader header,
104                              final Base64URL encryptedKey,
105                              final Base64URL iv,
106                              final Base64URL cipherText,
107                              final Base64URL authenticationTag) {
108
109                this.header = header;
110
111                this.encryptedKey = encryptedKey;
112
113                this.iv = iv;
114
115                if (cipherText == null) {
116
117                        throw new IllegalArgumentException("The cipher text must not be null");
118                }
119
120                this.cipherText = cipherText;
121
122                this.authenticationTag = authenticationTag;
123        }
124
125
126        /**
127         * Gets the modified JWE header.
128         *
129         * @return The modified JWE header, {@code null} of not.
130         */
131        public JWEHeader getHeader() {
132
133                return header;
134        }
135
136
137        /**
138         * Gets the encrypted key.
139         *
140         * @return The encrypted key, {@code null} if not required by 
141         *         the JWE algorithm or {@code recipients} are specified.
142         */
143        public Base64URL getEncryptedKey() {
144
145                return encryptedKey;
146        }
147
148
149        /**
150         * Gets the initialisation vector (IV).
151         *
152         * @return The initialisation vector (IV), {@code null} if not required
153         *         by the JWE algorithm.
154         */
155        public Base64URL getInitializationVector() {
156
157                return iv;
158        }
159
160
161        /**
162         * Gets the cipher text.
163         *
164         * @return The cipher text.
165         */
166        public Base64URL getCipherText() {
167
168                return cipherText;
169        }
170
171
172        /**
173         * Gets the authentication tag.
174         *
175         * @return The authentication tag, {@code null} if the encryption
176         *         algorithm provides built-in integrity checking.
177         */
178        public Base64URL getAuthenticationTag() {
179
180                return authenticationTag;
181        }
182}