/// <summary>
        /// Creates a new instance of a <see cref="DpapiNGXmlEncryptor"/>.
        /// </summary>
        /// <param name="protectionDescriptorRule">The rule string from which to create the protection descriptor.</param>
        /// <param name="flags">Flags controlling the creation of the protection descriptor.</param>
        /// <param name="services">An optional <see cref="IServiceProvider"/> to provide ancillary services.</param>
        public DpapiNGXmlEncryptor([NotNull] string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags, IServiceProvider services)
        {
            CryptoUtil.AssertPlatformIsWindows8OrLater();

            int ntstatus = UnsafeNativeMethods.NCryptCreateProtectionDescriptor(protectionDescriptorRule, (uint)flags, out _protectionDescriptorHandle);
            UnsafeNativeMethods.ThrowExceptionForNCryptStatus(ntstatus);
            CryptoUtil.AssertSafeHandleIsValid(_protectionDescriptorHandle);

            _logger = services.GetLogger<DpapiNGXmlEncryptor>();
        }
Example #2
0
        public DpapiNGXmlEncryptor(string protectionDescriptor, DpapiNGProtectionDescriptorFlags protectionDescriptorFlags = DpapiNGProtectionDescriptorFlags.None)
        {
            if (String.IsNullOrEmpty(protectionDescriptor))
            {
                throw new Exception("TODO: Null or empty.");
            }

            int ntstatus = UnsafeNativeMethods.NCryptCreateProtectionDescriptor(protectionDescriptor, (uint)protectionDescriptorFlags, out _protectionDescriptorHandle);

            UnsafeNativeMethods.ThrowExceptionForNCryptStatus(ntstatus);
            CryptoUtil.AssertSafeHandleIsValid(_protectionDescriptorHandle);
        }
