From 2ba91d492d3bf26a81857b7d9f30444533e904cb Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 31 Jul 2024 09:47:51 +0200 Subject: [PATCH] Make sure OTA job documents can be deserialized with no codesigning properties in the document --- src/ota/encoding/json.rs | 39 ++++++++++++++++++++++++++++++++------- src/ota/encoding/mod.rs | 8 +++++--- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/ota/encoding/json.rs b/src/ota/encoding/json.rs index 45ea3f2..61b3b23 100644 --- a/src/ota/encoding/json.rs +++ b/src/ota/encoding/json.rs @@ -32,7 +32,8 @@ pub struct FileDescription<'a> { #[serde(rename = "fileid")] pub fileid: u8, #[serde(rename = "certfile")] - pub certfile: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] + pub certfile: Option<&'a str>, #[serde(rename = "update_data_url")] #[serde(skip_serializing_if = "Option::is_none")] pub update_data_url: Option<&'a str>, @@ -59,20 +60,20 @@ pub struct FileDescription<'a> { } impl<'a> FileDescription<'a> { - pub fn signature(&self) -> Signature { + pub fn signature(&self) -> Option { if let Some(sig) = self.sha1_rsa { - return Signature::Sha1Rsa(heapless::String::try_from(sig).unwrap()); + return Some(Signature::Sha1Rsa(heapless::String::try_from(sig).unwrap())); } if let Some(sig) = self.sha256_rsa { - return Signature::Sha256Rsa(heapless::String::try_from(sig).unwrap()); + return Some(Signature::Sha256Rsa(heapless::String::try_from(sig).unwrap())); } if let Some(sig) = self.sha1_ecdsa { - return Signature::Sha1Ecdsa(heapless::String::try_from(sig).unwrap()); + return Some(Signature::Sha1Ecdsa(heapless::String::try_from(sig).unwrap())); } if let Some(sig) = self.sha256_ecdsa { - return Signature::Sha256Ecdsa(heapless::String::try_from(sig).unwrap()); + return Some(Signature::Sha256Ecdsa(heapless::String::try_from(sig).unwrap())); } - unreachable!() + None } } @@ -147,4 +148,28 @@ mod tests { ); } } + + #[test] + fn deserializ() { + let data = r#"{ + "protocols": [ + "MQTT" + ], + "streamname": "AFR_OTA-d11032e9-38d5-4dca-8c7c-1e6f24533ede", + "files": [ + { + "filepath": "3.8.4", + "filesize": 537600, + "fileid": 0, + "certfile": null, + "fileType": 0, + "update_data_url": null, + "auth_scheme": null, + "sig--": null + } + ] + }"#; + + let job: (OtaJob, _) = serde_json_core::from_str(&data).unwrap(); + } } diff --git a/src/ota/encoding/mod.rs b/src/ota/encoding/mod.rs index bc68c67..7c88700 100644 --- a/src/ota/encoding/mod.rs +++ b/src/ota/encoding/mod.rs @@ -60,10 +60,10 @@ pub struct FileContext { pub filepath: heapless::String<64>, pub filesize: usize, pub fileid: u8, - pub certfile: heapless::String<64>, + pub certfile: Option>, pub update_data_url: Option>, pub auth_scheme: Option>, - pub signature: Signature, + pub signature: Option, pub file_type: Option, pub protocols: heapless::Vec, @@ -110,7 +110,9 @@ impl FileContext { filesize: file_desc.filesize, protocols: job_data.ota_document.protocols, fileid: file_desc.fileid, - certfile: heapless::String::try_from(file_desc.certfile).unwrap(), + certfile: file_desc + .certfile + .map(|cert| heapless::String::try_from(cert).unwrap()), update_data_url: file_desc .update_data_url .map(|s| heapless::String::try_from(s).unwrap()),