This is a mirror of official site: http://jasper-net.blogspot.com/

How to enumerate all certificates on a smart card (PowerShell)

| Tuesday, December 21, 2010
Some time ago I assisted my colleague Jeff Bowles with the development of a PowerShell script which enumerates all certificates on a smart card. Basically the replacement to CAPICOM.Store.Open CAPICOM_SMART_CARD_USER_STORE.

He developed a sample that returns a System.Security.Cryptography.X509Certificates.X509Store object with the certificates in the card. The sample tries to emulate what logonUI.exe does during smart card logon as documented at Certificate Enumeration.

Note that this code is also a great example that shows how we can use PowerShell to call Win32 API the same way we do it with any .NET application through P/Invoke mechanism:

function Get-SCUserStore {

[string]$providerName ="Microsoft Base Smart Card Crypto Provider"

# import CrytoAPI from advapi32.dll
$signature = @"
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetProvParam(
  IntPtr hProv,
  uint dwParam,
  byte[] pbProvData,
  ref uint pdwProvDataLen,  
  uint dwFlags);  

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDestroyKey(
  IntPtr hKey);    
   
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(
  ref IntPtr hProv,
  string pszContainer,
  string pszProvider,
  uint dwProvType,
  long dwFlags);
     
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetUserKey(
  IntPtr hProv,  
  uint dwKeySpec,
  ref IntPtr phUserKey);
   
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetKeyParam(
  IntPtr hKey,
  uint dwParam,
  byte[] pbData,
  ref uint pdwDataLen,
  uint dwFlags);
   
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptReleaseContext(
  IntPtr hProv,
  uint dwFlags);
"@

$CryptoAPI = Add-Type -member $signature -name advapiUtils -Namespace CryptoAPI -passthru

# set some constants for CryptoAPI
$AT_KEYEXCHANGE = 1


Read more: Decrypt my World

Posted via email from .NET Info

0 comments: