如何在cpprestsdk中修复“ssl握手中的错误”?

时间:2016-02-22 18:59:52

标签: c++ openssl casablanca

我正在使用cpprestsdk“卡萨布兰卡”主分支与https网址,它在Windows和osx上运行但是当我在linux上运行时我收到“错误是ssl握手”

C++ exception with description "Error in SSL handshake" thrown in the test body.

我尝试使用firefox打开这个网址。

当我使用它与http url时,它工作正常 我在一个名为“http_client_asio.cpp”

的文件中检查了我发现此消息的代码
void write_request()
    {
        // Only perform handshake if a TLS connection and not being reused.
        if (m_connection->is_ssl() && !m_connection->is_reused())
        {
            const auto weakCtx = std::weak_ptr<asio_context>(shared_from_this());
            m_connection->async_handshake(boost::asio::ssl::stream_base::client,
                                          m_http_client->client_config(),
                                          m_http_client->base_uri().host(),
                                          boost::bind(&asio_context::handle_handshake, shared_from_this(), boost::asio::placeholders::error),

                                          // Use a weak_ptr since the verify_callback is stored until the connection is destroyed.
                                          // This avoids creating a circular reference since we pool connection objects.
                                          [weakCtx](bool preverified, boost::asio::ssl::verify_context &verify_context)
                                          {
                                              auto this_request = weakCtx.lock();
                                              if(this_request)
                                              {
                                                  return this_request->handle_cert_verification(preverified, verify_context);
                                              }
                                              return false;
                                          });
        }
        else
        {
            m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error));
        }
    }

    void handle_handshake(const boost::system::error_code& ec)
    {
        if (!ec)
        {
            m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error));
        }
        else
        {
            report_error("Error in SSL handshake", ec, httpclient_errorcode_context::handshake);
        }
    }

在客户端我喜欢这样的http客户端

http_client client(U("https://www.bing.com/"));

我该如何修复此错误?

1 个答案:

答案 0 :(得分:3)

我在尝试连接到https网址时遇到了同样的问题。

首先,如果您不需要连接到https,则只需将您的网址替换为http网址。

另一种解决方案是将http_client_config凭据验证设置为false。像这样:

    http_client_config config;
    config.set_validate_certificates(false);

但是,如果您需要使用包含安全性的https连接,则必须包含本地证书文件(通常是CRT或DEM文件)并将其设置为http_client配置中的回调:

    http_client_config config;
    config.set_ssl_context_callback([]((boost::asio::ssl::context &ctx) {
        ctx.load_verify_file("PATH_TO_CERTIFICATE_FILE");
    });

您可以在此处查看有关这些功能的更多信息:

https://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1client_1_1http__client__config.html