码迷,mamicode.com
首页 > Web开发 > 详细

使用curl,libcurl访问Https

时间:2017-05-20 01:03:48      阅读:3359      评论:0      收藏:0      [点我收藏+]

标签:手动   nal   ica   fail   文件下载   oid   ssh   更新   tab   

编译curl,libcurl

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

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 HTTPS参数含义

一、CURL_VERIFY_PEER

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

二、CURL_VERIFY_HOST

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

 

CURL基于WinSSL和OpenSSL访问HTTPS示例

一、忽略证书验证

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

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

二、HTTPS CURL示例

WinSSL:

int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, std::string &strErrorBuf, const char * pCaPath)  
{  
    CURLcode res;  
    CURL* curl = curl_easy_init();  
    char errbuf[CURL_ERROR_SIZE];
    if(NULL == curl)  
    {  
        return CURLE_FAILED_INIT;  
    }  
    if(m_bDebug)  
    {  
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
        curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
    }  
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    /* provide a buffer to store errors in */
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
    curl_easy_setopt(curl, CURLOPT_POST, 1);  
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT_NUM);  
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT_NUM);  
    /* set the error buffer as empty before performing a request */
    errbuf[0] = 0;
    res = curl_easy_perform(curl);  
    curl_easy_cleanup(curl);  
    strErrorBuf = errbuf;
    return res;  
}  

OpenSSL:

int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, std::string &strErrorBuf, const char * pCaPath)  
{  
    CURLcode res;  
    CURL* curl = curl_easy_init();  
    char errbuf[CURL_ERROR_SIZE];
    if(NULL == curl)  
    {  
        return CURLE_FAILED_INIT;  
    }  
    if(m_bDebug)  
    {  
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
        curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
    }  
    curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
    /* provide a buffer to store errors in */
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
    curl_easy_setopt(curl, CURLOPT_POST, 1);  
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
    if(pCaPath){
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);  
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
        curl_easy_setopt(curl, CURLOPT_CAINFO, 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");
    }
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, TIME_OUT_NUM);  
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIME_OUT_NUM);  
    /* set the error buffer as empty before performing a request */
    errbuf[0] = 0;
    res = curl_easy_perform(curl);  
    curl_easy_cleanup(curl);  
    strErrorBuf = errbuf;
    return res;  
}  

 

参考:

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

使用curl,libcurl访问Https

标签:手动   nal   ica   fail   文件下载   oid   ssh   更新   tab   

原文地址:http://www.cnblogs.com/chenyangchun/p/6868102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!