Implement From/TryFrom for conversions of Historical and its members.

This commit is contained in:
Heiko Schaefer 2021-08-27 13:55:55 +02:00
parent 73829a6b27
commit 64f05e93f5
2 changed files with 26 additions and 15 deletions

View file

@ -4,6 +4,7 @@
use crate::card_do::{CardCapabilities, CardServiceData, Historical}; use crate::card_do::{CardCapabilities, CardServiceData, Historical};
use crate::errors::OpenpgpCardError; use crate::errors::OpenpgpCardError;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::convert::TryFrom;
impl CardCapabilities { impl CardCapabilities {
pub fn get_command_chaining(&self) -> bool { pub fn get_command_chaining(&self) -> bool {
@ -17,8 +18,10 @@ impl CardCapabilities {
pub fn get_extended_length_information(&self) -> bool { pub fn get_extended_length_information(&self) -> bool {
self.extended_length_information self.extended_length_information
} }
}
pub fn from(data: [u8; 3]) -> Self { impl From<[u8; 3]> for CardCapabilities {
fn from(data: [u8; 3]) -> Self {
let byte3 = data[2]; let byte3 = data[2];
let command_chaining = byte3 & 0x80 != 0; let command_chaining = byte3 & 0x80 != 0;
@ -33,8 +36,8 @@ impl CardCapabilities {
} }
} }
impl CardServiceData { impl From<u8> for CardServiceData {
pub fn from(data: u8) -> Self { fn from(data: u8) -> Self {
let select_by_full_df_name = data & 0x80 != 0; let select_by_full_df_name = data & 0x80 != 0;
let select_by_partial_df_name = data & 0x40 != 0; let select_by_partial_df_name = data & 0x40 != 0;
let dos_available_in_ef_dir = data & 0x20 != 0; let dos_available_in_ef_dir = data & 0x20 != 0;
@ -70,8 +73,12 @@ impl Historical {
pub fn get_card_capabilities(&self) -> Option<&CardCapabilities> { pub fn get_card_capabilities(&self) -> Option<&CardCapabilities> {
self.cc.as_ref() self.cc.as_ref()
} }
}
pub fn from(data: &[u8]) -> Result<Self, OpenpgpCardError> { impl TryFrom<&[u8]> for Historical {
type Error = OpenpgpCardError;
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
let len = data.len(); let len = data.len();
if len < 4 { if len < 4 {
@ -189,6 +196,7 @@ impl Historical {
mod test { mod test {
use super::*; use super::*;
use anyhow::Result; use anyhow::Result;
use std::convert::TryInto;
#[test] #[test]
fn test_split_tl() { fn test_split_tl() {
@ -201,8 +209,9 @@ mod test {
#[test] #[test]
fn test_gnuk() -> Result<()> { fn test_gnuk() -> Result<()> {
// gnuk 1.2 stable // gnuk 1.2 stable
let data = [0x0, 0x31, 0x84, 0x73, 0x80, 0x1, 0x80, 0x5, 0x90, 0x0]; let data: &[u8] =
let hist = Historical::from(&data[..])?; &[0x0, 0x31, 0x84, 0x73, 0x80, 0x1, 0x80, 0x5, 0x90, 0x0];
let hist: Historical = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
@ -231,8 +240,9 @@ mod test {
#[test] #[test]
fn test_floss34() -> Result<()> { fn test_floss34() -> Result<()> {
// floss shop openpgp smartcard 3.4 // floss shop openpgp smartcard 3.4
let data = [0x0, 0x31, 0xf5, 0x73, 0xc0, 0x1, 0x60, 0x5, 0x90, 0x0]; let data: &[u8] =
let hist = Historical::from(&data[..])?; &[0x0, 0x31, 0xf5, 0x73, 0xc0, 0x1, 0x60, 0x5, 0x90, 0x0];
let hist: Historical = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
@ -261,8 +271,8 @@ mod test {
#[test] #[test]
fn test_yk5() -> Result<()> { fn test_yk5() -> Result<()> {
// yubikey 5 // yubikey 5
let data = [0x0, 0x73, 0x0, 0x0, 0xe0, 0x5, 0x90, 0x0]; let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0xe0, 0x5, 0x90, 0x0];
let hist = Historical::from(&data[..])?; let hist: Historical = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
@ -284,8 +294,8 @@ mod test {
#[test] #[test]
fn test_yk4() -> Result<()> { fn test_yk4() -> Result<()> {
// yubikey 4 // yubikey 4
let data = [0x0, 0x73, 0x0, 0x0, 0x80, 0x5, 0x90, 0x0]; let data: &[u8] = &[0x0, 0x73, 0x0, 0x0, 0x80, 0x5, 0x90, 0x0];
let hist = Historical::from(&data[..])?; let hist: Historical = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,
@ -307,11 +317,11 @@ mod test {
#[test] #[test]
fn test_yk_neo() -> Result<()> { fn test_yk_neo() -> Result<()> {
// yubikey neo // yubikey neo
let data = [ let data: &[u8] = &[
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::from(&data[..])?; let hist: Historical = data.try_into()?;
assert_eq!( assert_eq!(
hist, hist,

View file

@ -6,6 +6,7 @@
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use std::collections::HashSet; use std::collections::HashSet;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::convert::TryInto;
use crate::algorithm::Algo; use crate::algorithm::Algo;
use crate::errors::OpenpgpCardError; use crate::errors::OpenpgpCardError;
@ -53,7 +54,7 @@ impl ApplicationRelatedData {
if let Some(hist) = hist { if let Some(hist) = hist {
log::debug!("Historical bytes: {:x?}", hist); log::debug!("Historical bytes: {:x?}", hist);
Historical::from(&hist.serialize()) (hist.serialize().as_slice()).try_into()
} else { } else {
Err(anyhow!("Failed to get historical bytes.").into()) Err(anyhow!("Failed to get historical bytes.").into())
} }