Adjust DO struct names to correspond to naming in the spec

This commit is contained in:
Heiko Schaefer 2021-09-02 21:54:44 +02:00
parent a415ec9a50
commit 6b3ae2cf62
8 changed files with 71 additions and 62 deletions

View file

@ -32,9 +32,10 @@ use sequoia_openpgp as openpgp;
use openpgp_card::algorithm::{Algo, AlgoInfo, Curve}; use openpgp_card::algorithm::{Algo, AlgoInfo, Curve};
use openpgp_card::card_do::{ use openpgp_card::card_do::{
ApplicationId, ApplicationRelatedData, Cardholder, ExtendedCap, ApplicationIdentifier, ApplicationRelatedData, CardholderRelatedData,
ExtendedLengthInfo, Features, Fingerprint, Historical, KeyGenerationTime, ExtendedCapabilities, ExtendedLengthInfo, Features, Fingerprint,
KeySet, PWStatus, SecuritySupportTemplate, Sex, HistoricalBytes, KeyGenerationTime, KeySet, PWStatusBytes,
SecuritySupportTemplate, Sex,
}; };
use openpgp_card::crypto_data::{ use openpgp_card::crypto_data::{
CardUploadableKey, Cryptogram, EccKey, EccType, Hash, PrivateKeyMaterial, CardUploadableKey, Cryptogram, EccKey, EccType, Hash, PrivateKeyMaterial,
@ -599,11 +600,11 @@ impl CardBase {
self.card_app.get_app_data() self.card_app.get_app_data()
} }
pub fn get_application_id(&self) -> Result<ApplicationId, Error> { pub fn get_application_id(&self) -> Result<ApplicationIdentifier, Error> {
self.ard.get_application_id() self.ard.get_application_id()
} }
pub fn get_historical(&self) -> Result<Historical, Error> { pub fn get_historical(&self) -> Result<HistoricalBytes, Error> {
self.ard.get_historical() self.ard.get_historical()
} }
@ -621,7 +622,9 @@ impl CardBase {
unimplemented!() unimplemented!()
} }
pub fn get_extended_capabilities(&self) -> Result<ExtendedCap, Error> { pub fn get_extended_capabilities(
&self,
) -> Result<ExtendedCapabilities, Error> {
self.ard.get_extended_capabilities() self.ard.get_extended_capabilities()
} }
@ -630,7 +633,7 @@ impl CardBase {
} }
/// PW status Bytes /// PW status Bytes
pub fn get_pw_status_bytes(&self) -> Result<PWStatus> { pub fn get_pw_status_bytes(&self) -> Result<PWStatusBytes> {
self.ard.get_pw_status_bytes() self.ard.get_pw_status_bytes()
} }
@ -676,7 +679,9 @@ impl CardBase {
} }
// --- cardholder related data (65) --- // --- cardholder related data (65) ---
pub fn get_cardholder_related_data(&mut self) -> Result<Cardholder> { pub fn get_cardholder_related_data(
&mut self,
) -> Result<CardholderRelatedData> {
self.card_app.get_cardholder_related_data() self.card_app.get_cardholder_related_data()
} }

View file

