From 42fdab716d94e7434ede355b640da7e9e20361a5 Mon Sep 17 00:00:00 2001 From: Florian Franzen Date: Thu, 8 Oct 2026 22:52:35 +0200 Subject: [PATCH] Delete a data object on PUT DATA with an empty object --- CHANGELOG.md | 1 + src/lib.rs | 5 +++ src/state.rs | 14 +++++++- tests/command_response.ron | 65 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1a0096..93db9dec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ default (5) - Fix the inverted parsing of the touch requirement in SET MANAGEMENT KEY's P2 (0xFF means no touch, 0xFE means touch required) +- Delete a data object on PUT DATA with an empty object ## [v0.7.0][] (2026-08-17) diff --git a/src/lib.rs b/src/lib.rs index 52aefebf..6c073c3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1012,6 +1012,11 @@ impl LoadedAuthenticator<'_, T> { }; use state::ContainerStorage; + // An empty object deletes + if data.is_empty() { + ContainerStorage(container).remove(self.trussed, self.options.storage); + return Ok(()); + } ContainerStorage(container).save(self.trussed, data, self.options.storage) } diff --git a/src/state.rs b/src/state.rs index 864ce5f8..e7ce59a5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1181,7 +1181,8 @@ impl ContainerStorage { fn default(self) -> Option> { match self.0 { - Container::CardHolderUniqueIdentifier => panic!("CHUID should alway be set"), + // Written at initialization, absent only once deleted + Container::CardHolderUniqueIdentifier => None, Container::CardCapabilityContainer => Some(Vec::from_slice(&CARD_CAP).unwrap()), Container::DiscoveryObject => Some(Vec::from_slice(&DISCOVERY_OBJECT).unwrap()), Container::PrintedInformation => Some(Vec::from_slice(&PRINTED_INFORMATION).unwrap()), @@ -1250,6 +1251,17 @@ impl ContainerStorage { } } + pub fn remove(self, client: &mut impl crate::Client, storage: Location) { + // Already absent is fine + try_syscall!(client.remove_file(storage, self.path())).ok(); + if matches!( + self.0.contact_access_rule(), + ReadAccessRule::PinOrOcc | ReadAccessRule::Pin + ) { + try_syscall!(client.remove_file(storage, self.path_key())).ok(); + } + } + pub fn save( self, client: &mut impl crate::Client, diff --git a/tests/command_response.ron b/tests/command_response.ron index 7bc7ecea..881b4b71 100644 --- a/tests/command_response.ron +++ b/tests/command_response.ron @@ -255,6 +255,71 @@ ) ] ), + IoTest( + name: "Empty PUT DATA deletes", + cmd_resp: [ + AuthenticateManagement( + key: ( + algorithm: Tdes, + key: "0102030405060708 0102030405060708 0102030405060708" + ), + mutual: false, + ), + PutData( + input: "5C 03 5FC10B 53 04 00010203", + ), + GetData( + input: "5C 03 5FC10B", + output: Data("53 04 00010203") + ), + PutData( + input: "5C 03 5FC10B 53 00", + ), + GetData( + input: "5C 03 5FC10B", + output: All(), + expected_status: NotFound + ), + PutData( + input: "5C 03 5FC10D 53 04 00010203", + ), + GetData( + input: "5C 03 5FC10C", + output: Data("53 08 C1 01 01 C2 01 00 FE 00") + ), + PutData( + input: "5C 03 5FC10D 53 00", + ), + GetData( + input: "5C 03 5FC10C", + output: Data("53 08 C1 01 00 C2 01 00 FE 00") + ), + PutData( + input: "5C 03 5FC103 53 04 00010203", + ), + VerifyApplicationPin(), + GetData( + input: "5C 03 5FC103", + output: Data("53 04 00010203") + ), + PutData( + input: "5C 03 5FC103 53 00", + ), + GetData( + input: "5C 03 5FC103", + output: All(), + expected_status: NotFound + ), + PutData( + input: "5C 03 5FC102 53 00", + ), + GetData( + input: "5C 03 5FC102", + output: All(), + expected_status: NotFound + ), + ] + ), IoTest( name: "PUT DATA", cmd_resp: [