Skip to content

Commit

Permalink
CCCID/CHUID refactoring
Browse files Browse the repository at this point in the history
- Move generate methods to the appropriate static types
- Remove redundant name prefixes (Rust [RFC#356])

[RFC#356]: rust-lang/rfcs#356
  • Loading branch information
tarcieri committed Dec 7, 2019
1 parent 3cf3c08 commit 2587a4a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
28 changes: 15 additions & 13 deletions src/cccid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ const CCC_TMPL: &[u8] = &[
0x00, 0xfe, 0x00,
];

/// Cardholder Capability Container (CCC) Identifier card ID
/// Cardholder Capability Container (CCC) Identifier Card ID
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct CccCardId(pub [u8; YKPIV_CCCID_SIZE]);
pub struct CardId(pub [u8; YKPIV_CCCID_SIZE]);

impl CardId {
/// Generate a random CCC Card ID
pub fn generate() -> Result<Self, Error> {
let mut id = [0u8; YKPIV_CCCID_SIZE];
getrandom(&mut id).map_err(|_| Error::RandomnessError)?;
Ok(Self(id))
}
}

/// Cardholder Capability Container (CCC) Identifier
#[derive(Copy, Clone)]
pub struct CCC(pub [u8; YKPIV_CCC_SIZE]);

impl CCC {
/// Return CardId component of CHUID
pub fn cccid(&self) -> Result<CccCardId, Error> {
/// Return CardId component of CCC
pub fn cccid(&self) -> Result<CardId, Error> {
let mut cccid = [0u8; YKPIV_CCCID_SIZE];
cccid.copy_from_slice(&self.0[CCC_ID_OFFS..(CCC_ID_OFFS + YKPIV_CCCID_SIZE)]);
Ok(CccCardId(cccid))
}

/// Generate a random CCCID
pub fn generate() -> Result<CccCardId, Error> {
let mut id = [0u8; YKPIV_CCCID_SIZE];
getrandom(&mut id).map_err(|_| Error::RandomnessError)?;
Ok(CccCardId(id))
Ok(CardId(cccid))
}

/// Get Cardholder Capability Container (CCC) ID
Expand All @@ -82,7 +84,7 @@ impl CCC {

let mut ccc = [0u8; YKPIV_CCC_SIZE];
ccc.copy_from_slice(&response[0..YKPIV_CCC_SIZE]);
Ok(CCC { 0: ccc })
Ok(Self(ccc))
}

/// Get Cardholder Capability Container (CCC) ID
Expand Down
18 changes: 10 additions & 8 deletions src/chuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ const CHUID_TMPL: &[u8] = &[

/// Cardholder Unique Identifier (CHUID) Card UUID/GUID value
#[derive(Copy, Clone, Debug)]
pub struct ChuidUuid(pub [u8; YKPIV_CARDID_SIZE]);
pub struct Uuid(pub [u8; YKPIV_CARDID_SIZE]);

impl Uuid {
/// Generate a random Cardholder Unique Identifier (CHUID)
pub fn generate() -> Result<Self, Error> {
let mut id = [0u8; YKPIV_CARDID_SIZE];
getrandom(&mut id).map_err(|_| Error::RandomnessError)?;
Ok(Self(id))
}
}

/// Cardholder Unique Identifier (CHUID)
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -87,13 +96,6 @@ impl CHUID {
Ok(expiration)
}

/// Generate a random Cardholder Unique Identifier (CHUID)
pub fn generate() -> Result<ChuidUuid, Error> {
let mut id = [0u8; YKPIV_CARDID_SIZE];
getrandom(&mut id).map_err(|_| Error::RandomnessError)?;
Ok(ChuidUuid(id))
}

/// Get Cardholder Unique Identifier (CHUID)
pub fn get(yubikey: &mut YubiKey) -> Result<CHUID, Error> {
let txn = yubikey.begin_transaction()?;
Expand Down
3 changes: 3 additions & 0 deletions src/yubikey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ impl YubiKey {
Err(Error::GenericError)
}

/// Open a connection to a YubiKey with the given serial number.


/// Reconnect to a YubiKey
#[cfg(feature = "untested")]
pub fn reconnect(&mut self) -> Result<(), Error> {
Expand Down

0 comments on commit 2587a4a

Please sign in to comment.