@@ -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