如何在Rust中使用客户端证书申请
问题描述:
我有一个项目,其中包含部署在带有Docker容器的Bluemix中的微服务。所有微服务都是用Java编写的,通信使用JKS文件。如何在Rust中使用客户端证书申请
我还用Express.js在Node.js中开发了一个微服务。消耗其他微服务,我用the Request module与option.agentOptions
功能和pfx 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与default example为HTTPS,但它失败:
4 | use solicit::http::client::tls::TlsConnector;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `tls` in `client`
我无法找到另一个箱子,图书馆或框架,我怎么能提出这个要求?
编辑
显然征求不是缺乏维护的替代,以便它不再是一种替代解决这个问题,Here's the reason。
答
目前,你应该更喜欢hyper
客户端solicit
。后者自2015年以来未更新,并且hyper
正在得到更好的维护。将hyper = "0.10.10"
和hyper-native-tls = "0.2.2"
添加到您的依赖关系。为了指定要使用的客户端证书,我们可以利用的功能。特别是,您正在寻找TlsConnectorBuilder
和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指出,当“TLS”功能被用于这种依赖使tls
子模块是唯一可用的。不过,这会导致进一步的依赖冲突(参见Why does solicit 0.4.4 attempt to use openssl 0.9.12 even though I have openssl 0.7.14 in my Cargo.toml?)。坚持hyper
而不是solicit
是一个更安全的选择。
Thanks @ E_net4,我补充说,但现在的错误是:'错误:Package openssl'v0.9.12'没有这些功能:'tlsv1_2,npn'',我也加了'openssl = {version =“0.9 .12“,features = [”v101“,”v102“,”v110“]}'但是第二个错误仍然存在。 – Deoxyseia
@Deoxyseia我用另一种方法更新了答案。 –
我认为修正Solicit不是关于安全主题的最佳解决方案,这正是我想要做的,特别是不得不将openssl修复为0.9。关于hyper并没有解决原来的问题,在服务器需要的情况下如何使用客户端证书来提出请求。 [更多信息](https://security.stackexchange.com/a/20847/116719) – Deoxyseia