c# - 在读卡器当前的智能卡上找到证书

标签 c# cryptography visual-studio-2013 smartcard

我正在使用 Visual Studio 2013 (C#) 使用来自智能卡的证书对文档进行数字签名。 我无法识别当前插入读卡器中的证书:(

Windows 从读卡器中插入的所有卡中复制证书并将其保存在商店中。我现在只想在读卡器中使用卡片。

我使用的代码是

public static byte[] Sign(Stream inData, string certSubject)
{

    // Access Personal (MY) certificate store of current user
    X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    my.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

    // Find the certificate we'll use to sign            
    RSACryptoServiceProvider csp = null;
    foreach (X509Certificate2 cert in my.Certificates)
    {
        if (cert.Subject.Contains(certSubject))
        {
            // We found it. 
            // Get its associated CSP and private key
            if (cert.HasPrivateKey) {
                csp = (RSACryptoServiceProvider)cert.PrivateKey;
                if (csp.CspKeyContainerInfo.HardwareDevice)
                    Console.WriteLine("hardware");                              
                    Console.WriteLine(cert.ToString());
            }
        }
    }
    if (csp == null)
    {
        throw new Exception("No valid cert was found");
    }

    // Hash the data
    SHA1Managed sha1 = new SHA1Managed();
    byte[] hash = sha1.ComputeHash(inData);

    // Sign the hash
    return csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}

但是当访问 cert.PrivateKey 用户时会提示将卡插入读卡器。如何检测并跳过此卡提示或检测读卡器中当前有相应卡的证书?

我只想使用读卡器中当前智能卡的证书。

最佳答案

恐怕无法使用标准 .NET API 检测读卡器中是否存在包含特定 X509Certificate2 对象的卡。我能想到的最好的事情(非常骇人听闻)是这样的:

public static X509Certificate2 GetDefaultCertificateStoredOnTheCard() 
{ 
    // Acquire public key stored in the default container of the currently inserted card
    CspParameters cspParameters = new CspParameters(1, "Microsoft Base Smart Card Crypto Provider"); 
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParameters); 
    string pubKeyXml = rsaProvider.ToXmlString(false); 

    // Find the certficate in the CurrentUser\My store that matches the public key
    X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
    x509Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 
    foreach (X509Certificate2 cert in x509Store.Certificates) 
    { 
        if ((cert.PublicKey.Key.ToXmlString(false) == pubKeyXml) && cert.HasPrivateKey)
            return cert; 
    } 

    return null; 
}

但是这种方法只有在满足以下条件时才是可靠的:

  1. 您的卡可通过微型驱动程序和 Microsoft Base Smart Card Crypto Provider 访问。
  2. 只有一个读卡器连接到您的计算机并带有智能卡。
  3. 当前插入读卡器的卡上只有一张证书。

当有多个读卡器连接了智能卡或卡上存在多个证书时,您无法确定通过此方法返回哪一个。

请注意还有其他可用的 API 可以访问智能卡。此类 API 的一个示例是 PKCS#11。对于简单的操作来说,这可能有点矫枉过正,但它可以让您完全控制您的卡和存储在卡上的对象。如果您有兴趣并且您的智能卡附带 PKCS#11 库,您可以查看我的项目 Pkcs11Interop为 .NET 环境带来 PKCS#11 API 的全部功能。

希望这有帮助:)

编辑以删除“单一证书”限制:

