Skip to content

Commit 3092657

Browse files
author
Anirav Kareddy
committed
fixed up javadoc comments
1 parent 7d2e3f0 commit 3092657

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/examples/java/software/amazon/encryption/s3/examples/ReEncryptInstructionFileExample.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public static void main(final String[] args) throws NoSuchAlgorithmException {
6565

6666
/**
6767
* This example demonstrates re-encrypting the encrypted data key in an instruction file with a new AES wrapping key.
68-
* The other cryptographic parameters in the instruction file such as the IV and wrapping algorithm remain unchanged.
6968
*
7069
* @param bucket The name of the Amazon S3 bucket to perform operations on.
7170
* @throws NoSuchAlgorithmException if AES algorithm is not available
@@ -166,7 +165,6 @@ public static void simpleAesKeyringReEncryptInstructionFile(final String bucket)
166165

167166
/**
168167
* This example demonstrates re-encrypting the encrypted data key in an instruction file with a new RSA wrapping key.
169-
* The other cryptographic parameters in the instruction file such as the IV and wrapping algorithm remain unchanged.
170168
*
171169
* @param bucket The name of the Amazon S3 bucket to perform operations on.
172170
* @throws NoSuchAlgorithmException if RSA algorithm is not available
@@ -258,7 +256,7 @@ public static void simpleRsaKeyringReEncryptInstructionFile(final String bucket)
258256
assertTrue(e.getMessage().contains("Unable to RSA-OAEP-SHA1 unwrap"));
259257
}
260258

261-
// Create a new client with the rotated AES key
259+
// Create a new client with the rotated RSA key
262260
S3EncryptionClient newClient = S3EncryptionClient.builder()
263261
.keyring(newKeyring)
264262
.instructionFileConfig(InstructionFileConfig.builder()

src/main/java/software/amazon/encryption/s3/S3EncryptionClient.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,13 @@ public static Consumer<AwsRequestOverrideConfiguration.Builder> withAdditionalCo
207207
* Key rotation scenarios:
208208
* - Legacy to V3: Can rotate same wrapping key from legacy wrapping algorithms to fully supported wrapping algorithms
209209
* - Within V3: When rotating the wrapping key, the new keyring must be different from the current keyring
210-
* - Enforce Rotation: When enabled, ensures old keyring cannot decrypt data encrypted by new keyring
211210
*
212211
* @param reEncryptInstructionFileRequest the request containing bucket, object key, new keyring, and optional instruction file suffix
213212
* @return ReEncryptInstructionFileResponse containing the bucket, object key, and instruction file suffix used
214213
* @throws S3EncryptionClientException if the new keyring has the same materials description as the current one
215214
*/
216215
public ReEncryptInstructionFileResponse reEncryptInstructionFile(ReEncryptInstructionFileRequest reEncryptInstructionFileRequest) {
217-
//GetObjectRequest MUST be kept the same
216+
//Build request to retrieve the encrypted object and its associated instruction file
218217
final GetObjectRequest request = GetObjectRequest.builder()
219218
.bucket(reEncryptInstructionFileRequest.bucket())
220219
.key(reEncryptInstructionFileRequest.key())
@@ -224,15 +223,13 @@ public ReEncryptInstructionFileResponse reEncryptInstructionFile(ReEncryptInstru
224223
ContentMetadataDecodingStrategy decodingStrategy = new ContentMetadataDecodingStrategy(_instructionFileConfig);
225224
ContentMetadata contentMetadata = decodingStrategy.decode(request, response.response());
226225

227-
// Algorithm Suite MUST be kept the same
226+
//Extract cryptographic parameters from the current instruction file that MUST be preserved during re-encryption
228227
final AlgorithmSuite algorithmSuite = contentMetadata.algorithmSuite();
229-
// Original Encrypted Data Key MUST be kept the same
230228
final EncryptedDataKey originalEncryptedDataKey = contentMetadata.encryptedDataKey();
231-
// Current Keyring's Materials Description MUST be kept the same
232229
final Map<String, String> currentKeyringMaterialsDescription = contentMetadata.encryptedDataKeyMatDescOrContext();
233-
// Content IV MUST be kept the same
234230
final byte[] iv = contentMetadata.contentIv();
235231

232+
//Decrypt the data key using the current keyring
236233
DecryptionMaterials decryptedMaterials = this._cryptoMaterialsManager.decryptMaterials(
237234
DecryptMaterialsRequest.builder()
238235
.algorithmSuite(algorithmSuite)
@@ -241,28 +238,27 @@ public ReEncryptInstructionFileResponse reEncryptInstructionFile(ReEncryptInstru
241238
.build()
242239
);
243240

244-
//Plaintext Data Key MUST be kept the same
245241
final byte[] plaintextDataKey = decryptedMaterials.plaintextDataKey();
246242

243+
//Prepare encryption materials with the decrypted data key
247244
EncryptionMaterials encryptionMaterials = EncryptionMaterials.builder()
248245
.algorithmSuite(algorithmSuite)
249246
.plaintextDataKey(plaintextDataKey)
250247
.s3Request(request)
251248
.build();
252249

253-
//New Keyring MUST be kept the same
254-
final RawKeyring newKeyring = reEncryptInstructionFileRequest.newKeyring();
255-
//Encrypted Materials MUST be kept the same
256-
final EncryptionMaterials encryptedMaterials = newKeyring.onEncrypt(encryptionMaterials);
257-
//New Keyring's Materials Description MUST be kept the same
258-
final Map<String, String> newMaterialsDescription = encryptedMaterials.materialsDescription().getMaterialsDescription();
250+
//Re-encrypt the data key with the new keyring while preserving other cryptographic parameters
251+
RawKeyring newKeyring = reEncryptInstructionFileRequest.newKeyring();
252+
EncryptionMaterials encryptedMaterials = newKeyring.onEncrypt(encryptionMaterials);
259253

254+
final Map<String, String> newMaterialsDescription = encryptedMaterials.materialsDescription().getMaterialsDescription();
255+
//Validate that the new keyring has different materials description than the old keyring
260256
if (newMaterialsDescription.equals(currentKeyringMaterialsDescription)) {
261257
throw new S3EncryptionClientException("New keyring must have new materials description!");
262258
}
263259

260+
//Create or update instruction file with the re-encrypted metadata while preserving IV
264261
ContentMetadataEncodingStrategy encodeStrategy = new ContentMetadataEncodingStrategy(_instructionFileConfig);
265-
266262
encodeStrategy.encodeMetadata(encryptedMaterials, iv, PutObjectRequest.builder()
267263
.bucket(reEncryptInstructionFileRequest.bucket())
268264
.key(reEncryptInstructionFileRequest.key())

0 commit comments

Comments
 (0)