@ -12,8 +12,8 @@ use anyhow::{anyhow, Result};
use crate::algorithm::{Algo, AlgoInfo, AlgoSimple, RsaAttrs}; use crate::algorithm::{Algo, AlgoInfo, AlgoSimple, RsaAttrs};
use crate::apdu::{commands, response::Response}; use crate::apdu::{commands, response::Response};
use crate::card_do::{ use crate::card_do::{
ApplicationRelatedData, Cardholder, Fingerprint, KeyGenerationTime, ApplicationRelatedData, CardholderRelatedData, Fingerprint,
PWStatus, SecuritySupportTemplate, Sex, KeyGenerationTime, PWStatusBytes, SecuritySupportTemplate, Sex,
}; };
use crate::crypto_data::{ use crate::crypto_data::{
CardUploadableKey, Cryptogram, EccType, Hash, PublicKeyMaterial, CardUploadableKey, Cryptogram, EccType, Hash, PublicKeyMaterial,
@ -168,12 +168,14 @@ impl CardApp {
} }
// --- cardholder related data (65) --- // --- cardholder related data (65) ---
pub fn get_cardholder_related_data(&mut self) -> Result<Cardholder> { pub fn get_cardholder_related_data(
&mut self,
) -> Result<CardholderRelatedData> {
let crd = commands::cardholder_related_data(); let crd = commands::cardholder_related_data();
let resp = apdu::send_command(&mut self.card_client, crd, true)?; let resp = apdu::send_command(&mut self.card_client, crd, true)?;
resp.check_ok()?; resp.check_ok()?;
Cardholder::try_from(resp.data()?) CardholderRelatedData::try_from(resp.data()?)
} }
// --- security support template (7a) --- // --- security support template (7a) ---
@ -546,7 +548,7 @@ impl CardApp {
/// (See OpenPGP card spec, pg. 28) /// (See OpenPGP card spec, pg. 28)
pub fn set_pw_status_bytes( pub fn set_pw_status_bytes(
&mut self, &mut self,
pw_status: &PWStatus, pw_status: &PWStatusBytes,
long: bool, long: bool,
) -> Result<Response, Error> { ) -> Result<Response, Error> {
let data = pw_status.serialize_for_put(long); let data = pw_status.serialize_for_put(long);

View file

@ -33,19 +33,19 @@ pub struct ApplicationRelatedData(pub(crate) Tlv);
impl ApplicationRelatedData { impl ApplicationRelatedData {
/// Application identifier (AID), ISO 7816-4 /// Application identifier (AID), ISO 7816-4
pub fn get_application_id(&self) -> Result<ApplicationId, Error> { pub fn get_application_id(&self) -> Result<ApplicationIdentifier, Error> {
// get from cached "application related data" // get from cached "application related data"
let aid = self.0.find(&[0x4f].into()); let aid = self.0.find(&[0x4f].into());
if let Some(aid) = aid { if let Some(aid) = aid {
Ok(ApplicationId::try_from(&aid.serialize()[..])?) Ok(ApplicationIdentifier::try_from(&aid.serialize()[..])?)
} else { } else {
Err(anyhow!("Couldn't get Application ID.").into()) Err(anyhow!("Couldn't get Application ID.").into())
} }
} }
/// Historical bytes /// Historical bytes
pub fn get_historical(&self) -> Result<Historical, Error> { pub fn get_historical(&self) -> Result<HistoricalBytes, Error> {
// get from cached "application related data" // get from cached "application related data"
let hist = self.0.find(&[0x5f, 0x52].into()); let hist = self.0.find(&[0x5f, 0x52].into());
@ -85,12 +85,14 @@ impl ApplicationRelatedData {
} }
/// Extended Capabilities /// Extended Capabilities
pub fn get_extended_capabilities(&self) -> Result<ExtendedCap, Error> { pub fn get_extended_capabilities(
&self,
) -> Result<ExtendedCapabilities, Error> {
// get from cached "application related data" // get from cached "application related data"
let ecap = self.0.find(&[0xc0].into()); let ecap = self.0.find(&[0xc0].into());
if let Some(ecap) = ecap { if let Some(ecap) = ecap {
Ok(ExtendedCap::try_from(&ecap.serialize()[..])?) Ok(ExtendedCapabilities::try_from(&ecap.serialize()[..])?)
} else { } else {
Err(anyhow!("Failed to get extended capabilities.").into()) Err(anyhow!("Failed to get extended capabilities.").into())
} }
@ -112,7 +114,7 @@ impl ApplicationRelatedData {
} }
/// PW status Bytes /// PW status Bytes
pub fn get_pw_status_bytes(&self) -> Result<PWStatus> { pub fn get_pw_status_bytes(&self) -> Result<PWStatusBytes> {
// get from cached "application related data" // get from cached "application related data"
let psb = self.0.find(&[0xc4].into()); let psb = self.0.find(&[0xc4].into());
@ -189,7 +191,7 @@ impl KeyGenerationTime {
/// 4.2.1 Application Identifier (AID) /// 4.2.1 Application Identifier (AID)
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct ApplicationId { pub struct ApplicationIdentifier {
application: u8, application: u8,
version: u16, version: u16,
manufacturer: u16, manufacturer: u16,
@ -198,7 +200,7 @@ pub struct ApplicationId {
/// 6 Historical Bytes /// 6 Historical Bytes
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Historical { pub struct HistoricalBytes {
/// category indicator byte /// category indicator byte
cib: u8, cib: u8,
@ -233,7 +235,7 @@ pub struct CardServiceData {
/// 4.4.3.7 Extended Capabilities /// 4.4.3.7 Extended Capabilities
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct ExtendedCap { pub struct ExtendedCapabilities {
features: HashSet<Features>, features: HashSet<Features>,
sm_algo: u8, sm_algo: u8,
max_len_challenge: u16, max_len_challenge: u16,
@ -265,7 +267,7 @@ pub struct ExtendedLengthInfo {
/// Cardholder Related Data (see spec pg. 22) /// Cardholder Related Data (see spec pg. 22)
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Cardholder { pub struct CardholderRelatedData {
name: Option<String>, name: Option<String>,
lang: Option<Vec<[char; 2]>>, lang: Option<Vec<[char; 2]>>,
sex: Option<Sex>, sex: Option<Sex>,
@ -304,7 +306,7 @@ impl From<u8> for Sex {
/// PW status Bytes (see spec page 23) /// PW status Bytes (see spec page 23)
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct PWStatus { pub struct PWStatusBytes {
pub(crate) pw1_cds_multi: bool, pub(crate) pw1_cds_multi: bool,
pub(crate) pw1_pin_block: bool, pub(crate) pw1_pin_block: bool,
pub(crate) pw1_len: u8, pub(crate) pw1_len: u8,
@ -316,7 +318,7 @@ pub struct PWStatus {
pub(crate) err_count_pw3: u8, pub(crate) err_count_pw3: u8,
} }
impl PWStatus { impl PWStatusBytes {
pub fn set_pw1_cds_multi(&mut self, val: bool) { pub fn set_pw1_cds_multi(&mut self, val: bool) {
self.pw1_cds_multi = val; self.pw1_cds_multi = val;
} }

View file

@ -7,9 +7,9 @@ use anyhow::Result;
use nom::{bytes::complete as bytes, number::complete as number}; use nom::{bytes::complete as bytes, number::complete as number};
use std::convert::TryFrom; 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, _) = bytes::tag([0xd2, 0x76, 0x0, 0x1, 0x24])(input)?;
let (input, application) = number::u8(input)?; let (input, application) = number::u8(input)?;
@ -22,7 +22,7 @@ fn parse(input: &[u8]) -> nom::IResult<&[u8], ApplicationId> {
Ok(( Ok((
input, input,
ApplicationId { ApplicationIdentifier {
application, application,
version, version,
manufacturer, 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; type Error = anyhow::Error;
fn try_from(data: &[u8]) -> Result<Self> { fn try_from(data: &[u8]) -> Result<Self> {
@ -39,7 +39,7 @@ impl TryFrom<&[u8]> for ApplicationId {
} }
} }
impl ApplicationId { impl ApplicationIdentifier {
pub fn application(&self) -> u8 { pub fn application(&self) -> u8 {
self.application self.application
} }
@ -79,12 +79,12 @@ mod test {
0x42, 0x40, 0x0, 0x0, 0x42, 0x40, 0x0, 0x0,
]; ];
let aid = ApplicationId::try_from(&data[..]) let aid = ApplicationIdentifier::try_from(&data[..])
.expect("failed to parse application id"); .expect("failed to parse application id");
assert_eq!( assert_eq!(
aid, aid,
ApplicationId { ApplicationIdentifier {
application: 0x1, application: 0x1,
version: 0x200, version: 0x200,
manufacturer: 0xfffe, manufacturer: 0xfffe,

View file

@ -7,10 +7,10 @@ use std::convert::TryFrom;
use anyhow::Result; use anyhow::Result;
use crate::card_do::{Cardholder, Sex}; use crate::card_do::{CardholderRelatedData, Sex};
use crate::tlv::{value::Value, Tlv}; use crate::tlv::{value::Value, Tlv};
impl Cardholder { impl CardholderRelatedData {
pub fn name(&self) -> Option<&str> { pub fn name(&self) -> Option<&str> {
self.name.as_deref() self.name.as_deref()
} }
@ -24,7 +24,7 @@ impl Cardholder {
} }
} }
impl TryFrom<&[u8]> for Cardholder { impl TryFrom<&[u8]> for CardholderRelatedData {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(data: &[u8]) -> Result<Self> { fn try_from(data: &[u8]) -> Result<Self> {
@ -49,7 +49,7 @@ impl TryFrom<&[u8]> for Cardholder {
.filter(|v| v.len() == 1) .filter(|v| v.len() == 1)
.map(|v| Sex::from(v[0])); .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, 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"); .expect("failed to parse cardholder");
assert_eq!( assert_eq!(
ch, ch,
Cardholder { CardholderRelatedData {
name: Some("Bar<<Foo".to_string()), name: Some("Bar<<Foo".to_string()),
lang: Some(vec![['d', 'e'], ['e', 'n']]), lang: Some(vec![['d', 'e'], ['e', 'n']]),
sex: Some(Sex::Female) sex: Some(Sex::Female)

View file

@ -8,7 +8,7 @@ use nom::{combinator, number::complete as number, sequence};
use std::collections::HashSet; use std::collections::HashSet;
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::card_do::{complete, ExtendedCap, Features}; use crate::card_do::{complete, ExtendedCapabilities, Features};
use crate::Error; use crate::Error;
fn features(input: &[u8]) -> nom::IResult<&[u8], HashSet<Features>> { fn features(input: &[u8]) -> nom::IResult<&[u8], HashSet<Features>> {
@ -58,7 +58,7 @@ fn parse(
)))(input) )))(input)
} }
impl ExtendedCap { impl ExtendedCapabilities {
pub fn features(&self) -> HashSet<Features> { pub fn features(&self) -> HashSet<Features> {
self.features.clone() self.features.clone()
} }
@ -68,7 +68,7 @@ impl ExtendedCap {
} }
} }
impl TryFrom<&[u8]> for ExtendedCap { impl TryFrom<&[u8]> for ExtendedCapabilities {
type Error = Error; type Error = Error;
fn try_from(input: &[u8]) -> Result<Self, Self::Error> { fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
@ -88,7 +88,7 @@ impl TryFrom<&[u8]> for ExtendedCap {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::card_do::extended_cap::{ExtendedCap, Features}; use crate::card_do::extended_cap::{ExtendedCapabilities, Features};
use hex_literal::hex; use hex_literal::hex;
use std::collections::HashSet; use std::collections::HashSet;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -97,11 +97,11 @@ mod test {
#[test] #[test]
fn test_ec() { fn test_ec() {
let data = hex!("7d 00 0b fe 08 00 00 ff 00 00"); 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!( assert_eq!(
ec, ec,
ExtendedCap { ExtendedCapabilities {
features: HashSet::from_iter(vec![ features: HashSet::from_iter(vec![
Features::GetChallenge, Features::GetChallenge,
Features::KeyImport, Features::KeyImport,

View file

@ -3,7 +3,7 @@
//! 6 Historical Bytes //! 6 Historical Bytes
use crate::card_do::{CardCapabilities, CardServiceData, Historical}; use crate::card_do::{CardCapabilities, CardServiceData, HistoricalBytes};
use crate::Error; use crate::Error;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::convert::TryFrom; use std::convert::TryFrom;
@ -71,13 +71,13 @@ fn split_tl(tl: u8) -> (u8, u8) {
(tag, len) (tag, len)
} }
impl Historical { impl HistoricalBytes {
pub fn get_card_capabilities(&self) -> Option<&CardCapabilities> { pub fn get_card_capabilities(&self) -> Option<&CardCapabilities> {
self.cc.as_ref() self.cc.as_ref()
} }
} }
impl TryFrom<&[u8]> for Historical { impl TryFrom<&[u8]> for HistoricalBytes {
type Error = Error; type Error = Error;
fn try_from(data: &[u8]) -> Result<Self, Self::Error> { fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
@ -213,11 +213,11 @@ mod test {
// gnuk 1.2 stable // gnuk 1.2 stable
let data: &[u8] = let data: &[u8] =
&[0x0, 0x31, 0x84, 0x73, 0x80, 0x1, 0x80, 0x5, 0x90, 0x0]; &[0x0, 0x31, 0x84, 0x73, 0x80, 0x1, 0x80, 0x5, 0x90, 0x0];
let hist: Historical = data.try_into()?; let hist: HistoricalBytes = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
Historical { HistoricalBytes {
cib: 0, cib: 0,
csd: Some(CardServiceData { csd: Some(CardServiceData {
select_by_full_df_name: true, select_by_full_df_name: true,
@ -244,11 +244,11 @@ mod test {
// floss shop openpgp smartcard 3.4 // floss shop openpgp smartcard 3.4
let data: &[u8] = let data: &[u8] =
&[0x0, 0x31, 0xf5, 0x73, 0xc0, 0x1, 0x60, 0x5, 0x90, 0x0]; &[0x0, 0x31, 0xf5, 0x73, 0xc0, 0x1, 0x60, 0x5, 0x90, 0x0];
let hist: Historical = data.try_into()?; let hist: HistoricalBytes = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
Historical { HistoricalBytes {
cib: 0, cib: 0,
csd: Some(CardServiceData { csd: Some(CardServiceData {
select_by_full_df_name: true, select_by_full_df_name: true,
@ -274,11 +274,11 @@ mod test {
fn test_yk5() -> Result<()> { fn test_yk5() -> Result<()> {
// yubikey 5 // yubikey 5
let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0xe0, 0x5, 0x90, 0x0]; 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!( assert_eq!(
hist, hist,
Historical { HistoricalBytes {
cib: 0, cib: 0,
csd: None, csd: None,
cc: Some(CardCapabilities { cc: Some(CardCapabilities {
@ -297,11 +297,11 @@ mod test {
fn test_yk4() -> Result<()> { fn test_yk4() -> Result<()> {
// yubikey 4 // yubikey 4
let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0x80, 0x5, 0x90, 0x0]; 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!( assert_eq!(
hist, hist,
Historical { HistoricalBytes {
cib: 0, cib: 0,
csd: None, csd: None,
cc: Some(CardCapabilities { cc: Some(CardCapabilities {
@ -323,11 +323,11 @@ mod test {
0x0, 0x73, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x73, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
]; ];
let hist: Historical = data.try_into()?; let hist: HistoricalBytes = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
Historical { HistoricalBytes {
cib: 0, cib: 0,
csd: None, csd: None,
cc: Some(CardCapabilities { cc: Some(CardCapabilities {

View file

@ -5,11 +5,11 @@
use anyhow::anyhow; use anyhow::anyhow;
use crate::card_do::PWStatus; use crate::card_do::PWStatusBytes;
use crate::Error; use crate::Error;
use std::convert::TryFrom; use std::convert::TryFrom;
impl PWStatus { impl PWStatusBytes {
/// PUT DO for PW Status Bytes accepts either 1 or 4 bytes of data. /// 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 /// This method generates the 1 byte version for 'long==false' and the
/// 4 bytes version for 'long==true'. /// 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; type Error = Error;
fn try_from(input: &[u8]) -> Result<Self, Self::Error> { fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
@ -84,12 +84,12 @@ mod test {
fn test() { fn test() {
let data = [0x0, 0x40, 0x40, 0x40, 0x3, 0x0, 0x3]; let data = [0x0, 0x40, 0x40, 0x40, 0x3, 0x0, 0x3];
let pws: PWStatus = let pws: PWStatusBytes =
(&data[..]).try_into().expect("failed to parse PWStatus"); (&data[..]).try_into().expect("failed to parse PWStatus");
assert_eq!( assert_eq!(
pws, pws,
PWStatus { PWStatusBytes {
pw1_cds_multi: false, pw1_cds_multi: false,
pw1_pin_block: false, pw1_pin_block: false,
pw1_len: 0x40, pw1_len: 0x40,