Ejemplo n.º 1
0
        new internal static bool CryptGetProvParam(
            SafeCryptProvHandle hProv,
            uint dwParam,
            StringBuilder pbData,
            ref uint dwDataLen,
            uint dwFlags)
        {
            bool result = CAPIMethods.CryptGetProvParam(hProv, dwParam, pbData, ref dwDataLen, dwFlags);

#if DEBUG
            if (!result)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != 0 && errorCode != CAPI.ERROR_NO_MORE_ITEMS)
                {
                    System.Diagnostics.Debug.WriteLine("Error " + errorCode);
                }
            }
#endif
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets all key containers from Capi provider.
        /// </summary>
        /// <param name="providerName">Provider name</param>
        /// <param name="type">Type</param>
        /// <param name="getMachineKeys">Indicates whether to obtain machine keys</param>
        /// <returns>Enumerator of key strings</returns>
        internal static IEnumerable <string> GetContainersFromCapiProvider(string providerName, uint type, bool getMachineKeys)
        {
            SafeCryptProvHandle provider = SafeCryptProvHandle.InvalidHandle;
            uint flags     = getMachineKeys ? CAPI.CRYPT_VERIFYCONTEXT | CAPI.CRYPT_MACHINE_KEYSET : CAPI.CRYPT_VERIFYCONTEXT;
            int  errorCode = 0;

            if (CAPI.CryptAcquireContext(ref provider,
                                         null,
                                         providerName,
                                         type,
                                         flags,
                                         out errorCode))
            {
                using (provider)
                {
                    uint dwMaxSize = 0;
                    if (CAPI.CryptGetProvParam(provider, CAPI.PP_ENUMCONTAINERS, null, ref dwMaxSize, CAPI.CRYPT_FIRST))
                    {
                        StringBuilder container = new StringBuilder(checked ((int)dwMaxSize));
                        if (CAPI.CryptGetProvParam(provider, CAPI.PP_ENUMCONTAINERS, container, ref dwMaxSize, CAPI.CRYPT_FIRST))
                        {
                            yield return(container.ToString());

                            bool success = true;
                            do
                            {
                                success = CAPI.CryptGetProvParam(provider, CAPI.PP_ENUMCONTAINERS, container, ref dwMaxSize, CAPI.CRYPT_NEXT);
                                if (success)
                                {
                                    yield return(container.ToString());

                                    dwMaxSize = (uint)container.Capacity;
                                }
                            } while (success);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        internal static bool CryptAcquireContext(
            ref SafeCryptProvHandle hCryptProv,
            string pwszContainer,
            string pwszProvider,
            uint dwProvType,
            uint dwFlags,
            out int errorCode)
        {
            errorCode = 0;

#if !RUNTIME_TYPE_NETCORE
            CspParameters parameters = new CspParameters();
            parameters.ProviderName     = pwszProvider;
            parameters.KeyContainerName = pwszContainer;
            parameters.ProviderType     = (int)dwProvType;
            parameters.KeyNumber        = -1;
            parameters.Flags            = (CspProviderFlags)((dwFlags & CAPI.CRYPT_MACHINE_KEYSET) == CAPI.CRYPT_MACHINE_KEYSET ? CspProviderFlags.UseMachineKeyStore : 0);

            KeyContainerPermission            kp    = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
            KeyContainerPermissionAccessEntry entry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Open);
            kp.AccessEntries.Add(entry);
            kp.Demand();
#endif

            bool rc = CAPIMethods.CryptAcquireContext(ref hCryptProv,
                                                      pwszContainer,
                                                      pwszProvider,
                                                      dwProvType,
                                                      dwFlags);

            if (!rc)
            {
                errorCode = Marshal.GetLastWin32Error();
            }

            return(rc);
        }
Ejemplo n.º 4
0
 internal protected extern static bool CryptAcquireContext(
     [In, Out] ref SafeCryptProvHandle hCryptProv,
     [In][MarshalAs(UnmanagedType.LPStr)] string pszContainer,
     [In][MarshalAs(UnmanagedType.LPStr)] string pszProvider,
     [In]     uint dwProvType,
     [In]     uint dwFlags);
Ejemplo n.º 5
0
 internal static extern bool CryptGetProvParam(
     [In] SafeCryptProvHandle hProv,
     [In] uint dwParam,
     [MarshalAs(UnmanagedType.LPStr)] StringBuilder pbData,
     ref uint dwDataLen,
     [In] uint dwFlags);