Use the openpgp_card::card_do::Fingerprint type instead of [u8; 20].

Add a TryFrom<&[u8]> implementation to Fingerprint.
This commit is contained in:
Heiko Schaefer 2021-08-25 21:25:20 +02:00
parent c6ba204293
commit f3bfecd185
5 changed files with 37 additions and 18 deletions

View file

@ -364,11 +364,9 @@ impl CardUploadableKey for SequoiaKey {
ts.into() ts.into()
} }
fn get_fp(&self) -> [u8; 20] { fn get_fp(&self) -> Result<Fingerprint, OpenpgpCardError> {
let fp = self.key.fingerprint(); let fp = self.key.fingerprint();
assert_eq!(fp.as_bytes().len(), 20); fp.as_bytes().try_into()
fp.as_bytes().try_into().unwrap()
} }
} }
@ -539,16 +537,13 @@ pub fn public_to_fingerprint(
pkm: &PublicKeyMaterial, pkm: &PublicKeyMaterial,
ts: SystemTime, ts: SystemTime,
kt: KeyType, kt: KeyType,
) -> Result<[u8; 20]> { ) -> Result<Fingerprint, OpenpgpCardError> {
// Transform PublicKeyMaterial into a Sequoia Key // Transform PublicKeyMaterial into a Sequoia Key
let key = public_key_material_to_key(pkm, kt, ts)?; let key = public_key_material_to_key(pkm, kt, ts)?;
// Get fingerprint from the Sequoia Key // Get fingerprint from the Sequoia Key
let fp = key.fingerprint(); let fp = key.fingerprint();
let fp = fp.as_bytes(); fp.as_bytes().try_into()
assert_eq!(fp.len(), 20);
Ok(fp.try_into()?)
} }
// -------- // --------

View file

@ -11,7 +11,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, PWStatus, SecuritySupportTemplate, Sex, ApplicationRelatedData, Cardholder, Fingerprint, PWStatus,
SecuritySupportTemplate, Sex,
}; };
use crate::crypto_data::{ use crate::crypto_data::{
CardUploadableKey, Cryptogram, EccType, Hash, PublicKeyMaterial, CardUploadableKey, Cryptogram, EccType, Hash, PublicKeyMaterial,
@ -487,12 +488,12 @@ impl CardApp {
pub fn set_fingerprint( pub fn set_fingerprint(
&mut self, &mut self,
fp: [u8; 20], fp: Fingerprint,
key_type: KeyType, key_type: KeyType,
) -> Result<Response, OpenpgpCardError> { ) -> Result<Response, OpenpgpCardError> {
let fp_cmd = commands::put_data( let fp_cmd = commands::put_data(
&[key_type.get_fingerprint_put_tag()], &[key_type.get_fingerprint_put_tag()],
fp.to_vec(), fp.as_bytes().to_vec(),
); );
apdu::send_command(self.card(), fp_cmd, false)?.try_into() apdu::send_command(self.card(), fp_cmd, false)?.try_into()
@ -620,7 +621,7 @@ impl CardApp {
&PublicKeyMaterial, &PublicKeyMaterial,
SystemTime, SystemTime,
KeyType, KeyType,
) -> Result<[u8; 20]>, ) -> Result<Fingerprint, OpenpgpCardError>,
key_type: KeyType, key_type: KeyType,
algo: Option<&Algo>, algo: Option<&Algo>,
) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> { ) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> {
@ -635,7 +636,7 @@ impl CardApp {
&PublicKeyMaterial, &PublicKeyMaterial,
SystemTime, SystemTime,
KeyType, KeyType,
) -> Result<[u8; 20]>, ) -> Result<Fingerprint, OpenpgpCardError>,
key_type: KeyType, key_type: KeyType,
algo: AlgoSimple, algo: AlgoSimple,
) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> { ) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> {

View file

@ -3,6 +3,8 @@
use anyhow::anyhow; use anyhow::anyhow;
use nom::{bytes::complete as bytes, combinator, sequence}; use nom::{bytes::complete as bytes, combinator, sequence};
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fmt; use std::fmt;
use crate::card_do::{Fingerprint, KeySet}; use crate::card_do::{Fingerprint, KeySet};
@ -14,6 +16,24 @@ impl From<[u8; 20]> for Fingerprint {
} }
} }
impl TryFrom<&[u8]> for Fingerprint {
type Error = OpenpgpCardError;
fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
log::trace!(
"Fingerprint from input: {:x?}, len {}",
input,
input.len()
);
// FIXME: return error
assert_eq!(input.len(), 20);
let array: [u8; 20] = input.try_into().unwrap();
Ok(array.into())
}
}
impl Fingerprint { impl Fingerprint {
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
&self.0 &self.0

View file

@ -8,6 +8,8 @@
use anyhow::Result; use anyhow::Result;
use crate::algorithm::Algo; use crate::algorithm::Algo;
use crate::card_do::Fingerprint;
use crate::errors::OpenpgpCardError;
/// A hash value that can be signed by the card. /// A hash value that can be signed by the card.
pub enum Hash<'a> { pub enum Hash<'a> {
@ -67,7 +69,7 @@ pub trait CardUploadableKey {
fn get_ts(&self) -> u32; fn get_ts(&self) -> u32;
/// fingerprint /// fingerprint
fn get_fp(&self) -> [u8; 20]; fn get_fp(&self) -> Result<Fingerprint, OpenpgpCardError>;
} }
/// Algorithm-independent container for private key material to upload to /// Algorithm-independent container for private key material to upload to

View file

@ -11,6 +11,7 @@ use crate::algorithm::{Algo, AlgoInfo, Curve, EccAttrs, RsaAttrs};
use crate::apdu::command::Command; use crate::apdu::command::Command;
use crate::apdu::commands; use crate::apdu::commands;
use crate::card_app::CardApp; use crate::card_app::CardApp;
use crate::card_do::Fingerprint;
use crate::crypto_data::{ use crate::crypto_data::{
CardUploadableKey, EccKey, EccPub, PrivateKeyMaterial, PublicKeyMaterial, CardUploadableKey, EccKey, EccPub, PrivateKeyMaterial, PublicKeyMaterial,
RSAKey, RSAPub, RSAKey, RSAPub,
@ -27,7 +28,7 @@ pub(crate) fn gen_key_with_metadata(
&PublicKeyMaterial, &PublicKeyMaterial,
SystemTime, SystemTime,
KeyType, KeyType,
) -> Result<[u8; 20]>, ) -> Result<Fingerprint, OpenpgpCardError>,
key_type: KeyType, key_type: KeyType,
algo: Option<&Algo>, algo: Option<&Algo>,
) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> { ) -> Result<(PublicKeyMaterial, u32), OpenpgpCardError> {
@ -210,7 +211,7 @@ pub(crate) fn upload_key(
card_app, card_app,
key_type, key_type,
key.get_ts(), key.get_ts(),
key.get_fp(), key.get_fp()?,
&algo, &algo,
key_cmd, key_cmd,
)?; )?;
@ -427,7 +428,7 @@ fn copy_key_to_card(
card_app: &mut CardApp, card_app: &mut CardApp,
key_type: KeyType, key_type: KeyType,
ts: u32, ts: u32,
fp: [u8; 20], fp: Fingerprint,
algo: &Algo, algo: &Algo,
key_cmd: Command, key_cmd: Command,
) -> Result<(), OpenpgpCardError> { ) -> Result<(), OpenpgpCardError> {