2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2005-08-24 13:07:27 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-12-30 19:36:05 -05:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-08-24 13:07:27 -04:00
|
|
|
*
|
2011-03-10 05:48:02 -05:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2015-06-18 04:17:02 -04:00
|
|
|
/* <DESC>
|
|
|
|
* CA cert in memory with OpenSSL to get a HTTPS page.
|
|
|
|
* </DESC>
|
2005-08-24 13:07:27 -04:00
|
|
|
*/
|
|
|
|
|
2017-12-19 02:17:39 -05:00
|
|
|
#include <openssl/err.h>
|
2005-08-24 13:07:27 -04:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
|
2005-08-24 13:07:27 -04:00
|
|
|
{
|
2017-12-19 02:17:39 -05:00
|
|
|
fwrite(ptr, size, nmemb, (FILE *)stream);
|
2016-02-11 03:42:38 -05:00
|
|
|
return (nmemb*size);
|
2005-08-24 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
|
2005-08-24 13:07:27 -04:00
|
|
|
{
|
2017-12-19 02:17:39 -05:00
|
|
|
CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
|
2018-12-30 19:36:05 -05:00
|
|
|
|
|
|
|
/** This example uses two (fake) certificates **/
|
|
|
|
static const char mypem[] =
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
|
|
"MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
|
|
|
|
"AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n"
|
|
|
|
"CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzEwOTM3MzdaMEIxEjAQ\n"
|
|
|
|
"BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQUND\n"
|
|
|
|
"VjELMAkGA1UEBhMCRVMwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCb\n"
|
|
|
|
"qau/YUqXry+XZpp0X9DZlv3P4uRm7x8fRzPCRKPfmt4ftVTdFXxpNRFvu8gMjmoY\n"
|
|
|
|
"HtiP2Ra8EEg2XPBjs5BaXCQ316PWywlxufEBcoSwfdtNgM3802/J+Nq2DoLSRYWo\n"
|
|
|
|
"G2ioPej0RGy9ocLLA76MPhMAhN9KSMDjIgro6TenGEyxCQ0jVn8ETdkXhBilyNpA\n"
|
|
|
|
"0KIV9VMJcRz/RROE5iZe+OCIHAr8Fraocwa48GOEAqDGWuzndN9wrqODJerWx5eH\n"
|
|
|
|
"k6fGioozl2A3ED6XPm4pFdahD9GILBKfb6qkxkLrQaLjlUPTAYVtjrs78y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KzgHVZhepA==\n"
|
2005-08-24 13:07:27 -04:00
|
|
|
"-----END CERTIFICATE-----\n";
|
2017-12-19 02:17:39 -05:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
|
|
|
|
X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
|
|
|
|
X509_INFO *itmp;
|
2019-05-20 05:50:23 -04:00
|
|
|
int i;
|
2018-12-30 19:36:05 -05:00
|
|
|
STACK_OF(X509_INFO) *inf;
|
|
|
|
(void)curl;
|
|
|
|
(void)parm;
|
2017-12-19 02:17:39 -05:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
if(!cts || !cbio) {
|
|
|
|
return rv;
|
2017-12-19 02:17:39 -05:00
|
|
|
}
|
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
|
2017-12-19 02:17:39 -05:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
if(!inf) {
|
|
|
|
BIO_free(cbio);
|
|
|
|
return rv;
|
|
|
|
}
|
2017-12-19 02:17:39 -05:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
if(itmp->crl) {
|
|
|
|
X509_STORE_add_crl(cts, itmp->crl);
|
2017-12-19 02:17:39 -05:00
|
|
|
}
|
|
|
|
}
|
2005-08-24 13:07:27 -04:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
sk_X509_INFO_pop_free(inf, X509_INFO_free);
|
|
|
|
BIO_free(cbio);
|
2014-05-09 07:33:21 -04:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
rv = CURLE_OK;
|
2017-12-19 02:17:39 -05:00
|
|
|
return rv;
|
2005-08-24 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2016-11-23 01:53:24 -05:00
|
|
|
CURL *ch;
|
2005-08-24 13:07:27 -04:00
|
|
|
CURLcode rv;
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
rv = curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
ch = curl_easy_init();
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
|
2005-08-24 13:07:27 -04:00
|
|
|
|
2018-12-30 19:36:05 -05:00
|
|
|
/* Turn off the default CA locations, otherwise libcurl will load CA
|
|
|
|
* certificates from the locations that were detected/specified at
|
|
|
|
* build-time
|
2017-12-19 02:17:39 -05:00
|
|
|
*/
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
|
|
|
|
|
|
|
|
/* first try: retrieve page without ca certificates -> should fail
|
|
|
|
* unless libcurl was built --with-ca-fallback enabled at build-time
|
2005-08-24 13:07:27 -04:00
|
|
|
*/
|
2017-09-09 17:09:06 -04:00
|
|
|
rv = curl_easy_perform(ch);
|
|
|
|
if(rv == CURLE_OK)
|
2005-08-24 13:07:27 -04:00
|
|
|
printf("*** transfer succeeded ***\n");
|
|
|
|
else
|
|
|
|
printf("*** transfer failed ***\n");
|
|
|
|
|
2017-12-19 02:17:39 -05:00
|
|
|
/* use a fresh connection (optional)
|
|
|
|
* this option seriously impacts performance of multiple transfers but
|
|
|
|
* it is necessary order to demonstrate this example. recall that the
|
|
|
|
* ssl ctx callback is only called _before_ an SSL connection is
|
|
|
|
* established, therefore it will not affect existing verified SSL
|
|
|
|
* connections already in the connection cache associated with this
|
|
|
|
* handle. normally you would set the ssl ctx function before making
|
|
|
|
* any transfers, and not use this option.
|
|
|
|
*/
|
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
|
|
|
|
|
2005-08-24 13:07:27 -04:00
|
|
|
/* second try: retrieve page using cacerts' certificate -> will succeed
|
2017-03-26 11:02:22 -04:00
|
|
|
* load the certificate by installing a function doing the necessary
|
2005-08-24 13:07:27 -04:00
|
|
|
* "modifications" to the SSL CONTEXT just before link init
|
|
|
|
*/
|
2017-09-09 17:09:06 -04:00
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
|
|
|
|
rv = curl_easy_perform(ch);
|
|
|
|
if(rv == CURLE_OK)
|
2005-08-24 13:07:27 -04:00
|
|
|
printf("*** transfer succeeded ***\n");
|
|
|
|
else
|
|
|
|
printf("*** transfer failed ***\n");
|
|
|
|
|
|
|
|
curl_easy_cleanup(ch);
|
|
|
|
curl_global_cleanup();
|
2007-07-12 17:11:10 -04:00
|
|
|
return rv;
|
2005-08-24 13:07:27 -04:00
|
|
|
}
|