Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save X509 certificate to a file

I am working on a HTTPS client and I managed to establish a secure connection and get the X509 certificate using:
X509 *cert = SSL_get_certificate(ssl); (ssl is SSL*).

How do I save the certificate to a file? Also, I need to get "Subject DN" and "Issuer DN" fields from the certificate.

like image 846
Andrei Sfrent Avatar asked May 22 '11 13:05

Andrei Sfrent


People also ask

How do I export my x509 certificate?

In the details pane, right-click the certificate you want to export, select All Tasks, and then click Export… This action will start the Certificate Export Wizard. On the Welcome to the Certificate Export Wizard page, click Next. On the Export Private Key page, click Yes, Export the Private Key, and then click Next.

Where are x509 certs stored?

Certificates stores are kept in the system registry under the keys HKEY_LOCAL_MACHINE\Software\Microsoft\SystemCertificates and HKEY_CURRENT_USER\Software\Microsoft\SystemCertificates. Each user has a MY certificate store which contains his/her personal certificates.

What is x509 certificate file?

An X. 509 certificate is a digital certificate that uses the widely accepted international X. 509 public key infrastructure (PKI) standard to verify that a public key belongs to the user, computer or service identity contained within the certificate.


2 Answers

-- How do I save the certificate to a file?

#include <openssl/pem.h>
int PEM_write_X509(FILE *fp, X509 *x);

-- Also, I need to get "Subject DN" and "Issuer DN" fields from the certificate.

#include <openssl/x509.h>
X509_NAME *     X509_get_issuer_name(X509 *a);
X509_NAME *     X509_get_subject_name(X509 *a); 
like image 146
Jumbogram Avatar answered Sep 27 '22 17:09

Jumbogram


To encode the certificate into a file you can use this OpenSSL function:

int i2d_X509_fp(X509 *x, FILE *fp);

It encodes the X509 structure pointed by x into file using the DER encoding. More details on the OpenSSL API reference.

like image 43
Jcs Avatar answered Sep 27 '22 18:09

Jcs