Skip to content

Instantly share code, notes, and snippets.

@yamnikov-oleg
Last active March 12, 2018 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamnikov-oleg/283e95a59685f5166ae3d53e5f39cb77 to your computer and use it in GitHub Desktop.
Save yamnikov-oleg/283e95a59685f5166ae3d53e5f39cb77 to your computer and use it in GitHub Desktop.
Пример получения списка сертификатов КриптоПро CSP
var hStore = Libcapi.CertOpenSystemStoreA(IntPtr.Zero, "MY");
Console.WriteLine("hStore = {0}", hStore);
Console.WriteLine("GetLastError: {0}", Librdrsup.GetLastError());
var pCtx = Libcapi.CertEnumCertificatesInStore(hStore, IntPtr.Zero);
while (pCtx.ToInt32() != 0)
{
var ctx = Marshal.PtrToStructure<Libcapi.CertContext>(pCtx);
var info = Marshal.PtrToStructure<Libcapi.CertInfo>(ctx.pCertInfo);
var buffer = new byte[info.Subject.cbData];
Marshal.Copy(info.Subject.pbData, buffer, 0, (int)info.Subject.cbData);
// Subject закодирован, внутри не ASCII, а бинарные данные
// Раскодировать можно с помощью CertGetNameString
var subject = Encoding.ASCII.GetString(buffer);
Console.WriteLine(
"Cert = {0}; hStore = {1}, version = {2}, subject = {3}",
pCtx, ctx.hCertStore, info.dwVersion, subject
);
var pNextCtx = Libcapi.CertEnumCertificatesInStore(hStore, pCtx);
Libcapi.CertFreeCertificateContext(pCtx);
pCtx = pNextCtx;
}
Console.WriteLine("CertCloseStore: {0}", Libcapi.CertCloseStore(hStore, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment