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