1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, 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 http://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 /* Example using a "in core" PEM certificate to retrieve a https page.
23  * Written by Theo Borm
24  */
25 
26 /* on a netBSD system with OPENSSL& LIBCURL installed from
27  * pkgsrc (using default paths) this program can be compiled using:
28  * gcc -I/usr/pkg/include -L/usr/pkg/lib -lcurl -Wl,-R/usr/pkg/lib -lssl
29  * -lcrypto -lz -o curlcacerttest curlcacerttest.c
30  * on other operating systems you may want to change paths to headers
31  * and libraries
32 */
33 #include <openssl/ssl.h>
34 #include <curl/curl.h>
35 #include <stdio.h>
36 
writefunction(void * ptr,size_t size,size_t nmemb,void * stream)37 size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
38 {
39   fwrite(ptr,size,nmemb,stream);
40   return(nmemb*size);
41 }
42 
sslctx_function(CURL * curl,void * sslctx,void * parm)43 static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
44 {
45   X509_STORE * store;
46   X509 * cert=NULL;
47   BIO * bio;
48   char * mypem = /* www.cacert.org */
49     "-----BEGIN CERTIFICATE-----\n"\
50     "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
51     "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
52     "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
53     "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
54     "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
55     "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ\n"\
56     "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC\n"\
57     "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ\n"\
58     "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6\n"\
59     "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y\n"\
60     "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7\n"\
61     "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc\n"\
62     "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k\n"\
63     "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q\n"\
64     "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ\n"\
65     "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU\n"\
66     "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826\n"\
67     "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w\n"\
68     "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY\n"\
69     "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe\n"\
70     "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0\n"\
71     "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy\n"\
72     "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw\n"\
73     "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0\n"\
74     "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl\n"\
75     "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC\n"\
76     "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg\n"\
77     "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB\n"\
78     "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc\n"\
79     "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
80     "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
81     "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
82     "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
83     "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
84     "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
85     "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
86     "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
87     "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
88     "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
89     "-----END CERTIFICATE-----\n";
90   /* get a BIO */
91   bio=BIO_new_mem_buf(mypem, -1);
92   /* use it to read the PEM formatted certificate from memory into an X509
93    * structure that SSL can use
94    */
95   PEM_read_bio_X509(bio, &cert, 0, NULL);
96   if (cert == NULL)
97     printf("PEM_read_bio_X509 failed...\n");
98 
99   /* get a pointer to the X509 certificate store (which may be empty!) */
100   store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
101 
102   /* add our certificate to this store */
103   if (X509_STORE_add_cert(store, cert)==0)
104     printf("error adding certificate\n");
105 
106   /* decrease reference counts */
107   X509_free(cert);
108   BIO_free(bio);
109 
110   /* all set to go */
111   return CURLE_OK ;
112 }
113 
main(void)114 int main(void)
115 {
116   CURL * ch;
117   CURLcode rv;
118 
119   rv=curl_global_init(CURL_GLOBAL_ALL);
120   ch=curl_easy_init();
121   rv=curl_easy_setopt(ch,CURLOPT_VERBOSE, 0L);
122   rv=curl_easy_setopt(ch,CURLOPT_HEADER, 0L);
123   rv=curl_easy_setopt(ch,CURLOPT_NOPROGRESS, 1L);
124   rv=curl_easy_setopt(ch,CURLOPT_NOSIGNAL, 1L);
125   rv=curl_easy_setopt(ch,CURLOPT_WRITEFUNCTION, *writefunction);
126   rv=curl_easy_setopt(ch,CURLOPT_WRITEDATA, stdout);
127   rv=curl_easy_setopt(ch,CURLOPT_HEADERFUNCTION, *writefunction);
128   rv=curl_easy_setopt(ch,CURLOPT_HEADERDATA, stderr);
129   rv=curl_easy_setopt(ch,CURLOPT_SSLCERTTYPE,"PEM");
130   rv=curl_easy_setopt(ch,CURLOPT_SSL_VERIFYPEER,1L);
131   rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
132 
133   /* first try: retrieve page without cacerts' certificate -> will fail
134    */
135   rv=curl_easy_perform(ch);
136   if (rv==CURLE_OK)
137     printf("*** transfer succeeded ***\n");
138   else
139     printf("*** transfer failed ***\n");
140 
141   /* second try: retrieve page using cacerts' certificate -> will succeed
142    * load the certificate by installing a function doing the nescessary
143    * "modifications" to the SSL CONTEXT just before link init
144    */
145   rv=curl_easy_setopt(ch,CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
146   rv=curl_easy_perform(ch);
147   if (rv==CURLE_OK)
148     printf("*** transfer succeeded ***\n");
149   else
150     printf("*** transfer failed ***\n");
151 
152   curl_easy_cleanup(ch);
153   curl_global_cleanup();
154   return rv;
155 }
156