Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get details about the SSL certificate returned by the server? #91

Open
domderen opened this issue Mar 14, 2022 · 1 comment
Open

Comments

@domderen
Copy link

domderen commented Mar 14, 2022

Hey,

I'm wondering if it is possible to obtain details of the SSL certificate returned by the server when using Hyper as an HTTPS client? I've got this example:

#![deny(warnings)]
#![warn(rust_2018_idioms)]
use std::{env};

use hyper::{Client};
use hyper_tls::HttpsConnector;

// A simple type alias so as to DRY.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

#[tokio::main]
async fn main() -> Result<()> {
    pretty_env_logger::init();

    // Some simple CLI args requirements...
    let url = match env::args().nth(1) {
        Some(url) => url,
        None => {
            println!("Usage: client <url>");
            return Ok(());
        }
    };

    // HTTPS requires picking a TLS implementation, so give a better
    // warning if the user tries to request an 'https' URL.
    let url = url.parse::<hyper::Uri>().unwrap();
    

    fetch_url(url).await
}

async fn fetch_url(url: hyper::Uri) -> Result<()> {
    let https = HttpsConnector::new();
    let client = Client::builder().build::<_, hyper::Body>(https);

    let res = client.get(url).await?;

    println!("Response: {}", res.status());
    println!("Headers: {:#?}\n", res.headers());

    println!("Extensions: {:#?}\n", res.extensions().len());

    // Stream the body, writing each chunk to stdout as we get it
    // (instead of buffering and printing at the end).
    // while let Some(next) = res.data().await {
    //     let chunk = next?;
    //     io::stdout().write_all(&chunk).await?;
    // }

    println!("\n\nDone!");

    Ok(())
}

And I'm wondering how can I use the https object or the response object to get the details of the server certificate?

I tried following the code, and it seems that the cert value is not exposed in the public API of this package, but I thought I'd ask.

Thanks in advance for your help!

@seanmonstar
Copy link
Member

The way to access it depends on if the library you're using for a connector includes it in the response extensions. For example, when using the HttpConnector, or one based on it, you can access some of the TCP info using HttpInfo. The hyper-tls library does not seem to include any extra TLS info. Perhaps others do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants