Skip to content

Instantly share code, notes, and snippets.

@herpiko
Created January 11, 2018 07:43
Show Gist options
  • Select an option

  • Save herpiko/c8ec5ba198748185f534f3e01712e2bf to your computer and use it in GitHub Desktop.

Select an option

Save herpiko/c8ec5ba198748185f534f3e01712e2bf to your computer and use it in GitHub Desktop.
Mutual auth with libcurl
const char *certFile = "C:/mutual/cert.pem";
const char *keyFile = "C:/mutual/key.pem";
const char *cacertsFile = "C:/mutual/ca.pem";
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 2L);
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8000");
// Mutual auth
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSLCERT, "C:/mutual/cert.pem");;
curl_easy_setopt(curl, CURLOPT_SSLKEY, "C:/mutual/key.pem");
curl_easy_setopt(curl, CURLOPT_CAINFO, "C:/mutual/ca.pem");
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2 |
CURL_SSLVERSION_MAX_DEFAULT);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
std::cout << "curl OK" << std::endl;
}
if(res == CURLE_OK) {
std::cout << "curl OK1" << std::endl;
}
// always cleanup
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment