C # Digital Certificate A3 CPF Token

2

I have to put a digital certificate of type CPF A3 token with password in an HttpWebRequest, so I was trying to use the X509Certificate2, as follows:

    private X509Certificate2 GetCert(string CertFile, string CertPass)
    {
        FileStream fs = new FileStream(CertFile, FileMode.Open);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        X509Certificate2 cert = new X509Certificate2(buffer, CertPass);
        fs.Close();
        fs.Dispose();
        return cert;
    }

    //chamo assim
    GetCert("C:\certificado.cer", password);

But I was informed that this was to read certificates on my computer, so I tried the code below

        X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        //my.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        my.Open(OpenFlags.ReadOnly);


        // Find the certificate we'll use to sign            
        RSACryptoServiceProvider csp = null;
        foreach (X509Certificate2 cert in my.Certificates)
        {
            var x509 = cert;
            byte[] rawData = x509.RawData;
            Console.WriteLine("Content Type: {0}", X509Certificate2.GetCertContentType(rawData));
            Console.WriteLine("Serial Number: {0}", x509.SerialNumber);
            Console.WriteLine("Friendly Name: {0}", x509.FriendlyName);
            //continue

Just that this code is working OK only for CNPJ certificates, when I use CPF it asks me to pry a certified pen drive ... One more strange thing is: he is reading all the certificates that one day I already I installed it on my computer ...

    
asked by anonymous 30.06.2016 / 22:14

1 answer

3

The A3 certificate offers extra protection, as the private key is inaccessible except for the hardware (Smart Card). You can not export it. What you export is just the public key. As for the password, it only allows access to the primary key within the Smart Card, because the password you type encrypts / decrypts the primary key as a security increment. Some references:

01.07.2016 / 04:06