Created
January 11, 2018 07:43
-
-
Save herpiko/c8ec5ba198748185f534f3e01712e2bf to your computer and use it in GitHub Desktop.
Mutual auth with libcurl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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