From 03e3d42ce8468138f16d7b8013dada6c53028c37 Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Sun, 11 Aug 2024 00:50:25 +0200 Subject: [PATCH 1/5] Added SshSig serializing/deserializing using serde --- ssh-key/src/sshsig.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ssh-key/src/sshsig.rs b/ssh-key/src/sshsig.rs index 2bbe5ae..87c5d81 100644 --- a/ssh-key/src/sshsig.rs +++ b/ssh-key/src/sshsig.rs @@ -14,6 +14,9 @@ use crate::{PrivateKey, PublicKey}; type Version = u32; +#[cfg(feature = "serde")] +use serde::{de, ser, Deserialize, Serialize}; + /// `sshsig` provides a general-purpose signature format based on SSH keys and /// wire formats. /// @@ -346,3 +349,24 @@ impl Encode for SignedData<'_> { Ok(()) } } + +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for SshSig { + fn deserialize(deserializer: D) -> core::result::Result + where + D: de::Deserializer<'de>, + { + let string = String::deserialize(deserializer)?; + string.parse::().map_err(de::Error::custom) + } +} + +#[cfg(feature = "serde")] +impl Serialize for SshSig { + fn serialize(&self, serializer: S) -> core::result::Result + where + S: ser::Serializer, + { + self.to_pem(LineEnding::LF).map_err(ser::Error::custom)?.serialize(serializer) + } +} From 1992daafdc33b697d6b67c501d6eef0f0d954578 Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Sun, 11 Aug 2024 00:55:28 +0200 Subject: [PATCH 2/5] Cargo fmtd --- ssh-key/src/sshsig.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ssh-key/src/sshsig.rs b/ssh-key/src/sshsig.rs index 87c5d81..25606ac 100644 --- a/ssh-key/src/sshsig.rs +++ b/ssh-key/src/sshsig.rs @@ -367,6 +367,8 @@ impl Serialize for SshSig { where S: ser::Serializer, { - self.to_pem(LineEnding::LF).map_err(ser::Error::custom)?.serialize(serializer) + self.to_pem(LineEnding::LF) + .map_err(ser::Error::custom)? + .serialize(serializer) } } From a423dbf4de6e790a52874cbccdeb1880d0326cf6 Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Tue, 13 Aug 2024 18:00:46 +0200 Subject: [PATCH 3/5] Added serializing and deserializing as non human readable --- ssh-key/src/sshsig.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/ssh-key/src/sshsig.rs b/ssh-key/src/sshsig.rs index 25606ac..bc07db1 100644 --- a/ssh-key/src/sshsig.rs +++ b/ssh-key/src/sshsig.rs @@ -15,6 +15,7 @@ use crate::{PrivateKey, PublicKey}; type Version = u32; #[cfg(feature = "serde")] +use std::io::Cursor; use serde::{de, ser, Deserialize, Serialize}; /// `sshsig` provides a general-purpose signature format based on SSH keys and @@ -205,6 +206,8 @@ impl SshSig { } /// Get the hash algorithm used to produce this signature. + + /// /// Data to be signed is first hashed with the specified `hash_alg`. /// This is done to limit the amount of data presented to the signature @@ -356,8 +359,15 @@ impl<'de> Deserialize<'de> for SshSig { where D: de::Deserializer<'de>, { - let string = String::deserialize(deserializer)?; - string.parse::().map_err(de::Error::custom) + if deserializer.is_human_readable() { + let string = String::deserialize(deserializer)?; + string.parse::().map_err(de::Error::custom) + } else { + let bytes = Vec::::deserialize(deserializer)?; + let mut bytes_slice: &[u8] = &bytes; + Self::decode(&mut bytes_slice).map_err(de::Error::custom) + } + } } @@ -367,8 +377,14 @@ impl Serialize for SshSig { where S: ser::Serializer, { - self.to_pem(LineEnding::LF) - .map_err(ser::Error::custom)? - .serialize(serializer) + if serializer.is_human_readable() { + self.to_pem(LineEnding::LF) + .map_err(ser::Error::custom)? + .serialize(serializer) + } else { + let mut bytes = Vec::new(); + self.encode(&mut bytes).map_err(ser::Error::custom)?; + bytes.serialize(serializer) + } } } From 050cbcc7f20a90543eb7f4dc47fe2b5a2b99b993 Mon Sep 17 00:00:00 2001 From: MicroPanda123 Date: Tue, 13 Aug 2024 18:02:52 +0200 Subject: [PATCH 4/5] Remove Cursor and cargo fmt --- ssh-key/src/sshsig.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ssh-key/src/sshsig.rs b/ssh-key/src/sshsig.rs index bc07db1..b02761c 100644 --- a/ssh-key/src/sshsig.rs +++ b/ssh-key/src/sshsig.rs @@ -15,7 +15,6 @@ use crate::{PrivateKey, PublicKey}; type Version = u32; #[cfg(feature = "serde")] -use std::io::Cursor; use serde::{de, ser, Deserialize, Serialize}; /// `sshsig` provides a general-purpose signature format based on SSH keys and @@ -207,7 +206,6 @@ impl SshSig { /// Get the hash algorithm used to produce this signature. - /// /// Data to be signed is first hashed with the specified `hash_alg`. /// This is done to limit the amount of data presented to the signature @@ -367,7 +365,6 @@ impl<'de> Deserialize<'de> for SshSig { let mut bytes_slice: &[u8] = &bytes; Self::decode(&mut bytes_slice).map_err(de::Error::custom) } - } } From fc65282a16290cf2c7bca9f34d758bc3d73bbeef Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 13 Aug 2024 10:18:52 -0600 Subject: [PATCH 5/5] Update ssh-key/src/sshsig.rs --- ssh-key/src/sshsig.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ssh-key/src/sshsig.rs b/ssh-key/src/sshsig.rs index b02761c..1b7c3c6 100644 --- a/ssh-key/src/sshsig.rs +++ b/ssh-key/src/sshsig.rs @@ -362,8 +362,7 @@ impl<'de> Deserialize<'de> for SshSig { string.parse::().map_err(de::Error::custom) } else { let bytes = Vec::::deserialize(deserializer)?; - let mut bytes_slice: &[u8] = &bytes; - Self::decode(&mut bytes_slice).map_err(de::Error::custom) + Self::decode(&mut bytes.as_slice()).map_err(de::Error::custom) } } }