Help getting x509 certificate properties

jsunn

Member
Joined
May 13, 2009
Messages
5
Location
Milwaukee, WI
Programming Experience
Beginner
Hi, I'm a pretty obvious newcomer to .net / VB. We have lots of administrative scripts that use CAPICOM to manage our EFS certificates on our corporate workstations. With Windows 7 CAPICOM is no longer supported so I'm trying to make it work with the X509Certificate2 class. We are trying to convert code that opens a users certificate store (CURRENT USER STORE), loops through the certificates in the collection, and gets determines if the certificate is exportable or not. To determine whether the certificate is exportable or not the CspKeyContainerInfo.Exportable Property must be accessed but I'm having trouble making the connection on how to access it as I loop through the certificates in the collection. here is the code I have so far below. Its just a form with a button, it loops through the current users certificates, showing the thumbprint for each. I would also like to know if the certificate is exportable as well. I think that the certificate container must be retrieved using CspKeyContainerInfo, but not sure how to make it work.

I've been looking at these MSDN pages as a reference:
CSPKeyContainerInfo

RsaCryptoServiceProvider.CspKeyContainerinfo


here is the code so far:
VB.NET:
Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Imports System.IO
Public Class Form1
 
 
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
        'Instantiate the Store / The store name is My, location is current user store
        Dim store As New X509Store("MY", StoreLocation.CurrentUser)
        'Open the stor
        store.Open(OpenFlags.ReadWrite)
        'Use the X509CertificateCollection class to get the certificates from the my store into a collection
        Dim collection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        'Declare x509 as the certificate object
 
        Dim x509 As New X509Certificate2
 
 
        'Loop through the certificates in the store, one by one
        '------------
        
 
        For Each x509 In collection
            
 
            'Show the thumbprint for each certificate in the users store
            MsgBox(x509.Thumbprint)
            'Code here to analyze whether certificate is exportable or not
 
 
        Next x509
 
        store.Close()
End Sub
 
Okay, I'll answer this myself since it might be useful to someone else:
VB.NET:
Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Imports System.IO
Public Class Form1
 
 
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
        'Instantiate the Store / The store name is My, location is current user store
        Dim store As New X509Store("MY", StoreLocation.CurrentUser)
        'Open the store
        store.Open(OpenFlags.ReadWrite)
        'Use the X509CertificateCollection class to get the certificates from the my store into a collection
        Dim rsa As New RSACryptoServiceProvider()
        Dim keyInfo As CspKeyContainerInfo
        Dim collection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        'Declare x509 as the certificate object
 
        Dim x509 As New X509Certificate2
 
 
        'Loop through the certificates in the store, one by one
        '------------
        
 
        For Each x509 In collection
            
            rsa = DirectCast(x509Cert.PrivateKey, RSACryptoServiceProvider)
            keyInfo = rsa.CspKeyContainerInfo

            'Show the thumbprint for each certificate in the users store
            MsgBox("Thumbprint:" & x509.Thumbprint & " Exportable:" & keyInfo.Exportable)
           
 
 
        Next x509
 
        store.Close()
End Sub
 
Back
Top