Skip to content

Commit

Permalink
check-cert: let fetch_server_cert() return the DER
Browse files Browse the repository at this point in the history
This doesn't leak the OpenSSL type into main() anymore.  Now, we
can cleanly separate fetching and checking library backends.

CMK-14683

Change-Id: I3c76ea74e3844b68d067a15f097ae3676d9b2bed
  • Loading branch information
Synss committed Nov 16, 2023
1 parent d40e822 commit 89644c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions packages/check-cert/src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use anyhow::{Context, Result};
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
use openssl::x509::X509;
use std::net::TcpStream;
use std::time::Duration;

Expand All @@ -13,7 +12,7 @@ pub fn fetch_server_cert(
port: &u16,
timeout: Option<Duration>,
use_sni: bool,
) -> Result<X509> {
) -> Result<Vec<u8>> {
let stream = TcpStream::connect(format!("{server}:{port}"))?;
stream.set_read_timeout(timeout)?;
let mut connector_builder = SslConnector::builder(SslMethod::tls())?;
Expand All @@ -33,5 +32,5 @@ pub fn fetch_server_cert(
.context("Failed unpacking peer cert chain")?
.to_owned();
stream.shutdown()?;
Ok(cert)
Ok(cert.to_der()?)
}
4 changes: 3 additions & 1 deletion packages/check-cert/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{Context, Result};
use check_cert::{checker, fetcher, output};
use clap::Parser;
use openssl::asn1::Asn1Time;
use openssl::x509::X509;
use std::time::Duration;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -46,7 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
std::process::exit(1);
}

let cert = fetcher::fetch_server_cert(
let der = fetcher::fetch_server_cert(
&args.url,
&args.port,
if args.timeout == 0 {
Expand All @@ -57,6 +58,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
!args.disable_sni,
)?;

let cert = X509::from_der(&der)?;
let out = output::Output::from(vec![checker::check_validity(
&args.url,
cert.not_after(),
Expand Down

0 comments on commit 89644c5

Please sign in to comment.