aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDaVieS <davies@npulse.net>2018-12-31 01:36:05 +0100
committerDaniel Stenberg <daniel@haxx.se>2019-02-27 09:33:03 +0100
commit0f6c6efd814a46a51981a29a5cb64c7a0858d5eb (patch)
tree8efe83c37eb00ea58560d9a8b3383efc995d5dd8 /docs
parent62a2534e4c31109b618a633db0970553c70d8061 (diff)
cacertinmem.c: use multiple certificates for loading CA-chain
Closes #3421
Diffstat (limited to 'docs')
-rw-r--r--docs/examples/cacertinmem.c148
1 files changed, 73 insertions, 75 deletions
diff --git a/docs/examples/cacertinmem.c b/docs/examples/cacertinmem.c
index 6f4c61cbf..9e15eb79d 100644
--- a/docs/examples/cacertinmem.c
+++ b/docs/examples/cacertinmem.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -29,7 +29,7 @@
#include <curl/curl.h>
#include <stdio.h>
-size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
+static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
return (nmemb*size);
@@ -38,88 +38,86 @@ size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
{
CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
- X509_STORE *store = NULL;
- X509 *cert = NULL;
- BIO *bio = NULL;
- char *mypem =
- /* CA for example.com. CN = DigiCert High Assurance EV Root CA */
+
+ /** This example uses two (fake) certificates **/
+ static const char mypem[] =
+ "-----BEGIN CERTIFICATE-----\n"
+ "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcN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 "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n"
+ "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n"
+ "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n"
+ "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n"
+ "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n"
+ "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n"
+ "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n"
+ "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
+ "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n"
+ "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
+ "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
+ "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
"-----END CERTIFICATE-----\n";
- /* clear the current thread's OpenSSL error queue */
- ERR_clear_error();
+ BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
+ X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
+ X509_INFO *itmp;
+ int i, count = 0;
+ STACK_OF(X509_INFO) *inf;
+ (void)curl;
+ (void)parm;
- /* get a BIO */
- bio = BIO_new_mem_buf(mypem, -1);
- if(!bio)
- goto err;
-
- /* use it to read the PEM formatted certificate from memory into an X509
- * structure that SSL can use
- */
- if(!PEM_read_bio_X509(bio, &cert, 0, NULL))
- goto err;
-
- /* get a pointer to the X509 certificate store (which may be empty!) */
- store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
- if(!store)
- goto err;
-
- /* add our certificate to this store */
- if(!X509_STORE_add_cert(store, cert)) {
- unsigned long error = ERR_peek_last_error();
-
- /* Ignore error X509_R_CERT_ALREADY_IN_HASH_TABLE which means the
- * certificate is already in the store. That could happen if
- * libcurl already loaded the certificate from a ca cert bundle
- * set at libcurl build-time or runtime.
- */
- if(ERR_GET_LIB(error) != ERR_LIB_X509 ||
- ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
- goto err;
-
- ERR_clear_error();
+ if(!cts || !cbio) {
+ return rv;
}
- rv = CURLE_OK;
+ inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
-err:
- if(rv != CURLE_OK) {
- char errbuf[256];
- unsigned long error = ERR_peek_last_error();
+ if(!inf) {
+ BIO_free(cbio);
+ return rv;
+ }
- fprintf(stderr, "error adding certificate\n");
- if(error) {
- ERR_error_string_n(error, errbuf, sizeof(errbuf));
- fprintf(stderr, "%s\n", errbuf);
+ for(i = 0; i < sk_X509_INFO_num(inf); i++) {
+ itmp = sk_X509_INFO_value(inf, i);
+ if(itmp->x509) {
+ X509_STORE_add_cert(cts, itmp->x509);
+ count++;
+ }
+ if(itmp->crl) {
+ X509_STORE_add_crl(cts, itmp->crl);
+ count++;
}
}
- X509_free(cert);
- BIO_free(bio);
- ERR_clear_error();
+ sk_X509_INFO_pop_free(inf, X509_INFO_free);
+ BIO_free(cbio);
+ rv = CURLE_OK;
return rv;
}
@@ -142,9 +140,9 @@ int main(void)
rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
rv = curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
- /* turn off the default CA locations (optional)
- * otherwise libcurl will load CA certificates from the locations that
- * were detected/specified at build-time
+ /* Turn off the default CA locations, otherwise libcurl will load CA
+ * certificates from the locations that were detected/specified at
+ * build-time
*/
rv = curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
rv = curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);