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