SSL证书和Boost asio

Jez*_*Jez 8 c++ boost openssl boost-asio

您好我正在尝试从通过C++使用https的网页下载内容.我从Boost asio示例中获取的基本客户端程序编译并运行良好,但是当我测试它时,例如Google:www.google.co.uk/?gws_rd = sl,它给出了错误"握手:证书验证失败" .

我认为这是因为ctx.set_default_verify_paths()不包含带有Google证书的路径(我在Windows上).

非常新的SSL,请你能帮助我以下的问题:

1)当我安装openSSL时,它是否在我的计算机上粘贴了一个受信任的认证机构列表?如果是这样,会导致Google的证书无法验证?

2)无论如何说我不关心验证,无论如何都要继续连接,就像在firefox中手动添加异常一样?我对连接是否可信并不特别感兴趣,因为我没有传输任何需要安全的东西.

任何一个的答案将不胜感激!

#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

using boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl;
typedef ssl::stream<tcp::socket> ssl_socket;

int main(int argc, char* argv[])
{
  try
  {
if (argc != 3)
{
  std::cout << argc;
  std::cout << "Usage: sync_client <server> <path>\n";
  std::cout << "Example:\n";
  std::cout << "  sync_client www.boost.org /LICENSE_1_0.txt\n";
  return 1;
}

boost::asio::io_service io_service;

// Create a context that uses the default paths for
// finding CA certificates.
ssl::context ctx(ssl::context::sslv23);
ctx.set_default_verify_paths();

// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "https");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

// Try each endpoint until we successfully establish a connection.
ssl_socket socket(io_service, ctx);
boost::asio::connect(socket.lowest_layer(), endpoint_iterator);
socket.lowest_layer().set_option(tcp::no_delay(true));

// Perform SSL handshake and verify the remote host's
// certificate.
socket.set_verify_mode(ssl::verify_peer);
socket.set_verify_callback(ssl::rfc2818_verification("host.name"));
socket.handshake(ssl_socket::client);

// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
request_stream << "Host: " << argv[1] << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

// Send the request.
boost::asio::write(socket, request);

// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");

// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);

if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
  std::cout << "Invalid response\n";
  return 1;
}
if (status_code != 200)
{
    std::cout << "Response returned with status code " << status_code << "\n";
    std::cout << status_message << "\n";
    // Read the response headers, which are terminated by a blank line.
    boost::asio::read_until(socket, response, "\r\n\r\n");

    // Process the response headers.
    std::string header;
    while (std::getline(response_stream, header) && header != "\r")
        std::cout << header << "\n";
    std::cout << "\n";
      return 1;
}
//code to read the data goes here, which works fine for http pages    
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << "\n";
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Tan*_*ury 12

通常通过操作系统,浏览器或单个软件包安装或更新受信任的证书.例如,在*nix世界中,证书通常可以通过ca-certificates包获得,证书安装在boost::asio::ssl::context::set_default_verify_paths()可以找到的位置.

验证验证失败,因为客户端正在尝试使用主机名验证(rfc2818)验证对等方的证书,并且正在检查文本"host.name"是否在证书中,并且服务器的证书未"host.name"列为名称.尝试改变:

socket.set_verify_callback(ssl::rfc2818_verification("host.name"));
Run Code Online (Sandbox Code Playgroud)

至:

socket.set_verify_callback(ssl::rfc2818_verification(argv[1]));
Run Code Online (Sandbox Code Playgroud)

要禁用对等认证,提供boost::asio::ssl::verify_noneboost::asio::ssl::stream::set_verify_mode():

socket.set_verify_mode(boost::asio::ssl::verify_none);
Run Code Online (Sandbox Code Playgroud)

Boost.Asio提供其他同行verify_mode.


当对等验证失败时,提供自定义回调以boost::asio::ssl::stream::set_verify_callback提供诊断信息会很有帮助.如文档中所述,处理程序签名必须是:

bool verify_callback(
  bool preverified, // True if the certificate passed pre-verification.
  verify_context& ctx // The peer certificate and other context.
);
Run Code Online (Sandbox Code Playgroud)

这是一个打印证书使用者名称的自定义仿函数:

///@brief Helper class that prints the current certificate's subject
///       name and the verification results.
template <typename Verifier>
class verbose_verification
{
public:
  verbose_verification(Verifier verifier)
    : verifier_(verifier)
  {}

  bool operator()(
    bool preverified,
    boost::asio::ssl::verify_context& ctx
  )
  {
    char subject_name[256];
    X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
    X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
    bool verified = verifier_(preverified, ctx);
    std::cout << "Verifying: " << subject_name << "\n"
                 "Verified: " << verified << std::endl;
    return verified;
  }
private:
  Verifier verifier_;
};

///@brief Auxiliary function to make verbose_verification objects.
template <typename Verifier>
verbose_verification<Verifier>
make_verbose_verification(Verifier verifier)
{
  return verbose_verification<Verifier>(verifier);
}
Run Code Online (Sandbox Code Playgroud)

它的用法:

socket.set_verify_callback(make_verbose_verification(
  boost::asio::ssl::rfc2818_verification(argv[1])));
Run Code Online (Sandbox Code Playgroud)

在我的机器上,当使用它并且set_default_verify_paths()没有被调用时,我得到以下输出:

$ ./a.out www.google.co.uk /?gws_rd=ssl
Verifying: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Verified: 0
Exception: handshake: certificate verify failed
Run Code Online (Sandbox Code Playgroud)

并在何时set_default_verify_paths()被调用:

$ ./a.out www.google.co.uk /?gws_rd=ssl
Verifying: /C=US/O=Equifax/OU=Equifax Secure Certificate Authority
Verified: 1
Verifying: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Verified: 1
Verifying: /C=US/O=Google Inc/CN=Google Internet Authority G2
Verified: 1
Verifying: /C=US/ST=California/L=Mountain View/O=Google Inc/CN=google.com
Verified: 1
Run Code Online (Sandbox Code Playgroud)

而当rfc2818_verification("host.name")使用:

$ ./a.out www.google.co.uk /?gws_rd=ssl
Verifying: /C=US/O=Equifax/OU=Equifax Secure Certificate Authority
Verified: 1
Verifying: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Verified: 1
Verifying: /C=US/O=Google Inc/CN=Google Internet Authority G2
Verified: 1
Verifying: /C=US/ST=California/L=Mountain View/O=Google Inc/CN=google.com
Verified: 0
Exception: handshake: certificate verify failed
Run Code Online (Sandbox Code Playgroud)