1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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.haxx.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 ***************************************************************************/
22 /* <DESC>
23 * CA cert in memory with OpenSSL to get a HTTPS page.
24 * </DESC>
25 */
26
27 #include <openssl/err.h>
28 #include <openssl/ssl.h>
29 #include <curl/curl.h>
30 #include <stdio.h>
31
writefunction(void * ptr,size_t size,size_t nmemb,void * stream)32 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
33 {
34 fwrite(ptr, size, nmemb, (FILE *)stream);
35 return (nmemb*size);
36 }
37
sslctx_function(CURL * curl,void * sslctx,void * parm)38 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
39 {
40 CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
41
42 /** This example uses two (fake) certificates **/
43 static const char mypem[] =
44 "-----BEGIN CERTIFICATE-----\n"
45 "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
46 "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n"
47 "CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzEwOTM3MzdaMEIxEjAQ\n"
48 "BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQU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2JwOV5vx+qQQEQIHriy1tvuWacN\n"
81 "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
82 "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
83 "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
84 "-----END CERTIFICATE-----\n";
85
86 BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
87 X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
88 X509_INFO *itmp;
89 int i, count = 0;
90 STACK_OF(X509_INFO) *inf;
91 (void)curl;
92 (void)parm;
93
94 if(!cts || !cbio) {
95 return rv;
96 }
97
98 inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
99
100 if(!inf) {
101 BIO_free(cbio);
102 return rv;
103 }
104
105 for(i = 0; i < sk_X509_INFO_num(inf); i++) {
106 itmp = sk_X509_INFO_value(inf, i);
107 if(itmp->x509) {
108 X509_STORE_add_cert(cts, itmp->x509);
109 count++;
110 }
111 if(itmp->crl) {
112 X509_STORE_add_crl(cts, itmp->crl);
113 count++;
114 }
115 }
116
117 sk_X509_INFO_pop_free(inf, X509_INFO_free);
118 BIO_free(cbio);
119
120 rv = CURLE_OK;
121 return rv;
122 }
123
main(void)124 int main(void)
125 {
126 CURL *ch;
127 CURLcode rv;
128
129 rv = curl_global_init(CURL_GLOBAL_ALL);
130 ch = curl_easy_init();
131 rv = curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
132 rv = curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
133 rv = curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
134 rv = curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
135 rv = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
136 rv = curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
137 rv = curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
138 rv = curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
139 rv = curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
140 rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
141 rv = curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
142
143 /* Turn off the default CA locations, otherwise libcurl will load CA
144 * certificates from the locations that were detected/specified at
145 * build-time
146 */
147 rv = curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
148 rv = curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
149
150 /* first try: retrieve page without ca certificates -> should fail
151 * unless libcurl was built --with-ca-fallback enabled at build-time
152 */
153 rv = curl_easy_perform(ch);
154 if(rv == CURLE_OK)
155 printf("*** transfer succeeded ***\n");
156 else
157 printf("*** transfer failed ***\n");
158
159 /* use a fresh connection (optional)
160 * this option seriously impacts performance of multiple transfers but
161 * it is necessary order to demonstrate this example. recall that the
162 * ssl ctx callback is only called _before_ an SSL connection is
163 * established, therefore it will not affect existing verified SSL
164 * connections already in the connection cache associated with this
165 * handle. normally you would set the ssl ctx function before making
166 * any transfers, and not use this option.
167 */
168 rv = curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
169
170 /* second try: retrieve page using cacerts' certificate -> will succeed
171 * load the certificate by installing a function doing the necessary
172 * "modifications" to the SSL CONTEXT just before link init
173 */
174 rv = curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
175 rv = curl_easy_perform(ch);
176 if(rv == CURLE_OK)
177 printf("*** transfer succeeded ***\n");
178 else
179 printf("*** transfer failed ***\n");
180
181 curl_easy_cleanup(ch);
182 curl_global_cleanup();
183 return rv;
184 }
185