使用boost asio和SSL时消息大小过大

Excessive message size with boost asio and SSL

本文关键字:消息 SSL boost asio 使用      更新时间:2023-10-16

我目前正在开发一个使用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,它解决了这个问题。谢谢大家的帮助。