Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion ssh-key/src/algorithm.rs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
//! Algorithm support.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::{fmt, str};
Expand DownExpand Up@@ -109,6 +109,16 @@ impl Decode for Algorithm {
}
}

impl Encode for Algorithm {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + self.as_str().len())
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_str(self.as_str())
}
}

impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
102 changes: 99 additions & 3 deletions ssh-key/src/base64.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,7 +57,7 @@ impl<'i> Decoder<'i> {
Ok(buf[0])
}

/// Decodes a `uint32` as described in [RFC4251 § 5]:
/// Decode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
Expand DownExpand Up@@ -113,7 +113,7 @@ impl<'i> Decoder<'i> {
Ok(result)
}

/// Decodes a `string` as described in [RFC4251 § 5]:
/// Decode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
Expand DownExpand Up@@ -146,9 +146,97 @@ impl<'i> Decoder<'i> {
}
}

/// Encoder trait.
pub(crate) trait Encode: Sized {
/// Get the length of this type encoded in bytes, prior to Base64 encoding.
fn encoded_len(&self) -> Result<usize>;

/// Attempt to encode a value of this type using the provided [`Encoder`].
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()>;
}

/// Stateful Base64 encoder.
pub(crate) struct Encoder<'o> {
inner: base64ct::Encoder<'o, base64ct::Base64>,
}

impl<'o> Encoder<'o> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub(crate) fn new(buffer: &'o mut [u8]) -> Result<Self> {
Ok(Self {
inner: base64ct::Encoder::new(buffer)?,
})
}

/// Encode the given byte slice as Base64.
pub(crate) fn encode(&mut self, bytes: &[u8]) -> Result<()> {
Ok(self.inner.encode(bytes)?)
}

/// Encode a `uint32` as described in [RFC4251 § 5]:
///
/// > Represents a 32-bit unsigned integer. Stored as four bytes in the
/// > order of decreasing significance (network byte order).
/// > For example: the value 699921578 (0x29b7f4aa) is stored as 29 b7 f4 aa.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_u32(&mut self, num: u32) -> Result<()> {
self.encode(&num.to_be_bytes())
}

/// Encode a `usize` as a `uint32` as described in [RFC4251 § 5].
///
/// Uses [`Encoder::encode_u32`] after converting from a `usize`, handling
/// potential overflow if `usize` is bigger than `u32`.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_usize(&mut self, num: usize) -> Result<()> {
self.encode_u32(u32::try_from(num)?)
}

/// Encodes `[u8]` into `byte[n]` as described in [RFC4251 § 5]:
///
/// > A byte represents an arbitrary 8-bit value (octet). Fixed length
/// > data is sometimes represented as an array of bytes, written
/// > byte[n], where n is the number of bytes in the array.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_byte_slice(&mut self, bytes: &[u8]) -> Result<()> {
self.encode_usize(bytes.len())?;
self.encode(bytes)
}

/// Encode a `string` as described in [RFC4251 § 5]:
///
/// > Arbitrary length binary string. Strings are allowed to contain
/// > arbitrary binary data, including null characters and 8-bit
/// > characters. They are stored as a uint32 containing its length
/// > (number of bytes that follow) and zero (= empty string) or more
/// > bytes that are the value of the string. Terminating null
/// > characters are not used.
/// >
/// > Strings are also used to store text. In that case, US-ASCII is
/// > used for internal names, and ISO-10646 UTF-8 for text that might
/// > be displayed to the user. The terminating null character SHOULD
/// > NOT normally be stored in the string. For example: the US-ASCII
/// > string "testing" is represented as 00 00 00 07 t e s t i n g. The
/// > UTF-8 mapping does not alter the encoding of US-ASCII characters.
///
/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
pub(crate) fn encode_str(&mut self, s: &str) -> Result<()> {
self.encode_byte_slice(s.as_bytes())
}

/// Finish encoding, returning the encoded Base64 as a `str`.
pub(crate) fn finish(self) -> Result<&'o str> {
Ok(self.inner.finish()?)
}
}

#[cfg(test)]
mod tests {
use super::Decoder;
use super::{Decoder, Encoder};

/// From `id_ecdsa_p256.pub`
const EXAMPLE_BASE64: &str =
Expand All@@ -168,4 +256,12 @@ mod tests {
let decoded = decoder.decode_into(&mut buf).unwrap();
assert_eq!(EXAMPLE_BIN, decoded);
}

#[test]
fn encode() {
let mut buffer = [0u8; EXAMPLE_BASE64.len()];
let mut encoder = Encoder::new(&mut buffer).unwrap();
encoder.encode(EXAMPLE_BIN).unwrap();
assert_eq!(EXAMPLE_BASE64, encoder.finish().unwrap());
}
}
8 changes: 8 additions & 0 deletions ssh-key/src/error.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,14 @@ impl From<core::str::Utf8Error> for Error {
}
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Error {
Error::CharacterEncoding
}
}

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
impl From<sec1::Error> for Error {
Expand Down
62 changes: 60 additions & 2 deletions ssh-key/src/public.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,16 @@ pub use self::ed25519::Ed25519PublicKey;
pub use self::{dsa::DsaPublicKey, rsa::RsaPublicKey};

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Algorithm, Error, Result,
};
use core::str::FromStr;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};

/// SSH public key.
#[derive(Clone, Debug)]
Expand DownExpand Up@@ -67,6 +70,33 @@ impl PublicKey {
})
}

/// Encode this public key as a OpenSSH-formatted public key.
pub fn encode_openssh<'o>(&self, out: &'o mut [u8]) -> Result<&'o str> {
#[cfg(not(feature = "alloc"))]
let comment = "";
#[cfg(feature = "alloc")]
let comment = &self.comment;

