使用curl,libcurl访问Https

https://www.cnblogs.com/chenyangchun/p/6868102.html

 

编译curl,libcurl

下载curl源码(git clone https://github.com/curl/curl),在目录curl\winbuild\BUILD.WINDOWS.txt文件中,详细介绍了使用nmake编译windows下curl及libcurl库的相关命令,摘录如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

nmake /f Makefile.vc mode=<static or dll> <options>

 

where <options> is one or many of:

  VC=<6,7,8,9,10,11,12,14>     - VC versions

  WITH_DEVEL=<path>            - Paths for the development files (SSL, zlib, etc.)

                                 Defaults to sibbling directory deps: ../deps

                                 Libraries can be fetched at http://windows.php.net/downloads/php-sdk/deps/

                                 Uncompress them into the deps folder.

  WITH_SSL=<dll or static>     - Enable OpenSSL support, DLL or static

  WITH_MBEDTLS=<dll or static> - Enable mbedTLS support, DLL or static

  WITH_CARES=<dll or static>   - Enable c-ares support, DLL or static

  WITH_ZLIB=<dll or static>    - Enable zlib support, DLL or static

  WITH_SSH2=<dll or static>    - Enable libSSH2 support, DLL or static

  ENABLE_SSPI=<yes or no>      - Enable SSPI support, defaults to yes

  ENABLE_IPV6=<yes or no>      - Enable IPv6, defaults to yes

  ENABLE_IDN=<yes or no>       - Enable use of Windows IDN APIs, defaults to yes

                                 Requires Windows Vista or later, or installation from:

                                 https://www.microsoft.com/downloads/details.aspx?FamilyID=AD6158D7-DDBA-416A-9109-07607425A815

  ENABLE_WINSSL=<yes or no>    - Enable native Windows SSL support, defaults to yes

  GEN_PDB=<yes or no>          - Generate Program Database (debug symbols for release build)

  DEBUG=<yes or no>            - Debug builds

  MACHINE=<x86 or x64>         - Target architecture (default is x86)

  由编译命令可知,编译curl主要有两种ssl模式,默认是基于windows的winssl编译,另一种是基于openssl加密库。

一、curl+winssl

命令:

nmake /f Makefile.vc mode=dll vc=10 

这时默认使用SSPI、IDN、WINSSL等技术,编译后使用windows系统自带的CA数字证书文件、ssl加密库winssl(Schannel and Secure Transport),这种编译方式有很多优点,一是因为使用windows自带的加密库,没有跨平台等考虑因素,性能自然是最优的;二是不用引入第三方库openssl,也不需要显示设置https CA数字证书文件或者打包根证书到软件中。但是缺点也是很明显的,因为windows有很多系统版本,不同版本的ssl有较大区别,早期windows上的ssl安全性能没那么高;最严重的一个问题是,windows xp及以下系统在国内用户量还是很大的,而windows xp不支持SNI技术,如果服务器使用了SNI技术,而且同一个域名配置了多个证书,有可能导致返回证书错误,导致https访问失败。

SNI:Server Name Indication,是为了应对虚拟服务器技术的兴起而产生的,就是允许同一台服务器布置多个域名,在发起https请求的时候,会将请求的域名加到https请求头中,服务端收到请求后,根据请求头中的域名返回对应的根证书。

二、curl+openssl

命令:

nmake /f Makefile.vc mode=dll VC=10 WITH_DEVEL=OpenSLL编译目录 ENABLE_SSPI=no ENABLE_WINSSL=no

这种编译方式,首先得下载OpenSSL源码或者已经编译好的OpenSSL库,放到指定目录并设置到参数WITH_DEVEL参数中,具体的编译方式可参考http://www.cnblogs.com/openiris/p/3812443.html

基于OpenSSL编译的curl和libcurl,一大优点是使用的较新的SSL加密算法, 安全性较高,而且不需要考虑不同的操作系统SSL库不同导致的各种问题;缺点就是需要单独引入OpenSSL库,需要手动从Mozilla导出根证书,编译到OpenSSL或者打包到软件中,在curl中显示设置加载。 curl官网提供CA数字证书文件下载,地址是https://curl.haxx.se/ca/cacert.pem,更新地址是https://curl.haxx.se/docs/caextract.html 。

 远程更新CA数字证书命令(证书发生改变了才会下载):

curl --remote-name --time-cond cacert.pem https://curl.haxx.se/ca/cacert.pem

 

查看 CURL和LIBCURL版本/SSL/支持协议/特性

使用curl -V可以查看编译好的libcurl库支持的功能,以及支持的ssl库:

libcurl+winssl编译:

libcurl+openssl编译:

CURL HTTPS参数含义

一、CURL_VERIFY_PEER

该参数含义是验证HTTPS请求对象的合法性,就是用第三方证书机构颁发的CA数字证书来解密服务端返回的证书,来验证其合法性。可在编译时就将CA数字证书编译进去,也可以通过参数CURLOPT_CAINFO 或者CURLOPT_CAPATH设置根证书。默认值为1。

二、CURL_VERIFY_HOST

该参数主要用于https请求时返回的证书是否与请求的域名相符合,避免被中间着篡改证书文件。默认值为2。

 

LIBCURL基于WinSSL和OpenSSL访问HTTPS示例

一、忽略证书验证

如果不想验证PEER和HOST的安全性,可以通过设置

1

2

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);//忽略证书检查

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

