Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / cacertinmem.c
blob352105aa5d7318d937c95ee093e5bde2a1998d9f
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: cacertinmem.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * Example using a "in core" PEM certificate to retrieve a https page.
11 * Written by Theo Borm
14 /* on a netBSD system with OPENSSL& LIBCURL installed from
15 * pkgsrc (using default paths) this program can be compiled using:
16 * gcc -I/usr/pkg/include -L/usr/pkg/lib -lcurl -Wl,-R/usr/pkg/lib -lssl
17 * -lcrypto -lz -o curlcacerttest curlcacerttest.c
18 * on other operating systems you may want to change paths to headers
19 * and libraries
21 #include <openssl/ssl.h>
22 #include <curl/curl.h>
23 #include <stdio.h>
25 size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
27 fwrite(ptr,size,nmemb,stream);
28 return(nmemb*size);
31 static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
33 X509_STORE * store;
34 X509 * cert=NULL;
35 BIO * bio;
36 char * mypem = /* www.cacert.org */
37 "-----BEGIN CERTIFICATE-----\n"\
38 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
39 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
40 "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
41 "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
42 "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
43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wj7UfdJUzYF\n"\
73 "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
74 "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
75 "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
76 "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
77 "-----END CERTIFICATE-----\n";
78 /* get a BIO */
79 bio=BIO_new_mem_buf(mypem, -1);
80 /* use it to read the PEM formatted certificate from memory into an X509
81 * structure that SSL can use
83 PEM_read_bio_X509(bio, &cert, 0, NULL);
84 if (cert == NULL)
85 printf("PEM_read_bio_X509 failed...\n");
87 /* get a pointer to the X509 certificate store (which may be empty!) */
88 store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
90 /* add our certificate to this store */
91 if (X509_STORE_add_cert(store, cert)==0)
92 printf("error adding certificate\n");
94 /* all set to go */
95 return CURLE_OK ;
98 int main(void)
100 CURL * ch;
101 CURLcode rv;
103 rv=curl_global_init(CURL_GLOBAL_ALL);
104 ch=curl_easy_init();
105 rv=curl_easy_setopt(ch,CURLOPT_VERBOSE, 0L);
106 rv=curl_easy_setopt(ch,CURLOPT_HEADER, 0L);
107 rv=curl_easy_setopt(ch,CURLOPT_NOPROGRESS, 1L);
108 rv=curl_easy_setopt(ch,CURLOPT_NOSIGNAL, 1L);
109 rv=curl_easy_setopt(ch,CURLOPT_WRITEFUNCTION, *writefunction);
110 rv=curl_easy_setopt(ch,CURLOPT_WRITEDATA, stdout);
111 rv=curl_easy_setopt(ch,CURLOPT_HEADERFUNCTION, *writefunction);
112 rv=curl_easy_setopt(ch,CURLOPT_WRITEHEADER, stderr);
113 rv=curl_easy_setopt(ch,CURLOPT_SSLCERTTYPE,"PEM");
114 rv=curl_easy_setopt(ch,CURLOPT_SSL_VERIFYPEER,1L);
115 rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.cacert.org/");
117 /* first try: retrieve page without cacerts' certificate -> will fail
119 rv=curl_easy_perform(ch);
120 if (rv==CURLE_OK)
121 printf("*** transfer succeeded ***\n");
122 else
123 printf("*** transfer failed ***\n");
125 /* second try: retrieve page using cacerts' certificate -> will succeed
126 * load the certificate by installing a function doing the nescessary
127 * "modifications" to the SSL CONTEXT just before link init
129 rv=curl_easy_setopt(ch,CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
130 rv=curl_easy_perform(ch);
131 if (rv==CURLE_OK)
132 printf("*** transfer succeeded ***\n");
133 else
134 printf("*** transfer failed ***\n");
136 curl_easy_cleanup(ch);
137 curl_global_cleanup();
138 return rv;