Skip to content

Commit

Permalink
Lints + cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Nov 6, 2021
1 parent 5b61554 commit 9a41828
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 135 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ jobs:
rust: [stable]

steps:
- name: Ubuntu dependencies
if: matrix.os == 'ubuntu-latest'
run: sudo apt-get update && sudo apt-get install -y libpcsclite-dev libudev-dev

- name: Checkout code
uses: actions/checkout@v2

Expand All @@ -24,7 +28,7 @@ jobs:
override: true
components: rustfmt, clippy

- name: cargo build
- name: cargo build --locked
uses: actions-rs/cargo@v1
with:
command: build
Expand Down
5 changes: 3 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ fn main() {
// }

// place side by side with binaries
let outdir = path::PathBuf::from(path::PathBuf::from(env_outdir).ancestors().skip(3).next().unwrap());
let outdir = path::PathBuf::from(path::PathBuf::from(env_outdir).ancestors().nth(3).unwrap());
fs::create_dir_all(&outdir).unwrap();
println!("{:?}", &outdir);

#[cfg(feature = "cli")] {
#[cfg(feature = "cli")]
{
use clap::Shell;

// Use clap to build completion files.
Expand Down
41 changes: 21 additions & 20 deletions src/apps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hex_literal::hex;

use crate::device_selection::{prompt_user_to_select_device, Device};
use crate::{Card, Result};
use crate::device_selection::{Device, prompt_user_to_select_device};

pub mod admin;
pub mod ndef;
Expand All @@ -10,20 +10,20 @@ pub mod piv;
pub mod provisioner;
pub mod tester;

pub const NFC_FORUM_RID: &'static [u8] = &hex!("D276000085");
pub const NIST_RID: &'static [u8] = &hex!("A000000308");
pub const SOLOKEYS_RID: &'static [u8] = &hex!("A000000847");
pub const YUBICO_RID: &'static [u8] = &hex!("A000000527");
pub const NFC_FORUM_RID: &[u8] = &hex!("D276000085");
pub const NIST_RID: &[u8] = &hex!("A000000308");
pub const SOLOKEYS_RID: &[u8] = &hex!("A000000847");
pub const YUBICO_RID: &[u8] = &hex!("A000000527");

pub const ADMIN_PIX: &'static [u8] = &hex!("00000001");
pub const NDEF_PIX: &'static [u8] = &hex!("0101");
pub const OATH_PIX: &'static [u8] = &hex!("2101");
pub const ADMIN_PIX: &[u8] = &hex!("00000001");
pub const NDEF_PIX: &[u8] = &hex!("0101");
pub const OATH_PIX: &[u8] = &hex!("2101");
// the full PIX ends with 0100 for version 01.00,
// truncated is enough to select
// pub const PIV_PIX: &'static [u8] = &hex!("000010000100");
pub const PIV_PIX: &'static [u8] = &hex!("00001000");
pub const PROVISIONER_PIX: &'static [u8] = &hex!("01000001");
pub const TESTER_PIX: &'static [u8] = &hex!("01000000");
// pub const PIV_PIX: &[u8] = &hex!("000010000100");
pub const PIV_PIX: &[u8] = &hex!("00001000");
pub const PROVISIONER_PIX: &[u8] = &hex!("01000001");
pub const TESTER_PIX: &[u8] = &hex!("01000000");

pub trait App: Sized {
const RID: &'static [u8];
Expand Down Expand Up @@ -53,11 +53,12 @@ pub trait App: Sized {
fn card(&mut self) -> &mut Card;

fn connect(uuid: Option<[u8; 16]>) -> Result<Card> {

let mut cards = Card::list(Default::default());

if cards.len() == 0 {
return Err(anyhow::anyhow!("Could not find any Solo 2 devices connected."));
if cards.is_empty() {
return Err(anyhow::anyhow!(
"Could not find any Solo 2 devices connected."
));
}

if cards.len() > 1 {
Expand All @@ -71,10 +72,11 @@ pub trait App: Sized {
}
}

return Err(anyhow::anyhow!("Could not find any Solo 2 device with uuid {}.", hex::encode(uuid)));

return Err(anyhow::anyhow!(
"Could not find any Solo 2 device with uuid {}.",
hex::encode(uuid)
));
} else {

let mut devices: Vec<Device> = Default::default();
for card in cards {
devices.push(card.into())
Expand All @@ -87,7 +89,6 @@ pub trait App: Sized {
// Only one card, use it.
Ok(cards.remove(0))
}

}

fn new(uuid: Option<[u8; 16]>) -> Result<Self>;
Expand All @@ -97,7 +98,7 @@ pub trait App: Sized {
}

fn call_with(&mut self, instruction: u8, data: &[u8]) -> Result<Vec<u8>> {
self.card().call(0, instruction, 0x00, 0x00, Some(&data))
self.card().call(0, instruction, 0x00, 0x00, Some(data))
}

fn print_aid() {
Expand Down
20 changes: 13 additions & 7 deletions src/apps/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl From<Version> for u32 {
}

impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
Expand All @@ -55,7 +55,11 @@ impl From<[u8; 4]> for Version {
let minor = ((version >> 6) & ((1 << 16) - 1)) as _;
let patch = (version & ((1 << 6) - 1)) as _;

Self { major, minor, patch }
Self {
major,
minor,
patch,
}
}
}

Expand Down Expand Up @@ -84,15 +88,17 @@ impl App {
bytes
.try_into()
.map_err(|_| anyhow::anyhow!("expected 16 byte UUID, got {}", &hex::encode(bytes)))
.map(|bytes| u128::from_be_bytes(bytes))
.map(u128::from_be_bytes)
}

pub fn version(&mut self) -> Result<Version> {
let version_bytes = self.call(Self::VERSION_COMMAND)?;
let bytes: [u8; 4] = version_bytes.as_slice()
.try_into()
.map_err(|_| anyhow::anyhow!("expected 4 bytes version, got {}", &hex::encode(version_bytes)))?;
let bytes: [u8; 4] = version_bytes.as_slice().try_into().map_err(|_| {
anyhow::anyhow!(
"expected 4 bytes version, got {}",
&hex::encode(version_bytes)
)
})?;
Ok(bytes.into())

}
}
2 changes: 1 addition & 1 deletion src/apps/provisioner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl App {
bytes
.try_into()
.map_err(|_| anyhow::anyhow!("expected 16 byte UUID, got {}", &hex::encode(bytes)))
.map(|bytes| u128::from_be_bytes(bytes))
.map(u128::from_be_bytes)
}

pub fn write_file(&mut self, data: &[u8], path: &str) -> Result<()> {
Expand Down
12 changes: 2 additions & 10 deletions src/bin/solo2/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ extern crate log;

mod cli;

// use core::convert::TryFrom;

use anyhow::anyhow;
use lpc55::bootloader::Bootloader;

use solo2;

#[tokio::main]
async fn main() {
pretty_env_logger::init_custom_env("SOLO2_LOG");
Expand All @@ -24,7 +20,6 @@ async fn main() {
}

async fn try_main(args: clap::ArgMatches<'_>) -> anyhow::Result<()> {

let uuid_vec_maybe = args.value_of("uuid").map(|uuid| hex::decode(uuid).unwrap());
let uuid = if let Some(uuid_vec) = uuid_vec_maybe {
if uuid_vec.len() != 16 {
Expand Down Expand Up @@ -177,9 +172,7 @@ async fn try_main(args: clap::ArgMatches<'_>) -> anyhow::Result<()> {
}
if let Some(args) = args.subcommand_matches("store-t1-pubkey") {
let pubkey_file = args.value_of("BYTES").unwrap();
let public_key: [u8; 32] = std::fs::read(pubkey_file)?
.as_slice()
.try_into()?;
let public_key: [u8; 32] = std::fs::read(pubkey_file)?.as_slice().try_into()?;
app.store_trussed_t1_intermediate_public_key(public_key)?;
}
if args.subcommand_matches("boot-to-bootrom").is_some() {
Expand All @@ -193,7 +186,7 @@ async fn try_main(args: clap::ArgMatches<'_>) -> anyhow::Result<()> {
let file = args.value_of("DATA").unwrap();
let data = std::fs::read(file)?;
let path = args.value_of("PATH").unwrap();
app.write_file(&data, &path)?;
app.write_file(&data, path)?;
}
}

Expand Down Expand Up @@ -232,7 +225,6 @@ async fn try_main(args: clap::ArgMatches<'_>) -> anyhow::Result<()> {
}

if let Some(args) = args.subcommand_matches("bootloader") {

if args.subcommand_matches("reboot").is_some() {
let bootloader = solo2::device_selection::find_bootloader(uuid)?;
bootloader.reboot();
Expand Down
41 changes: 23 additions & 18 deletions src/device_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use anyhow::anyhow;
use lpc55::bootloader::Bootloader;

use crate::{Card};
use crate::Card;

pub enum Device {
Card(Card),
Expand All @@ -15,7 +15,9 @@ impl Device {
/// Not guaranteed to work with other devices.
pub fn uuid(&self) -> crate::Result<u128> {
match self {
Device::Card(card) => card.uuid.ok_or(anyhow!("Device does not have a UUID")),
Device::Card(card) => card
.uuid
.ok_or_else(|| anyhow!("Device does not have a UUID")),
Device::Bootloader(bootloader) => Ok(bootloader.uuid),
}
}
Expand Down Expand Up @@ -50,8 +52,7 @@ impl From<Bootloader> for Device {
/// Return a specific bootloader that is connected.
/// If no uuid is specified and there are multiple connected, the user will be prompted.
pub fn find_bootloader(uuid: Option<[u8; 16]>) -> crate::Result<Bootloader> {
let bootloaders =
Bootloader::list();
let bootloaders = Bootloader::list();

if let Some(uuid) = uuid {
let uuid_native = u128::from_be_bytes(uuid);
Expand All @@ -60,9 +61,11 @@ pub fn find_bootloader(uuid: Option<[u8; 16]>) -> crate::Result<Bootloader> {
return Ok(bootloader);
}
}
return Err(anyhow!("Could not find any Solo 2 device with uuid {}.", hex::encode(uuid)));
return Err(anyhow!(
"Could not find any Solo 2 device with uuid {}.",
hex::encode(uuid)
));
} else {

let mut devices: Vec<Device> = Default::default();
for bootloader in bootloaders {
devices.push(bootloader.into())
Expand All @@ -71,33 +74,35 @@ pub fn find_bootloader(uuid: Option<[u8; 16]>) -> crate::Result<Bootloader> {
let selected = prompt_user_to_select_device(devices)?;
selected.bootloader()
}

}


/// Have user select device from list of devices.
pub fn prompt_user_to_select_device(mut devices: Vec<Device>) -> crate::Result<Device> {
use std::io::{stdin,stdout,Write};
use std::io::{stdin, stdout, Write};

println!(
"Multiple devices connected.
"Multiple devices connected.
Enter 0-{} to select: ",
devices.len()
);

for i in 0 .. devices.len() {
match &devices[i] {
for (i, item) in devices.iter().enumerate() {
match item {
Device::Bootloader(bootloader) => {
println!(
"{} - Bootloader UUID: {}",
i,
hex::encode(bootloader.uuid.to_be_bytes())
);

},
}
Device::Card(card) => {
if let Some(uuid) = card.uuid {
println!("{} - \"{}\" UUID: {}", i, card.reader_name, hex::encode(uuid.to_be_bytes()));
println!(
"{} - \"{}\" UUID: {}",
i,
card.reader_name,
hex::encode(uuid.to_be_bytes())
);
} else {
println!("{} - \"{}\"", i, card.reader_name);
}
Expand All @@ -109,7 +114,9 @@ Enter 0-{} to select: ",
stdout().flush().unwrap();

let mut input = String::new();
stdin().read_line(&mut input).expect("Did not enter a correct string");
stdin()
.read_line(&mut input)
.expect("Did not enter a correct string");

// remove whitespace
input.retain(|c| !c.is_whitespace());
Expand All @@ -121,6 +128,4 @@ Enter 0-{} to select: ",
} else {
Ok(devices.remove(index))
}

}

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
extern crate log;

pub mod apps;
pub mod device_selection;
#[cfg(feature = "dev-pki")]
pub mod dev_pki;
pub mod device_selection;
pub mod error;
pub use error::{Error, Result};
pub mod smartcard;
Expand Down
Loading

0 comments on commit 9a41828

Please sign in to comment.