[英]boost asio encryption in a loop without closing connection
我正在嘗試擴展 boost asio 庫中的示例。 到目前為止我所取得的成功。
1) 運行 echo 客戶端和服務器。
2) 擴展回顯服務器以讀取消息,直到標准輸入的文件結束。
3) 運行 ssl 客戶端服務器示例。
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/ssl/client.cpp
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/ssl/server.cpp
我需要什么幫助:我希望能夠將發送/接收的消息放入客戶端的循環中。 我不想繞圈子
client c(io_service, ctx, iterator);
io_service.run();
因為這將驗證每條消息的證書。 我嘗試在 handle_handshake 和其他函數周圍放置一個循環,但它們不起作用。 直到函數結束才會發生發送。 我永遠不會收到回復。
我願意避免使用異步 IO,但我仍然想要加密。
我必須從阻塞客戶端開始,並使用 https 從 ssl 客戶端和 ssl 示例中添加我需要的內容:
這適用於未經修改的 ssl 服務器。
//
// ssl_blocking_tcp_echo_client.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
using boost::asio::ip::tcp;
enum { max_length = 1024 };
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 preverified;
}
int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
return 1;
}
boost::asio::io_service io_service;
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.load_verify_file("server.crt");
ctx.set_verify_mode(boost::asio::ssl::verify_peer);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_service, ctx);
tcp::resolver resolver(io_service);
boost::asio::connect(socket.lowest_layer(), resolver.resolve({argv[1], argv[2]}) );
socket.set_verify_callback(
boost::bind(&verify_certificate, _1, _2));
socket.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client);
while(std::cin)
{
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = std::strlen(request);
boost::asio::write(socket, boost::asio::buffer(request, request_length));
char reply[max_length];
size_t reply_length = boost::asio::read(socket,
boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
在開始之前,我根據 Shootfast 對這個問題的回答修改了服務器和客戶端。 我不得不稍微修改它,因為有些東西太短了。 我將此命令更改為 2048 而不是原來的 512。
openssl dhparam -out dh512.pem 2048
下面引用。
好的,對於將來發現此問題的任何人,您需要創建證書並對其進行適當簽名。 以下是Linux的命令:
//生成私鑰
openssl genrsa -des3 -out server.key 1024
//生成證書簽名請求
openssl req -new -key server.key -out server.csr
//使用私鑰簽署證書
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
//刪除密碼要求(例如需要)
cp server.key server.key.secure
openssl rsa -in server.key.secure -out server.key
//生成dhparam文件
openssl dhparam -out dh512.pem 2048
完成后,您需要更改 server.cpp 和 client.cpp 中的文件名。
服務器.cpp
context_.use_certificate_chain_file("server.crt");
context_.use_private_key_file("server.key", boost::asio::ssl::context::pem);
context_.use_tmp_dh_file("dh512.pem");
客戶端
ctx.load_verify_file("server.crt");
那么它應該一切正常!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.