Skip to content

Commit

Permalink
Improve Debug impls
Browse files Browse the repository at this point in the history
For Der and AlgorithmIdentifier, emit hex bytes rather than decimal
digits separated by commas. The decimal output is impossible to read and
challenging to convert into a better format. For example:

[ ... 105, 103, 105, 67, 101, 114, 116, 44, 32, 73, ... ]

Also, add a Debug bound on SignatureVerificationAlgorithm. This helps
upstream implement Debug in a way that can list out which
SignatureVerificationAlgorithms are in use.
  • Loading branch information
jsha authored and cpu committed Nov 28, 2023
1 parent b52e9a4 commit db311aa
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,12 @@ impl CertificateDer<'_> {
/// `signature_alg_id()`). Note that both of these `AlgorithmIdentifier`s include
/// the parameters encoding, so separate `SignatureVerificationAlgorithm`s are needed
/// for each possible public key or signature parameters.
pub trait SignatureVerificationAlgorithm: Send + Sync {
///
/// Debug implementations should list the public key algorithm identifier and
/// signature algorithm identifier in human friendly form (i.e. not encoded bytes),
/// along with the name of the implementing library (to distinguish different
/// implementations of the same algorithms).
pub trait SignatureVerificationAlgorithm: Send + Sync + fmt::Debug {
/// Verify a signature.
///
/// `public_key` is the `subjectPublicKey` value from a `SubjectPublicKeyInfo` encoding
Expand Down Expand Up @@ -377,7 +382,7 @@ pub struct InvalidSignature;
/// ]
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct AlgorithmIdentifier(&'static [u8]);

impl AlgorithmIdentifier {
Expand All @@ -395,6 +400,12 @@ impl AsRef<[u8]> for AlgorithmIdentifier {
}
}

impl fmt::Debug for AlgorithmIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex(f, self.0)
}
}

impl Deref for AlgorithmIdentifier {
type Target = [u8];

Expand Down Expand Up @@ -481,7 +492,7 @@ impl From<Vec<u8>> for Der<'static> {

impl fmt::Debug for Der<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Der").field(&self.as_ref()).finish()
hex(f, self.as_ref())
}
}

Expand Down Expand Up @@ -509,3 +520,31 @@ impl DerInner<'_> {
})
}
}

// Format an iterator of u8 into a hex string
fn hex<'a>(f: &mut fmt::Formatter<'_>, payload: impl IntoIterator<Item = &'a u8>) -> fmt::Result {
for (i, b) in payload.into_iter().enumerate() {
if i == 0 {
write!(f, "0x")?;
}
write!(f, "{:02x}", b)?;
}
Ok(())
}

#[cfg(feature = "std")]
mod tests {
use super::*;

#[test]
fn der_debug() {
let der = Der::from_slice(&[0x01, 0x02, 0x03]);
assert_eq!(format!("{:?}", der), "0x010203");
}

#[test]
fn alg_id_debug() {
let alg_id = AlgorithmIdentifier::from_slice(&[0x01, 0x02, 0x03]);
assert_eq!(format!("{:?}", alg_id), "0x010203");
}
}

0 comments on commit db311aa

Please sign in to comment.