使用boost asio和SSL时消息大小过大
Excessive message size with boost asio and SSL
我目前正在开发一个使用SSL来保护传输的服务器。在接受一个约定后,服务器进行握手,如果成功,它将发送一个数据包给客户端。
我的代码在Windows上运行得很好,但是当我在Debian 7上运行它时,当我试图写那个数据包时,我得到了一个"消息大小过大"的错误。
我很确定它不是来自我的证书,因为我试着用相同的证书运行boost asio SSL示例,它工作得很好。
代码如下:
服务器初始化:
Server::Server(configuration::Configuration const & conf, utils::Logger & log): _cmds(conf, log), _conf(conf), _logger(log), _ctx(boost::asio::ssl::context::sslv23)
{
tcp::endpoint endpoint(tcp::v4(), conf.getPort());
configuration::SSL_conf ssl = conf.getSLLInfos();
try
{
this->_ctx.set_options(boost::asio::ssl::context::default_workarounds
| boost::asio::ssl::context::no_sslv2
| boost::asio::ssl::context::single_dh_use);
this->_ctx.set_password_callback(boost::bind(&Server::_getPass, this, _1, _2));
this->_ctx.use_certificate_file(ssl.certificate.string(), boost::asio::ssl::context_base::pem);
this->_ctx.use_private_key_file(ssl.key.string(), boost::asio::ssl::context_base::pem);
this->_ctx.use_tmp_dh_file(ssl.certificate.string());
this->_acceptor.reset(new tcp::acceptor(this->_service, endpoint));
this->_logger.logMsg(3, "Starting listen on port " + std::to_string(endpoint.port()));
for (int i = 0; i < 256; ++i)
this->_c.push_back(std::shared_ptr<Client>(new Client(this->_service, this->_cmds, conf, log, i, this->_ctx)));
this->_acceptor->async_accept(this->_c.front()->getSocket(),
boost::bind(&Server::_handleAccept, this,
this->_c.front(),
boost::asio::placeholders::error));
}
catch (boost::system::system_error & err)
{
this->_logger.logErr(err.what());
}
}
会话初始化:
void Client::init()
{
this->_logger.logMsg(3, "Client [" + std::to_string(this->_id) + "] initialized");
this->_isAlive = true;
this->_socket.async_handshake(boost::asio::ssl::stream_base::server,
boost::bind(&Client::handleHandshake, this,
boost::asio::placeholders::error));
}
手工回调:
void Client::handleHandshake(boost::system::error_code const & err)
{
if (!err)
{
memset(&this->_header, 0, sizeof(protocol::header));
this->_keepAlive = this->_conf.getKeepAlive();
this->_header.keepAlive = 1;
this->_logger.logStruct<protocol::header>("Client [" + std::to_string(this->_id) + "] Sending handhsake: ", this->_header);
boost::asio::async_write(this->_socket, boost::asio::buffer(&this->_header, sizeof(protocol::header)),
boost::bind(&Client::handleWrite, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
this->kill();
}
最后,我收到错误的写回调:
void Client::handleWrite(boost::system::error_code const & err, std::size_t)
{
if (!err && this->_keepAlive)
{
clearBuff(this->_outBuff);
boost::asio::async_read(this->_socket, boost::asio::buffer(&this->_header, sizeof(protocol::header)),
boost::bind(&Client::handleHeaderRead, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
this->_logger.logErr("Client [" + std::to_string(this->_id) + "] write failed : " + err.message());
this->kill();
}
}
正如我所说的handleWrite回调收到一个错误的值"消息大小过大"。我真的不明白为什么,因为我使用boost asio是很标准的,我的证书是自签名的,可以与其他应用程序一起使用。
谢谢你的帮助
尝试配置特定的SSL协议版本(例如仅SSLv3)。这减少了要发送的项目列表。
另外,考虑发送一个最小化的证书包(在服务器上的证书文件中),因为客户机不会对链中的大多数根证书感兴趣,除非它已经知道它们
从您的描述来看,问题不在于SSL握手本身,而在于Client::handleHandshake
中对async_write
的调用中的缓冲区。
我建议记录sizeof(_header)
和sizeof(protocol::header)
,并在两个实现中比较它们。
顺便说一句,你在哪里为_header
分配内存?是在构造函数中完成的吗?因为在Client::init()
好吧,正如在评论中所说,问题是,出于某种原因,让客户端和服务器端决定选择哪个版本似乎把事情搞砸了。通过强制使用SSLV3,它解决了这个问题。谢谢大家的帮助。
相关文章:
- boost::进程间消息队列引发错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- ROS2 动态消息模板
- C++秘密消息学校作业
- glad 导致 glfwSwapBuffers 返回错误消息
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- Qt 错误消息"qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed"
- 带有 TLS v. 1.2 和 SSL v.3 电子邮件客户端的 Boost.Asio 上下文:ssl3 警报意外消息
- boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它可以正常工作,为什么
- 使用boost asio和SSL时消息大小过大