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

jww*_*jww 7 c++ ssl boost openssl boost-asio

我在看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.
Run Code Online (Sandbox Code Playgroud)

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

  • 在Context对象上禁用SSLv2,SSLv3和Compression
  • 为链建设和检查提供适当的根证书
  • 调用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.
Run Code Online (Sandbox Code Playgroud)

和:

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.
Run Code Online (Sandbox Code Playgroud)

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

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

Kar*_*hik 7

简短回答:你引用的链接中的Boost回调函数不会验证任何内容.它返回由OpenSSL(via 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,这是调用OpenSSL SSL_set_verify来设置回调函数的C++模块.看看如何verify_callback_function实施.它只是调用应用程序回调.