openssh::Encapsulation::encode(out, self.algorithm().as_str(), comment, |encoder| {
self.key_data.encode(encoder)
})
}

/// Encode this public key as an OpenSSH-formatted public key, allocating a
/// [`String`] for the result.
#[cfg(feature = "alloc")]
pub fn to_openssh(&self) -> Result<String> {
let encoded_len = 2
+ self.algorithm().as_str().len()
+ (self.key_data.encoded_len()? * 4 / 3)
+ self.comment.len();

let mut buf = vec![0u8; encoded_len];
let actual_len = self.encode_openssh(&mut buf)?.len();
buf.truncate(actual_len);
Ok(String::from_utf8(buf)?)
}

/// Get the digital signature [`Algorithm`] used by this key.
pub fn algorithm(&self) -> Algorithm {
self.key_data.algorithm()
Expand All@@ -81,6 +111,13 @@ impl FromStr for PublicKey {
}
}

#[cfg(feature = "alloc")]
impl ToString for PublicKey {
fn to_string(&self) -> String {
self.to_openssh().expect("SSH public key encoding error")
}
}

/// Public key data.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
Expand DownExpand Up@@ -202,3 +239,24 @@ impl Decode for KeyData {
}
}
}

impl Encode for KeyData {
fn encoded_len(&self) -> Result<usize> {
let alg_len = self.algorithm().encoded_len()?;
let key_len = match self {
Self::Ed25519(key) => key.encoded_len()?,
#[allow(unreachable_patterns)]
_ => return Err(Error::Algorithm),
};
Ok(alg_len + key_len)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
self.algorithm().encode(encoder)?;
match self {
Self::Ed25519(key) => key.encode(encoder),
#[allow(unreachable_patterns)]
_ => Err(Error::Algorithm),
}
}
}
12 changes: 11 additions & 1 deletion ssh-key/src/public/ed25519.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
//! Edwards Digital Signature Algorithm (EdDSA) over Curve25519.

use crate::{
base64::{self, Decode},
base64::{self, Decode, Encode},
Error, Result,
};
use core::fmt;
Expand DownExpand Up@@ -37,6 +37,16 @@ impl Decode for Ed25519PublicKey {
}
}

impl Encode for Ed25519PublicKey {
fn encoded_len(&self) -> Result<usize> {
Ok(4 + Self::BYTE_SIZE)
}

fn encode(&self, encoder: &mut base64::Encoder<'_>) -> Result<()> {
encoder.encode_byte_slice(self.as_ref())
}
}

impl fmt::Display for Ed25519PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:X}", self)
Expand Down
49 changes: 43 additions & 6 deletions ssh-key/src/public/openssh.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
//! ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com
//! ```

use crate::{Error, Result};
use crate::{base64, Error, Result};
use core::str;

/// OpenSSH public key encapsulation parser.
Expand All@@ -31,8 +31,8 @@ pub(crate) struct Encapsulation<'a> {
impl<'a> Encapsulation<'a> {
/// Parse the given binary data.
pub(super) fn decode(mut bytes: &'a [u8]) -> Result<Self> {
let algorithm_id = parse_segment_str(&mut bytes)?;
let base64_data = parse_segment(&mut bytes)?;
let algorithm_id = decode_segment_str(&mut bytes)?;
let base64_data = decode_segment(&mut bytes)?;
let comment = str::from_utf8(bytes)
.map_err(|_| Error::CharacterEncoding)?
.trim_end();
Expand All@@ -48,10 +48,34 @@ impl<'a> Encapsulation<'a> {
comment,
})
}

/// Encode data with OpenSSH public key encapsulation.
pub(super) fn encode<'o, F>(
out: &'o mut [u8],
algorithm_id: &str,
comment: &str,
f: F,
) -> Result<&'o str>
where
F: FnOnce(&mut base64::Encoder<'_>) -> Result<()>,
{
let mut offset = 0;
encode_str(out, &mut offset, algorithm_id)?;
encode_str(out, &mut offset, " ")?;

let mut encoder = base64::Encoder::new(&mut out[offset..])?;
f(&mut encoder)?;
let base64_len = encoder.finish()?.len();

offset += base64_len;
encode_str(out, &mut offset, " ")?;
encode_str(out, &mut offset, comment)?;
Ok(str::from_utf8(&out[..offset])?)
}
}

/// Parse a segment of the public key.
fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
fn decode_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
let start = *bytes;
let mut len = 0;

Expand DownExpand Up@@ -81,8 +105,21 @@ fn parse_segment<'a>(bytes: &mut &'a [u8]) -> Result<&'a [u8]> {
}

/// Parse a segment of the public key as a `&str`.
fn parse_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(parse_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
fn decode_segment_str<'a>(bytes: &mut &'a [u8]) -> Result<&'a str> {
str::from_utf8(decode_segment(bytes)?).map_err(|_| Error::CharacterEncoding)
}

/// Encode a segment of the public key.
fn encode_str(out: &mut [u8], offset: &mut usize, s: &str) -> Result<()> {
let bytes = s.as_bytes();

if *offset + bytes.len() > out.len() {
return Err(Error::Length);
}

out[*offset..][..bytes.len()].copy_from_slice(bytes);
*offset += bytes.len();
Ok(())
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions ssh-key/tests/public_key.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -212,3 +212,10 @@ fn decode_rsa_4096_openssh() {

assert_eq!("user@example.com", ossh_key.comment);
}

#[cfg(feature = "alloc")]
#[test]
fn encode_ed25519_openssh() {
let ossh_key = PublicKey::from_openssh(OSSH_ED25519_EXAMPLE).unwrap();
assert_eq!(OSSH_ED25519_EXAMPLE.trim_end(), &ossh_key.to_string())
}