ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 安全断开asio SSL插槽的正确方法是什么?

2019-09-23 04:04:11  阅读:279  来源: 互联网

标签:c ssl boost-asio


boost-asio SSL / TLS TCP套接字通过tcp :: socket实现为ssl :: stream:

boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;

TLS协议中,加密安全关闭涉及各方交换close_notify消息.简单地关闭最低层可能会使会话容易受到truncation attack的影响.

boost asio ssl async_shutdown always finishes with an error? @Tanner Sansbury详细描述了SSL关闭过程的许多场景,并建议在关闭套接字之前使用async_shutdown后跟async_write来断开SSL流:

ssl_socket.async_shutdown(...);
const char buffer[] = "";
async_write(ssl_socket, buffer, [](...) { ssl_socket.close(); }) 

在ssl :: stream上执行async_shutdown会发送SSL close_notify消息并等待另一端的响应.当async_shutdown发送close_notify时,将通知在async_shutdown之后写入流的目的,以便可以关闭套接字而无需等待响应.但是,在当前(1.59)版本的boost中,对async_write的调用失败了……

How to gracefully shutdown a boost asio ssl client? @maxschlepzig建议关闭底层TCP套接字的接收器:

ssl_socket.lowest_layer()::shutdown(tcp::socket::shutdown_receive);

这会产生一个简短的读取错误,并且在错误处理程序中检测到async_shutdown时会调用async_shutdown:

// const boost::system::error_code &ec
if (ec.category() == asio::error::get_ssl_category() &&
  ec.value()    == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ))
{
  // -> not a real error:
  do_ssl_async_shutdown();
}

或取消对套接字的读/写操作,然后调用SSL异步关闭,即:

boost::system::error_code ec;
ssl_socket.cancel(ec);
ssl_socket.async_shutdown([](...) { ssl_socket.close(); };

我目前正在使用这最后一种方法,因为它适用于当前版本的boost.

安全断开boost-asio SSL插槽的正确/最佳方法是什么?

解决方法:

要安全断开连接,请执行关闭操作,然后在关闭完成后关闭基础传输.因此,您当前使用的方法将执行安全断开连接:

boost::system::error_code ec;
ssl_socket.cancel(ec);
ssl_socket.async_shutdown([](...) { ssl_socket.close(); };

请注意,当以下情况之一时,当前的async_shutdown操作将被视为已完成:

>远程对等方已收到close_notify.
>远程对等体关闭套接字.
>该操作已被取消.

因此,如果资源绑定到套接字或连接的生存期,则这些资源将保持活动状态,等待远程对等方采取操作或直到操作在本地取消.但是,安全关闭不需要等待close_notify响应.如果资源绑定到连接,并且在发送关闭时本地连接被认为是死的,那么不等待远程对等方采取操作可能是值得的:

ssl_socket.async_shutdown(...);
const char buffer[] = "";
async_write(ssl_socket, boost::asio::buffer(buffer),
    [](...) { ssl_socket.close(); })

当客户端发送close_notify消息时,客户端会保证客户端不会通过安全连接发送其他数据.本质上,async_write()用于检测客户端何时发送了close_notify,并且在完成处理程序中,将关闭底层传输,导致async_shutdown()完成boost :: asio :: error :: operation_aborted .如linked answer中所述,async_write()操作预计会失败.

… as the write side of PartyA‘s SSL stream has closed, the async_write() operation will fail with an SSL error indicating the protocol has been shutdown.

06002

The failed async_write() operation will then explicitly close the underlying transport, causing the async_shutdown() operation that is waiting for PartyB‘s close_notify to be cancelled.

标签:c,ssl,boost-asio
来源: https://codeday.me/bug/20190923/1813827.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有