linux c++ curl https 请求并双向验证SSL证书
1、配置curl https请求需要提供 CA证书、客户端证书和客户端秘钥,这三个文件的pem格式。
分别对应 curl_easy_setopt() 函数的 下面三个参数:
CURLOPT_CAINFO - path to Certificate Authority (CA) bundle
CURLOPT_SSLKEY - specify private keyfile for TLS and SSL client cert
CURLOPT_SSLCERT - set SSL client certificate
一般创建SSL证书时会生成 ca.crt , client.crt, client.key, server.crt, server.key 等,而 curl客户端请求,只需要将 ca.crt , client.crt, client.key转成相应的 pem格式 使用。转换方法如下:
1)将 CRT 转成 PEM---
不能直接将 .crt 转成 .pem,需要经过 .der 中转
openssl x509 -in client.crt -out client.der -outform der
openssl x509 -in client.der -inform der -outform pem -out client.pem openssl x509 -in ca.crt -out ca.der -outform der
openssl x509 -in ca.der -inform der -outform pem -out ca_info.pem
2)将 .key 转成 .pem
不能直接将 .key 转成 .pem,需要经过 .der 中转
openssl rsa -in client.key -out client.der -outform DER
openssl rsa -inform DER -outform PEM -in client.der -out client_key.pem
2、配置 curl https请求
1) 官方例程如下:
curl 接口文档说明:
https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
https://curl.haxx.se/libcurl/c/CURLOPT_CAINFO.html
https://curl.haxx.se/libcurl/c/https.html
CURLOPT_CAINFO - path to Certificate Authority (CA) bundle
CURLOPT_SSLKEY - specify private keyfile for TLS and SSL client cert
CURLOPT_SSLCERT - set SSL client certificate
按下面代码部分进行配置,即可访问
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); // 下面两个为验证对方和验证主机名,若为0,则跳过验证,我这个服务器必须验证才能得到请求数据
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 1L);
// 配置 https 请求所需证书
curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/certs/cabundle.pem");
curl_easy_setopt(curl, CURLOPT_SSLCERT, "client.pem");
curl_easy_setopt(curl, CURLOPT_SSLKEY, "key.pem");
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "s3cret"); ret = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
2)我的代码如下
这是一个完整的 curl 发送 https get 请求,并带中文参数
#include <iostream>
#include <sstream>
#include <jsoncpp/json/json.h>
#include <curl/curl.h>
#include <exception>
#include <string>
#include <iostream>
#include <stdlib.h> int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
unsigned long sizes = size * nmemb;
if (writerData == NULL)
return -; writerData->append(data, sizes);
return sizes;
} string parseJsonResponse_question(string input)
{
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(input, root);
if(!parsingSuccessful)
{
std::cout<<"!!! Failed to parse the response data"<< std::endl;
return "";
}
const Json::Value text = root["obj"]["question"];
string result = text.asString();
return result;
} string HttpsGetRequest_question(string input)
{
string buffer, ling_result; // 对请求参数中的中文和特殊字符(如空格等)进行处理,方可使用
char * escape_control = curl_escape(input.c_str(), input.size());
input = escape_control;
curl_free(escape_control); string str_url= "https://*.*.*.*/question?question=" + input; // alter *.*.*.* by your server address try
{
CURL *pCurl = NULL;
CURLcode res; // In windows, this will init the winsock stuff
curl_global_init(CURL_GLOBAL_ALL);
// get a curl handle
pCurl = curl_easy_init();
if (NULL != pCurl)
{
// 设置超时时间为8秒
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, );
curl_easy_setopt(pCurl, CURLOPT_URL, str_url.c_str()); // 下面两个为验证对方和验证主机名,若为0,则跳过验证,我这个服务器必须验证才能得到请求数据
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 1L); // 配置 https 请求所需证书
curl_easy_setopt(pCurl,CURLOPT_CAINFO,"/etc/msc/ca_info.pem");
curl_easy_setopt(pCurl, CURLOPT_SSLCERT, "/etc/msc/client.pem");
curl_easy_setopt(pCurl, CURLOPT_SSLKEY, "/etc/msc/client_key.pem");
curl_easy_setopt(pCurl, CURLOPT_KEYPASSWD, "your_key_password"); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer); // Perform the request, res will get the return code
res = curl_easy_perform(pCurl);
// Check for errors
if (res != CURLE_OK)
{
printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(pCurl);
}
curl_global_cleanup();
}
catch (std::exception &ex)
{
printf("curl exception %s.\n", ex.what());
}
if(buffer.empty())
{
std::cout<< "!!! ERROR The sever response NULL" << std::endl;
}
else
{
ling_result = parseJsonResponse_question(buffer);
}
return ling_result;
}
参考:
http://blog.csdn.net/rztyfx/article/details/6919220
https://segmentfault.com/a/1190000011709784
linux c++ curl https 请求并双向验证SSL证书的更多相关文章
- 请求https前缀的网站验证SSL证书的解决方案之一
from requests.packages.urllib3.exceptions import InsecureRequestWarning # 禁用安全请求警告requests.packages. ...
- Requests对HTTPS请求验证SSL证书
SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socket layer(SSL)安全协议是由Netscape Communication公司设计开发.该安全协议主 ...
- .Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2
.Net Core 发送https请求 .net core 调用数字证书 使用X509Certificate2 .NET下面的 .netfromwork使用和asp.net core下使用方式不一样 ...
- TortoiseGit 访问https远程仓库,上报SSL证书错误解决方法
报错 在使用TortoiseGit时,clone自己搭建的gitlab报如错SSL certificate problem: self signed certificate 原因:自行搭建的gitla ...
- Https系列之一:https的简单介绍及SSL证书的生成
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- linux下Tomcat+OpenSSL配置单向&双向认证(自制证书)
背景 由于ios将在2017年1月1日起强制实施ATS安全策略,所有通讯必须使用https传输,本文只针对自制证书,但目前尚不确定自制证书是否能通过appstore审核. 1.必须支持传输层安全(TL ...
- linux tomcat【9.0.12】 使用 ssl证书 配置 https 的具体操作 【使用 域名 】
1.前言 根据上一个随笔,已经可以正式在 阿里云服务器发布 工程了 ,但是用的协议默认是 http ,端口80 但是 http不安全 ,容易被拦截抓包 ,于是出来了个 https tomcat发布 对 ...
- [从零开始搭网站六]为域名申请免费SSL证书(https),并为Tomcat配置https域名所用的多SSL证书
点击下面连接查看从零开始搭网站全系列 从零开始搭网站 由于国内的网络环境比较恶劣,运营商流量劫持的情况比较严重,一般表现为别人打开你的网站的时候会弹一些莫名其妙的广告...更过分的会跳转至别的网站. ...
- postman进行https接口测试所遇到的ssl证书问题,参考别人方法
参考文档: https://learning.getpostman.com/docs/postman/sending_api_requests/certificates/ 随着 https 的推动,更 ...
随机推荐
- ASP.NET MVC案例教程(一) 准备
ASP.NET MVC案例教程(一) 前言 ASP.NET MVC作为微软官方的MVC解决方案,推出有一段时间了.可以说自动推出以来,一直广受关注.在经历了漫长的Preview之后,前几天终于推出了其 ...
- function的toString方法
javascript的创建多行字符串的除了平时常见的”“+ 反斜杠\ 还有ES6的` ` 在js51上发现一个很有意思的方法 function heredoc(fn) { return fn.toSt ...
- MySQL MHA 报错处理
安装环境:CentOS 6.5 MySQL 5.7.22 MHA 0.56 1.找不到mysql 命令 Sat Mar 23 07:17:50 2019 - [info] Connecting to ...
- 使用查询分析器和SQLCMD分别登录远程的SQL2005的1434端口
SQLCMD是操作SQLSERVER的一个命令行工具, 而查询分析器是它的图形工具 查询分析器(SQL2005下叫managerment studio),连接远程的SQLSERVER2005, ...
- 【js】字符串反转可实现的几种方式
方式1: 这种方式比较简单,推荐使用 字符串转数组,反转数组,数组转字符串. split(""):根据空字符串拆分数组 reverse():数组反转元素位置 join(" ...
- docker容器与大数据组件的冲突点
1.容器里面安装spark,外面的程序(安装spark主机的容器)会连接不上集群.理由:这个组件用的akka,连接上集群,会提示: akka.ErrorMonitor: dropping messag ...
- index read-only
系统重启后,Eleastisearch6.5.0在给 Eleastisearch 更新索引的时候报了一个错误:ClusterBlockException[blocked by: [FORBIDDEN/ ...
- bzoj 5185 Lifeguards - 动态规划 - 贪心
题目传送门 传送点I 传送点II 题目大意 给定$n$个区间,问恰好删去其中$k$个,剩下的区间的并的最大总长度. 显然被包含的区间一定不优.再加上被包含的区间对计数不友好.直接把它删掉. 注意到题目 ...
- New需谨慎
New is Glue When you’re working in a strongly typed language like C# or Visual Basic, instantiating ...
- 基于 arduino 的低功耗无线传感结点设计
发送端 仿真图: *仿真图中使用使用TMP传感器(LM34)代替实际使用的DHT11传感器. 连接方式: DHT11的正极(VCC)与5V电源接口连接 DHT11的负极(GND)与GND连接 DHT1 ...