Implement get_pw_status_bytes()

This commit is contained in:
Heiko Schaefer 2021-07-03 21:23:17 +02:00
parent 2a836546b3
commit d1f854f2f0
4 changed files with 70 additions and 3 deletions

View file

@ -48,6 +48,9 @@ fn main() -> Result<(), Box<dyn Error>> {
let ext = oc.get_extended_capabilities()?;
println!("extended_capabilities {:#x?}", ext);
let pws = oc.get_pw_status_bytes()?;
println!("PW Status Bytes {:#x?}", pws);
// cardholder
let ch = oc.get_cardholder_related_data()?;

View file

@ -11,7 +11,7 @@ use parse::{
algo_attrs::Algo, algo_info::AlgoInfo, application_id::ApplicationId,
cardholder::CardHolder, extended_cap::ExtendedCap, extended_cap::Features,
extended_length_info::ExtendedLengthInfo, fingerprint,
historical::Historical, KeySet,
historical::Historical, pw_status::PWStatus, KeySet,
};
use tlv::Tlv;
@ -384,8 +384,20 @@ impl CardBase {
}
}
pub fn get_pw_status_bytes() {
unimplemented!()
/// PW status Bytes
pub fn get_pw_status_bytes(&self) -> Result<PWStatus> {
// get from cached "application related data"
let psb = self.ard.find(&Tag::from([0xc4]));
if let Some(psb) = psb {
let pws = PWStatus::try_from(&psb.serialize())?;
log::debug!("PW Status: {:x?}", pws);
Ok(pws)
} else {
Err(anyhow!("Failed to get PW status Bytes.").into())
}
}
pub fn get_fingerprints(

View file

@ -12,6 +12,7 @@ pub mod extended_cap;
pub mod extended_length_info;
pub mod fingerprint;
pub mod historical;
pub mod pw_status;
use anyhow::{anyhow, Error};

View file

@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2021 Heiko Schaefer <heiko@schaefer.name>
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::errors::OpenpgpCardError;
use anyhow::anyhow;
#[derive(Debug)]
pub struct PWStatus {
pub(crate) pw1_cds_multi: bool,
pub(crate) pw1_derived: bool,
pub(crate) pw1_len: u8,
pub(crate) rc_len: u8,
pub(crate) pw3_derived: bool,
pub(crate) pw3_len: u8,
pub(crate) err_count_pw1: u8,
pub(crate) err_count_rst: u8,
pub(crate) err_count_pw3: u8,
}
impl PWStatus {
pub fn try_from(input: &[u8]) -> Result<Self, OpenpgpCardError> {
if input.len() == 7 {
let pw1_cds_multi = input[0] == 0x01;
let pw1_derived = input[1] & 0x80 != 0;
let pw1_len = input[1] & 0x7f;
let rc_len = input[2];
let pw3_derived = input[3] & 0x80 != 0;
let pw3_len = input[3] & 0x7f;
let err_count_pw1 = input[4];
let err_count_rst = input[5];
let err_count_pw3 = input[6];
Ok(Self {
pw1_cds_multi,
pw1_derived,
pw1_len,
rc_len,
pw3_derived,
pw3_len,
err_count_pw1,
err_count_rst,
err_count_pw3,
})
} else {
Err(OpenpgpCardError::InternalError(anyhow!(
"Unexpected length of PW Status Bytes: {}",
input.len()
)))
}
}
}