DpapiXmlEncryptionTests.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Xml.Linq;
  5. using Microsoft.AspNetCore.DataProtection.Test.Shared;
  6. using Microsoft.AspNetCore.Testing;
  7. using Microsoft.AspNetCore.Testing.xunit;
  8. using Microsoft.Extensions.Logging.Abstractions;
  9. using Xunit;
  10. namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
  11. {
  12. public class DpapiXmlEncryptionTests
  13. {
  14. [ConditionalTheory]
  15. [ConditionalRunTestOnlyOnWindows]
  16. [InlineData(true)]
  17. [InlineData(false)]
  18. public void Encrypt_CurrentUserOrLocalMachine_Decrypt_RoundTrips(bool protectToLocalMachine)
  19. {
  20. // Arrange
  21. var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");
  22. var encryptor = new DpapiXmlEncryptor(protectToLocalMachine, NullLoggerFactory.Instance);
  23. var decryptor = new DpapiXmlDecryptor();
  24. // Act & assert - run through encryptor and make sure we get back an obfuscated element
  25. var encryptedXmlInfo = encryptor.Encrypt(originalXml);
  26. Assert.Equal(typeof(DpapiXmlDecryptor), encryptedXmlInfo.DecryptorType);
  27. Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);
  28. // Act & assert - run through decryptor and make sure we get back the original value
  29. var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement);
  30. XmlAssert.Equal(originalXml, roundTrippedElement);
  31. }
  32. #if NET461
  33. [ConditionalFact]
  34. [ConditionalRunTestOnlyOnWindows]
  35. public void Encrypt_CurrentUser_Decrypt_ImpersonatedAsAnonymous_Fails()
  36. {
  37. // Arrange
  38. var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");
  39. var encryptor = new DpapiXmlEncryptor(protectToLocalMachine: false, loggerFactory: NullLoggerFactory.Instance);
  40. var decryptor = new DpapiXmlDecryptor();
  41. // Act & assert - run through encryptor and make sure we get back an obfuscated element
  42. var encryptedXmlInfo = encryptor.Encrypt(originalXml);
  43. Assert.Equal(typeof(DpapiXmlDecryptor), encryptedXmlInfo.DecryptorType);
  44. Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);
  45. // Act & assert - run through decryptor (while impersonated as anonymous) and verify failure
  46. ExceptionAssert2.ThrowsCryptographicException(() =>
  47. AnonymousImpersonation.Run(() => decryptor.Decrypt(encryptedXmlInfo.EncryptedElement)));
  48. }
  49. #elif NETCOREAPP2_1
  50. #else
  51. #error Target framework needs to be updated
  52. #endif
  53. }
  54. }