Advertisement
Guest User

Untitled

a guest
Aug 6th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <curl/curl.h>
  3.  
  4.  
  5. int main(int argc, char** argv){
  6.     if(argc<4){
  7.         printf("no args");
  8.         return 0;
  9.     }
  10.     int i;
  11.     CURL *curl;
  12.     CURLcode res;
  13.     FILE *headerfile;
  14.     const char *pPassphrase = "test13";
  15.  
  16.     char *url    = argv[1];
  17.     char *pCertFile    = argv[2]; //"testcert.pem";
  18.     char *pCACertFile  = argv[4]; //"cacert.pem";
  19.  
  20.     const char *pKeyName;
  21.     const char *pKeyType;
  22.  
  23.     printf("%s : %s | %s\n", url, pCertFile, pCACertFile);
  24.  
  25.     const char *pEngine;
  26.  
  27. #ifdef USE_ENGINE
  28.     pKeyName    = "rsa_test";
  29.     pKeyType    = "ENG";
  30.     pEngine     = "chil";                        /* for nChiper HSM... */
  31. #else
  32.     pKeyName    = argv[3];
  33.     pKeyType    = "PEM";
  34.     pEngine     = NULL;
  35. #endif
  36.  
  37.  
  38.     headerfile = fopen("dumpit", "w");
  39.  
  40.     curl_global_init(CURL_GLOBAL_DEFAULT);
  41.  
  42.     curl = curl_easy_init();
  43.     if(curl) {
  44.  
  45.  
  46.         /* what call to write: */
  47.         curl_easy_setopt(curl, CURLOPT_URL, url);
  48.         curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
  49.  
  50.         for(i = 0; i < 1; i++) /* single-iteration loop, just to break out from */
  51.         {
  52.             if (pEngine)                         /* use crypto engine */
  53.             {
  54.                 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
  55.                 {                                         /* load the crypto engine */
  56.                     fprintf(stderr,"can't set crypto engine\n");
  57.                     break;
  58.                 }
  59.                 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK)
  60.                 { /* set the crypto engine as default */
  61.                     /* only needed for the first time you load
  62.                          a engine in a curl object... */
  63.                     fprintf(stderr,"can't set crypto engine as default\n");
  64.                     break;
  65.                 }
  66.             }
  67.             /* cert is stored PEM coded in file... */
  68.             /* since PEM is default, we needn't set it for PEM */
  69.             curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  70.  
  71.             /* set the cert for client authentication */
  72.             curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
  73.  
  74.             /* sorry, for engine we must set the passphrase
  75.                  (if the key has one...) */
  76.             if (pPassphrase)
  77.                 curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);
  78.  
  79.             /* if we use a key stored in a crypto engine,
  80.                  we must set the key type to "ENG" */
  81.             curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
  82.  
  83.             /* set the private key (file or ID in engine) */
  84.             curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
  85.  
  86.             /* set the file with the certs vaildating the server */
  87.             curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
  88.  
  89.             /* disconnect if we can't validate server's cert */
  90.             curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER, 1);
  91.  
  92.             /* Perform the request, res will get the return code */
  93.             res = curl_easy_perform(curl);
  94.             /* Check for errors */
  95.             if(res != CURLE_OK)
  96.                 fprintf(stderr, "curl_easy_perform() failed: %s\n",
  97.                                 curl_easy_strerror(res));
  98.  
  99.             /* we are done... */
  100.         }
  101.         /* always cleanup */
  102.         curl_easy_cleanup(curl);
  103.     }
  104.  
  105.     curl_global_cleanup();
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement