diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-07-03 10:42:57 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-07-03 10:42:57 +0200 |
commit | e9912a42603619d7b64c89dd72db80bdcc5f0419 (patch) | |
tree | 0314e91d66f3e6bce66af04e39a8943e4cf449aa | |
parent | 4a2c51173f9dc48ebd8649982388750ab9dccec7 (diff) |
openpgp: Dot-encode unknown curves in impl fmt::Display.
- Fixes #884.
-rw-r--r-- | openpgp/src/types/mod.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 0a3bfe0b..275632e2 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -460,6 +460,40 @@ impl Curve { impl fmt::Display for Curve { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::Curve::*; + + struct DotEncoded<'o>(&'o [u8]); + impl fmt::Display for DotEncoded<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut oid = self.0; + if oid.is_empty() { + write!(f, "[invalid]")?; + return Ok(()); + } + + // The first octet encodes two values. + let first = oid[0] / 40; + let second = oid[0] % 40; + oid = &oid[1..]; + write!(f, "{}.{}", first, second)?; + + let mut acc: usize = 0; + for b in oid { + if b & 0x80 > 0 { + acc *= 0x80; + acc += (b & 0x7f) as usize; + } else { + acc *= 0x80; + acc += (b & 0x7f) as usize; + write!(f, ".{}", acc)?; + acc = 0; + } + } + + Ok(()) + } + } + + if f.alternate() { match *self { NistP256 => f.write_str("NIST curve P-256"), @@ -474,7 +508,7 @@ impl fmt::Display for Curve { Cv25519 => f.write_str("Elliptic curve Diffie-Hellman using D.J. Bernstein's Curve25519"), Unknown(ref oid) - => write!(f, "Unknown curve (OID: {:?})", oid), + => write!(f, "Unknown curve (OID: {})", DotEncoded(oid)), } } else { match *self { @@ -490,7 +524,7 @@ impl fmt::Display for Curve { Cv25519 => f.write_str("Curve25519"), Unknown(ref oid) - => write!(f, "Unknown curve {:?}", oid), + => write!(f, "Unknown curve {}", DotEncoded(oid)), } } } |