// 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. using System; using System.IO; using Microsoft.AspNetCore.Cryptography; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption; using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel; using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.AspNetCore.DataProtection.Repositories; using Microsoft.AspNetCore.DataProtection.XmlEncryption; using Microsoft.Extensions.Options; using Microsoft.Win32; #if !NETSTANDARD1_3 // [[ISSUE60]] Remove this #ifdef when Core CLR gets support for EncryptedXml using System.Security.Cryptography.X509Certificates; #endif namespace Microsoft.Extensions.DependencyInjection { /// /// Default instances for the Data Protection system. /// internal static class DataProtectionServiceDescriptors { /// /// An backed by the host-provided defaults. /// public static ServiceDescriptor ConfigureOptions_DataProtectionOptions() { return ServiceDescriptor.Transient>(services => { return new ConfigureOptions(options => { options.ApplicationDiscriminator = services.GetApplicationUniqueIdentifier(); }); }); } /// /// An where the key lifetime is specified explicitly. /// public static ServiceDescriptor ConfigureOptions_DefaultKeyLifetime(int numDays) { return ServiceDescriptor.Transient>(services => { return new ConfigureOptions(options => { options.NewKeyLifetime = TimeSpan.FromDays(numDays); }); }); } /// /// An backed by default algorithmic options. /// public static ServiceDescriptor IAuthenticatedEncryptorConfiguration_Default() { return IAuthenticatedEncryptorConfiguration_FromOptions(new AuthenticatedEncryptionOptions()); } /// /// An backed by an . /// public static ServiceDescriptor IAuthenticatedEncryptorConfiguration_FromOptions(IInternalAuthenticatedEncryptionOptions options) { return ServiceDescriptor.Singleton(options.ToConfiguration); } #if !NETSTANDARD1_3 // [[ISSUE60]] Remove this #ifdef when Core CLR gets support for EncryptedXml /// /// An backed by the default implementation. /// public static ServiceDescriptor ICertificateResolver_Default() { return ServiceDescriptor.Singleton(); } #endif /// /// An backed by the default keyring. /// public static ServiceDescriptor IDataProtectionProvider_Default() { return ServiceDescriptor.Singleton( services => DataProtectionProviderFactory.GetProviderFromServices( options: services.GetRequiredService>().Value, services: services, mustCreateImmediately: true /* this is the ultimate fallback */)); } /// /// An ephemeral . /// public static ServiceDescriptor IDataProtectionProvider_Ephemeral() { return ServiceDescriptor.Singleton(services => new EphemeralDataProtectionProvider(services)); } /// /// An backed by a given implementation type. /// /// /// The implementation type name is provided as a string so that we can provide activation services. /// public static ServiceDescriptor IKeyEscrowSink_FromTypeName(string implementationTypeName) { return ServiceDescriptor.Singleton(services => services.GetActivator().CreateInstance(implementationTypeName)); } /// /// An backed by the default XML key manager. /// public static ServiceDescriptor IKeyManager_Default() { return ServiceDescriptor.Singleton(services => new XmlKeyManager(services)); } #if !NETSTANDARD1_3 // [[ISSUE60]] Remove this #ifdef when Core CLR gets support for EncryptedXml /// /// An backed by an X.509 certificate. /// public static ServiceDescriptor IXmlEncryptor_Certificate(X509Certificate2 certificate) { return ServiceDescriptor.Singleton(services => new CertificateXmlEncryptor(certificate, services)); } /// /// An backed by an X.509 certificate. /// public static ServiceDescriptor IXmlEncryptor_Certificate(string thumbprint) { return ServiceDescriptor.Singleton(services => new CertificateXmlEncryptor( thumbprint: thumbprint, certificateResolver: services.GetRequiredService(), services: services)); } #endif /// /// An backed by DPAPI. /// public static ServiceDescriptor IXmlEncryptor_Dpapi(bool protectToMachine) { CryptoUtil.AssertPlatformIsWindows(); return ServiceDescriptor.Singleton(services => new DpapiXmlEncryptor(protectToMachine, services)); } /// /// An backed by DPAPI-NG. /// public static ServiceDescriptor IXmlEncryptor_DpapiNG(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags) { CryptoUtil.AssertPlatformIsWindows8OrLater(); return ServiceDescriptor.Singleton(services => new DpapiNGXmlEncryptor(protectionDescriptorRule, flags, services)); } /// /// An backed by a file system. /// public static ServiceDescriptor IXmlRepository_FileSystem(DirectoryInfo directory) { return ServiceDescriptor.Singleton(services => new FileSystemXmlRepository(directory, services)); } /// /// An backed by volatile in-process memory. /// public static ServiceDescriptor IXmlRepository_InMemory() { return ServiceDescriptor.Singleton(services => new EphemeralXmlRepository(services)); } /// /// An backed by the Windows registry. /// public static ServiceDescriptor IXmlRepository_Registry(RegistryKey registryKey) { return ServiceDescriptor.Singleton(services => new RegistryXmlRepository(registryKey, services)); } } }