Don't panic if a long command is sent and the backend reports no support for extended length

This case should not happen during normal operation with the pcsc backend. But the condition was triggered in tests with an alternate CardBackend implementation (see: https://gitlab.com/openpgp-card/openpgp-card/-/issues/69).
This commit is contained in:
Heiko Schaefer 2023-04-13 18:15:58 +02:00
parent 629eecd510
commit 4a042d703f
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D

View file

@ -81,7 +81,7 @@ impl Command {
let nc = self.data.len() as u16; let nc = self.data.len() as u16;
let mut buf = vec![self.cla, self.ins, self.p1, self.p2]; let mut buf = vec![self.cla, self.ins, self.p1, self.p2];
buf.extend(Self::make_lc(nc, ext_len)); buf.extend(Self::make_lc(nc, ext_len)?);
buf.extend(&self.data); buf.extend(&self.data);
buf.extend(Self::make_le(nc, ext_len, expect_response)); buf.extend(Self::make_le(nc, ext_len, expect_response));
@ -89,21 +89,19 @@ impl Command {
} }
/// Encode len for Lc field /// Encode len for Lc field
fn make_lc(len: u16, ext_len: bool) -> Vec<u8> { fn make_lc(len: u16, ext_len: bool) -> Result<Vec<u8>, crate::Error> {
if !ext_len { if !ext_len && len > 0xff {
assert!( return Err(crate::Error::InternalError(format!(
len <= 0xff, "Command len = {len:x?}, but extended length is unsupported by backend"
"{}", )));
"unexpected: len = {len:x?}, but ext says Short"
);
} }
if len == 0 { if len == 0 {
vec![] Ok(vec![])
} else if !ext_len { } else if !ext_len {
vec![len as u8] Ok(vec![len as u8])
} else { } else {
vec![0, (len >> 8) as u8, (len & 255) as u8] Ok(vec![0, (len >> 8) as u8, (len & 255) as u8])
} }
} }