Sunday, May 5, 2013

Private key information from the smart card


The way of getting the private key information from the smart card when it supports Microsoft CryptoAPI

using System.Security.Cryptography.X509Certificates;
usingSystem.Security.Cryptography;

public static X509Certificate2GetSignedCertificate()
{
X509Certificate2signingCert = null;
var myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
var smartCardCerts = new List<X509Certificate2>();

myStore.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 cert inmyStore.Certificates)
       {
if (!cert.HasPrivateKey) continue;          


var rsa = cert.PrivateKey as RSACryptoServiceProvider;

if (rsa == null) continue
if (rsa.CspKeyContainerInfo.HardwareDevice) 
{
signingCert = cert;
myStore.Close();
return signingCert;
}
        }
   return signingCert;


}

No comments: