in src/EncryptionUtilsV2.cs [496:547]
internal static EncryptionInstructions BuildInstructionsUsingInstructionFileV2(GetObjectResponse response, EncryptionMaterialsBase materials)
{
using (TextReader textReader = new StreamReader(response.ResponseStream))
{
var jsonData = JsonMapper.ToObject(textReader);
if (jsonData[XAmzKeyV2] != null)
{
// The envelope contains data in V2 format
var encryptedEnvelopeKey = Base64DecodedDataValue(jsonData, XAmzKeyV2);
var decryptedEnvelopeKey = DecryptNonKmsEnvelopeKeyV2(encryptedEnvelopeKey, materials);
var initializationVector = Base64DecodedDataValue(jsonData, XAmzIV);
var materialDescription = JsonMapper.ToObject<Dictionary<string, string>>((string)jsonData[XAmzMatDesc]);
var cekAlgorithm = StringValue(jsonData, XAmzCekAlg);
var wrapAlgorithm = StringValue(jsonData, XAmzWrapAlg);
var instructions = new EncryptionInstructions(materialDescription, decryptedEnvelopeKey, null,
initializationVector, wrapAlgorithm, cekAlgorithm);
return instructions;
}
else if (jsonData[XAmzKey] != null)
{
// The envelope contains data in V1 format
var encryptedEnvelopeKey = Base64DecodedDataValue(jsonData, XAmzKey);
var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey(encryptedEnvelopeKey, materials);
var initializationVector = Base64DecodedDataValue(jsonData, XAmzIV);
var materialDescription = JsonMapper.ToObject<Dictionary<string, string>>((string)jsonData[XAmzMatDesc]);
var instructions = new EncryptionInstructions(materialDescription, decryptedEnvelopeKey, null, initializationVector);
return instructions;
}
else if (jsonData[EncryptedEnvelopeKey] != null)
{
// The envelope contains data in older format
var encryptedEnvelopeKey = Base64DecodedDataValue(jsonData, EncryptedEnvelopeKey);
var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey(encryptedEnvelopeKey, materials);
var initializationVector = Base64DecodedDataValue(jsonData, IV);
return new EncryptionInstructions(materials.MaterialsDescription, decryptedEnvelopeKey, initializationVector);
}
else
{
throw new ArgumentException("Missing parameters required for decryption");
}
}
}