二、LibCurl HTTPS 示例

WinSSL:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

void winssl_Https() 

    CURLcode res; 

    CURL* curl = curl_easy_init();     

  if(NULL == curl) 

    

        return CURLE_FAILED_INIT; 

    

    //...

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);//winssl编译时使用windows自带的根证书

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);

   //...

  curl_easy_cleanup(curl);

}

 OpenSSL:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

void openssl_https(const char * pCaPath) 

    CURLcode res; 

    CURL* curl = curl_easy_init();    

  if(NULL == curl) 

    

        return CURLE_FAILED_INIT; 

    

  //...

  if(pCaPath){

        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);//openssl编译时使用curl官网或者firefox导出的第三方根证书文件 

        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);

        curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);/*pCaPath为证书路径 */

   else{     

     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);

     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);

     curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem");//cacert.pem为curl官网下载的根证书文件

  }

  //...

  curl_easy_cleanup(curl);

}

  

参考资料:

a) https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html

b) https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html

c) https://curl.haxx.se/docs/sslcerts.html

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在c++中使用curl进行https访问时,需要设置证书才能正常进行访问。这里提供一种基于libcurl的方法来设置证书。 1. 首先,需要下载证书文件。可以从CA机构或者自己的服务器上获取证书文件。假设证书文件名为"ca.pem",并且放在当前目录下。 2. 使用curl_easy_setopt()函数设置证书。代码如下: ```c++ // 初始化curl CURL *curl = curl_easy_init(); if (curl) { // 设置请求的url curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com"); // 设置证书 curl_easy_setopt(curl, CURLOPT_CAINFO, "ca.pem"); // 执行请求 CURLcode res = curl_easy_perform(curl); // 检查请求是否成功 if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } // 清理curl curl_easy_cleanup(curl); } ``` 在上面的代码中,使用curl_easy_setopt()函数来设置证书,其中CURLOPT_CAINFO选项指定了证书的文件名。如果证书放在其他目录下,需要指定完整的路径。 3. 编译运行代码。在编译时需要链接curl库。例如,在Linux下可以使用以下命令编译代码: ``` g++ main.cpp -lcurl -o main ``` 在Windows下可以使用以下命令: ``` g++ main.cpp -lcurl -lssl -lcrypto -lz -o main.exe ``` 注意,Windows下需要链接的库要比Linux下多一些。 以上就是使用curl设置证书的方法。需要注意的是,证书文件的格式必须是PEM格式,否则curl将无法识别。如果证书文件不是PEM格式,可以使用openssl将其转换为PEM格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值