2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2005-08-25 01:07:27 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-05-12 05:40:58 +08:00
|
|
|
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-08-25 01:07:27 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08: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-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08: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 16:17:02 +08:00
|
|
|
/* <DESC>
|
|
|
|
* CA cert in memory with OpenSSL to get a HTTPS page.
|
|
|
|
* </DESC>
|
2005-08-25 01:07:27 +08:00
|
|
|
*/
|
|
|
|
|
2017-12-19 15:17:39 +08:00
|
|
|
#include <openssl/err.h>
|
2005-08-25 01:07:27 +08:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-04-04 04:51:52 +08:00
|
|
|
size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
|
2005-08-25 01:07:27 +08:00
|
|
|
{
|
2017-12-19 15:17:39 +08:00
|
|
|
fwrite(ptr, size, nmemb, (FILE *)stream);
|
2016-02-11 16:42:38 +08:00
|
|
|
return (nmemb*size);
|
2005-08-25 01:07:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
|
2005-08-25 01:07:27 +08:00
|
|
|
{
|
2017-12-19 15:17:39 +08:00
|
|
|
CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
X509_STORE *store = NULL;
|
2017-09-10 05:09:06 +08:00
|
|
|
X509 *cert = NULL;
|
2017-12-19 15:17:39 +08:00
|
|
|
BIO *bio = NULL;
|
|
|
|
char *mypem =
|
|
|
|
/* CA for example.com. CN = DigiCert High Assurance EV Root CA */
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
|
|
"MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n"
|
|
|
|
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
|
|
|
|
"d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n"
|
|
|
|
"ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\n"
|
|
|
|
"MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n"
|
|
|
|
"LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\n"
|
|
|
|
"RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\n"
|
|
|
|
"+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\n"
|
|
|
|
"PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\n"
|
|
|
|
"xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\n"
|
|
|
|
"Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\n"
|
|
|
|
"hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\n"
|
|
|
|
"EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\n"
|
|
|
|
"MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\n"
|
|
|
|
"FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\n"
|
|
|
|
"nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\n"
|
|
|
|
"eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\n"
|
|
|
|
"hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\n"
|
|
|
|
"Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n"
|
|
|
|
"vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n"
|
|
|
|
"+OkuE6N36B9K\n"
|
2005-08-25 01:07:27 +08:00
|
|
|
"-----END CERTIFICATE-----\n";
|
2017-12-19 15:17:39 +08:00
|
|
|
|
|
|
|
/* clear the current thread's OpenSSL error queue */
|
|
|
|
ERR_clear_error();
|
|
|
|
|
2005-08-25 01:07:27 +08:00
|
|
|
/* get a BIO */
|
2017-09-10 05:09:06 +08:00
|
|
|
bio = BIO_new_mem_buf(mypem, -1);
|
2017-12-19 15:17:39 +08:00
|
|
|
if(!bio)
|
|
|
|
goto err;
|
|
|
|
|
2005-08-25 01:07:27 +08:00
|
|
|
/* use it to read the PEM formatted certificate from memory into an X509
|
|
|
|
* structure that SSL can use
|
|
|
|
*/
|
2017-12-19 15:17:39 +08:00
|
|
|
if(!PEM_read_bio_X509(bio, &cert, 0, NULL))
|
|
|
|
goto err;
|
2005-08-25 01:07:27 +08:00
|
|
|
|
|
|
|
/* get a pointer to the X509 certificate store (which may be empty!) */
|
2017-09-10 05:09:06 +08:00
|
|
|
store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
|
2017-12-19 15:17:39 +08:00
|
|
|
if(!store)
|
|
|
|
goto err;
|
2005-08-25 01:07:27 +08:00
|
|
|
|
|
|
|
/* add our certificate to this store */
|
2017-12-19 15:17:39 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CURLE_OK;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if(rv != CURLE_OK) {
|
|
|
|
char errbuf[256];
|
|
|
|
unsigned long error = ERR_peek_last_error();
|
|
|
|
|
|
|
|
fprintf(stderr, "error adding certificate\n");
|
|
|
|
if(error) {
|
2018-05-12 05:40:58 +08:00
|
|
|
ERR_error_string_n(error, errbuf, sizeof(errbuf));
|
2017-12-19 15:17:39 +08:00
|
|
|
fprintf(stderr, "%s\n", errbuf);
|
|
|
|
}
|
|
|
|
}
|
2005-08-25 01:07:27 +08:00
|
|
|
|
2014-05-09 19:33:21 +08:00
|
|
|
X509_free(cert);
|
|
|
|
BIO_free(bio);
|
2017-12-19 15:17:39 +08:00
|
|
|
ERR_clear_error();
|
2014-05-09 19:33:21 +08:00
|
|
|
|
2017-12-19 15:17:39 +08:00
|
|
|
return rv;
|
2005-08-25 01:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
CURL *ch;
|
2005-08-25 01:07:27 +08:00
|
|
|
CURLcode rv;
|
|
|
|
|
2017-09-10 05:09:06 +08: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-25 01:07:27 +08:00
|
|
|
|
2017-12-19 15:17:39 +08:00
|
|
|
/* turn off the default CA locations (optional)
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/* first try: retrieve page without ca certificates -> should fail
|
|
|
|
* unless libcurl was built --with-ca-fallback enabled at build-time
|
2005-08-25 01:07:27 +08:00
|
|
|
*/
|
2017-09-10 05:09:06 +08:00
|
|
|
rv = curl_easy_perform(ch);
|
|
|
|
if(rv == CURLE_OK)
|
2005-08-25 01:07:27 +08:00
|
|
|
printf("*** transfer succeeded ***\n");
|
|
|
|
else
|
|
|
|
printf("*** transfer failed ***\n");
|
|
|
|
|
2017-12-19 15:17:39 +08: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-25 01:07:27 +08:00
|
|
|
/* second try: retrieve page using cacerts' certificate -> will succeed
|
2017-03-26 23:02:22 +08:00
|
|
|
* load the certificate by installing a function doing the necessary
|
2005-08-25 01:07:27 +08:00
|
|
|
* "modifications" to the SSL CONTEXT just before link init
|
|
|
|
*/
|
2017-09-10 05:09:06 +08:00
|
|
|
rv = curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
|
|
|
|
rv = curl_easy_perform(ch);
|
|
|
|
if(rv == CURLE_OK)
|
2005-08-25 01:07:27 +08:00
|
|
|
printf("*** transfer succeeded ***\n");
|
|
|
|
else
|
|
|
|
printf("*** transfer failed ***\n");
|
|
|
|
|
|
|
|
curl_easy_cleanup(ch);
|
|
|
|
curl_global_cleanup();
|
2007-07-13 05:11:10 +08:00
|
|
|
return rv;
|
2005-08-25 01:07:27 +08:00
|
|
|
}
|