How to resolve a Rust Reqwest Error: Invalid Certificate

1.5k Views Asked by At

Quick question, does reqwest allow self-signed certificates? I have created a tls enabled rust-warp webserver. And I have created a reqwest client to make requests to this server for testing purposes. The cert added to add_root_certificate is the same certificate which the server is using. It's odd because when I use other clients such as Python or CURL there are no issues making the requests, and I do not get a TLS error on my server.

Here's my sample rust code:

let mut buf = Vec::new();
File::open("my_cert.pem")?
  .read_to_end(&mut buf)?;
let cert = reqwest::Certificate::from_pem(&buf)?;
let client = reqwest::Client::builder()
  .add_root_certificate(cert)
  .build()?;

let mut map = HashMap::new();
map.insert("name", "John");

let res = client.post("https://IP:PORT/endpoint").json(&map).send().await?;

And here's my sample Python code:

path_to_cert = "path/to/cert/my_cert.pem"
route = "https://IP:PORT/endpoint"
payload = {
"name": "John",
}
res = requests.post(url, data = json.dumps(payload), verify=path_to_cert)

Not sure what is causing this issue? Any help would be greatly appreciated, thank you!

I have built a rust-warp server, and I am posting data to it for testing. I expect my rust-request to successfully post data to my server, but my server logs:

TLS alert received AlertMessagePayload { level: Fatal, description: CertificateUnknown, }

1

There are 1 best solutions below

0
Breedly On

It's a bug: https://github.com/seanmonstar/reqwest/issues/1260

You'll need to use rustls-tls as your tls backend for now.

cargo.toml

[dependencies]
reqwest = { version = "0.11.24", features = ["blocking", "rustls-tls"] }

main.rs

let client = reqwest::blocking::ClientBuilder::new().add_root_certificate(ca_cert)
    .use_rustls_tls()
    .build()?;