Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh-key: add SshSig serializing/deserializing using serde #264

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions ssh-key/src/sshsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -202,6 +205,7 @@ 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
Expand Down Expand Up @@ -346,3 +350,38 @@ impl Encode for SignedData<'_> {
Ok(())
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for SshSig {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
if deserializer.is_human_readable() {
let string = String::deserialize(deserializer)?;
string.parse::<SshSig>().map_err(de::Error::custom)
} else {
let bytes = Vec::<u8>::deserialize(deserializer)?;
let mut bytes_slice: &[u8] = &bytes;
Self::decode(&mut bytes_slice).map_err(de::Error::custom)
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[cfg(feature = "serde")]
impl Serialize for SshSig {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: ser::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)
}
}
}
Loading