|
| 1 | +package software.amazon.encryption.s3; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.charset.Charset; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.security.InvalidAlgorithmParameterException; |
| 7 | +import java.security.InvalidKeyException; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | +import java.security.SecureRandom; |
| 10 | +import java.security.spec.InvalidParameterSpecException; |
| 11 | +import java.util.Base64; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Map.Entry; |
| 15 | +import javax.crypto.BadPaddingException; |
| 16 | +import javax.crypto.Cipher; |
| 17 | +import javax.crypto.IllegalBlockSizeException; |
| 18 | +import javax.crypto.NoSuchPaddingException; |
| 19 | +import javax.crypto.SecretKey; |
| 20 | +import javax.crypto.spec.GCMParameterSpec; |
| 21 | +import javax.crypto.spec.IvParameterSpec; |
| 22 | +import software.amazon.awssdk.awscore.exception.AwsServiceException; |
| 23 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 24 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 25 | +import software.amazon.awssdk.protocols.jsoncore.JsonWriter; |
| 26 | +import software.amazon.awssdk.protocols.jsoncore.JsonWriter.JsonGenerationException; |
| 27 | +import software.amazon.awssdk.services.s3.S3Client; |
| 28 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 29 | +import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
| 30 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
| 31 | +import software.amazon.awssdk.utils.IoUtils; |
| 32 | +import software.amazon.encryption.s3.materials.DefaultMaterialsManager; |
| 33 | +import software.amazon.encryption.s3.materials.DefaultMaterialsManager.EncryptionMaterialsRequest; |
| 34 | +import software.amazon.encryption.s3.materials.EncryptedDataKey; |
| 35 | +import software.amazon.encryption.s3.materials.EncryptionMaterials; |
| 36 | + |
| 37 | +public class S3EncryptionClient implements S3Client { |
| 38 | + |
| 39 | + private final S3Client _wrappedClient; |
| 40 | + private final DefaultMaterialsManager _materialsManager; |
| 41 | + |
| 42 | + public S3EncryptionClient(S3Client client, DefaultMaterialsManager materialsManager) { |
| 43 | + _wrappedClient = client; |
| 44 | + _materialsManager = materialsManager; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public PutObjectResponse putObject(PutObjectRequest putObjectRequest, RequestBody requestBody) |
| 49 | + throws AwsServiceException, SdkClientException, S3Exception { |
| 50 | + |
| 51 | + // Get content encryption key |
| 52 | + EncryptionMaterials materials = _materialsManager.getEncryptionMaterials(new EncryptionMaterialsRequest()); |
| 53 | + SecretKey contentKey = materials.dataKey(); |
| 54 | + // Encrypt content |
| 55 | + byte[] iv = new byte[12]; // default GCM IV length |
| 56 | + new SecureRandom().nextBytes(iv); |
| 57 | + |
| 58 | + final String contentEncryptionAlgorithm = "AES/GCM/NoPadding"; |
| 59 | + final Cipher cipher; |
| 60 | + try { |
| 61 | + cipher = Cipher.getInstance(contentEncryptionAlgorithm); |
| 62 | + //GCMParameterSpec defaultSpec = cipher.getParameters().getParameterSpec(GCMParameterSpec.class); |
| 63 | + cipher.init(Cipher.ENCRYPT_MODE, contentKey, new GCMParameterSpec(128, iv)); |
| 64 | + } catch (NoSuchAlgorithmException |
| 65 | + | NoSuchPaddingException |
| 66 | + | InvalidAlgorithmParameterException |
| 67 | + | InvalidKeyException e) { |
| 68 | + throw new RuntimeException(e); |
| 69 | + }/* catch (InvalidParameterSpecException e) { |
| 70 | + throw new RuntimeException(e); |
| 71 | + }*/ |
| 72 | + |
| 73 | + byte[] ciphertext; |
| 74 | + try { |
| 75 | + byte[] input = IoUtils.toByteArray(requestBody.contentStreamProvider().newStream()); |
| 76 | + ciphertext = cipher.doFinal(input); |
| 77 | + } catch (IOException e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } catch (IllegalBlockSizeException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } catch (BadPaddingException e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + |
| 85 | + // Save content metadata into request |
| 86 | + Base64.Encoder encoder = Base64.getEncoder(); |
| 87 | + Map<String,String> metadata = new HashMap<>(putObjectRequest.metadata()); |
| 88 | + EncryptedDataKey edk = materials.encryptedDataKeys().get(0); |
| 89 | + metadata.put("x-amz-key-v2", encoder.encodeToString(edk.ciphertext())); |
| 90 | + metadata.put("x-amz-iv", encoder.encodeToString(iv)); |
| 91 | + metadata.put("x-amz-matdesc", /* TODO: JSON encoded */ "{}"); |
| 92 | + metadata.put("x-amz-cek-alg", contentEncryptionAlgorithm); |
| 93 | + metadata.put("x-amz-tag-len", /* TODO: take from algo suite */ "128"); |
| 94 | + metadata.put("x-amz-wrap-alg", edk.keyProviderId()); |
| 95 | + |
| 96 | + try (JsonWriter jsonWriter = JsonWriter.create()) { |
| 97 | + jsonWriter.writeStartObject(); |
| 98 | + for (Entry<String,String> entry : materials.encryptionContext().entrySet()) { |
| 99 | + jsonWriter.writeFieldName(entry.getKey()).writeValue(entry.getValue()); |
| 100 | + } |
| 101 | + jsonWriter.writeEndObject(); |
| 102 | + |
| 103 | + String jsonEncryptionContext = new String(jsonWriter.getBytes(), StandardCharsets.UTF_8); |
| 104 | + metadata.put("x-amz-matdesc", jsonEncryptionContext); |
| 105 | + } catch (JsonGenerationException e) { |
| 106 | + throw new RuntimeException(e); |
| 107 | + } |
| 108 | + |
| 109 | + putObjectRequest = putObjectRequest.toBuilder().metadata(metadata).build(); |
| 110 | + |
| 111 | + return _wrappedClient.putObject(putObjectRequest, RequestBody.fromBytes(ciphertext)); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String serviceName() { |
| 116 | + return _wrappedClient.serviceName(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void close() { |
| 121 | + _wrappedClient.close(); |
| 122 | + } |
| 123 | +} |
0 commit comments