Extract slot UI indexing into util functions

This commit is contained in:
Jack Grigg
2021-03-29 19:17:44 +13:00
parent 850f96cd2c
commit 4ea8506e49
3 changed files with 21 additions and 24 deletions
+15 -1
View File
@@ -1,13 +1,27 @@
use x509_parser::{certificate::X509Certificate, der_parser::oid::Oid};
use yubikey_piv::{
key::RetiredSlotId,
policy::{PinPolicy, TouchPolicy},
Key, YubiKey,
};
use crate::{error::Error, p256::Recipient, yubikey::Stub, PLUGIN_NAME};
use crate::{error::Error, p256::Recipient, yubikey::Stub, PLUGIN_NAME, USABLE_SLOTS};
pub(crate) const POLICY_EXTENSION_OID: &[u64] = &[1, 3, 6, 1, 4, 1, 41482, 3, 8];
pub(crate) fn ui_to_slot(slot: u8) -> Result<RetiredSlotId, Error> {
// Use 1-indexing in the UI for niceness
USABLE_SLOTS
.get(slot as usize - 1)
.cloned()
.ok_or(Error::InvalidSlot(slot))
}
pub(crate) fn slot_to_ui(slot: &RetiredSlotId) -> u8 {
// Use 1-indexing in the UI for niceness
USABLE_SLOTS.iter().position(|s| s == slot).unwrap() as u8 + 1
}
pub(crate) fn pin_policy_from_string(s: String) -> Result<PinPolicy, Error> {
match s.as_str() {
"always" => Ok(PinPolicy::Always),