LibCurl supports URL transfer over HTTPS protocol. In this post we’ll use LibCurl to download a webpage over HTTPSprotocol. To get started, make sure that you have setup your system as outlined in one of my previous posts. Note that LibCurl comes with SSL libraries included and hence you need not to install any extra packages.

Getting started

Let’s first try to download a HTTPS webpage without SSL using following code:

 1 #include <stdio.h>
 2 #define CURL_STATICLIB
 3 #include <curl/curl.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     CURL *curl;
 8     CURLcode res;
 9 
10     curl = curl_easy_init();
11     if (curl)
12     {
13         curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
14         /* google.com is redirected, so we tell LibCurl to follow redirection */
15         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
16 
17         /* Perform the request, res will get the return code */
18         res = curl_easy_perform(curl);
19         /* Check for errors */
20         if(res != CURLE_OK)
21             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
22 
23         /* Always cleanup */
24         curl_easy_cleanup(curl);
25     }
26 
27     return 0;
28 }

When we compile and run the above program, we get following error:

curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates

Well we can force LibCurl not to verify the authencity of peer’s certificate by modifying the program as below:

 1 #include <stdio.h>
 2 #define CURL_STATICLIB
 3 #include <curl/curl.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     CURL *curl;
 8     CURLcode res;
 9 
10     curl = curl_easy_init();
11     if (curl)
12     {
13         curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
14         /* google.com is redirected, so we tell LibCurl to follow redirection */
15         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
16         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
17         /* Perform the request, res will get the return code */
18         res = curl_easy_perform(curl);
19         /* Check for errors */
20         if(res != CURLE_OK)
21             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
22 
23         /* Always cleanup */
24         curl_easy_cleanup(curl);
25     }
26 
27     return 0;
28 }

This will resolve the error we were getting earlier but this is not secure as we are bypassing the verification of SSLcertificte presented by peer.

LibCurl with SSL

Now let’s modify the code as below to enable SSL certificate verification by LibCurl:

 1 #include <stdio.h>
 2 #define CURL_STATICLIB
 3 #include <curl/curl.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     CURL *curl;
 8     CURLcode res;
 9 
10     curl = curl_easy_init();
11     if (curl)
12     {
13         curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
14         /* google.com is redirected, so we tell LibCurl to follow redirection */
15         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
16         /* SSL Options */
17         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
18         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
19         /* Provide CA Certs from http://curl.haxx.se/docs/caextract.html */
20         curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt");
21 
22         /* Perform the request, res will get the return code */
23         res = curl_easy_perform(curl);
24         /* Check for errors */
25         if(res != CURLE_OK)
26             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
27 
28         /* Always cleanup */
29         curl_easy_cleanup(curl);
30     }
31 
32     return 0;
33 }

LibCurl depends on ca-bundle.crt to verify server’s certificate. CA bundle extract is provided by LibCurl itself and you can download it from here. We need to place the ca-bundle.crt in same folder as our executable. Also notice that we have enabled CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST in lines 17 and 18. When CURLOPT_SSL_VERIFYPEER is enabled, LibCurl verifes if the ceriticate presented is authentiate or not. If the verification fails to prove that the certificate is authentic, the connection fails. Authenticating the certificate is not enough to be sure about the server. We also want to ensure that the server is the server we mean to be talking to. When CURLOPT_SSL_VERIFYHOST is enabled, LibCurl checks that the host name in the certificate is valid for the host name we’re connecting to. If LibCurl fails to verify this, the connection fails.

 

 

 

 

 

來源

https://pranavprakash.net/2014/09/27/using-libcurl-with-ssl/

創作者介紹

只是記事本

mybeauty 發表在 痞客邦 PIXNET 留言(0) 人氣()