simplessl.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  * Shows HTTPS usage with client certs and optional ssl engine use.
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 
00028 #include <curl/curl.h>
00029 
00030 /* some requirements for this to work:
00031    1.   set pCertFile to the file with the client certificate
00032    2.   if the key is passphrase protected, set pPassphrase to the
00033         passphrase you use
00034    3.   if you are using a crypto engine:
00035    3.1. set a #define USE_ENGINE
00036    3.2. set pEngine to the name of the crypto engine you use
00037    3.3. set pKeyName to the key identifier you want to use
00038    4.   if you don't use a crypto engine:
00039    4.1. set pKeyName to the file name of your client key
00040    4.2. if the format of the key file is DER, set pKeyType to "DER"
00041 
00042    !! verify of the server certificate is not implemented here !!
00043 
00044    **** This example only works with libcurl 7.9.3 and later! ****
00045 
00046 */
00047 
00048 int main(void)
00049 {
00050   CURL *curl;
00051   CURLcode res;
00052   FILE *headerfile;
00053   const char *pPassphrase = NULL;
00054 
00055   static const char *pCertFile = "testcert.pem";
00056   static const char *pCACertFile="cacert.pem";
00057   static const char *pHeaderFile = "dumpit";
00058 
00059   const char *pKeyName;
00060   const char *pKeyType;
00061 
00062   const char *pEngine;
00063 
00064 #ifdef USE_ENGINE
00065   pKeyName  = "rsa_test";
00066   pKeyType  = "ENG";
00067   pEngine   = "chil";            /* for nChiper HSM... */
00068 #else
00069   pKeyName  = "testkey.pem";
00070   pKeyType  = "PEM";
00071   pEngine   = NULL;
00072 #endif
00073 
00074   headerfile = fopen(pHeaderFile, "wb");
00075 
00076   curl_global_init(CURL_GLOBAL_DEFAULT);
00077 
00078   curl = curl_easy_init();
00079   if(curl) {
00080     /* what call to write: */
00081     curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
00082     curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
00083 
00084     do { /* dummy loop, just to break out from */
00085       if(pEngine) {
00086         /* use crypto engine */
00087         if(curl_easy_setopt(curl, CURLOPT_SSLENGINE, pEngine) != CURLE_OK) {
00088           /* load the crypto engine */
00089           fprintf(stderr, "can't set crypto engine\n");
00090           break;
00091         }
00092         if(curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
00093           /* set the crypto engine as default */
00094           /* only needed for the first time you load
00095              a engine in a curl object... */
00096           fprintf(stderr, "can't set crypto engine as default\n");
00097           break;
00098         }
00099       }
00100       /* cert is stored PEM coded in file... */
00101       /* since PEM is default, we needn't set it for PEM */
00102       curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
00103 
00104       /* set the cert for client authentication */
00105       curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
00106 
00107       /* sorry, for engine we must set the passphrase
00108          (if the key has one...) */
00109       if(pPassphrase)
00110         curl_easy_setopt(curl, CURLOPT_KEYPASSWD, pPassphrase);
00111 
00112       /* if we use a key stored in a crypto engine,
00113          we must set the key type to "ENG" */
00114       curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, pKeyType);
00115 
00116       /* set the private key (file or ID in engine) */
00117       curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);
00118 
00119       /* set the file with the certs vaildating the server */
00120       curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);
00121 
00122       /* disconnect if we can't validate server's cert */
00123       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
00124 
00125       /* Perform the request, res will get the return code */
00126       res = curl_easy_perform(curl);
00127       /* Check for errors */
00128       if(res != CURLE_OK)
00129         fprintf(stderr, "curl_easy_perform() failed: %s\n",
00130                 curl_easy_strerror(res));
00131 
00132       /* we are done... */
00133     } while(0);
00134     /* always cleanup */
00135     curl_easy_cleanup(curl);
00136   }
00137 
00138   curl_global_cleanup();
00139 
00140   return 0;
00141 }


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