Boost、asio、https 和主机/证书验证

Boost, asio, https, and host/certificate verifcation

本文关键字:证书 验证 主机 asio https Boost      更新时间:2023-10-16

我正在查看Boost的SSL客户端。评论中提到了OpenSSL(对不起,没有行号(:

// The verify callback can be used to check whether the certificate that is
// being presented is valid for the peer. For example, RFC 2818 describes
// the steps involved in doing this for HTTPS. Consult the OpenSSL
// documentation for more details. Note that the callback is called once
// for each certificate in the certificate chain, starting from the root
// certificate authority.

正确使用和验证OpenSSL可能很棘手。根据经验,我知道我必须执行以下操作才能正确使用库:

  • 禁用上下文对象的 SSLv2、SSLv3 和压缩
  • 为链构建和检查提供正确的根证书
  • 调用SSL_get_peer_certificate并验证证书是否为非 NULL
  • 呼叫SSL_get_verify_result并验证结果是否X509_V_OK
  • 执行名称匹配(CN 或 SAN 必须与请求的主机匹配(

OpenSSL 1.1.0 将提供名称检查,但目前仅在 HEAD 中。从 OpenSSL 更改日志:

Integrate hostname, email address and IP address checking with certificate
verification. New verify options supporting checking in opensl utility.

和:

New functions to check a hostname email or IP address against a
certificate. Add options x509 utility to print results of checks against
a certificate.

我没有看到 Boost 在哪里执行客户端代码中的任何配置或检查。

Boost 究竟在配置什么,在使用 SSL 时,它在asio库组件中检查或验证什么?

简短回答:来自您引用的链接的 Boost 回调函数不会验证任何内容。它返回OpenSSL(通过bool preverified(提供给它的任何初步验证结果。如果需要任何细粒度验证(如 CN 匹配等(,则必须通过回调显式完成。

长答案:当OpenSSL(或OpenSSL的Boost包装器(调用验证函数时,在本例中为bool verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx),OpenSSL已经完成了一组初步(或强制性(验证。文档中对此进行了说明。

从最深的嵌套级别(根 CA 证书(开始检查证书链,然后向上检查对等方的证书。在每个级别检查签名和颁发者属性。每当发现验证错误时,错误号存储在x509_ctx中,并以 preverify_ok=0 调用verify_callback。通过应用 X509_CTX_store_* 函数verify_callback可以找到有问题的证书并执行其他步骤(请参阅示例(。如果未发现证书错误,则在升级到下一级别之前,将使用 preverify_ok=1 调用verify_callback。

该文档还引用了一个示例,说明如何编写更细粒度的验证回调。您可以根据自己的需求从中汲取灵感。

编辑:为了确保Boost的内部回调函数除了调用应用程序回调函数之外不会做任何特殊的事情,我看了一下engine.ipp,这是一个C++模块,它调用OpenSSL的SSL_set_verify来设置回调函数。看看verify_callback_function是如何实现的。它只是调用应用程序回调。