Пример #1
0
        internal static NCryptProviderName[] EnumerateStorageProviders()
        {
            uint             providerCount  = 0;
            SafeNCryptBuffer providerBuffer = null;

            try
            {
                // Ask CNG for the raw list of providers
                ErrorCode enumStatus = UnsafeNativeMethods.NCryptEnumStorageProviders(out providerCount,
                                                                                      out providerBuffer,
                                                                                      0);

                if (enumStatus != ErrorCode.Success)
                {
                    throw new CryptographicException((int)enumStatus);
                }

                // Copy the provider names into a managed array
                NCryptProviderName[] providers = new NCryptProviderName[providerCount];
                for (uint i = 0; i < providers.Length; ++i)
                {
                    providers[i] = providerBuffer.ReadArray <NCryptProviderName>(i);
                }

                return(providers);
            }
            finally
            {
                if (providerBuffer != null)
                {
                    providerBuffer.Dispose();
                }
            }
        }
Пример #2
0
        internal static NCryptKeyName[] EnumerateKeys(SafeNCryptProviderHandle provider,
                                                      CngKeyOpenOptions openOptions)
        {
            Debug.Assert(provider != null && !provider.IsClosed && !provider.IsInvalid, "Invalid provider");

            IntPtr           enumState       = IntPtr.Zero;
            SafeNCryptBuffer algorithmBuffer = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                List <NCryptKeyName> keys = new List <NCryptKeyName>();

                ErrorCode enumStatus = ErrorCode.Success;

                // Loop over the NCryptEnumKeys until it tells us that there are no more keys to enumerate
                do
                {
                    enumStatus = UnsafeNativeMethods.NCryptEnumKeys(provider,
                                                                    null,
                                                                    out algorithmBuffer,
                                                                    ref enumState,
                                                                    openOptions);

                    if (enumStatus == ErrorCode.Success)
                    {
                        keys.Add(algorithmBuffer.ReadArray <NCryptKeyName>(0));
                    }
                    else if (enumStatus != ErrorCode.NoMoreItems)
                    {
                        throw new CryptographicException((int)enumStatus);
                    }
                }while (enumStatus == ErrorCode.Success);

                return(keys.ToArray());
            }
            finally
            {
                if (enumState != IntPtr.Zero)
                {
                    UnsafeNativeMethods.NCryptFreeBuffer(enumState);
                }

                if (algorithmBuffer != null)
                {
                    algorithmBuffer.Dispose();
                }
            }
        }
Пример #3
0
        internal static NCryptAlgorithmName[] EnumerateAlgorithms(SafeNCryptProviderHandle provider,
                                                                  NCryptAlgorithmOperations operations)
        {
            Debug.Assert(provider != null && !provider.IsClosed && !provider.IsInvalid, "Invalid provider");

            uint             algorithmCount  = 0;
            SafeNCryptBuffer algorithmBuffer = null;

            try
            {
                // Ask CNG for the list of algorithms
                ErrorCode enumStatus = UnsafeNativeMethods.NCryptEnumAlgorithms(provider,
                                                                                operations,
                                                                                out algorithmCount,
                                                                                out algorithmBuffer,
                                                                                0);
                if (enumStatus != ErrorCode.Success)
                {
                    throw new CryptographicException((int)enumStatus);
                }

                // Copy the algorithm names into a managed array
                NCryptAlgorithmName[] algorithms = new NCryptAlgorithmName[algorithmCount];
                for (uint i = 0; i < algorithms.Length; ++i)
                {
                    algorithms[i] = algorithmBuffer.ReadArray <NCryptAlgorithmName>(i);
                }

                return(algorithms);
            }
            finally
            {
                if (algorithmBuffer != null)
                {
                    algorithmBuffer.Dispose();
                }
            }
        }
Пример #4
0
 internal static extern ErrorCode NCryptEnumStorageProviders([Out] out uint pdwProviderCount,
                                                             [Out] out SafeNCryptBuffer ppProviderList,
                                                             int dwFlags);
Пример #5
0
 internal static extern ErrorCode NCryptEnumKeys(SafeNCryptProviderHandle hProvider,
                                                 [In, MarshalAs(UnmanagedType.LPWStr)] string pszScope,
                                                 [Out] out SafeNCryptBuffer ppKeyName,
                                                 [In, Out] ref IntPtr ppEnumState,
                                                 CngKeyOpenOptions dwFlags);
Пример #6
0
 internal static extern ErrorCode NCryptEnumAlgorithms(SafeNCryptProviderHandle hProvider,
                                                       NCryptAlgorithmOperations dwAlgOperations,
                                                       [Out] out uint pdwAlgCount,
                                                       [Out] out SafeNCryptBuffer ppAlgList,
                                                       int dwFlags);