我稍微修改了代码。它现在使用非托管 Crypto API 枚举由 Microsoft Base Smart Card Crypto Provider 管理的所有容器的名称,然后在 CurrentUser\My 存储中搜索相应的 X509Certificate2 对象。请注意,这种方法也非常 hackish,提供的代码可能无法与市场上所有可用的卡/微型驱动程序一起可靠地工作。让用户从内置的证书选择对话框中选择正确的证书通常更好更容易。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace CSP
{
    public static class BaseSmartCardCryptoProvider
    {
        private const string _providerName = "Microsoft Base Smart Card Crypto Provider";

        private static class NativeMethods
        {
            public const uint PROV_RSA_FULL = 0x00000001;
            public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
            public const uint CRYPT_FIRST = 0x00000001;
            public const uint CRYPT_NEXT = 0x00000002;
            public const uint ERROR_NO_MORE_ITEMS = 0x00000103;
            public const uint PP_ENUMCONTAINERS = 0x00000002;

            [DllImport("advapi32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
            public static extern bool CryptAcquireContext(
            ref IntPtr phProv,
            [MarshalAs(UnmanagedType.LPStr)] string pszContainer,
            [MarshalAs(UnmanagedType.LPStr)] string pszProvider,
            uint dwProvType,
            uint dwFlags);

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

            [DllImport("advapi32.dll", SetLastError = true)]
            public static extern bool CryptReleaseContext(
            IntPtr hProv,
            uint dwFlags);
        }

        public static List<X509Certificate2> GetCertificates()
        {
            List<X509Certificate2> certs = new List<X509Certificate2>();

            X509Store x509Store = null;

            try
            {
                x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                x509Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

                List<string> containers = GetKeyContainers();

                foreach (string container in containers)
                {
                    CspParameters cspParameters = new CspParameters((int)NativeMethods.PROV_RSA_FULL, _providerName, container);
                    cspParameters.Flags = CspProviderFlags.UseExistingKey;
                    string pubKeyXml = null;
                    using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParameters))
                        pubKeyXml = rsaProvider.ToXmlString(false);

                    foreach (X509Certificate2 cert in x509Store.Certificates)
                    {
                        if ((cert.PublicKey.Key.ToXmlString(false) == pubKeyXml) && cert.HasPrivateKey)
                            certs.Add(cert);
                    }
                }
            }
            finally
            {
                if (x509Store != null)
                {
                    x509Store.Close();
                    x509Store = null;
                }
            }

            return certs;
        }

        private static List<string> GetKeyContainers()
        {
            List<string> containers = new List<string>();

            IntPtr hProv = IntPtr.Zero;

            try
            {
                if (!NativeMethods.CryptAcquireContext(ref hProv, null, _providerName, NativeMethods.PROV_RSA_FULL, NativeMethods.CRYPT_VERIFYCONTEXT))
                    throw new Win32Exception(Marshal.GetLastWin32Error());

                uint pcbData = 0;
                uint dwFlags = NativeMethods.CRYPT_FIRST;
                if (!NativeMethods.CryptGetProvParam(hProv, NativeMethods.PP_ENUMCONTAINERS, null, ref pcbData, dwFlags))
                    throw new Win32Exception(Marshal.GetLastWin32Error());

                StringBuilder sb = new StringBuilder((int)pcbData + 1);
                while (NativeMethods.CryptGetProvParam(hProv, NativeMethods.PP_ENUMCONTAINERS, sb, ref pcbData, dwFlags))
                {
                    containers.Add(sb.ToString());
                    dwFlags = NativeMethods.CRYPT_NEXT;
                }

                int err = Marshal.GetLastWin32Error();
                if (err != NativeMethods.ERROR_NO_MORE_ITEMS)
                    throw new Win32Exception(err);

                if (hProv != IntPtr.Zero)
                {
                    if (!NativeMethods.CryptReleaseContext(hProv, 0))
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    hProv = IntPtr.Zero;
                }
            }
            catch
            {
                if (hProv != IntPtr.Zero)
                {
                    if (!NativeMethods.CryptReleaseContext(hProv, 0))
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    hProv = IntPtr.Zero;
                }

                throw;
            }

            return containers;
        }
    }
}

只需调用所提供类的 GetCertificates() 方法来检查此代码是否适用于您的卡:

List<X509Certificate2> certs = CSP.BaseSmartCardCryptoProvider.GetCertificates();

关于c# - 在读卡器当前的智能卡上找到证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236116/

相关文章:

c# - 多个 Controller ,相同的 Controller 名称

c# - HtmlAgilityPack NextSibling.InnerText 值为空

windows - 通过 Powershell 为证书的私钥分配权限 (Win 2012 R2)

c# - 使用 Interop 处理字节顺序的标准是什么?

c++ - 使用 rapidjson 和 ATL CString

c# - ASP.NET EntityFramework 获取数据库名称

c# - 同一 AppPool 中跨应用程序的共享变量?

java - 使用 JCE/JCA 从主 key 派生 secret

c - 如何在 Microsoft Visual Studio 2013 中添加 libusb

c++ - 对比 2013 年 : breakpoint into cppunittest test throws exception