【问题标题】:Configure asio context to use tls配置 asio 上下文以使用 tls
【发布时间】:2015-09-01 15:20:34
【问题描述】:

你能告诉我(或显示更好的代码)如何配置 boost::asio::ssl::context 以与 TLS 一起正常工作吗?我使用 websocket-server (websocket++),它使用 boost::asio 进行配置。我应该设置哪些选项才能使其正常工作?我知道我应该设置

context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
ctx->set_options(boost::asio::ssl::context::default_workarounds);

使用 sslv23 及更高版本(包括我的 TLS)。我想以某种方式设置我保存在内存中的 RSA 密钥。 Websockets 还必须以某种方式设置它以通过此 TLS 身份验证。以及如何在 websockets 中设置这些所需的参数?

ps:很抱歉提出了一些愚蠢的问题

【问题讨论】:

    标签: c++ boost websocket openssl protocols


    【解决方案1】:

    WebSocket++ 使用基于策略的方法在端点上启用 TLS 支持。 Boost.Asio TLS 支持的配置策略在 websocketpp/config/asio.hpp 头文件中。包含它后,使用websocketpp::config::asio_tls 策略实例化服务器将允许通过提供给set_tls_init_handler() 的回调提供boost::asio::ssl::context

    websocketpp::lib::shared_ptr<boost::asio::ssl::context> on_tls_init(
        websocketpp::connection_hdl
    )
    {
      auto ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(
          boost::asio::ssl::context::sslv23);
      // ... configure ctx as desired
      return ctx;
    }
    
    int main()
    {
      using websocketpp::lib::placeholders::_1;
      using websocketpp::lib::bind;
    
      // Create server with Asio TLS configuration.
      websocketpp::server<websocketpp::config::asio_tls> server;
      server.init_asio();
    
      // Set the handler which will return the `ssl::context`.
      server.set_tls_init_handler(bind(&on_tls_init, _1));
    
      // set other handlers, listen, accept, run, etc...
    }
    

    有关 WebSocket++ TLS 支持的完整示例,请参阅官方 echo_server_tls example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2019-08-17
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多