Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature flag that controls PIN caching. #506

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pbkdf2 = { version = "0.12", default-features = false, features = ["hmac"] }
pcsc = "2.3.1"
rand_core = { version = "0.6", features = ["std"] }
rsa = "0.9"
secrecy = "0.8"
secrecy = { version = "0.8", optional = true }
sha1 = { version = "0.10", features = ["oid"] }
sha2 = { version = "0.10", features = ["oid"] }
subtle = "2"
Expand All @@ -53,7 +53,9 @@ once_cell = "1"
signature = "2"

[features]
default = ["cache-pin"]
untested = []
cache-pin = ["secrecy"]

[package.metadata.docs.rs]
all-features = true
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ pub use crate::{
policy::{PinPolicy, TouchPolicy},
reader::Context,
setting::{Setting, SettingSource},
yubikey::{CachedPin, Serial, Version, YubiKey},
yubikey::{Serial, Version, YubiKey},
};

#[cfg(feature = "cache-pin")]
pub use crate::yubikey::CachedPin;

#[cfg(feature = "untested")]
pub use crate::{mscmap::MsContainer, msroots::MsRoots};

Expand Down
13 changes: 11 additions & 2 deletions src/yubikey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ use {
transaction::ChangeRefAction,
Buffer, ObjectId,
},
secrecy::ExposeSecret,
std::time::{SystemTime, UNIX_EPOCH},
};

#[cfg(all(feature = "untested", feature = "cache-pin"))]
use secrecy::ExposeSecret;

/// Flag for PUK blocked
pub(crate) const ADMIN_FLAGS_1_PUK_BLOCKED: u8 = 0x01;

Expand All @@ -75,6 +77,7 @@ pub(crate) const KEY_CARDMGM: u8 = 0x9b;
const TAG_DYN_AUTH: u8 = 0x7c;

/// Cached YubiKey PIN.
#[cfg(feature = "cache-pin")]
pub type CachedPin = secrecy::SecretVec<u8>;

/// YubiKey serial number.
Expand Down Expand Up @@ -160,6 +163,7 @@ impl Display for Version {
pub struct YubiKey {
pub(crate) card: Card,
pub(crate) name: String,
#[cfg(feature = "cache-pin")]
pub(crate) pin: Option<CachedPin>,
pub(crate) version: Version,
pub(crate) serial: Serial,
Expand Down Expand Up @@ -242,7 +246,7 @@ impl YubiKey {
}

/// Reconnect to a YubiKey.
#[cfg(feature = "untested")]
#[cfg(all(feature = "untested", feature = "cache-pin"))]
#[cfg_attr(docsrs, doc(cfg(feature = "untested")))]
pub fn reconnect(&mut self) -> Result<()> {
info!("trying to reconnect to current reader");
Expand Down Expand Up @@ -281,6 +285,7 @@ impl YubiKey {
let Self {
card,
name,
#[cfg(feature = "cache-pin")]
pin,
version,
serial,
Expand All @@ -291,6 +296,7 @@ impl YubiKey {
Self {
card,
name,
#[cfg(feature = "cache-pin")]
pin,
version,
serial,
Expand Down Expand Up @@ -430,6 +436,7 @@ impl YubiKey {
txn.verify_pin(pin)?;
}

#[cfg(feature = "cache-pin")]
if !pin.is_empty() {
self.pin = Some(CachedPin::new(pin.into()))
}
Expand Down Expand Up @@ -488,6 +495,7 @@ impl YubiKey {
txn.change_ref(ChangeRefAction::ChangePin, current_pin, new_pin)?;
}

#[cfg(feature = "cache-pin")]
if !new_pin.is_empty() {
self.pin = Some(CachedPin::new(new_pin.into()));
}
Expand Down Expand Up @@ -730,6 +738,7 @@ impl<'a> TryFrom<&'a Reader<'_>> for YubiKey {
let yubikey = YubiKey {
card,
name: String::from(reader.name()),
#[cfg(feature = "cache-pin")]
pin: None,
version,
serial,
Expand Down