From 56f445993247d800e3ad46216605ce9841ba04f5 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Thu, 29 Jul 2021 18:09:46 +0200 Subject: [PATCH] When the card doesn't support command chaining, throw CommandTooLong error if the command is too long. (This currently happens with the scdc backend when uploading rsa4096 keys, because scdc additionally limits command size) --- openpgp-card/src/apdu/mod.rs | 8 ++++++++ openpgp-card/src/errors.rs | 3 +++ 2 files changed, 11 insertions(+) diff --git a/openpgp-card/src/apdu/mod.rs b/openpgp-card/src/apdu/mod.rs index d82723b..760ab87 100644 --- a/openpgp-card/src/apdu/mod.rs +++ b/openpgp-card/src/apdu/mod.rs @@ -183,6 +183,14 @@ fn send_command_low_level( } else { let serialized = cmd.serialize(ext)?; + // Can't send this command to the card, because it is too long and + // the card doesn't support command chaining. + if serialized.len() > max_cmd_bytes { + return Err(OpenpgpCardError::CommandTooLong(serialized.len())); + } + + log::debug!(" -> APDU command: {:x?}", &serialized); + let resp = card_client.transmit(&serialized, buf_size)?; log::debug!(" <- APDU response: {:x?}", resp); diff --git a/openpgp-card/src/errors.rs b/openpgp-card/src/errors.rs index 61b7ff4..cd1773b 100644 --- a/openpgp-card/src/errors.rs +++ b/openpgp-card/src/errors.rs @@ -11,6 +11,9 @@ pub enum OpenpgpCardError { #[error("OpenPGP card error status {0}")] OcStatus(OcErrorStatus), + #[error("Command too long ({0} bytes)")] + CommandTooLong(usize), + #[error("Internal error {0}")] InternalError(anyhow::Error), }