Print this article Edit this article

Building Certificates Into Code

Below is code that, instead of reading a certificate from a file, it is build directly into the C code:

BIO * bio;
X509_STORE * store;
X509 * x509;

static char certificate[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIFeTCCBGGgAwIBAgIEATEv0DANBgkqhkiG9w0BAQQFADBpMQswCQYDVQQGEwJV\n"
...
"v97cCh8PsPOehr0XJQ==\n"
"-----END CERTIFICATE-----\n";

/* Make string into BIO */
bio = BIO_new_mem_buf(certificate, sizeof(certificate)-1);
if (bio == NULL)
goto error;

/* Convert string to certificate */
x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
BIO_free(bio);
if (x509 == NULL)
goto error;

/* Add to certificate store */
store = SSL_CTX_get_cert_store(puidcl->ctx);
if (X509_STORE_add_cert(store, x509) == 0) {
X509_free(x509);
goto error;
}

Last Modified: Dec 19, 2016 11:12 am US/Eastern
Created: Feb 14, 2008 8:54 am US/Eastern by admin
JumpURL: