diff --git a/openpgp-card-sequoia/src/lib.rs b/openpgp-card-sequoia/src/lib.rs index 1364dd2..10d1235 100644 --- a/openpgp-card-sequoia/src/lib.rs +++ b/openpgp-card-sequoia/src/lib.rs @@ -32,9 +32,10 @@ use sequoia_openpgp as openpgp; use openpgp_card::algorithm::{Algo, AlgoInfo, Curve}; use openpgp_card::card_do::{ - ApplicationId, ApplicationRelatedData, Cardholder, ExtendedCap, - ExtendedLengthInfo, Features, Fingerprint, Historical, KeyGenerationTime, - KeySet, PWStatus, SecuritySupportTemplate, Sex, + ApplicationIdentifier, ApplicationRelatedData, CardholderRelatedData, + ExtendedCapabilities, ExtendedLengthInfo, Features, Fingerprint, + HistoricalBytes, KeyGenerationTime, KeySet, PWStatusBytes, + SecuritySupportTemplate, Sex, }; use openpgp_card::crypto_data::{ CardUploadableKey, Cryptogram, EccKey, EccType, Hash, PrivateKeyMaterial, @@ -599,11 +600,11 @@ impl CardBase { self.card_app.get_app_data() } - pub fn get_application_id(&self) -> Result { + pub fn get_application_id(&self) -> Result { self.ard.get_application_id() } - pub fn get_historical(&self) -> Result { + pub fn get_historical(&self) -> Result { self.ard.get_historical() } @@ -621,7 +622,9 @@ impl CardBase { unimplemented!() } - pub fn get_extended_capabilities(&self) -> Result { + pub fn get_extended_capabilities( + &self, + ) -> Result { self.ard.get_extended_capabilities() } @@ -630,7 +633,7 @@ impl CardBase { } /// PW status Bytes - pub fn get_pw_status_bytes(&self) -> Result { + pub fn get_pw_status_bytes(&self) -> Result { self.ard.get_pw_status_bytes() } @@ -676,7 +679,9 @@ impl CardBase { } // --- cardholder related data (65) --- - pub fn get_cardholder_related_data(&mut self) -> Result { + pub fn get_cardholder_related_data( + &mut self, + ) -> Result { self.card_app.get_cardholder_related_data() } diff --git a/openpgp-card/src/card_app.rs b/openpgp-card/src/card_app.rs index 5b51767..44b6c30 100644 --- a/openpgp-card/src/card_app.rs +++ b/openpgp-card/src/card_app.rs @@ -12,8 +12,8 @@ use anyhow::{anyhow, Result}; use crate::algorithm::{Algo, AlgoInfo, AlgoSimple, RsaAttrs}; use crate::apdu::{commands, response::Response}; use crate::card_do::{ - ApplicationRelatedData, Cardholder, Fingerprint, KeyGenerationTime, - PWStatus, SecuritySupportTemplate, Sex, + ApplicationRelatedData, CardholderRelatedData, Fingerprint, + KeyGenerationTime, PWStatusBytes, SecuritySupportTemplate, Sex, }; use crate::crypto_data::{ CardUploadableKey, Cryptogram, EccType, Hash, PublicKeyMaterial, @@ -168,12 +168,14 @@ impl CardApp { } // --- cardholder related data (65) --- - pub fn get_cardholder_related_data(&mut self) -> Result { + pub fn get_cardholder_related_data( + &mut self, + ) -> Result { let crd = commands::cardholder_related_data(); let resp = apdu::send_command(&mut self.card_client, crd, true)?; resp.check_ok()?; - Cardholder::try_from(resp.data()?) + CardholderRelatedData::try_from(resp.data()?) } // --- security support template (7a) --- @@ -546,7 +548,7 @@ impl CardApp { /// (See OpenPGP card spec, pg. 28) pub fn set_pw_status_bytes( &mut self, - pw_status: &PWStatus, + pw_status: &PWStatusBytes, long: bool, ) -> Result { let data = pw_status.serialize_for_put(long); diff --git a/openpgp-card/src/card_do.rs b/openpgp-card/src/card_do.rs index 9803851..dfa506b 100644 --- a/openpgp-card/src/card_do.rs +++ b/openpgp-card/src/card_do.rs @@ -33,19 +33,19 @@ pub struct ApplicationRelatedData(pub(crate) Tlv); impl ApplicationRelatedData { /// Application identifier (AID), ISO 7816-4 - pub fn get_application_id(&self) -> Result { + pub fn get_application_id(&self) -> Result { // get from cached "application related data" let aid = self.0.find(&[0x4f].into()); if let Some(aid) = aid { - Ok(ApplicationId::try_from(&aid.serialize()[..])?) + Ok(ApplicationIdentifier::try_from(&aid.serialize()[..])?) } else { Err(anyhow!("Couldn't get Application ID.").into()) } } /// Historical bytes - pub fn get_historical(&self) -> Result { + pub fn get_historical(&self) -> Result { // get from cached "application related data" let hist = self.0.find(&[0x5f, 0x52].into()); @@ -85,12 +85,14 @@ impl ApplicationRelatedData { } /// Extended Capabilities - pub fn get_extended_capabilities(&self) -> Result { + pub fn get_extended_capabilities( + &self, + ) -> Result { // get from cached "application related data" let ecap = self.0.find(&[0xc0].into()); if let Some(ecap) = ecap { - Ok(ExtendedCap::try_from(&ecap.serialize()[..])?) + Ok(ExtendedCapabilities::try_from(&ecap.serialize()[..])?) } else { Err(anyhow!("Failed to get extended capabilities.").into()) } @@ -112,7 +114,7 @@ impl ApplicationRelatedData { } /// PW status Bytes - pub fn get_pw_status_bytes(&self) -> Result { + pub fn get_pw_status_bytes(&self) -> Result { // get from cached "application related data" let psb = self.0.find(&[0xc4].into()); @@ -189,7 +191,7 @@ impl KeyGenerationTime { /// 4.2.1 Application Identifier (AID) #[derive(Debug, Eq, PartialEq)] -pub struct ApplicationId { +pub struct ApplicationIdentifier { application: u8, version: u16, manufacturer: u16, @@ -198,7 +200,7 @@ pub struct ApplicationId { /// 6 Historical Bytes #[derive(Debug, PartialEq)] -pub struct Historical { +pub struct HistoricalBytes { /// category indicator byte cib: u8, @@ -233,7 +235,7 @@ pub struct CardServiceData { /// 4.4.3.7 Extended Capabilities #[derive(Debug, Eq, PartialEq)] -pub struct ExtendedCap { +pub struct ExtendedCapabilities { features: HashSet, sm_algo: u8, max_len_challenge: u16, @@ -265,7 +267,7 @@ pub struct ExtendedLengthInfo { /// Cardholder Related Data (see spec pg. 22) #[derive(Debug, PartialEq)] -pub struct Cardholder { +pub struct CardholderRelatedData { name: Option, lang: Option>, sex: Option, @@ -304,7 +306,7 @@ impl From for Sex { /// PW status Bytes (see spec page 23) #[derive(Debug, PartialEq)] -pub struct PWStatus { +pub struct PWStatusBytes { pub(crate) pw1_cds_multi: bool, pub(crate) pw1_pin_block: bool, pub(crate) pw1_len: u8, @@ -316,7 +318,7 @@ pub struct PWStatus { pub(crate) err_count_pw3: u8, } -impl PWStatus { +impl PWStatusBytes { pub fn set_pw1_cds_multi(&mut self, val: bool) { self.pw1_cds_multi = val; } diff --git a/openpgp-card/src/card_do/application_id.rs b/openpgp-card/src/card_do/application_id.rs index 1c6f409..9fe7dc5 100644 --- a/openpgp-card/src/card_do/application_id.rs +++ b/openpgp-card/src/card_do/application_id.rs @@ -7,9 +7,9 @@ use anyhow::Result; use nom::{bytes::complete as bytes, number::complete as number}; use std::convert::TryFrom; -use crate::card_do::{complete, ApplicationId}; +use crate::card_do::{complete, ApplicationIdentifier}; -fn parse(input: &[u8]) -> nom::IResult<&[u8], ApplicationId> { +fn parse(input: &[u8]) -> nom::IResult<&[u8], ApplicationIdentifier> { let (input, _) = bytes::tag([0xd2, 0x76, 0x0, 0x1, 0x24])(input)?; let (input, application) = number::u8(input)?; @@ -22,7 +22,7 @@ fn parse(input: &[u8]) -> nom::IResult<&[u8], ApplicationId> { Ok(( input, - ApplicationId { + ApplicationIdentifier { application, version, manufacturer, @@ -31,7 +31,7 @@ fn parse(input: &[u8]) -> nom::IResult<&[u8], ApplicationId> { )) } -impl TryFrom<&[u8]> for ApplicationId { +impl TryFrom<&[u8]> for ApplicationIdentifier { type Error = anyhow::Error; fn try_from(data: &[u8]) -> Result { @@ -39,7 +39,7 @@ impl TryFrom<&[u8]> for ApplicationId { } } -impl ApplicationId { +impl ApplicationIdentifier { pub fn application(&self) -> u8 { self.application } @@ -79,12 +79,12 @@ mod test { 0x42, 0x40, 0x0, 0x0, ]; - let aid = ApplicationId::try_from(&data[..]) + let aid = ApplicationIdentifier::try_from(&data[..]) .expect("failed to parse application id"); assert_eq!( aid, - ApplicationId { + ApplicationIdentifier { application: 0x1, version: 0x200, manufacturer: 0xfffe, diff --git a/openpgp-card/src/card_do/cardholder.rs b/openpgp-card/src/card_do/cardholder.rs index 4eb443d..36fcf97 100644 --- a/openpgp-card/src/card_do/cardholder.rs +++ b/openpgp-card/src/card_do/cardholder.rs @@ -7,10 +7,10 @@ use std::convert::TryFrom; use anyhow::Result; -use crate::card_do::{Cardholder, Sex}; +use crate::card_do::{CardholderRelatedData, Sex}; use crate::tlv::{value::Value, Tlv}; -impl Cardholder { +impl CardholderRelatedData { pub fn name(&self) -> Option<&str> { self.name.as_deref() } @@ -24,7 +24,7 @@ impl Cardholder { } } -impl TryFrom<&[u8]> for Cardholder { +impl TryFrom<&[u8]> for CardholderRelatedData { type Error = anyhow::Error; fn try_from(data: &[u8]) -> Result { @@ -49,7 +49,7 @@ impl TryFrom<&[u8]> for Cardholder { .filter(|v| v.len() == 1) .map(|v| Sex::from(v[0])); - Ok(Cardholder { name, lang, sex }) + Ok(CardholderRelatedData { name, lang, sex }) } } @@ -64,12 +64,12 @@ mod test { 0x2d, 0x4, 0x64, 0x65, 0x65, 0x6e, 0x5f, 0x35, 0x1, 0x32, ]; - let ch = Cardholder::try_from(&data[..]) + let ch = CardholderRelatedData::try_from(&data[..]) .expect("failed to parse cardholder"); assert_eq!( ch, - Cardholder { + CardholderRelatedData { name: Some("Bar< nom::IResult<&[u8], HashSet> { @@ -58,7 +58,7 @@ fn parse( )))(input) } -impl ExtendedCap { +impl ExtendedCapabilities { pub fn features(&self) -> HashSet { self.features.clone() } @@ -68,7 +68,7 @@ impl ExtendedCap { } } -impl TryFrom<&[u8]> for ExtendedCap { +impl TryFrom<&[u8]> for ExtendedCapabilities { type Error = Error; fn try_from(input: &[u8]) -> Result { @@ -88,7 +88,7 @@ impl TryFrom<&[u8]> for ExtendedCap { #[cfg(test)] mod test { - use crate::card_do::extended_cap::{ExtendedCap, Features}; + use crate::card_do::extended_cap::{ExtendedCapabilities, Features}; use hex_literal::hex; use std::collections::HashSet; use std::convert::TryFrom; @@ -97,11 +97,11 @@ mod test { #[test] fn test_ec() { let data = hex!("7d 00 0b fe 08 00 00 ff 00 00"); - let ec = ExtendedCap::try_from(&data[..]).unwrap(); + let ec = ExtendedCapabilities::try_from(&data[..]).unwrap(); assert_eq!( ec, - ExtendedCap { + ExtendedCapabilities { features: HashSet::from_iter(vec![ Features::GetChallenge, Features::KeyImport, diff --git a/openpgp-card/src/card_do/historical.rs b/openpgp-card/src/card_do/historical.rs index b1b2aa8..1462cd5 100644 --- a/openpgp-card/src/card_do/historical.rs +++ b/openpgp-card/src/card_do/historical.rs @@ -3,7 +3,7 @@ //! 6 Historical Bytes -use crate::card_do::{CardCapabilities, CardServiceData, Historical}; +use crate::card_do::{CardCapabilities, CardServiceData, HistoricalBytes}; use crate::Error; use anyhow::{anyhow, Result}; use std::convert::TryFrom; @@ -71,13 +71,13 @@ fn split_tl(tl: u8) -> (u8, u8) { (tag, len) } -impl Historical { +impl HistoricalBytes { pub fn get_card_capabilities(&self) -> Option<&CardCapabilities> { self.cc.as_ref() } } -impl TryFrom<&[u8]> for Historical { +impl TryFrom<&[u8]> for HistoricalBytes { type Error = Error; fn try_from(data: &[u8]) -> Result { @@ -213,11 +213,11 @@ mod test { // gnuk 1.2 stable let data: &[u8] = &[0x0, 0x31, 0x84, 0x73, 0x80, 0x1, 0x80, 0x5, 0x90, 0x0]; - let hist: Historical = data.try_into()?; + let hist: HistoricalBytes = data.try_into()?; assert_eq!( hist, - Historical { + HistoricalBytes { cib: 0, csd: Some(CardServiceData { select_by_full_df_name: true, @@ -244,11 +244,11 @@ mod test { // floss shop openpgp smartcard 3.4 let data: &[u8] = &[0x0, 0x31, 0xf5, 0x73, 0xc0, 0x1, 0x60, 0x5, 0x90, 0x0]; - let hist: Historical = data.try_into()?; + let hist: HistoricalBytes = data.try_into()?; assert_eq!( hist, - Historical { + HistoricalBytes { cib: 0, csd: Some(CardServiceData { select_by_full_df_name: true, @@ -274,11 +274,11 @@ mod test { fn test_yk5() -> Result<()> { // yubikey 5 let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0xe0, 0x5, 0x90, 0x0]; - let hist: Historical = data.try_into()?; + let hist: HistoricalBytes = data.try_into()?; assert_eq!( hist, - Historical { + HistoricalBytes { cib: 0, csd: None, cc: Some(CardCapabilities { @@ -297,11 +297,11 @@ mod test { fn test_yk4() -> Result<()> { // yubikey 4 let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0x80, 0x5, 0x90, 0x0]; - let hist: Historical = data.try_into()?; + let hist: HistoricalBytes = data.try_into()?; assert_eq!( hist, - Historical { + HistoricalBytes { cib: 0, csd: None, cc: Some(CardCapabilities { @@ -323,11 +323,11 @@ mod test { 0x0, 0x73, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ]; - let hist: Historical = data.try_into()?; + let hist: HistoricalBytes = data.try_into()?; assert_eq!( hist, - Historical { + HistoricalBytes { cib: 0, csd: None, cc: Some(CardCapabilities { diff --git a/openpgp-card/src/card_do/pw_status.rs b/openpgp-card/src/card_do/pw_status.rs index 898d218..b129160 100644 --- a/openpgp-card/src/card_do/pw_status.rs +++ b/openpgp-card/src/card_do/pw_status.rs @@ -5,11 +5,11 @@ use anyhow::anyhow; -use crate::card_do::PWStatus; +use crate::card_do::PWStatusBytes; use crate::Error; use std::convert::TryFrom; -impl PWStatus { +impl PWStatusBytes { /// PUT DO for PW Status Bytes accepts either 1 or 4 bytes of data. /// This method generates the 1 byte version for 'long==false' and the /// 4 bytes version for 'long==true'. @@ -40,7 +40,7 @@ impl PWStatus { } } -impl TryFrom<&[u8]> for PWStatus { +impl TryFrom<&[u8]> for PWStatusBytes { type Error = Error; fn try_from(input: &[u8]) -> Result { @@ -84,12 +84,12 @@ mod test { fn test() { let data = [0x0, 0x40, 0x40, 0x40, 0x3, 0x0, 0x3]; - let pws: PWStatus = + let pws: PWStatusBytes = (&data[..]).try_into().expect("failed to parse PWStatus"); assert_eq!( pws, - PWStatus { + PWStatusBytes { pw1_cds_multi: false, pw1_pin_block: false, pw1_len: 0x40,