Conversion of X509 structure to the string with PEM encoded certificate:
char *X509_to_PEM(X509 *cert) {
BIO *bio = NULL;
char *pem = NULL;
if (NULL == cert) {
return NULL;
}
bio = BIO_new(BIO_s_mem());
if (NULL == bio) {
return NULL;
}
if (0 == PEM_write_bio_X509(bio, cert)) {
BIO_free(bio);
return NULL;
}
pem = (char *) malloc(bio->num_write + 1);
if (NULL == pem) {
BIO_free(bio);
return NULL;
}
memset(pem, 0, bio->num_write + 1);
BIO_read(bio, pem, bio->num_write);
BIO_free(bio);
return pem;
}
Conversion of PEM encoded certificate to the X509 structure:
X509 *PEM_to_X509(char *pem) {
X509 *cert = NULL;
BIO *bio = NULL;
if (NULL == pem) {
return NULL;
}
bio = BIO_new_mem_buf(pem, strlen(pem));
if (NULL == bio) {
return NULL;
}
cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
BIO_free(bio);
return cert;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…