|
17 | 17 | import software.amazon.encryption.s3.materials.DecryptionMaterials;
|
18 | 18 | import software.amazon.encryption.s3.materials.EncryptedDataKey;
|
19 | 19 |
|
20 |
| -import javax.crypto.Cipher; |
21 |
| -import javax.crypto.SecretKey; |
22 |
| -import javax.crypto.spec.GCMParameterSpec; |
23 |
| -import javax.crypto.spec.IvParameterSpec; |
24 | 20 | import java.nio.ByteBuffer;
|
25 |
| -import java.security.GeneralSecurityException; |
26 | 21 | import java.util.Collections;
|
27 | 22 | import java.util.List;
|
28 | 23 | import java.util.concurrent.CompletableFuture;
|
@@ -143,42 +138,23 @@ public void onStream(SdkPublisher<ByteBuffer> ciphertextPublisher) {
|
143 | 138 | long[] desiredRange = RangedGetUtils.getRange(materials.getContentRange());
|
144 | 139 | long[] cryptoRange = RangedGetUtils.getCryptoRange(materials.getContentRange());
|
145 | 140 | AlgorithmSuite algorithmSuite = materials.algorithmSuite();
|
146 |
| - SecretKey contentKey = materials.dataKey(); |
147 |
| - final int tagLength = algorithmSuite.cipherTagLengthBits(); |
148 | 141 | byte[] iv = contentMetadata.contentIv();
|
149 | 142 | if (algorithmSuite == AlgorithmSuite.ALG_AES_256_CTR_IV16_TAG16_NO_KDF) {
|
150 | 143 | iv = AesCtrUtils.adjustIV(iv, cryptoRange[0]);
|
151 | 144 | }
|
152 |
| - try { |
153 |
| - final Cipher cipher = CryptoFactory.createCipher(algorithmSuite.cipherName(), materials.cryptoProvider()); |
154 |
| - switch (algorithmSuite) { |
155 |
| - case ALG_AES_256_GCM_IV12_TAG16_NO_KDF: |
156 |
| - cipher.init(Cipher.DECRYPT_MODE, contentKey, new GCMParameterSpec(tagLength, iv)); |
157 |
| - break; |
158 |
| - case ALG_AES_256_CTR_IV16_TAG16_NO_KDF: |
159 |
| - case ALG_AES_256_CBC_IV16_NO_KDF: |
160 |
| - cipher.init(Cipher.DECRYPT_MODE, contentKey, new IvParameterSpec(iv)); |
161 |
| - break; |
162 |
| - default: |
163 |
| - throw new S3EncryptionClientException("Unknown algorithm: " + algorithmSuite.cipherName()); |
164 |
| - } |
165 |
| - |
166 |
| - if (algorithmSuite.equals(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF) |
167 |
| - || algorithmSuite.equals(AlgorithmSuite.ALG_AES_256_CTR_IV16_TAG16_NO_KDF) |
168 |
| - || _enableDelayedAuthentication) { |
169 |
| - // CBC and GCM with delayed auth enabled use a standard publisher |
170 |
| - CipherPublisher plaintextPublisher = new CipherPublisher(ciphertextPublisher, |
171 |
| - getObjectResponse.contentLength(), desiredRange, contentMetadata.contentRange(), algorithmSuite.cipherTagLengthBits(), materials, iv); |
172 |
| - wrappedAsyncResponseTransformer.onStream(plaintextPublisher); |
173 |
| - } else { |
174 |
| - // Use buffered publisher for GCM when delayed auth is not enabled |
175 |
| - BufferedCipherPublisher plaintextPublisher = new BufferedCipherPublisher(ciphertextPublisher, |
176 |
| - getObjectResponse.contentLength(), materials, iv, _bufferSize); |
177 |
| - wrappedAsyncResponseTransformer.onStream(plaintextPublisher); |
178 |
| - } |
179 |
| - |
180 |
| - } catch (GeneralSecurityException e) { |
181 |
| - throw new S3EncryptionClientException("Unable to " + algorithmSuite.cipherName() + " content decrypt.", e); |
| 145 | + |
| 146 | + if (algorithmSuite.equals(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF) |
| 147 | + || algorithmSuite.equals(AlgorithmSuite.ALG_AES_256_CTR_IV16_TAG16_NO_KDF) |
| 148 | + || _enableDelayedAuthentication) { |
| 149 | + // CBC and GCM with delayed auth enabled use a standard publisher |
| 150 | + CipherPublisher plaintextPublisher = new CipherPublisher(ciphertextPublisher, |
| 151 | + getObjectResponse.contentLength(), desiredRange, contentMetadata.contentRange(), algorithmSuite.cipherTagLengthBits(), materials, iv); |
| 152 | + wrappedAsyncResponseTransformer.onStream(plaintextPublisher); |
| 153 | + } else { |
| 154 | + // Use buffered publisher for GCM when delayed auth is not enabled |
| 155 | + BufferedCipherPublisher plaintextPublisher = new BufferedCipherPublisher(ciphertextPublisher, |
| 156 | + getObjectResponse.contentLength(), materials, iv, _bufferSize); |
| 157 | + wrappedAsyncResponseTransformer.onStream(plaintextPublisher); |
182 | 158 | }
|
183 | 159 | }
|
184 | 160 | }
|
|
0 commit comments