[/
    Copyright (c) 2025 Mohammad Nejati

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

    Official repository: https://github.com/boostorg/beast
]


[section SSL/TLS 证书]

[/-----------------------------------------------------------------------------]

[heading 证书授权机构]

证书授权机构（CA）是一种可信实体，负责对数字证书进行签名，以便用户验证其真实性。由于服务器证书数量庞大且更新频繁，为每台服务器单独存储所有证书并不现实。因此，用户只需存储有限数量的根证书，即可在需要时验证服务器证书。

Boost.Asio 提供多种加载证书授权机构证书的方法：

* [@boost:/doc/html/boost_asio/reference/ssl__context/add_certificate_authority.html `net::ssl::context::add_certificate_authority`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/add_verify_path.html `net::ssl::context::add_verify_path`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/load_verify_file.html `net::ssl::context::load_verify_file`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/set_default_verify_paths.html `net::ssl::context::set_default_verify_paths`]

设置对端验证非常重要，这样当证书验证失败时，TLS/SSL 握手也会随之失败：

[snippet_core_6]

客户端还需验证证书中的主机名或 IP 地址是否与预期一致。[@boost:/doc/html/boost_asio/reference/ssl__host_name_verification.html `net::ssl::host_name_verification`] 这个辅助函数对象，可以按照 RFC 6125 描述的规则来完成验证：

[snippet_core_7]

服务器也可以请求并验证客户端证书，以对客户端进行身份认证：

[snippet_core_8]

[/-----------------------------------------------------------------------------]

[heading 服务器证书]

服务器证书是一种数字证书，用于确认服务器身份是客户端的合法目的地。它包含可验证的签名，用于确保证书由受信任的证书授权机构（CA）签发。

当服务器证书由中间证书授权机构签发，且客户端缺少这些中间证书时，服务器应向客户端提供所有相关证书。这使得客户端能够根据根证书验证证书链中的最终证书。

以下 Boost.Asio 方法可用于加载单个证书或证书链：

* [@boost:/doc/html/boost_asio/reference/ssl__context/use_certificate.html `net::ssl::context::use_certificate`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_certificate_file.html `net::ssl::context::use_certificate_file`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_certificate_chain.html `net::ssl::context::use_certificate_chain`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_certificate_chain_file.html `net::ssl::context::use_certificate_chain_file`]

[/-----------------------------------------------------------------------------]

[heading 客户端证书]

服务器可以通过要求并验证客户端证书来对客户端进行身份认证，从而阻止没有有效证书和私钥的客户端访问。服务器通过修改对端验证设置来实施这一机制：

[snippet_core_8]

若使用该机制，则需将必要的 CA 证书加载到服务器的 SSL 上下文中，以便能够验证客户端的证书。

[/-----------------------------------------------------------------------------]

[heading 通用名称与主题备用名称]

主题备用名称（SAN）是 X.509 证书中的一种扩展字段，允许将多个域名、子域名或 IP 地址关联到同一张 SSL/TLS 证书上。在此之前，证书主题中的通用名称字段只能包含单个主机名。

[@https://datatracker.ietf.org/doc/html/rfc6125#appendix-B.2 RFC 6125]
该建议指出，如果证书中包含 SAN 的 dNSName 字段，客户端必须忽略主题 CN 字段。某些现代浏览器（例如 Google Chrome）仅检查 SSL/TLS 证书中的 SAN 部分，并拒绝仅包含 CN 字段的证书。

[/-----------------------------------------------------------------------------]

[heading 私钥]

证书的私钥在 SSL/TLS 握手过程中是必需的，用于证明证书的提供方是该证书的合法所有者。

以下 Boost.Asio 方法可用于加载私钥：

* [@boost:/doc/html/boost_asio/reference/ssl__context/use_private_key.html `net::ssl::context::use_private_key`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_private_key_file.html `net::ssl::context::use_private_key_file`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_rsa_private_key.html `net::ssl::context::use_rsa_private_key`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_rsa_private_key_file.html `net::ssl::context::use_rsa_private_key_file`]

如果私钥受密码保护，可以使用 [@boost:/doc/html/boost_asio/reference/ssl__context/set_password_callback.html net::ssl::context::set_password_callback] 设置一个可调用对象，用于获取该密码。

[/-----------------------------------------------------------------------------]

[heading 自签名证书与自颁发证书]

自颁发证书是指证书的颁发者与主体为同一实体。

自签名证书是一种自颁发证书，其数字签名可使用证书内的公钥进行验证。

[warning
    安装不受信任、自颁发或自签名的 CA 证书会带来安全风险。
    significant security risk, as there are no restrictions on the domains for
    which it can issue certificates. This allows attackers to generate
    fraudulent certificates for any public domain, enabling man-in-the-middle
    attacks if they gain access to your network.
]

[/-----------------------------------------------------------------------------]

[heading Diffie-Hellman（DH）参数]

Diffie-Hellman（DH）密钥交换是一种密码学协议，允许双方在不安全的通信通道上安全地建立共享密钥。该密钥交换过程涉及双方约定一组参数，即 Diffie-Hellman 参数，其中包括一个大素数 `p` 和一个生成元 `g`。由于生成这些参数是一项计算开销较大的任务，用户可能更倾向于在启动时提供预先计算好的值。

以下 Boost.Asio 方法可用于加载 DH 参数：

* [@boost:/doc/html/boost_asio/reference/ssl__context/use_tmp_dh.html `net::ssl::context::use_tmp_dh`]
* [@boost:/doc/html/boost_asio/reference/ssl__context/use_tmp_dh_file.html `net::ssl::context::use_tmp_dh_file`]

如果未提供 DH 参数，OpenSSL 将拒绝执行任何使用基于 DHE 的加密套件的握手，但仍可与其他加密套件（例如基于 ECDHE 的套件）正常工作。

[/-----------------------------------------------------------------------------]

[heading 自颁发证书示例]

在以下示例中，将生成一个自签名的 CA 证书，并使用该证书来签发服务器证书和客户端证书。

* 生成 CA 证书：

```
openssl req -new -newkey rsa:4096 -keyout ca.key -x509 -out ca.crt -subj "/CN=localhost" -days 365
```


* 生成服务器证书签名请求（CSR）：

```
openssl req -new -newkey rsa:4096 -keyout server.key -out server.csr -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
```


* Sign the Server CSR using our CA: 

```
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -copy_extensions copy -days 365 -out server.crt 
```


* 生成客户端证书签名请求（CSR）：

```
openssl req -new -newkey rsa:4096 -keyout client.key -out client.csr -subj "/CN=client.1"
```


* 使用 CA 对客户端 CSR 进行签名： 

```
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -days 365 -out client.crt 
```

* 生成 DH 参数文件：

```
openssl dhparam -out dh4096.pem 4096
```

服务器端示例：[path_link example/doc/ssl/server.cpp server.cpp]

注意，该服务器的配置方式要求对客户端证书进行请求和验证。如果需要禁用此功能，可以注释掉示例中的相关代码行。

可以使用以下 cURL 命令测试该服务器：

```
curl https://localhost:8080 --cacert ca.crt --cert client.crt --key client.key
```

此外，也可以使用客户端示例：[path_link example/doc/ssl/client.cpp client.cpp]

[endsect]
