cacertinmem.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * CA cert in memory with OpenSSL to get a HTTPS page.
00024  * </DESC>
00025  */
00026 
00027 #include <openssl/ssl.h>
00028 #include <curl/curl.h>
00029 #include <stdio.h>
00030 
00031 size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
00032 {
00033   fwrite(ptr, size, nmemb, stream);
00034   return (nmemb*size);
00035 }
00036 
00037 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
00038 {
00039   X509_STORE *store;
00040   X509 *cert=NULL;
00041   BIO *bio;
00042   char *mypem = /* www.cacert.org */
00043     "-----BEGIN CERTIFICATE-----\n"\
00044     "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
00045     "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
00046     "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
00047     "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
00048     "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
00049     "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ\n"\
00050     "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC\n"\
00051     "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ\n"\
00052     "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6\n"\
00053     "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y\n"\
00054     "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7\n"\
00055     "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc\n"\
00056     "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k\n"\
00057     "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q\n"\
00058     "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ\n"\
00059     "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU\n"\
00060     "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826\n"\
00061     "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w\n"\
00062     "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY\n"\
00063     "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe\n"\
00064     "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0\n"\
00065     "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy\n"\
00066     "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw\n"\
00067     "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0\n"\
00068     "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl\n"\
00069     "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC\n"\
00070     "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg\n"\
00071     "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB\n"\
00072     "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc\n"\
00073     "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
00074     "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
00075     "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
00076     "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
00077     "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
00078     "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
00079     "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
00080     "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
00081     "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
00082     "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
00083     "-----END CERTIFICATE-----\n";
00084   /* get a BIO */
00085   bio=BIO_new_mem_buf(mypem, -1);
00086   /* use it to read the PEM formatted certificate from memory into an X509
00087    * structure that SSL can use
00088    */
00089   PEM_read_bio_X509(bio, &cert, 0, NULL);
00090   if(cert == NULL)
00091     printf("PEM_read_bio_X509 failed...\n");
00092 
00093   /* get a pointer to the X509 certificate store (which may be empty!) */
00094   store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
00095 
00096   /* add our certificate to this store */
00097   if(X509_STORE_add_cert(store, cert)==0)
00098     printf("error adding certificate\n");
00099 
00100   /* decrease reference counts */
00101   X509_free(cert);
00102   BIO_free(bio);
00103 
00104   /* all set to go */
00105   return CURLE_OK;
00106 }
00107 
00108 int main(void)
00109 {
00110   CURL *ch;
00111   CURLcode rv;
00112 
00113   rv=curl_global_init(CURL_GLOBAL_ALL);
00114   ch=curl_easy_init();
00115   rv=curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
00116   rv=curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
00117   rv=curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
00118   rv=curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
00119   rv=curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
00120   rv=curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
00121   rv=curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
00122   rv=curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
00123   rv=curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
00124   rv=curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
00125   rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
00126 
00127   /* first try: retrieve page without cacerts' certificate -> will fail
00128    */
00129   rv=curl_easy_perform(ch);
00130   if(rv==CURLE_OK)
00131     printf("*** transfer succeeded ***\n");
00132   else
00133     printf("*** transfer failed ***\n");
00134 
00135   /* second try: retrieve page using cacerts' certificate -> will succeed
00136    * load the certificate by installing a function doing the nescessary
00137    * "modifications" to the SSL CONTEXT just before link init
00138    */
00139   rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
00140   rv=curl_easy_perform(ch);
00141   if(rv==CURLE_OK)
00142     printf("*** transfer succeeded ***\n");
00143   else
00144     printf("*** transfer failed ***\n");
00145 
00146   curl_easy_cleanup(ch);
00147   curl_global_cleanup();
00148   return rv;
00149 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:01