Extract the certification chain in c #

2

I would like to have access to the certification chain of a certificate installed in the windows certificate store, I currently get this string but from a pfx file with its key, but now they changed the form and want it to be extracted from a installed certificate. the current code that works for me is the sgt:

string certPath = "nombre del cert";
string certPass = "clave";
X509Certificate2Collection collection = new X509Certificate2Collection();

collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

int ncontador = 0;
foreach (X509Certificate2 certR in collection ) 
{

}

Someone can support me, so that instead of extracting it from a file, extract it from an installed certificate (I want to have access to the certification chain from the root certificate)

    
asked by Jose Aguilar 17.05.2017 в 15:37
source

2 answers

1

I found a solution at least for what I need:

var miCert = new X509Certificate2(pathToCert, password);

X509Chain chain = new X509Chain();
chain.Build(miCert);
for (int i = 0; i < chain.ChainElements.Count; i++)
{
    MessageBox.Show(chain.ChainElements[i].Certificate.Issuer);
}

The first line would edit it so that it has the object (from the certificate store) .

    
answered by 17.05.2017 в 19:06
1

You can actually access the certificate store that is installed on windows.

The following code first shows a list of installed certificates and then shows the information of a particular one.

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

namespace ListadoCertificadosLocales
{
    class Program
    {
        static void Main(string[] args)
        {
            MostrarListadoCertificados();

            Console.WriteLine("----------------------");

            MostrarInfoCertificado("Root", StoreLocation.CurrentUser);

            Console.ReadKey();
        }

        /// <summary>
        /// Fuente: https://msdn.microsoft.com/es-es/library/system.security.cryptography.x509certificates.x509store(v=vs.110).aspx
        /// </summary>
        private static void MostrarListadoCertificados()
        {
            Console.WriteLine("\r\nExists Certs Name and Location");
            Console.WriteLine("------ ----- -------------------------");

            foreach (StoreLocation storeLocation in (StoreLocation[])
                Enum.GetValues(typeof(StoreLocation)))
            {
                foreach (StoreName storeName in (StoreName[])
                    Enum.GetValues(typeof(StoreName)))
                {
                    X509Store store = new X509Store(storeName, storeLocation);

                    try
                    {
                        store.Open(OpenFlags.OpenExistingOnly);

                        Console.WriteLine("Yes    {0,4}  {1}, {2}",
                            store.Certificates.Count, store.Name, store.Location);
                    }
                    catch (CryptographicException)
                    {
                        Console.WriteLine("No           {0}, {1}",
                            store.Name, store.Location);
                    }
                }
                Console.WriteLine();
            }
        }

        /// <summary>
        /// Muestra por consola la informacion para un almacen
        /// </summary>
        /// <param name="nombreAlmacen">Nombre del almacen que se quiere acceder</param>
        /// <param name="ubicacion">Especifica la ubicación del almacén de certificados X.509.</param>
        private static void MostrarInfoCertificado(string nombreAlmacen, StoreLocation ubicacion)
        {
            try
            {
                X509Store almacen = new X509Store(nombreAlmacen, ubicacion);
                almacen.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection storecollection = (X509Certificate2Collection)almacen.Certificates;
                Console.WriteLine("Nombre almacen: {0}", almacen.Name);
                Console.WriteLine("Ubicacion almacen: {0}", almacen.Location);
                foreach (X509Certificate2 x509 in storecollection)
                {
                    Console.WriteLine("Nombre certificado: {0}", x509.Subject);
                }
            }
            catch (CryptographicException cryexc)
            {
                Console.WriteLine("Error: MostrarInfoCertificado:{0}", cryexc.Message);
            }
        }
    }
}
    
answered by 17.05.2017 в 16:14