< Summary

Class:Azure.Extensions.AspNetCore.DataProtection.Keys.AzureKeyVaultXmlDecryptor
Assembly:Azure.Extensions.AspNetCore.DataProtection.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\extensions\Azure.Extensions.AspNetCore.DataProtection.Keys\src\AzureKeyVaultXmlDecryptor.cs
Covered lines:18
Uncovered lines:0
Coverable lines:18
Total lines:56
Line coverage:100% (18 of 18)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Decrypt(...)-100%100%
DecryptAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\extensions\Azure.Extensions.AspNetCore.DataProtection.Keys\src\AzureKeyVaultXmlDecryptor.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.IO;
 6using System.Threading.Tasks;
 7using System.Xml.Linq;
 8using Azure.Core.Cryptography;
 9using Microsoft.AspNetCore.DataProtection.XmlEncryption;
 10using Microsoft.Extensions.DependencyInjection;
 11
 12namespace Azure.Extensions.AspNetCore.DataProtection.Keys
 13{
 14#pragma warning disable CA1812 // False positive, AzureKeyVaultXmlDecryptor is used in AzureKeyVaultXmlEncryptor
 15    internal class AzureKeyVaultXmlDecryptor : IXmlDecryptor
 16#pragma warning restore
 17    {
 18        private readonly IKeyEncryptionKeyResolver _client;
 19
 220        public AzureKeyVaultXmlDecryptor(IServiceProvider serviceProvider)
 21        {
 222            _client = serviceProvider.GetService<IKeyEncryptionKeyResolver>();
 223        }
 24
 25        public XElement Decrypt(XElement encryptedElement)
 26        {
 427            return Task.Run(() => DecryptAsync(encryptedElement)).GetAwaiter().GetResult();
 28        }
 29
 30        private async Task<XElement> DecryptAsync(XElement encryptedElement)
 31        {
 232            var kid = (string)encryptedElement.Element("kid");
 233            var symmetricKey = Convert.FromBase64String((string)encryptedElement.Element("key"));
 234            var symmetricIV = Convert.FromBase64String((string)encryptedElement.Element("iv"));
 35
 236            var encryptedValue = Convert.FromBase64String((string)encryptedElement.Element("value"));
 37
 238            var key = await _client.ResolveAsync(kid).ConfigureAwait(false);
 239            var result = await key.UnwrapKeyAsync(AzureKeyVaultXmlEncryptor.DefaultKeyEncryption, symmetricKey).Configur
 40
 41            byte[] decryptedValue;
 242            using (var symmetricAlgorithm = AzureKeyVaultXmlEncryptor.DefaultSymmetricAlgorithmFactory())
 43            {
 244                using (var decryptor = symmetricAlgorithm.CreateDecryptor(result, symmetricIV))
 45                {
 246                    decryptedValue = decryptor.TransformFinalBlock(encryptedValue, 0, encryptedValue.Length);
 247                }
 248            }
 49
 250            using (var memoryStream = new MemoryStream(decryptedValue))
 51            {
 252                return XElement.Load(memoryStream);
 53            }
 254        }
 55    }
 56}