|
1 | 1 | package software.amazon.encryption.s3; |
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.IOException; |
4 | 5 | import java.nio.charset.Charset; |
5 | 6 | import java.nio.charset.StandardCharsets; |
|
9 | 10 | import java.security.SecureRandom; |
10 | 11 | import java.security.spec.InvalidParameterSpecException; |
11 | 12 | import java.util.Base64; |
| 13 | +import java.util.Collections; |
12 | 14 | import java.util.HashMap; |
| 15 | +import java.util.List; |
13 | 16 | import java.util.Map; |
14 | 17 | import java.util.Map.Entry; |
15 | 18 | import javax.crypto.BadPaddingException; |
|
19 | 22 | import javax.crypto.SecretKey; |
20 | 23 | import javax.crypto.spec.GCMParameterSpec; |
21 | 24 | import javax.crypto.spec.IvParameterSpec; |
| 25 | +import javax.crypto.spec.SecretKeySpec; |
22 | 26 | import software.amazon.awssdk.awscore.exception.AwsServiceException; |
| 27 | +import software.amazon.awssdk.core.ResponseInputStream; |
23 | 28 | import software.amazon.awssdk.core.exception.SdkClientException; |
24 | 29 | import software.amazon.awssdk.core.sync.RequestBody; |
| 30 | +import software.amazon.awssdk.core.sync.ResponseTransformer; |
| 31 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 32 | +import software.amazon.awssdk.protocols.jsoncore.JsonNode; |
| 33 | +import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser; |
25 | 34 | import software.amazon.awssdk.protocols.jsoncore.JsonWriter; |
26 | 35 | import software.amazon.awssdk.protocols.jsoncore.JsonWriter.JsonGenerationException; |
27 | 36 | import software.amazon.awssdk.services.s3.S3Client; |
| 37 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 38 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 39 | +import software.amazon.awssdk.services.s3.model.InvalidObjectStateException; |
| 40 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
28 | 41 | import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
29 | 42 | import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
30 | 43 | import software.amazon.awssdk.services.s3.model.S3Exception; |
31 | 44 | import software.amazon.awssdk.utils.IoUtils; |
| 45 | +import software.amazon.encryption.s3.materials.DecryptionMaterials; |
32 | 46 | import software.amazon.encryption.s3.materials.DefaultMaterialsManager; |
| 47 | +import software.amazon.encryption.s3.materials.DefaultMaterialsManager.DecryptionMaterialsRequest; |
33 | 48 | import software.amazon.encryption.s3.materials.DefaultMaterialsManager.EncryptionMaterialsRequest; |
34 | 49 | import software.amazon.encryption.s3.materials.EncryptedDataKey; |
35 | 50 | import software.amazon.encryption.s3.materials.EncryptionMaterials; |
@@ -111,6 +126,88 @@ public PutObjectResponse putObject(PutObjectRequest putObjectRequest, RequestBod |
111 | 126 | return _wrappedClient.putObject(putObjectRequest, RequestBody.fromBytes(ciphertext)); |
112 | 127 | } |
113 | 128 |
|
| 129 | + @Override |
| 130 | + public <T> T getObject(GetObjectRequest getObjectRequest, ResponseTransformer<GetObjectResponse, T> responseTransformer) |
| 131 | + throws NoSuchKeyException, InvalidObjectStateException, AwsServiceException, SdkClientException, S3Exception { |
| 132 | + ResponseInputStream<GetObjectResponse> objectStream = _wrappedClient.getObject(getObjectRequest); |
| 133 | + byte[] output; |
| 134 | + try { |
| 135 | + output = IoUtils.toByteArray(objectStream); |
| 136 | + } catch (IOException e) { |
| 137 | + throw new RuntimeException(e); |
| 138 | + } |
| 139 | + |
| 140 | + GetObjectResponse response = objectStream.response(); |
| 141 | + Map<String, String> metadata = response.metadata(); |
| 142 | + |
| 143 | + // Build encrypted data key |
| 144 | + Base64.Decoder decoder = Base64.getDecoder(); |
| 145 | + byte[] edkCiphertext = decoder.decode(metadata.get("x-amz-key-v2")); |
| 146 | + String keyProviderId = metadata.get("x-amz-wrap-alg"); |
| 147 | + EncryptedDataKey edk = EncryptedDataKey.builder() |
| 148 | + .ciphertext(edkCiphertext) |
| 149 | + .keyProviderId(keyProviderId) |
| 150 | + .build(); |
| 151 | + List<EncryptedDataKey> encryptedDataKeys = Collections.singletonList(edk); |
| 152 | + |
| 153 | + // Get encryption context |
| 154 | + final Map<String, String> encryptionContext = new HashMap<>(); |
| 155 | + final String jsonEncryptionContext = metadata.get("x-amz-matdesc"); |
| 156 | + try { |
| 157 | + JsonNodeParser parser = JsonNodeParser.create(); |
| 158 | + JsonNode objectNode = parser.parse(jsonEncryptionContext); |
| 159 | + |
| 160 | + for (Map.Entry<String, JsonNode> entry : objectNode.asObject().entrySet()) { |
| 161 | + encryptionContext.put(entry.getKey(), entry.getValue().asString()); |
| 162 | + } |
| 163 | + } catch (Exception e) { |
| 164 | + throw new RuntimeException(e); |
| 165 | + } |
| 166 | + |
| 167 | + // Get decryption materials |
| 168 | + final String contentEncryptionAlgorithm = metadata.get("x-amz-cek-alg"); |
| 169 | + int algorithmSuiteId = 0; |
| 170 | + if (contentEncryptionAlgorithm.equals("AES/GCM/NoPadding")) { |
| 171 | + algorithmSuiteId = 0x0078; |
| 172 | + } |
| 173 | + |
| 174 | + DecryptionMaterialsRequest request = new DecryptionMaterialsRequest(); |
| 175 | + request.encryptionContext = encryptionContext; |
| 176 | + request.algorithmSuiteId = algorithmSuiteId; |
| 177 | + request.encryptedDataKeys = encryptedDataKeys; |
| 178 | + DecryptionMaterials materials = _materialsManager.getDecryptionMaterials(request); |
| 179 | + |
| 180 | + // Get content encryption information |
| 181 | + SecretKey contentKey = new SecretKeySpec(materials.plaintextDataKey(), "AES"); |
| 182 | + final int tagLength = Integer.parseInt(metadata.get("x-amz-tag-len")); |
| 183 | + byte[] iv = decoder.decode(metadata.get("x-amz-iv")); |
| 184 | + final Cipher cipher; |
| 185 | + byte[] plaintext; |
| 186 | + try { |
| 187 | + cipher = Cipher.getInstance(contentEncryptionAlgorithm); |
| 188 | + cipher.init(Cipher.DECRYPT_MODE, contentKey, new GCMParameterSpec(tagLength, iv)); |
| 189 | + plaintext = cipher.doFinal(output); |
| 190 | + } catch (NoSuchAlgorithmException |
| 191 | + | NoSuchPaddingException |
| 192 | + | InvalidAlgorithmParameterException |
| 193 | + | InvalidKeyException e) { |
| 194 | + throw new RuntimeException(e); |
| 195 | + } catch (IllegalBlockSizeException e) { |
| 196 | + throw new RuntimeException(e); |
| 197 | + } catch (BadPaddingException e) { |
| 198 | + throw new RuntimeException(e); |
| 199 | + } |
| 200 | + |
| 201 | + try { |
| 202 | + return responseTransformer.transform(response, |
| 203 | + AbortableInputStream.create(new ByteArrayInputStream(plaintext))); |
| 204 | + } catch (Exception e) { |
| 205 | + throw new RuntimeException(e); |
| 206 | + } |
| 207 | + |
| 208 | +// return _wrappedClient.getObject(getObjectRequest, responseTransformer); |
| 209 | + } |
| 210 | + |
114 | 211 | @Override |
115 | 212 | public String serviceName() { |
116 | 213 | return _wrappedClient.serviceName(); |
|
0 commit comments