Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to add a certificate from buffer in OpenSSL
X509 *cert;
char *zCert;
BIO *mem;
zCert = // get your certificate text buffer here (C string with certificate in PEM format)
mem = BIO_new(BIO_s_mem());
BIO_puts(mem, zCert);
cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
free(zCert);
BIO_free(mem);
// set certificate to sslCtx
X509_STORE_add_cert(SSL_CTX_get_cert_store(sslCtx), cert);
@skabbes

This comment has been minimized.

Copy link

skabbes commented Apr 8, 2014

for anyone else who comes looking, if you have more than one pem file in zCert, you'll need to loop.

while (cert = PEM_read_bio_X509(mem, NULL, 0, NULL)) {
    X509_STORE_add_cert(SSL_CTX_get_cert_store(sslCtx), cert);
}
@emka002

This comment has been minimized.

Copy link

emka002 commented Apr 29, 2015

Great! Many thanks, thats exactly what i'm looking for.

@LesnyRumcajs

This comment has been minimized.

Copy link

LesnyRumcajs commented Jan 12, 2016

I wonder how many other occult functions does OpenSSL have - the official documentation doesn't say a word about it even though it's extremely useful. Thanks, my code shrinked by many lines!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.