使用curl,libcurl访问Https
编译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和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的安全性,可以通过设置
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);//忽略证书检查
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
二、LibCurl HTTPS 示例
WinSSL:
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:
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
使用curl,libcurl访问Https的更多相关文章
- ACME[free https] Linux中使用curl命令访问https站点4种常见错误和解决方法
free https certification generator https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 每一种客户 ...
- curl 无法访问 https 协议
转自http://blog.mutoo.im/2013/12/curl-could-not-communicate-with-https-sites.html mac升级为10.10以后,homebr ...
- [PHP] curl访问https与CA证书问题
CA证书,用来在调用HTTPS资源的时候,验证对方网站是否是CA颁布的证书,而不是自己随便生成的 curl命令1.需要下载CA证书 文件地址是 http://curl.haxx.se/ca/cacer ...
- php中curl不支持https的解决办法
在php程序中使用curl去访问https站点时,报错:Protocol https not supported or disabled in libcurl 该错误信息表示php当时编译时使用的cu ...
- libcurl 支持openssl 但不能访问https
重新编译了libcurl 去访问https 地址还是不能访问 从网上找到了解决方案: curl有两种方式使用https : 1. 设定为不验证证书和HOST code = curl_easy_seto ...
- python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
python 调用curl访问一个网页时,出现error: curl: (1) Protocol "'https" not supported or disabled in lib ...
- curl+个人证书(又叫客户端证书)访问https站点
摘自http://blog.csdn.net/chary8088/article/details/22990741 curl+个人证书(又叫客户端证书)访问https站点 目前,大公司的OA管理系统( ...
- centos7内核升级及curl访问https证书过期处理
centos7内核升级及curl访问https证书过期处理 先看下当前系统的linux内核版本 uname -r 3.10.0-229.el7.x86_64 升级步骤 1.rpm --import h ...
- 使用curl访问https
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具.然而在使用cr ...
随机推荐
- Customer segmentation – LifeCycle Grids, CLV and CAC with R(转)
We studied a very powerful approach for customer segmentation in the previous post, which is based o ...
- Interactive pivot tables with R(转)
I love interactive pivot tables. That is the number one reason why I keep using spreadsheet software ...
- SQL写操作 设置内容 (数组转字符串)
SQL写操作 设置内容 (数组转字符串) SQL set内容 SQL操作数组转字符串 SQL写操作 set内容 (数组转字符串) [ 封装方法 ] function getSqlSet( $data ...
- Zepto源码分析-callbacks模块
// Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT licens ...
- 将 FFmpeg 移植到 Android平台 (完整版)
首先需要去FFmpeg的官网http://www.ffmpeg.org/去下载FFmpeg的源码,目前的版本号为FFmpeg3.3(Hilbert). 下载的文件为压缩包,解压后得到ffmpeg-3. ...
- 源代码安装软件-MySQL
一.源码安装 1.经典的源代码安装三步曲: 1.编译前的配置 ./configure 2.编译 make 3.安装 make install 2.源代码软件安装步骤: 1.下载软件包 2.校验软件包 ...
- Python教程(2.2)——数据类型与变量
和C/C++.Java一样,Python也有数据类型和变量两个概念. 数据类型 Python中的几个基本数据类型为整数(integer/int).浮点数(float/float).布尔值(boolea ...
- HTML表格表单综合——用户注册表
今天学习了表格和表单知识,我综合了他们添加了一些拓展知识做了一个用户注册表,以下面代码来整理表格和表单知识: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...
- 动态添加Redis密码认证
如果redis已在线上业务使用中,但没有添加密码认证,那么如何在不影响业务服务的前提下给redis添加密码认证,就是一个需要仔细考虑的问题. 本文描述一种可行的方案,适用于客户端使用了jedis连接池 ...
- 前端css要加的一些
编码格式 @charset "utf-8"; body的外边距设置 margin:0; 标签设置 form,ul,ol,li设置为padding:0; ul,ol,li设置为lis ...