internal static EncryptionInstructions BuildInstructionsUsingInstructionFileV2()

in src/EncryptionUtilsV2.cs [483:534]


        internal static EncryptionInstructions BuildInstructionsUsingInstructionFileV2(GetObjectResponse response, EncryptionMaterialsBase materials)
        {
            using (TextReader textReader = new StreamReader(response.ResponseStream))
            {
                var keyValuePair = JsonUtils.ToDictionary(textReader.ReadToEnd());

                if (keyValuePair.ContainsKey(XAmzKeyV2))
                {
                    // The envelope contains data in V2 format
                    var encryptedEnvelopeKey = Base64DecodedDataValue(keyValuePair, XAmzKeyV2);
                    var decryptedEnvelopeKey = DecryptNonKmsEnvelopeKeyV2(encryptedEnvelopeKey, materials);

                    var initializationVector = Base64DecodedDataValue(keyValuePair, XAmzIV);
                    var materialDescription = JsonUtils.ToDictionary((string)keyValuePair[XAmzMatDesc]);

                    var cekAlgorithm = StringValue(keyValuePair, XAmzCekAlg);
                    var wrapAlgorithm = StringValue(keyValuePair, XAmzWrapAlg);

                    var instructions = new EncryptionInstructions(materialDescription, decryptedEnvelopeKey, null,
                        initializationVector, wrapAlgorithm, cekAlgorithm);

                    return instructions;
                }
                else if (keyValuePair.ContainsKey(XAmzKey))
                {
                    // The envelope contains data in V1 format
                    var encryptedEnvelopeKey = Base64DecodedDataValue(keyValuePair, XAmzKey);
                    var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey(encryptedEnvelopeKey, materials);

                    var initializationVector = Base64DecodedDataValue(keyValuePair, XAmzIV);
                    var materialDescription = JsonUtils.ToDictionary((string)keyValuePair[XAmzMatDesc]);

                    var instructions = new EncryptionInstructions(materialDescription, decryptedEnvelopeKey, null, initializationVector);

                    return instructions;
                }
                else if (keyValuePair.ContainsKey(EncryptedEnvelopeKey))
                {
                    // The envelope contains data in older format
                    var encryptedEnvelopeKey = Base64DecodedDataValue(keyValuePair, EncryptedEnvelopeKey);
                    var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey(encryptedEnvelopeKey, materials);

                    var initializationVector = Base64DecodedDataValue(keyValuePair, IV);

                    return new EncryptionInstructions(materials.MaterialsDescription, decryptedEnvelopeKey, initializationVector);
                }
                else
                {
                    throw new ArgumentException("Missing parameters required for decryption");
                }
            }
        }