升压短耳::,SSL连接问题 [英] Boost::Asio, SSL Connection Problems
问题描述
我试图解决我的问题,现在几天,只是不能它后面得到。我试着做了boost ::短耳库和OpenSSL的SSL连接。有一个例子code,如何做到这一点:的http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp
I tried to solve my Problem for a few days now and just can't get behind it. I try to do an SSL Connection with the Boost::Asio Library and OpenSSL. There is an Example Code, how to do this: http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp
它建立并运行良好,甚至验证似乎工作,但是当我发出一个简单的请求,没有任何反应相当长的一段时间,然后我得到一个错误信息:读取失败:短读。我想,我只是在做请求行本身就是错误的,但我无法弄清楚它是如何拥有如此。我想这两个以下行:
It builds and runs fine and even the verifying seems to work, but when I send a simple request, nothing happens for quite a long time, and then i get an error Message: "Read Failed: short read". I guess, I'm just doing the request line itself wrong, but i can't figure out how it has to be. I tried both the following lines:
GET / HTTP/1.1<CR><LF>Host: www.google.de<CR><LF>Connection: close<CR><LF><CR><LF>
GET / HTTP/1.1\r\nHost: www.google.de\r\nConnection: close\r\n\r\n
我不能发表图片,因为这是我的第一篇,但你可以找到的CMD-窗口位置:
I can't post images, because this is my first Post, but you can find the cmd-Window here:
先谢谢了!
更新:我得到它的工作! :D非常感谢sehe,我不得不在你的code从行更改77:
Update: I got it to work! :D Big thanks to sehe, i just had to change line 77 in your code from:
size_t request_length = strlen(request_);
到
size_t request_length = strlen(raw);
现在它工作得很好,THX对您有所帮助:)
Now it works just fine, thx for your help :)
我还是不明白,为什么作为供我升压例如code不工作。
I still don't understand, why the boost example code isn't working as intended for me..
推荐答案
哈!
我发现更多的时间和它看了一遍。我的code这里( http://coliru.stacked-crooked.com/a/2042c3d24a16c6ac )已经 几乎的正确的。除了一个很简单的错误:
I've found more time and looked at it again. My code here (http://coliru.stacked-crooked.com/a/2042c3d24a16c6ac) was already almost correct. Except for one really simple bug:
size_t request_length = strlen(request_);
看起来无害的,不是吗?除... 要求_
在这一点上初始化。它需要的是(在上下文中):
Looks innocuous, no? Except... request_
was uninitialized at this point. It needed to be (in context):
static char const raw[] = "GET / HTTP/1.1\r\nHost: www.google.de\r\nConnection: close\r\n\r\n";
static_assert(sizeof(raw)<=sizeof(request_), "too large");
size_t request_length = strlen(raw);
std::copy(raw, raw+request_length, request_);
当然,这个问题并没有您链接到官方升压样品中存在(因为它直接读取与的std :: cin.getline
请求缓冲区)。但是,像我一样,你可能已经犯了同样的初学者的错误。
Of course, this problem did not exist in the official Boost sample you linked to (as it reads directly into the request buffer with std::cin.getline
). However, like me, you might have made the same beginner's mistake.
希望这有助于。
附件是一个,你甚至面包车切换到使用HTTPS或者不使用preprocessor指令完全工作演示:
Attached is a fully working demo that you van even switch to use HTTPS or not using preprocessor directive:
#define SEHE_USE_SSL
还请注意,我用
ctx.set_default_verify_paths();
所以你可以使用你的机器上注册的默认root权限证书(如果你的OpenSSL安装都有它)。
so you can use the default root authority certificates registered on your machine (if your openssl installation has it).
#define SEHE_USE_SSL
#define BOOST_ASIO_ENABLE_HANDLER_TRACKING
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
class client
{
public:
client(boost::asio::io_service& io_service,
boost::asio::ssl::context& context,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
: socket_(io_service
#ifdef SEHE_USE_SSL
, context)
{
socket_.set_verify_mode(boost::asio::ssl::verify_peer);
socket_.set_verify_callback(
boost::bind(&client::verify_certificate, this, _1, _2));
#else
)
{
(void) context;
#endif
boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error));
}
bool verify_certificate(bool preverified,
boost::asio::ssl::verify_context& ctx)
{
// The verify callback can be used to check whether the certificate that is
// being presented is valid for the peer. For example, RFC 2818 describes
// the steps involved in doing this for HTTPS. Consult the OpenSSL
// documentation for more details. Note that the callback is called once
// for each certificate in the certificate chain, starting from the root
// certificate authority.
// In this example we will simply print the certificate's subject name.
char subject_name[256];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
std::cout << "Verifying " << subject_name << "\n";
return true || preverified;
}
void handle_connect(const boost::system::error_code& error)
{
#ifdef SEHE_USE_SSL
if (!error)
{
socket_.async_handshake(boost::asio::ssl::stream_base::client,
boost::bind(&client::handle_handshake, this,
boost::asio::placeholders::error));
}
else
{
std::cout << "Connect failed: " << error.message() << "\n";
}
#else
handle_handshake(error);
#endif
}
void handle_handshake(const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Enter message: ";
static char const raw[] = "GET / HTTP/1.1\r\nHost: www.google.de\r\nConnection: close\r\n\r\n";
static_assert(sizeof(raw)<=sizeof(request_), "too large");
size_t request_length = strlen(raw);
std::copy(raw, raw+request_length, request_);
{
// used this for debugging:
std::ostream hexos(std::cout.rdbuf());
for(auto it = raw; it != raw+request_length; ++it)
hexos << std::hex << std::setw(2) << std::setfill('0') << std::showbase << ((short unsigned) *it) << " ";
std::cout << "\n";
}
boost::asio::async_write(socket_,
boost::asio::buffer(request_, request_length),
boost::bind(&client::handle_write, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Handshake failed: " << error.message() << "\n";
}
}
void handle_write(const boost::system::error_code& error,
size_t /*bytes_transferred*/)
{
if (!error)
{
std::cout << "starting read loop\n";
boost::asio::async_read_until(socket_,
//boost::asio::buffer(reply_, sizeof(reply_)),
reply_, '\n',
boost::bind(&client::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
std::cout << "Write failed: " << error.message() << "\n";
}
}
void handle_read(const boost::system::error_code& error, size_t /*bytes_transferred*/)
{
if (!error)
{
std::cout << "Reply: " << &reply_ << "\n";
}
else
{
std::cout << "Read failed: " << error.message() << "\n";
}
}
private:
#ifdef SEHE_USE_SSL
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
#else
boost::asio::ip::tcp::socket socket_;
#endif
char request_[1024];
//char reply_ [1024];
boost::asio::streambuf reply_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: client <host> <port>\n";
return 1;
}
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(argv[1], argv[2]);
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.set_default_verify_paths();
client c(io_service, ctx, iterator);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
这篇关于升压短耳::,SSL连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!