Skip to content

Commit

Permalink
Run FMT (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
obelisk authored Apr 17, 2022
1 parent f66e482 commit 150e00d
Show file tree
Hide file tree
Showing 48 changed files with 1,242 additions and 848 deletions.
11 changes: 4 additions & 7 deletions benches/certs_per_second.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use criterion::{criterion_group, criterion_main, Criterion};

use sshcerts::yubikey::{
RetiredSlotId,
SlotId,
Yubikey,
};
use sshcerts::yubikey::{RetiredSlotId, SlotId, Yubikey};

fn generate_certs(n: u64) -> () {
let data = [0; 32];
let mut yk = Yubikey::new().unwrap();
for _ in 0..n {
yk.ssh_cert_signer(&data, &SlotId::Retired(RetiredSlotId::R19)).unwrap();
yk.ssh_cert_signer(&data, &SlotId::Retired(RetiredSlotId::R19))
.unwrap();
}
}

Expand All @@ -19,4 +16,4 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions examples/into-ssh-pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn help() {

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() != 2 {
return help();
}
Expand All @@ -27,4 +27,4 @@ fn main() {
Ok(public_key) => println!("{}", public_key),
Err(e) => println!("Error: {}", e),
}
}
}
10 changes: 4 additions & 6 deletions examples/new-fido-sshkey.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;

use clap::{Command, Arg};
use clap::{Arg, Command};

use sshcerts::fido::generate::generate_new_ssh_key;

Expand Down Expand Up @@ -38,7 +38,6 @@ fn main() {
)
.get_matches();


let pin = if let Some(pin) = matches.value_of("pin") {
Some(pin.to_owned())
} else {
Expand All @@ -55,7 +54,7 @@ fn main() {
Ok(key) => {
println!("{:#}", key.private_key.pubkey);

if let Some(out) = matches.value_of("out") {
if let Some(out) = matches.value_of("out") {
let mut out = File::create(out).unwrap();
key.private_key.write(&mut out).unwrap();
} else {
Expand All @@ -64,10 +63,9 @@ fn main() {
let serialized = String::from_utf8(buf.into_inner().unwrap()).unwrap();
println!("Your new private key handle:\n{}", serialized);
}

},
}
Err(e) => {
println!("Failed to generate new SSH Key: {}", e.to_string());
}
}
}
}
17 changes: 8 additions & 9 deletions examples/sign-with-file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;

use clap::{Command, Arg};
use clap::{Arg, Command};

use sshcerts::*;

Expand All @@ -16,44 +16,43 @@ fn main() {
.long("signing_key")
.short('s')
.required(true)
.takes_value(true)
.takes_value(true),
)
.arg(
Arg::new("pin")
.help("If using an SK key handle, what PIN to use with the key (not always needed)")
.long("pin")
.short('p')
.required(false)
.takes_value(true)
.takes_value(true),
)
.arg(
Arg::new("principal")
.help("Add this principal to the certificate")
.long("principal")
.short('n')
.default_value("ubuntu")
.takes_value(true)
.takes_value(true),
)
.arg(
Arg::new("file")
.help("The key to sign with the CA into an SSH certificate")
.long("file")
.short('f')
.required(true)
.takes_value(true)
.takes_value(true),
)
.get_matches();


let ssh_pubkey = PublicKey::from_path(matches.value_of("file").unwrap()).unwrap();
let mut ca_private_key = PrivateKey::from_path(matches.value_of("sign").unwrap()).unwrap();

if let Some(pin) = matches.value_of("pin") {
ca_private_key.set_pin(pin);
}


let user_cert = Certificate::builder(&ssh_pubkey, CertType::User, &ca_private_key.pubkey).unwrap()
let user_cert = Certificate::builder(&ssh_pubkey, CertType::User, &ca_private_key.pubkey)
.unwrap()
.serial(0x0)
.key_id("key_id")
.principal(matches.value_of("principal").unwrap())
Expand All @@ -63,4 +62,4 @@ fn main() {
.sign(&ca_private_key);

println!("{}", user_cert.unwrap());
}
}
27 changes: 14 additions & 13 deletions examples/sign-with-yubikey.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::env;

use clap::{Command, Arg};
use clap::{Arg, Command};

use sshcerts::*;
use sshcerts::ssh::SSHCertificateSigner;
use sshcerts::yubikey::piv::{SlotId, Yubikey};
use sshcerts::*;

use std::convert::TryFrom;


