1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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  ***************************************************************************/
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     "BgNVBAMMCUFDQ1ZSQUlaMTEQMA4GA1UECwwHUEtJQUNDVjENMAsGA1UECgwEQUND\n"
49     "VjELMAkGA1UEBhMCRVMwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCb\n"
50     "qau/YUqXry+XZpp0X9DZlv3P4uRm7x8fRzPCRKPfmt4ftVTdFXxpNRFvu8gMjmoY\n"
51     "HtiP2Ra8EEg2XPBjs5BaXCQ316PWywlxufEBcoSwfdtNgM3802/J+Nq2DoLSRYWo\n"
52     "G2ioPej0RGy9ocLLA76MPhMAhN9KSMDjIgro6TenGEyxCQ0jVn8ETdkXhBilyNpA\n"
53     "0KIV9VMJcRz/RROE5iZe+OCIHAr8Fraocwa48GOEAqDGWuzndN9wrqODJerWx5eH\n"
54     "k6fGioozl2A3ED6XPm4pFdahD9GILBKfb6qkxkLrQaLjlUPTAYVtjrs78yM2x/47\n"
55     "JyCpZET/LtZ1qmxNYEAZSUNUY9rizLpm5U9EelvZaoErQNV/+QEnWCzI7UiRfD+m\n"
56     "AM/EKXMRNt6GGT6d7hmKG9Ww7Y49nCrADdg9ZuM8Db3VlFzi4qc1GwQA9j9ajepD\n"
57     "vV+JHanBsMyZ4k0ACtrJJ1vnE5Bc5PUzolVt3OAJTS+xJlsndQAJxGJ3KQhfnlms\n"
58     "tn6tn1QwIgPBHnFk/vk4CpYY3QIUrCPLBhwepH2NDd4nQeit2hW3sCPdK6jT2iWH\n"
59     "7ehVRE2I9DZ+hJp4rPcOVkkO1jMl1oRQQmwgEh0q1b688nCBpHBgvgW1m54ERL5h\n"
60     "I6zppSSMEYCUWqKiuUnSwdzRp+0xESyeGabu4VXhwOrPDYTkF7eifKXeVSUG7szA\n"
61     "h1xA2syVP1XgNce4hL60Xc16gwFy7ofmXx2utYXGJt/mwZrpHgJHnyqobalbz+xF\n"
62     "d3+YJ5oyXSrjhO7FmGYvliAd3djDJ9ew+f7Zfc3Qn48LFFhRny+Lwzgt3uiP1o2H\n"
63     "pPVWQxaZLPSkVrQ0uGE3ycJYgBugl6H8WY3pEfbRD0tVNEYqi4Y7\n"
64     "-----END CERTIFICATE-----\n"
65     "-----BEGIN CERTIFICATE-----\n"
66     "MIIFtTCCA52gAwIBAgIIYY3HhjsBggUwDQYJKoZIhvcNAQEFBQAwRDEWMBQGA1UE\n"
67     "AwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZFRElDT00x\n"
68     "CzAJBgNVBAYTAkVTMB4XDTA4MDQxODE2MjQyMloXDTI4MDQxMzE2MjQyMlowRDEW\n"
69     "MBQGA1UEAwwNQUNFRElDT00gUm9vdDEMMAoGA1UECwwDUEtJMQ8wDQYDVQQKDAZF\n"
70     "RElDT00xCzAJBgNVBAYTAkVTMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC\n"
71     "AgEA/5KV4WgGdrQsyFhIyv2AVClVYyT/kGWbEHV7w2rbYgIB8hiGtXxaOLHkWLn7\n"
72     "09gtn70yN78sFW2+tfQh0hOR2QetAQXW8713zl9CgQr5auODAKgrLlUTY4HKRxx7\n"
73     "XBZXehuDYAQ6PmXDzQHe3qTWDLqO3tkE7hdWIpuPY/1NFgu3e3eM+SW10W2ZEi5P\n"
74     "gvoFNTPhNahXwOf9jU8/kzJPeGYDdwdY6ZXIfj7QeQCM8htRM5u8lOk6e25SLTKe\n"
75     "I6RF+7YuE7CLGLHdztUdp0J/Vb77W7tH1PwkzQSulgUV1qzOMPPKC8W64iLgpq0i\n"
76     "5ALudBF/TP94HTXa5gI06xgSYXcGCRZj6hitoocf8seACQl1ThCojz2GuHURwCRi\n"
77     "ipZ7SkXp7FnFvmuD5uHorLUwHv4FB4D54SMNUI8FmP8sX+g7tq3PgbUhh8oIKiMn\n"
78     "MCArz+2UW6yyetLHKKGKC5tNSixthT8Jcjxn4tncB7rrZXtaAWPWkFtPF2Y9fwsZ\n"
79     "o5NjEFIqnxQWWOLcpfShFosOkYuByptZ+thrkQdlVV9SH686+5DdaaVbnG0OLLb6\n"
80     "zqylfDJKZ0DcMDQj3dcEI2bw/FWAp/tmGYI1Z2JwOV5vx+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   int i;
89   STACK_OF(X509_INFO) *inf;
90   (void)curl;
91   (void)parm;
92 
93   if(!cts || !cbio) {
94     return rv;
95   }
96 
97   inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
98 
99   if(!inf) {
100     BIO_free(cbio);
101     return rv;
102   }
103 
104   for(i = 0; i < sk_X509_INFO_num(inf); i++) {
105     X509_INFO *itmp = sk_X509_INFO_value(inf, i);
106     if(itmp->x509) {
107       X509_STORE_add_cert(cts, itmp->x509);
108     }
109     if(itmp->crl) {
110       X509_STORE_add_crl(cts, itmp->crl);
111     }
112   }
113 
114   sk_X509_INFO_pop_free(inf, X509_INFO_free);
115   BIO_free(cbio);
116 
117   rv = CURLE_OK;
118   return rv;
119 }
120 
main(void)121 int main(void)
122 {
123   CURL *ch;
124   CURLcode rv;
125 
126   curl_global_init(CURL_GLOBAL_ALL);
127   ch = curl_easy_init();
128   curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
129   curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
130   curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
131   curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
132   curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
133   curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
134   curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
135   curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
136   curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
137   curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
138   curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
139 
140   /* Turn off the default CA locations, otherwise libcurl will load CA
141    * certificates from the locations that were detected/specified at
142    * build-time
143    */
144   curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
145   curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
146 
147   /* first try: retrieve page without ca certificates -> should fail
148    * unless libcurl was built --with-ca-fallback enabled at build-time
149    */
150   rv = curl_easy_perform(ch);
151   if(rv == CURLE_OK)
152     printf("*** transfer succeeded ***\n");
153   else
154     printf("*** transfer failed ***\n");
155 
156   /* use a fresh connection (optional)
157    * this option seriously impacts performance of multiple transfers but
158    * it is necessary order to demonstrate this example. recall that the
159    * ssl ctx callback is only called _before_ an SSL connection is
160    * established, therefore it will not affect existing verified SSL
161    * connections already in the connection cache associated with this
162    * handle. normally you would set the ssl ctx function before making
163    * any transfers, and not use this option.
164    */
165   curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
166 
167   /* second try: retrieve page using cacerts' certificate -> will succeed
168    * load the certificate by installing a function doing the necessary
169    * "modifications" to the SSL CONTEXT just before link init
170    */
171   curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
172   rv = curl_easy_perform(ch);
173   if(rv == CURLE_OK)
174     printf("*** transfer succeeded ***\n");
175   else
176     printf("*** transfer failed ***\n");
177 
178   curl_easy_cleanup(ch);
179   curl_global_cleanup();
180   return rv;
181 }
182