|
Forums -> Security Library Forum
Private key from .p12 file |
|
|
by Waqqas Farooq [mwaqqas at mail dot com] posted on 2004/05/31 |
|
I have a certificate as a .p12 file. A .p12 file contains:
• Certificate Identification
• Certificate Public Key
• Certificate Private Key
• Certification Authority Chain
I am having problems using the private key. I have come to know that the private key can be decrypted using the password provided.
When I run the sample WebClient provided along the library I get an exception:
"Exception occurred while connecting: System.ArgumentException: If a certificate is specified, it must have a private key."
How do i extract and use the private key?
Thanks in advance. |
by Moises [moises_branco at hotmail dot com] posted on 2004/05/31 |
|
I have the same problem with a p12 file when using the method Certificate.CreateFromPfxFile. The property Private Key of cert variable becomes null, and the instruction "options.Certificate = cert;" produces the same exception (If a certificate is specified, it must have a private key.)
Using the code below, wich retrieve certificate from machine's certificate store, the exception don't occurs:
"
CertificateStore store = new CertificateStore(CertificateStore.MyStore);
bool CertificadoEncontrado = false;
Certificate cert = null;
issuerName = "MY CA"; //CERTIFICATE AUTHORITY NAME
foreach (Certificate certtmp in store.EnumCertificates())
{
if ((certtmp.GetIssuerName() == issuerName) && certtmp.IsCurrent && (!CertificadoEncontrado)) {
System.Console.WriteLine("\nNome do certificado de cliente: " + certtmp.GetName().ToString());
// ASSOCIA UM CERTIFICADO DE CLIENTE AO OBJETO cert
cert = certtmp;
CertificadoEncontrado = true;
break;
}
}
if (CertificadoEncontrado)
{
System.Console.WriteLine("\nCertificado digital de cliente ENCONTRADO.\n");
options.Certificate = cert;
}
else
{
System.Console.WriteLine("\nO certificado digital de cliente não foi encontrado.\n");
}
"
The property Private Key becomes valid, but the property SupportsDigitalSignature still remains false (?). |
by Moises [moises_branco at hotmail dot com] posted on 2004/05/31 |
|
A correction: the exception is thrown when creating a new instance of SecureSocket (s = new SecureSocket...). |
by Waqqas Farooq [mwaqqas at mail dot com] posted on 2004/06/01 |
|
Thank you for your help I've the code running fine. I was unaware that there was a certificate chain and you had to go through the chain to find the private key. I used the following to find the certificate with private key.
CertificateStore store = CertificateStore.CreateFromPfxFile(filename, password, true);
Certificate cert = null;
foreach (Certificate certtmp in store.EnumCertificates()){
if (certtmp.HasPrivateKey())
{
cert = certtmp;
System.Console.WriteLine("Found private key.");
break;
}
}
Thank you. |
|
|