如何在 Rust 中使用客户端证书发出请求

How to make a request with client certificate in Rust

我有一个项目,在 Bluemix 中部署了带有 Docker 容器的微服务。所有的微服务都是用 Java 编写的,并且使用 JKS 文件进行通信。

我还在 Node.js 中使用 Express.js 开发了一个微服务。为了使用其他微服务,我使用了具有 option.agentOptions 功能的 the Request modulepfx file,如下所示:

var options = {
        uri: config.get("https://www.example.com/ms/service"),
        method: 'POST',
        body: data,
        json: true,
        headers: {
            'Content-Type': 'application/json; charset=UTF-8'
        },
        agentOptions: {
            pfx: fs.readFileSync(config.get("/path/to/file.pfx")),
            passphrase: config.get("passphraseText"),
            servername: config.get("serverName")
        }
    };

request(options, function (error, response, data) {
     //handing response
});

我尝试将 the Solicit crate with default example 用于 HTTPS,但失败了:

4 | use solicit::http::client::tls::TlsConnector;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `tls` in `client`

我找不到另一个箱子、库或框架来制作它,我该如何提出这个要求?


编辑

显然,由于缺乏维护,Solicit 不是替代方案,因此它不再是这个问题的替代解决方案,

目前,您应该更喜欢 hyper 客户端而不是 solicit。后者自 2015 年以来就没有更新过, hyper 正在得到更好的维护。将 hyper = "0.10.10"hyper-native-tls = "0.2.2" 添加到您的依赖项中。为了指定要使用的客户端证书,我们可以利用 native_tls 的功能。特别是,TlsConnectorBuilder and Pkcs12 是您要查找的内容。

use std::fs::File;
use std::io::Read;
use hyper::client::Client;
use hyper::net::HttpsConnector;
use hyper_native_tls::NativeTlsClient;
use hyper_native_tls::native_tls::{TlsConnector, Pkcs12};

// fetch the PKCS12 client certificate
let cert = {
    let cert_file = File::open("/path/to/cert.pfx")?;
    let mut cert_raw = Vec::new();
    cert_file.read_to_end(&mut cert_raw)?;
    Pkcs12::from_der(&cert_raw, "mypassword")?
};

// specify the TLS connection with the builder pattern 
let tls_conn = TlsConnector::builder()
    .identity(cert)?
    .build()?;
let ssl = NativeTlsClient::from(tls_conn)?;
let https_conn = HttpsConnector::new(ssl);

// proceed as usual
let client = Client::with_connector(https_conn);
let endpoint = "https://www.example.com/ms/service");
let resp = client.get(endpoint).send()?;

solicit中,documentation states that the tls submodule was only available when the "tls" feature is enabled for this dependency. Nevertheless, this would lead to further dependency conflicts (see )。坚持 hyper 而不是 solicit 是一个更安全的选择。