Skip to content

Commit

Permalink
Command line utility
Browse files Browse the repository at this point in the history
For now it can only parse an end entity DER certificate. Can be
extended further later.

Is not meant to be public, does not provide a stable interface.

Useful mostly for debugging of WebPKI itself.

```
% cargo run -p webpki-bin -- print-cert ./tests/netflix/ca.der
...
target/debug/webpki: failed to parse a cert ./tests/netflix/ca.der: BadDER
zsh: exit 1     cargo run -p webpki-bin -- print-cert ./tests/netflix/ca.der

% cargo run -p webpki-bin -- print-cert ./tests/netflix/ee.der
...
cert ./tests/netflix/ee.der parsed successfully
```

I agree to license my contributions to each file under the terms
given at the top of each file I changed.
  • Loading branch information
stepancheg committed Feb 17, 2021
1 parent 082a744 commit 3ed5630
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ rpath = false
lto = true
debug-assertions = false
codegen-units = 1

[workspace]
members = [
".",
"webpki-bin",
]
27 changes: 27 additions & 0 deletions webpki-bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2021 Brian Smith.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

[package]
authors = ["Brian Smith <[email protected]>"]
description = "Test binaries for webpki crate."
edition = "2018"
license-file = "../LICENSE"
name = "webpki-bin"
repository = "https://github.com/briansmith/webpki"
version = "0.0.0"
publish = false

[dependencies]
webpki = { version = "*", path = "..", features = ["std"] }
clap = "3.0.0-beta.2"
61 changes: 61 additions & 0 deletions webpki-bin/src/bin/webpki.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use clap::Clap;
use std::convert::TryFrom;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;

#[derive(Clap)]
#[clap(about = "Parse and print a certificate")]
struct PrintCert {
filename: PathBuf,
}

#[derive(Clap)]
enum Command {
PrintCert(PrintCert),
}

#[derive(Clap)]
#[clap(about = "Utility to debug webpki. Does not have a stable API.")]
struct Opts {
#[clap(subcommand)]
command: Command,
}

fn print_error_and_exit(s: String) -> ! {
let arg0 = env::args().next();
let arg0 = arg0.as_ref().map(|s| s.as_str()).unwrap_or("webpki");
eprintln!("{}: {}", arg0, s);
process::exit(1)
}

fn print_cert(args: PrintCert) {
let der = match fs::read(&args.filename) {
Ok(der) => der,
Err(e) => print_error_and_exit(format!(
"cannot read file {}: {}",
args.filename.display(),
e
)),
};

match webpki::EndEntityCert::try_from(der.as_slice()) {
Ok(cert) => cert,
Err(e) => print_error_and_exit(format!(
"failed to parse a cert {}: {}",
&args.filename.display(),
e
)),
};

eprintln!("cert {} parsed successfully", &args.filename.display());
// TODO: actually print something about the certificate
}

fn main() {
let opts: Opts = Opts::parse();
match opts.command {
Command::PrintCert(args) => print_cert(args),
}
}

0 comments on commit 3ed5630

Please sign in to comment.