fn slot_parser(slot: &str) -> Option<SlotId> {
// If first character is R, then we need to parse the nice
// notation
Expand All @@ -17,8 +16,8 @@ fn slot_parser(slot: &str) -> Option<SlotId> {
match slot_value {
Ok(v) if v <= 20 => Some(SlotId::try_from(0x81_u8 + v).unwrap()),
_ => None,
}
} else if slot.len() == 4 && slot.starts_with("0x"){
}
} else if slot.len() == 4 && slot.starts_with("0x") {
let slot_value = hex::decode(&slot[2..]).unwrap()[0];
Some(SlotId::try_from(slot_value).unwrap())
} else {
Expand All @@ -29,7 +28,9 @@ fn slot_parser(slot: &str) -> Option<SlotId> {
fn slot_validator(slot: &str) -> Result<(), String> {
match slot_parser(slot) {
Some(_) => Ok(()),
None => Err(String::from("Provided slot was not valid. Should be R1 - R20 or a raw hex identifier")),
None => Err(String::from(
"Provided slot was not valid. Should be R1 - R20 or a raw hex identifier",
)),
}
}

Expand Down Expand Up @@ -60,23 +61,23 @@ fn main() {
.short('s')
.required(true)
.validator(slot_validator)
.takes_value(true)
.takes_value(true),
)
.arg(
Arg::new("principal")
.help("Add this principal to the certificate")
.long("principal")
.short('n')
.default_value("ubuntu")
.takes_value(true)
.takes_value(true),
)
.arg(
Arg::new("key")
.help("The key to sign with the Yubikey into an SSH certificate")
.long("key")
.short('f')
.required(true)
.takes_value(true)
.takes_value(true),
)
.get_matches();

Expand All @@ -85,10 +86,10 @@ fn main() {
let yk_pubkey = yk.ssh_cert_fetch_pubkey(&slot).unwrap();
let ssh_pubkey = PublicKey::from_path(matches.value_of("key").unwrap()).unwrap();

let yk_signer = YubikeySigner{slot};

let yk_signer = YubikeySigner { slot };

let user_cert = Certificate::builder(&ssh_pubkey, CertType::User, &yk_pubkey).unwrap()
let user_cert = Certificate::builder(&ssh_pubkey, CertType::User, &yk_pubkey)
.unwrap()
.serial(0xFEFEFEFEFEFEFEFE)
.key_id("key_id")
.principal(matches.value_of("principal").unwrap())
Expand All @@ -98,4 +99,4 @@ fn main() {
.sign(&yk_signer);

println!("{}", user_cert.unwrap());
}
}
8 changes: 4 additions & 4 deletions examples/ssh-cert-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn help() {

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() != 2 {
return help();
}
Expand All @@ -25,14 +25,14 @@ fn main() {

for (i, line) in contents.split('\n').into_iter().enumerate() {
if line.is_empty() {
break
break;
}

match Certificate::from_string(line) {
Ok(c) => println!("{:#}", c),
Err(e) => {
println!("Line {}: Certificate not valid: {}", i, e);
}
};
}
}
}
8 changes: 3 additions & 5 deletions examples/ssh-pkey-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn help() {

fn main() -> Result<(), String> {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
help();
return Ok(());
Expand All @@ -27,9 +27,7 @@ fn main() -> Result<(), String> {
Ok(c) => {
println!("{:#}", c);
Ok(())
},
Err(e) => {
Err(format!("{}: Private key at {} not valid", e, &args[1]))
}
Err(e) => Err(format!("{}: Private key at {} not valid", e, &args[1])),
}
}
}
14 changes: 8 additions & 6 deletions examples/ssh-pubkey-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn help() {

fn main() -> Result<(), String> {
let args: Vec<String> = env::args().collect();

if args.len() < 2 {
help();
return Ok(());
Expand All @@ -19,11 +19,13 @@ fn main() -> Result<(), String> {

match PublicKey::from_path(path) {
Ok(c) => {
println!("256 SHA256:{} {}", c.fingerprint().hash, c.comment.unwrap_or("no comment".to_string()));
println!(
"256 SHA256:{} {}",
c.fingerprint().hash,
c.comment.unwrap_or("no comment".to_string())
);
Ok(())
},
Err(e) => {
Err(format!("{}: Private key at {} not valid", e, &args[1]))
}
Err(e) => Err(format!("{}: Private key at {} not valid", e, &args[1])),
}
}
}
21 changes: 13 additions & 8 deletions examples/yk-fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ fn main() {
let mut yk = Yubikey::new().unwrap();

println!("Normal Slots:");
for slot in [0x9a, 0x9c, 0x9e, 0x9d, 0x9e, 0xf9].iter().map(|x| *x as u8) {
for slot in [0x9a, 0x9c, 0x9e, 0x9d, 0x9e, 0xf9]
.iter()
.map(|x| *x as u8)
{
let slot = SlotId::try_from(slot).unwrap();
match (yk.fetch_subject(&slot), yk.ssh_cert_fetch_pubkey(&slot)) {
match (yk.fetch_subject(&slot), yk.ssh_cert_fetch_pubkey(&slot)) {
(Ok(subj), Ok(cert)) => {
let attest = yk.fetch_attestation(&slot);
println!("\t{:?}:\t[Fingerprint: {}] [Attest: {}] Subject: [{}]",
println!(
"\t{:?}:\t[Fingerprint: {}] [Attest: {}] Subject: [{}]",
&slot,
cert.fingerprint().hash,
if attest.is_ok() {"Yes" } else { "No "},
if attest.is_ok() { "Yes" } else { "No " },
subj
)
}
Expand All @@ -39,14 +43,15 @@ fn main() {
match (yk.fetch_subject(&slot), yk.ssh_cert_fetch_pubkey(&slot)) {
(Ok(subj), Ok(cert)) => {
let attest = yk.fetch_attestation(&slot);
println!("\t{:?}:\t[Fingerprint: {}] [Attest: {}] Subject: [{}]",
println!(
"\t{:?}:\t[Fingerprint: {}] [Attest: {}] Subject: [{}]",
slot,
cert.fingerprint().hash,
if attest.is_ok() {"Yes" } else { "No "},
if attest.is_ok() { "Yes" } else { "No " },
subj,
)
},
}
_ => println!("\t{:?}:\tNo cert found", slot),
}
}
}
}
Loading

0 comments on commit 150e00d

Please sign in to comment.