// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if !DNXCORE50 // [[ISSUE60]] Remove this #ifdef when Core CLR gets support for EncryptedXml using System; using System.Security.Cryptography.Xml; using System.Xml; using System.Xml.Linq; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNet.DataProtection.XmlEncryption { /// /// An that decrypts XML elements by using the class. /// public sealed class EncryptedXmlDecryptor : IInternalEncryptedXmlDecryptor, IXmlDecryptor { private readonly IInternalEncryptedXmlDecryptor _decryptor; /// /// Creates a new instance of an . /// public EncryptedXmlDecryptor() : this(services: null) { } /// /// Creates a new instance of an . /// /// An optional to provide ancillary services. public EncryptedXmlDecryptor(IServiceProvider services) { _decryptor = services?.GetService() ?? this; } /// /// Decrypts the specified XML element. /// /// An encrypted XML element. /// The decrypted form of . /// public XElement Decrypt(XElement encryptedElement) { if (encryptedElement == null) { throw new ArgumentNullException(nameof(encryptedElement)); } // // ... // // EncryptedXml works with XmlDocument, not XLinq. When we perform the conversion // we'll wrap the incoming element in a dummy element since encrypted XML // doesn't handle encrypting the root element all that well. var xmlDocument = new XmlDocument(); xmlDocument.Load(new XElement("root", encryptedElement).CreateReader()); var elementToDecrypt = (XmlElement)xmlDocument.DocumentElement.FirstChild; // Perform the decryption and update the document in-place. var encryptedXml = new EncryptedXml(xmlDocument); _decryptor.PerformPreDecryptionSetup(encryptedXml); encryptedXml.DecryptDocument(); // Strip the element back off and convert the XmlDocument to an XElement. return XElement.Load(xmlDocument.DocumentElement.FirstChild.CreateNavigator().ReadSubtree()); } void IInternalEncryptedXmlDecryptor.PerformPreDecryptionSetup(EncryptedXml encryptedXml) { // no-op } } } #endif