xref: /aosp_15_r20/external/curl/docs/examples/cacertinmem.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker /* <DESC>
25*6236dae4SAndroid Build Coastguard Worker  * CA cert in memory with OpenSSL to get an HTTPS page.
26*6236dae4SAndroid Build Coastguard Worker  * </DESC>
27*6236dae4SAndroid Build Coastguard Worker  */
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #include <openssl/err.h>
30*6236dae4SAndroid Build Coastguard Worker #include <openssl/ssl.h>
31*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
32*6236dae4SAndroid Build Coastguard Worker #include <stdio.h>
33*6236dae4SAndroid Build Coastguard Worker 
writefunction(void * ptr,size_t size,size_t nmemb,void * stream)34*6236dae4SAndroid Build Coastguard Worker static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
35*6236dae4SAndroid Build Coastguard Worker {
36*6236dae4SAndroid Build Coastguard Worker   fwrite(ptr, size, nmemb, (FILE *)stream);
37*6236dae4SAndroid Build Coastguard Worker   return (nmemb*size);
38*6236dae4SAndroid Build Coastguard Worker }
39*6236dae4SAndroid Build Coastguard Worker 
sslctx_function(CURL * curl,void * sslctx,void * parm)40*6236dae4SAndroid Build Coastguard Worker static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
41*6236dae4SAndroid Build Coastguard Worker {
42*6236dae4SAndroid Build Coastguard Worker   CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
43*6236dae4SAndroid Build Coastguard Worker 
44*6236dae4SAndroid Build Coastguard Worker   /** This example uses two (fake) certificates **/
45*6236dae4SAndroid Build Coastguard Worker   static const char mypem[] =
46*6236dae4SAndroid Build Coastguard Worker     "-----BEGIN CERTIFICATE-----\n"
47*6236dae4SAndroid Build Coastguard Worker     "MIIH0zCCBbugAwIBAgIIXsO3pkN/pOAwDQYJKoZIhvcNAQEFBQAwQjESMBAGA1UE\n"
48*6236dae4SAndroid Build Coastguard Worker     "AwwJQUNDVlJBSVoxMRAwDgYDVQQLDAdQS0lBQ0NWMQ0wCwYDVQQKDARBQ0NWMQsw\n"
49*6236dae4SAndroid Build Coastguard Worker     "CQYDVQQGEwJFUzAeFw0xMTA1MDUwOTM3MzdaFw0zMDEyMzEwOTM3MzdaMEIxEjAQ\n"
50*6236dae4SAndroid Build Coastguard Worker     "BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQUND\n"
51*6236dae4SAndroid Build Coastguard Worker     "VjELMAkGA1UEBhMCRVMwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCb\n"
52*6236dae4SAndroid Build Coastguard Worker     "qau/YUqXry+XZpp0X9DZlv3P4uRm7x8fRzPCRKPfmt4ftVTdFXxpNRFvu8gMjmoY\n"
53*6236dae4SAndroid Build Coastguard Worker     "HtiP2Ra8EEg2XPBjs5BaXCQ316PWywlxufEBcoSwfdtNgM3802/J+Nq2DoLSRYWo\n"
54*6236dae4SAndroid Build Coastguard Worker     "G2ioPej0RGy9ocLLA76MPhMAhN9KSMDjIgro6TenGEyxCQ0jVn8ETdkXhBilyNpA\n"
55*6236dae4SAndroid Build Coastguard Worker     "0KIV9VMJcRz/RROE5iZe+OCIHAr8Fraocwa48GOEAqDGWuzndN9wrqODJerWx5eH\n"
56*6236dae4SAndroid Build Coastguard Worker     "k6fGioozl2A3ED6XPm4pFdahD9GILBKfb6qkxkLrQaLjlUPTAYVtjrs78yM2x/47\n"
57*6236dae4SAndroid Build Coastguard Worker     "JyCpZET/LtZ1qmxNYEAZSUNUY9rizLpm5U9EelvZaoErQNV/+QEnWCzI7UiRfD+m\n"
58*6236dae4SAndroid Build Coastguard Worker     "AM/EKXMRNt6GGT6d7hmKG9Ww7Y49nCrADdg9ZuM8Db3VlFzi4qc1GwQA9j9ajepD\n"
59*6236dae4SAndroid Build Coastguard Worker     "vV+JHanBsMyZ4k0ACtrJJ1vnE5Bc5PUzolVt3OAJTS+xJlsndQAJxGJ3KQhfnlms\n"
60*6236dae4SAndroid Build Coastguard Worker     "tn6tn1QwIgPBHnFk/vk4CpYY3QIUrCPLBhwepH2NDd4nQeit2hW3sCPdK6jT2iWH\n"
61*6236dae4SAndroid Build Coastguard Worker     "7ehVRE2I9DZ+hJp4rPcOVkkO1jMl1oRQQmwgEh0q1b688nCBpHBgvgW1m54ERL5h\n"
62*6236dae4SAndroid Build Coastguard Worker     "I6zppSSMEYCUWqKiuUnSwdzRp+0xESyeGabu4VXhwOrPDYTkF7eifKXeVSUG7szA\n"
63*6236dae4SAndroid Build Coastguard Worker     "h1xA2syVP1XgNce4hL60Xc16gwFy7ofmXx2utYXGJt/mwZrpHgJHnyqobalbz+xF\n"
64*6236dae4SAndroid Build Coastguard Worker     "d3+YJ5oyXSrjhO7FmGYvliAd3djDJ9ew+f7Zfc3Qn48LFFhRny+Lwzgt3uiP1o2H\n"
65*6236dae4SAndroid Build Coastguard Worker     "pPVWQxaZLPSkVrQ0uGE3ycJYgBugl6H8WY3pEfbRD0tVNEYqi4Y7\n"
66*6236dae4SAndroid Build Coastguard Worker     "-----END CERTIFICATE-----\n"
67*6236dae4SAndroid Build Coastguard Worker     "-----BEGIN CERTIFICATE-----\n"
68*6236dae4SAndroid Build Coastguard Worker     "MIIFtTCCA52gAwIBAgIIYY3HhjsBggUwDQYJKoZIhvcNAQEFBQAwRDEWMBQGA1UE\n"
69*6236dae4SAndroid Build Coastguard Worker     "AwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZFRElDT00x\n"
70*6236dae4SAndroid Build Coastguard Worker     "CzAJBgNVBAYTAkVTMB4XDTA4MDQxODE2MjQyMloXDTI4MDQxMzE2MjQyMlowRDEW\n"
71*6236dae4SAndroid Build Coastguard Worker     "MBQGA1UEAwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZF\n"
72*6236dae4SAndroid Build Coastguard Worker     "RElDT00xCzAJBgNVBAYTAkVTMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC\n"
73*6236dae4SAndroid Build Coastguard Worker     "AgEA/5KV4WgGdrQsyFhIyv2AVClVYyT/kGWbEHV7w2rbYgIB8hiGtXxaOLHkWLn7\n"
74*6236dae4SAndroid Build Coastguard Worker     "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n"
75*6236dae4SAndroid Build Coastguard Worker     "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n"
76*6236dae4SAndroid Build Coastguard Worker     "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n"
77*6236dae4SAndroid Build Coastguard Worker     "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n"
78*6236dae4SAndroid Build Coastguard Worker     "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n"
79*6236dae4SAndroid Build Coastguard Worker     "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n"
80*6236dae4SAndroid Build Coastguard Worker     "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n"
81*6236dae4SAndroid Build Coastguard Worker     "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
82*6236dae4SAndroid Build Coastguard Worker     "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+qQQEQIHriy1tvuWacN\n"
83*6236dae4SAndroid Build Coastguard Worker     "GHk0vFQYXlPKNFHtRQrmjseCNj6nOGOpMCwXEGCSn1WHElkQwg9naRHMTh5+Spqt\n"
84*6236dae4SAndroid Build Coastguard Worker     "r0CodaxWkHS4oJyleW/c6RrIaQXpuvoDs3zk4E7Czp3otkYNbn5XOmeUwssfnHdK\n"
85*6236dae4SAndroid Build Coastguard Worker     "Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
86*6236dae4SAndroid Build Coastguard Worker     "-----END CERTIFICATE-----\n";
87*6236dae4SAndroid Build Coastguard Worker 
88*6236dae4SAndroid Build Coastguard Worker   BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
89*6236dae4SAndroid Build Coastguard Worker   X509_STORE  *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
90*6236dae4SAndroid Build Coastguard Worker   int i;
91*6236dae4SAndroid Build Coastguard Worker   STACK_OF(X509_INFO) *inf;
92*6236dae4SAndroid Build Coastguard Worker   (void)curl;
93*6236dae4SAndroid Build Coastguard Worker   (void)parm;
94*6236dae4SAndroid Build Coastguard Worker 
95*6236dae4SAndroid Build Coastguard Worker   if(!cts || !cbio) {
96*6236dae4SAndroid Build Coastguard Worker     return rv;
97*6236dae4SAndroid Build Coastguard Worker   }
98*6236dae4SAndroid Build Coastguard Worker 
99*6236dae4SAndroid Build Coastguard Worker   inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
100*6236dae4SAndroid Build Coastguard Worker 
101*6236dae4SAndroid Build Coastguard Worker   if(!inf) {
102*6236dae4SAndroid Build Coastguard Worker     BIO_free(cbio);
103*6236dae4SAndroid Build Coastguard Worker     return rv;
104*6236dae4SAndroid Build Coastguard Worker   }
105*6236dae4SAndroid Build Coastguard Worker 
106*6236dae4SAndroid Build Coastguard Worker   for(i = 0; i < sk_X509_INFO_num(inf); i++) {
107*6236dae4SAndroid Build Coastguard Worker     X509_INFO *itmp = sk_X509_INFO_value(inf, i);
108*6236dae4SAndroid Build Coastguard Worker     if(itmp->x509) {
109*6236dae4SAndroid Build Coastguard Worker       X509_STORE_add_cert(cts, itmp->x509);
110*6236dae4SAndroid Build Coastguard Worker     }
111*6236dae4SAndroid Build Coastguard Worker     if(itmp->crl) {
112*6236dae4SAndroid Build Coastguard Worker       X509_STORE_add_crl(cts, itmp->crl);
113*6236dae4SAndroid Build Coastguard Worker     }
114*6236dae4SAndroid Build Coastguard Worker   }
115*6236dae4SAndroid Build Coastguard Worker 
116*6236dae4SAndroid Build Coastguard Worker   sk_X509_INFO_pop_free(inf, X509_INFO_free);
117*6236dae4SAndroid Build Coastguard Worker   BIO_free(cbio);
118*6236dae4SAndroid Build Coastguard Worker 
119*6236dae4SAndroid Build Coastguard Worker   rv = CURLE_OK;
120*6236dae4SAndroid Build Coastguard Worker   return rv;
121*6236dae4SAndroid Build Coastguard Worker }
122*6236dae4SAndroid Build Coastguard Worker 
main(void)123*6236dae4SAndroid Build Coastguard Worker int main(void)
124*6236dae4SAndroid Build Coastguard Worker {
125*6236dae4SAndroid Build Coastguard Worker   CURL *ch;
126*6236dae4SAndroid Build Coastguard Worker   CURLcode rv;
127*6236dae4SAndroid Build Coastguard Worker 
128*6236dae4SAndroid Build Coastguard Worker   curl_global_init(CURL_GLOBAL_ALL);
129*6236dae4SAndroid Build Coastguard Worker   ch = curl_easy_init();
130*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
131*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
132*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
133*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
134*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
135*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
136*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
137*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
138*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
139*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
140*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
141*6236dae4SAndroid Build Coastguard Worker 
142*6236dae4SAndroid Build Coastguard Worker   /* Turn off the default CA locations, otherwise libcurl loads CA
143*6236dae4SAndroid Build Coastguard Worker    * certificates from the locations that were detected/specified at
144*6236dae4SAndroid Build Coastguard Worker    * build-time
145*6236dae4SAndroid Build Coastguard Worker    */
146*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
147*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
148*6236dae4SAndroid Build Coastguard Worker 
149*6236dae4SAndroid Build Coastguard Worker   /* first try: retrieve page without ca certificates -> should fail
150*6236dae4SAndroid Build Coastguard Worker    * unless libcurl was built --with-ca-fallback enabled at build-time
151*6236dae4SAndroid Build Coastguard Worker    */
152*6236dae4SAndroid Build Coastguard Worker   rv = curl_easy_perform(ch);
153*6236dae4SAndroid Build Coastguard Worker   if(rv == CURLE_OK)
154*6236dae4SAndroid Build Coastguard Worker     printf("*** transfer succeeded ***\n");
155*6236dae4SAndroid Build Coastguard Worker   else
156*6236dae4SAndroid Build Coastguard Worker     printf("*** transfer failed ***\n");
157*6236dae4SAndroid Build Coastguard Worker 
158*6236dae4SAndroid Build Coastguard Worker   /* use a fresh connection (optional) this option seriously impacts
159*6236dae4SAndroid Build Coastguard Worker    * performance of multiple transfers but it is necessary order to
160*6236dae4SAndroid Build Coastguard Worker    * demonstrate this example. recall that the ssl ctx callback is only called
161*6236dae4SAndroid Build Coastguard Worker    * _before_ an SSL connection is established, therefore it does not affect
162*6236dae4SAndroid Build Coastguard Worker    * existing verified SSL connections already in the connection cache
163*6236dae4SAndroid Build Coastguard Worker    * associated with this handle. normally you would set the ssl ctx function
164*6236dae4SAndroid Build Coastguard Worker    * before making any transfers, and not use this option.
165*6236dae4SAndroid Build Coastguard Worker    */
166*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
167*6236dae4SAndroid Build Coastguard Worker 
168*6236dae4SAndroid Build Coastguard Worker   /* second try: retrieve page using cacerts' certificate -> succeeds to load
169*6236dae4SAndroid Build Coastguard Worker    * the certificate by installing a function doing the necessary
170*6236dae4SAndroid Build Coastguard Worker    * "modifications" to the SSL CONTEXT just before link init
171*6236dae4SAndroid Build Coastguard Worker    */
172*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
173*6236dae4SAndroid Build Coastguard Worker   rv = curl_easy_perform(ch);
174*6236dae4SAndroid Build Coastguard Worker   if(rv == CURLE_OK)
175*6236dae4SAndroid Build Coastguard Worker     printf("*** transfer succeeded ***\n");
176*6236dae4SAndroid Build Coastguard Worker   else
177*6236dae4SAndroid Build Coastguard Worker     printf("*** transfer failed ***\n");
178*6236dae4SAndroid Build Coastguard Worker 
179*6236dae4SAndroid Build Coastguard Worker   curl_easy_cleanup(ch);
180*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
181*6236dae4SAndroid Build Coastguard Worker   return rv;
182*6236dae4SAndroid Build Coastguard Worker }
183