Example #3
0
        /// <summary>
        /// Creates a new instance of a <see cref="DpapiNGXmlEncryptor"/>.
        /// </summary>
        /// <param name="protectionDescriptorRule">The rule string from which to create the protection descriptor.</param>
        /// <param name="flags">Flags controlling the creation of the protection descriptor.</param>
        /// <param name="services">An optional <see cref="IServiceProvider"/> to provide ancillary services.</param>
        public DpapiNGXmlEncryptor(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags, IServiceProvider services)
        {
            if (protectionDescriptorRule == null)
            {
                throw new ArgumentNullException(nameof(protectionDescriptorRule));
            }

            CryptoUtil.AssertPlatformIsWindows8OrLater();

            int ntstatus = UnsafeNativeMethods.NCryptCreateProtectionDescriptor(protectionDescriptorRule, (uint)flags, out _protectionDescriptorHandle);

            UnsafeNativeMethods.ThrowExceptionForNCryptStatus(ntstatus);
            CryptoUtil.AssertSafeHandleIsValid(_protectionDescriptorHandle);

            _logger = services.GetLogger <DpapiNGXmlEncryptor>();
        }
        /// <summary>
        /// Configures keys to be encrypted with Windows CNG DPAPI before being persisted to storage.
        /// </summary>
        /// <param name="protectionDescriptorRule">The descriptor rule string with which to protect the key material.</param>
        /// <param name="flags">Flags that should be passed to the call to 'NCryptCreateProtectionDescriptor'.
        /// The default value of this parameter is <see cref="DpapiNGProtectionDescriptorFlags.None"/>.</param>
        /// <returns>The 'this' instance.</returns>
        /// <remarks>
        /// See https://msdn.microsoft.com/en-us/library/windows/desktop/hh769091(v=vs.85).aspx
        /// and https://msdn.microsoft.com/en-us/library/windows/desktop/hh706800(v=vs.85).aspx
        /// for more information on valid values for the the <paramref name="descriptor"/>
        /// and <paramref name="flags"/> arguments.
        /// This API is only supported on Windows 8 / Windows Server 2012 and higher.
        /// </remarks>
        public DataProtectionConfiguration ProtectKeysWithDpapiNG(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
        {
            if (protectionDescriptorRule == null)
            {
                throw new ArgumentNullException(nameof(protectionDescriptorRule));
            }

            Use(DataProtectionServiceDescriptors.IXmlEncryptor_DpapiNG(protectionDescriptorRule, flags));
            return(this);
        }
Example #5
0
 /// <summary>
 /// Creates a new instance of a <see cref="DpapiNGXmlEncryptor"/>.
 /// </summary>
 /// <param name="protectionDescriptorRule">The rule string from which to create the protection descriptor.</param>
 /// <param name="flags">Flags controlling the creation of the protection descriptor.</param>
 public DpapiNGXmlEncryptor(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
     : this(protectionDescriptorRule, flags, services : null)
 {
 }
Example #6
0
    public static IDataProtectionBuilder ProtectKeysWithDpapiNG(this IDataProtectionBuilder builder, string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        if (protectionDescriptorRule == null)
        {
            throw new ArgumentNullException(nameof(protectionDescriptorRule));
        }

        builder.Services.AddSingleton <IConfigureOptions <KeyManagementOptions> >(services =>
        {
            var loggerFactory = services.GetService <ILoggerFactory>() ?? NullLoggerFactory.Instance;
            return(new ConfigureOptions <KeyManagementOptions>(options =>
            {
                CryptoUtil.AssertPlatformIsWindows8OrLater();
                options.XmlEncryptor = new DpapiNGXmlEncryptor(protectionDescriptorRule, flags, loggerFactory);
            }));
        });

        return(builder);
    }
 /// <summary>
 /// An <see cref="IXmlEncryptor"/> backed by DPAPI-NG.
 /// </summary>
 public static ServiceDescriptor IXmlEncryptor_DpapiNG(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
 {
     CryptoUtil.AssertPlatformIsWindows8OrLater();
     return ServiceDescriptor.Singleton<IXmlEncryptor>(services => new DpapiNGXmlEncryptor(protectionDescriptorRule, flags, services));
 }
        /// <summary>
        /// Configures keys to be encrypted with Windows CNG DPAPI before being persisted to storage.
        /// </summary>
        /// <param name="builder">The <see cref="IDataProtectionBuilder"/>.</param>
        /// <param name="protectionDescriptorRule">The descriptor rule string with which to protect the key material.</param>
        /// <param name="flags">Flags that should be passed to the call to 'NCryptCreateProtectionDescriptor'.
        /// The default value of this parameter is <see cref="DpapiNGProtectionDescriptorFlags.None"/>.</param>
        /// <returns>A reference to the <see cref="IDataProtectionBuilder" /> after this operation has completed.</returns>
        /// <remarks>
        /// See https://msdn.microsoft.com/en-us/library/windows/desktop/hh769091(v=vs.85).aspx
        /// and https://msdn.microsoft.com/en-us/library/windows/desktop/hh706800(v=vs.85).aspx
        /// for more information on valid values for the the <paramref name="protectionDescriptorRule"/>
        /// and <paramref name="flags"/> arguments.
        /// This API is only supported on Windows 8 / Windows Server 2012 and higher.
        /// </remarks>
        public static IDataProtectionBuilder ProtectKeysWithDpapiNG(this IDataProtectionBuilder builder, string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (protectionDescriptorRule == null)
            {
                throw new ArgumentNullException(nameof(protectionDescriptorRule));
            }

            Use(builder.Services, DataProtectionServiceDescriptors.IXmlEncryptor_DpapiNG(protectionDescriptorRule, flags));
            return(builder);
        }
 /// <summary>
 /// Creates a new instance of a <see cref="DpapiNGXmlEncryptor"/>.
 /// </summary>
 /// <param name="protectionDescriptorRule">The rule string from which to create the protection descriptor.</param>
 /// <param name="flags">Flags controlling the creation of the protection descriptor.</param>
 public DpapiNGXmlEncryptor([NotNull] string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
     : this(protectionDescriptorRule, flags, services: null)
 {
 }
Example #10
0
 /// <summary>
 /// An <see cref="IXmlEncryptor"/> backed by DPAPI-NG.
 /// </summary>
 public static ServiceDescriptor IXmlEncryptor_DpapiNG(string protectionDescriptorRule, DpapiNGProtectionDescriptorFlags flags)
 {
     CryptoUtil.AssertPlatformIsWindows8OrLater();
     return(ServiceDescriptor.Singleton <IXmlEncryptor>(services => new DpapiNGXmlEncryptor(protectionDescriptorRule, flags, services)));
 }