codingoutloud
11/3/2012 - 3:03 AM

Dump all digital certificates in Windows certificate store to stdout

Dump all digital certificates in Windows certificate store to stdout

// Iterates through all of the X.509 digital certificates installed in the certificate store 
// on a Windows operating system, dumping out some metadata about each. Each certificate, in 
// each Certificate Store, from each Certificate Location is included.
// 
// Bill Wilder | @codingoutloud | Oct 2012
// Original: https://gist.github.com/4005661

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

namespace DumpAllWindowsCerts
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var sl in Enum.GetValues(typeof (StoreLocation)))
            {
                Console.WriteLine(String.Format("Store Location: {0}", sl));
                foreach (var sn in Enum.GetValues(typeof (StoreName)))
                {
                    var store = new X509Store((StoreName) sn, (StoreLocation) sl);
                    store.Open(OpenFlags.ReadOnly);

                    Console.WriteLine(String.Format("  Store Location/Store Name: {0}/{1}",
                                                    store.Location, store.Name));
                    foreach (X509Certificate2 c in store.Certificates)
                    {
                        Console.WriteLine(String.Format("{0} {1}/{2} {0}",
                                                        new string('-', 15), store.Location, store.Name));
                        if (!true)
                        {
                            var stop = true;
                        }
                        Console.WriteLine("{0}" +
                                          "\tCertificate Subject Name: {1}" +
                                          "\n\t  Has private key? {2} Is archived? {3}" +
                                          "\n\t  X.509 version: {4}" +
                                          "\n\t  Key algorithm: {5} Signature algorithm: {6}" +
                                          "\n\t  Issuer: {7}" +
                                          "\n\t  {8} extensions",
                                          String.IsNullOrEmpty(c.FriendlyName)
                                              ? "" : String.Format("\t[Store Friendly Name: {0}]\n", c.FriendlyName),
                                          c.SubjectName.Name,
                                          // FriendlyName is a store concept, not cert?
                                          c.HasPrivateKey, c.Archived,
                                          c.Version,
                                          c.GetKeyAlgorithm(), c.SignatureAlgorithm,
                                          c.IssuerName.Name,
                                          c.Extensions.Count);
                        foreach (var ext in c.Extensions)
                        {
                            Console.WriteLine("\t    OID = {0} {1}", ext.Oid.FriendlyName,
                                              ext.Critical ? "[Critical]" : "");
                        }
                    }
                    store.Close();
                }
            }
        }
    }
}