From 786515a7f4ffd6b1f51c116c4b73af3ba9d93b7e Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Wed, 25 Aug 2021 17:29:21 +0200 Subject: [PATCH] Make fields of Command private. --- openpgp-card/src/apdu/command.rs | 26 +++++++++++++++++++++----- openpgp-card/src/apdu/mod.rs | 18 +++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/openpgp-card/src/apdu/command.rs b/openpgp-card/src/apdu/command.rs index 44c9beb..8494b10 100644 --- a/openpgp-card/src/apdu/command.rs +++ b/openpgp-card/src/apdu/command.rs @@ -8,17 +8,17 @@ use anyhow::Result; #[derive(Clone, Debug)] pub(crate) struct Command { // Class byte (CLA) - pub cla: u8, + cla: u8, // Instruction byte (INS) - pub ins: u8, + ins: u8, // Parameter bytes (P1/P2) - pub p1: u8, - pub p2: u8, + p1: u8, + p2: u8, // NOTE: data must be smaller than 64 kbyte - pub data: Vec, + data: Vec, } impl Command { @@ -34,6 +34,22 @@ impl Command { } } + pub(crate) fn get_ins(&self) -> u8 { + self.ins + } + + pub(crate) fn get_p1(&self) -> u8 { + self.p1 + } + + pub(crate) fn get_p2(&self) -> u8 { + self.p2 + } + + pub(crate) fn get_data(&self) -> &[u8] { + &self.data + } + fn encode_len(len: u16, ext: Le) -> Vec { if len > 0xff || ext == Le::Long { vec![0, (len as u16 >> 8) as u8, (len as u16 & 255) as u8] diff --git a/openpgp-card/src/apdu/mod.rs b/openpgp-card/src/apdu/mod.rs index f7586e9..878315b 100644 --- a/openpgp-card/src/apdu/mod.rs +++ b/openpgp-card/src/apdu/mod.rs @@ -130,21 +130,25 @@ fn send_command_low_level( log::trace!("buf_size {}", buf_size); - if chaining_support && !cmd.data.is_empty() { + if chaining_support && !cmd.get_data().is_empty() { // Send command in chained mode log::debug!("chained command mode"); // Break up payload into chunks that fit into one command, each - let chunks: Vec<_> = cmd.data.chunks(max_cmd_bytes).collect(); + let chunks: Vec<_> = cmd.get_data().chunks(max_cmd_bytes).collect(); for (i, d) in chunks.iter().enumerate() { let last = i == chunks.len() - 1; - let partial = Command { - cla: if last { 0x00 } else { 0x10 }, - data: d.to_vec(), - ..cmd - }; + + let cla = if last { 0x00 } else { 0x10 }; + let partial = Command::new( + cla, + cmd.get_ins(), + cmd.get_p1(), + cmd.get_p2(), + d.to_vec(), + ); let serialized = partial .serialize(ext)