在 Rust 中使用 Rustls 和 Hyper 实现 HTTPS 服务器

Implementing HTTPS Server using Rustls with Hyper in Rust

我正在尝试使用 Rustls 和 Hyper 实现 HTTPS 服务器,但我无法获得如何实现相同的正确示例。为此,我遵循并尝试了 hyper-rustls 存储库 here (Hyper Rustls server example)

上给出的示例

总是报这个错误

FAILED: error accepting connection: TLS Error: Custom { kind: InvalidData, error: AlertReceived(CertificateUnknown) }

我对 Rust 完全陌生,因此不知道如何通过 Hyper 正确实施 HTTPS。我也经历了与此相关的问题 here

但是还是找不到解决办法。如果需要更多信息,请告诉我。

看来您的问题不在于 Hyper 或 Rust,而是在于 TLS。默认情况下,当您通过 HTTPS 建立连接时,客户端会验证服务器证书的真实性。证书需要由受信任的机构签名:有关详细信息,请参见例如 this page.

要验证,请使用 curl:

$ curl https://localhost:1337/echo -X POST -v --insecure
...
*  SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
...
< HTTP/2 200 
< date: Sun, 12 Apr 2020 12:45:03 GMT
< 

所以这很好用。如果删除 --insecure 标志,curl 将拒绝建立连接:

$ curl https://localhost:1337/echo -X POST -v
...
curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

要解决此问题,您需要:

  1. 使用正确签名的证书而不是自签名证书,或者
  2. 将您的客户端配置为不验证证书,或者
  3. 将您的客户端配置为信任您的特定自签名证书。

在生产中,您唯一的选择是 (1)。在开发过程中,您可以使用 (2) 或 (3)。