Advertisement
Guest User

curl memory leak pkcs12 client certificate

a guest
Jan 18th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. /* gcc -lcurl -Wall -g -O2 -o curlproblem curlproblem.c */
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include <curl/curl.h>
  9.  
  10. #define SERVERURL               "https://accounts.google.com"
  11. #define CLIENTCERTFILEPKCS12    "client.p12"
  12. #define PKCS12IMPORTPASSWORD    "foobar"
  13.  
  14. #define CLIENTCERTFILEPEM       "client.crt"
  15. #define CLIENTKEYFILEPEM        "client.key"
  16.  
  17. static void dieif(bool aCondition, const char *aMsg) {
  18.     if (aCondition) {
  19.         fprintf(stderr, "Die: %s\n", aMsg);
  20.         exit(EXIT_FAILURE);
  21.     }
  22. }
  23.  
  24. int main(int argc, char **argv) {
  25.     dieif(curl_global_init(CURL_GLOBAL_ALL | CURL_GLOBAL_SSL) != CURLE_OK, "curl_global_init");
  26.  
  27.     CURL *curlCtx = curl_easy_init();
  28.     dieif(!curlCtx, "curl_easy_init");
  29.  
  30.     dieif(curl_easy_setopt(curlCtx, CURLOPT_URL, SERVERURL) != CURLE_OK, "curl_easy_setopt 1");
  31.  
  32.     if ((argc == 2) && !strcmp(argv[1], "pem")) {
  33.         fprintf(stderr, "Using PEM\n");
  34.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLCERTTYPE, "PEM") != CURLE_OK, "curl_easy_setopt 2");
  35.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLCERT, CLIENTCERTFILEPEM) != CURLE_OK, "curl_easy_setopt 3");
  36.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLKEYTYPE, "PEM") != CURLE_OK, "curl_easy_setopt 4");
  37.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLKEY, CLIENTKEYFILEPEM) != CURLE_OK, "curl_easy_setopt 5");
  38.     } else {
  39.         fprintf(stderr, "Using PKCS#12\n");
  40.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLCERTTYPE, "P12") != CURLE_OK, "curl_easy_setopt 6");
  41.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLCERT, CLIENTCERTFILEPKCS12) != CURLE_OK, "curl_easy_setopt 7");
  42.         dieif(curl_easy_setopt(curlCtx, CURLOPT_SSLKEYPASSWD, PKCS12IMPORTPASSWORD) != CURLE_OK, "curl_easy_setopt 8");
  43.     }
  44.  
  45.     dieif(curl_easy_perform(curlCtx) != CURLE_OK, "curl_easy_perform");
  46.    
  47.     curl_easy_cleanup(curlCtx);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement