Cryptopro c# Get the list of all certificate containers


public class Win32
    {
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CryptAcquireContext(
        ref IntPtr hProv,
        string pszContainer,
        string pszProvider,
        uint dwProvType,
        uint dwFlags);

        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool CryptGetProvParam(
        IntPtr hProv,
        uint dwParam,
        [In, Out] byte[] pbData,
        ref uint dwDataLen,
        uint dwFlags);

        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool CryptGetProvParam(
        IntPtr hProv,
        uint dwParam,
        [MarshalAs(UnmanagedType.LPStr)] StringBuilder pbData,
        ref uint dwDataLen,
        uint dwFlags);

        [DllImport("advapi32.dll")]
        public static extern bool CryptReleaseContext(
        IntPtr hProv,
        uint dwFlags);
    }
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections;
using System.Text;

namespace CryptoHelper
{
    public static class CryptoHelper
    {
        public static string[] GetContainerNames()
        {
            int BUFFSIZE = 512;
            ArrayList containernames = new ArrayList();
            uint pcbData = 0;
            //String provider = null; //can use null, for default provider
            String provider = "Crypto-Pro GOST R 34.10-2001 Cryptographic Service Provider";
            String container = null;   //required for crypt_verifycontext 
            uint type = PROV_RSA_FULL;
            uint cspflags = CRYPT_VERIFYCONTEXT | CSPKEYTYPE;  //no private key access required.
            uint enumflags = PP_ENUMCONTAINERS;  //specify container enumeration functdionality
            IntPtr hProv = IntPtr.Zero;
            uint dwFlags = CRYPT_FIRST;

            bool gotcsp = Win32.CryptAcquireContext(ref hProv, container, provider, type, cspflags);
            if (!gotcsp)
            {
                showWin32Error(Marshal.GetLastWin32Error());
                return null;
            }


            StringBuilder sb = null;
            Win32.CryptGetProvParam(hProv, enumflags, sb, ref pcbData, dwFlags);
            BUFFSIZE = (int)(2 * pcbData);
            sb = new StringBuilder(BUFFSIZE);

            /*  ----------  Get KeyContainer Names ------------- */
            dwFlags = CRYPT_FIRST;  //required initalization
            while (Win32.CryptGetProvParam(hProv, enumflags, sb, ref pcbData, dwFlags))
            {
                dwFlags = 0;      //required to continue entire enumeration
                containernames.Add(sb.ToString());
            }
            if (hProv != IntPtr.Zero)
                Win32.CryptReleaseContext(hProv, 0);

            if (containernames.Count == 0)
                return null;
            else
                return (string[])containernames.ToArray(Type.GetType("System.String"));
        }

        const uint PROV_RSA_FULL = 0x00000001;
        const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
        static uint CSPKEYTYPE = 0;
        const uint PP_ENUMCONTAINERS = 0x00000002;
        const uint CRYPT_FIRST = 0x00000001;

        private static void showWin32Error(int errorcode)
        {
            Win32Exception myEx = new Win32Exception(errorcode);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Error code:\t 0x{0:X}", myEx.ErrorCode);
            Console.WriteLine("Error message:\t {0}\n", myEx.Message);
            Console.ForegroundColor = ConsoleColor.White;
        }
    }
}

7 comments:

  1. спасибо, добрый человек

    ReplyDelete
  2. А как можно получить даты действия сертифика с... по... ?

    ReplyDelete
    Replies
    1. Надо смотреть, но лень. Вы бы сказали как пытаетесь это сделать и чего именно не получается я бы подсказал.

      Delete
    2. Хотя так навскидку, вот так получить дату по X509Certificate2.NotAfter . Вот c тоже наверняка свойство есть.

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Спасибо огромное!!!

    ReplyDelete

Note: only a member of this blog may post a comment.