1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * CA cert in memory with OpenSSL to get an HTTPS page.
26 * </DESC>
27 */
28
29 #include <openssl/err.h>
30 #include <openssl/ssl.h>
31 #include <curl/curl.h>
32 #include <stdio.h>
33
writefunction(void * ptr,size_t size,size_t nmemb,void * stream)34 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
35 {
36 fwrite(ptr, size, nmemb, (FILE *)stream);
37 return (nmemb*size);
38 }
39
sslctx_function(CURL * curl,void * sslctx,void * parm)40 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
41 {
42 CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
43
44 #if defined(__GNUC__) || defined(__clang__)
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Woverlength-strings"
47 #endif
48
49 /** This example uses two (fake) certificates **/
50 static const char mypem[] =
51 "-----BEGIN CERTIFICATE-----\n"
52 "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
53 "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n"
54 "CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzE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fShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
87 "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n"
88 "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
89 "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
90 "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
91 "-----END CERTIFICATE-----\n";
92
93 #if defined(__GNUC__) || defined(__clang__)
94 #pragma GCC diagnostic pop
95 #endif
96
97 BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
98 X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
99 int i;
100 STACK_OF(X509_INFO) *inf;
101 (void)curl;
102 (void)parm;
103
104 if(!cts || !cbio) {
105 return rv;
106 }
107
108 inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
109
110 if(!inf) {
111 BIO_free(cbio);
112 return rv;
113 }
114
115 for(i = 0; i < sk_X509_INFO_num(inf); i++) {
116 X509_INFO *itmp = sk_X509_INFO_value(inf, i);
117 if(itmp->x509) {
118 X509_STORE_add_cert(cts, itmp->x509);
119 }
120 if(itmp->crl) {
121 X509_STORE_add_crl(cts, itmp->crl);
122 }
123 }
124
125 sk_X509_INFO_pop_free(inf, X509_INFO_free);
126 BIO_free(cbio);
127
128 rv = CURLE_OK;
129 return rv;
130 }
131
main(void)132 int main(void)
133 {
134 CURL *ch;
135 CURLcode rv;
136
137 curl_global_init(CURL_GLOBAL_ALL);
138 ch = curl_easy_init();
139 curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
140 curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
141 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
142 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
143 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
144 curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
145 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
146 curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
147 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
148 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
149 curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
150
151 /* Turn off the default CA locations, otherwise libcurl loads CA
152 * certificates from the locations that were detected/specified at
153 * build-time
154 */
155 curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
156 curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
157
158 /* first try: retrieve page without ca certificates -> should fail
159 * unless libcurl was built --with-ca-fallback enabled at build-time
160 */
161 rv = curl_easy_perform(ch);
162 if(rv == CURLE_OK)
163 printf("*** transfer succeeded ***\n");
164 else
165 printf("*** transfer failed ***\n");
166
167 /* use a fresh connection (optional) this option seriously impacts
168 * performance of multiple transfers but it is necessary order to
169 * demonstrate this example. recall that the ssl ctx callback is only called
170 * _before_ an SSL connection is established, therefore it does not affect
171 * existing verified SSL connections already in the connection cache
172 * associated with this handle. normally you would set the ssl ctx function
173 * before making any transfers, and not use this option.
174 */
175 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
176
177 /* second try: retrieve page using cacerts' certificate -> succeeds to load
178 * the certificate by installing a function doing the necessary
179 * "modifications" to the SSL CONTEXT just before link init
180 */
181 curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
182 rv = curl_easy_perform(ch);
183 if(rv == CURLE_OK)
184 printf("*** transfer succeeded ***\n");
185 else
186 printf("*** transfer failed ***\n");
187
188 curl_easy_cleanup(ch);
189 curl_global_cleanup();
190 return (int)rv;
191 }
192