Skip to content

Conversation

@tarcieri
Copy link
Member

OpenSSH private keys contain a comment field in the binary serialization encoded as an RFC4251 string type, which is described as follows:

Arbitrary length binary string.  Strings are allowed to contain
arbitrary binary data, including null characters and 8-bit
characters

Previously this field was stored using Rust's String type which constrains the allowed encoding of the comments to UTF-8, leading to a failure to parse keys containing such comments.

This has been changed to store a Vec<u8>, with helper methods comment_str and comment_str_lossy to obtain &str references, and comment bytes to get the raw value. The original comment methods have been deprecated.

The lossy algorithm decodes as much of the string as possible as UTF-8.

The public key serialization, which encodes the key as a one-line string, uses the lossy encoding of the comment so as to still remain String-compatible. If there's desire to correct this, it will require more substantial changes to the entire public key decoder/encoder.

Fixes #331

OpenSSH private keys contain a comment field in the binary serialization
encoded as an RFC4251 `string` type, which is described as follows:

    Arbitrary length binary string.  Strings are allowed to contain
    arbitrary binary data, including null characters and 8-bit
    characters

Previously this field was stored using Rust's `String` type which
constrains the allowed encoding of the comments to UTF-8, leading to a
failure to parse keys containing such comments.

This has been changed to store a `Vec<u8>`, with helper methods
`comment_str` and `comment_str_lossy` to obtain `&str` references, and
`comment` bytes to get the raw value. The original `comment` methods
have been deprecated.

The lossy algorithm decodes as much of the string as possible as UTF-8.

The public key serialization, which encodes the key as a one-line
string, uses the lossy encoding of the comment so as to still remain
`String`-compatible. If there's desire to correct this, it will require
more substantial changes to the entire public key decoder/encoder.

Fixes #331
@tarcieri tarcieri merged commit c7aaebd into master Mar 26, 2025
14 checks passed
@tarcieri tarcieri deleted the ssh-key/non-utf8-comment-support branch April 19, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support non-UTF8 key comments?

2 participants