CCCID/CHUID refactoring

- Move generate methods to the appropriate static types
- Remove redundant name prefixes (Rust [RFC#356])

[RFC#356]: https://github.com/rust-lang/rfcs/pull/356
This commit is contained in:
Tony Arcieri
2019-12-07 12:39:52 -08:00
parent 3cf3c0867f
commit 2587a4ac1e
3 changed files with 28 additions and 21 deletions
+15 -13
View File
@@ -48,27 +48,29 @@ const CCC_TMPL: &[u8] = &[
0x00, 0xfe, 0x00, 0x00, 0xfe, 0x00,
]; ];
/// Cardholder Capability Container (CCC) Identifier card ID /// Cardholder Capability Container (CCC) Identifier Card ID
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[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 /// Cardholder Capability Container (CCC) Identifier
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct CCC(pub [u8; YKPIV_CCC_SIZE]); pub struct CCC(pub [u8; YKPIV_CCC_SIZE]);
impl CCC { impl CCC {
/// Return CardId component of CHUID /// Return CardId component of CCC
pub fn cccid(&self) -> Result<CccCardId, Error> { pub fn cccid(&self) -> Result<CardId, Error> {
let mut cccid = [0u8; YKPIV_CCCID_SIZE]; let mut cccid = [0u8; YKPIV_CCCID_SIZE];
cccid.copy_from_slice(&self.0[CCC_ID_OFFS..(CCC_ID_OFFS + YKPIV_CCCID_SIZE)]); cccid.copy_from_slice(&self.0[CCC_ID_OFFS..(CCC_ID_OFFS + YKPIV_CCCID_SIZE)]);
Ok(CccCardId(cccid)) Ok(CardId(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))
} }
/// Get Cardholder Capability Container (CCC) ID /// Get Cardholder Capability Container (CCC) ID
@@ -82,7 +84,7 @@ impl CCC {
let mut ccc = [0u8; YKPIV_CCC_SIZE]; let mut ccc = [0u8; YKPIV_CCC_SIZE];
ccc.copy_from_slice(&response[0..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 /// Get Cardholder Capability Container (CCC) ID
+10 -8
View File
@@ -57,7 +57,16 @@ const CHUID_TMPL: &[u8] = &[
/// Cardholder Unique Identifier (CHUID) Card UUID/GUID value /// Cardholder Unique Identifier (CHUID) Card UUID/GUID value
#[derive(Copy, Clone, Debug)] #[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) /// Cardholder Unique Identifier (CHUID)
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@@ -87,13 +96,6 @@ impl CHUID {
Ok(expiration) 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) /// Get Cardholder Unique Identifier (CHUID)
pub fn get(yubikey: &mut YubiKey) -> Result<CHUID, Error> { pub fn get(yubikey: &mut YubiKey) -> Result<CHUID, Error> {
let txn = yubikey.begin_transaction()?; let txn = yubikey.begin_transaction()?;
+3
View File
@@ -155,6 +155,9 @@ impl YubiKey {
Err(Error::GenericError) Err(Error::GenericError)
} }
/// Open a connection to a YubiKey with the given serial number.
/// Reconnect to a YubiKey /// Reconnect to a YubiKey
#[cfg(feature = "untested")] #[cfg(feature = "untested")]
pub fn reconnect(&mut self) -> Result<(), Error> { pub fn reconnect(&mut self) -> Result<(), Error> {