Skip to content

Commit ff20ecf

Browse files
authored
ssh-key: avoid zero-length decodes via base64ct (#216)
Adjust to the changes to `base64ct` that reject zero-length decode() calls as invalid. The underlying trigger for this change was to avoid problems with decoding certain OpenSSH private keys. If a "PEM-like" OpenSSH format private key: 1. has no comment; 2. is of a length such that there is no padding bytes at the end of the "Unencrypted list of N private keys"; ***and*** 3. is of a length such that the base64-encoded form of the key does not require any `=` bytes for padding; then `ssh_key::private::PrivateKey::from_openssh` failed to successfully parse the key. The root cause was `base64ct` (prior to RustCrypto/formats#1387) incorrectly rejecting zero-length reads, *only* when the base64-encoded buffer was empty. This only happened for reading empty comments, because comments are at the end of the key. It *wouldn't* happen if: 1. the key had a comment, because that wouldn't be a zero-length read; 2. there were padding bytes in the "Unencrypted list of N private keys", because those padding bytes would still be in the buffer, so the buffer wasn't empty; *or* 3. there were base64 `=` padding characters, because those would cause the decoder to believe a read would succeed (even though it wouldn't if the read was longer than zero bytes). Debugging this was... an amusement. The most reliable way to demonstrate this was with an ECDSA P-256 key without a comment (easily generated with `ssh-keygen -t ecdsa -b 256 -C '' -N '' -f <file>`), because they're *usually* (but not *always*) an appropriate length to trigger the bug. Amusingly (cough), the existing ECDSA P-256 test key in `ssh-key/tests/examples/id_ecdsa_p256` is one of the outliers, as its `d` is 33 bytes, rather than 32, which means that key (when stripped of its comment) *doesn't* trigger the bug.
1 parent 3b41fd9 commit ff20ecf

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

ssh-encoding/src/reader.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ pub struct NestedReader<'r, R: Reader> {
178178

179179
impl<'r, R: Reader> Reader for NestedReader<'r, R> {
180180
fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
181+
if out.is_empty() {
182+
return Ok(out);
183+
}
184+
181185
let remaining_len = self
182186
.remaining_len
183187
.checked_sub(out.len())
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS
3+
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQRx3l5o/ZI7bNGXguxVI/VmDd/SIwUo
4+
nlZHbHSmwBSeHPT7RisjBbiXnS829RrZ2o+Ix34GFtLN7z+SBHViPRVuAAAAmOCDDMLggw
5+
zCAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHHeXmj9kjts0ZeC
6+
7FUj9WYN39IjBSieVkdsdKbAFJ4c9PtGKyMFuJedLzb1Gtnaj4jHfgYW0s3vP5IEdWI9FW
7+
4AAAAgNEF96jnfIuhkq4ECZqNPe98Fv1SFb5evUQAq3/MtkKEAAAAA
8+
-----END OPENSSH PRIVATE KEY-----

ssh-key/tests/private_key.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const OPENSSH_RSA_4096_EXAMPLE: &str = include_str!("examples/id_rsa_4096");
4646
#[cfg(feature = "alloc")]
4747
const OPENSSH_OPAQUE_EXAMPLE: &str = include_str!("examples/id_opaque");
4848

49+
/// OpenSSH-formatted private key with no internal or external padding, and no comment
50+
/// Trips a corner case in base64ct
51+
#[cfg(feature = "ecdsa")]
52+
const OPENSSH_PADLESS_WONDER_EXAMPLE: &str = include_str!("examples/padless_wonder");
53+
4954
/// Get a path into the `tests/scratch` directory.
5055
#[cfg(feature = "std")]
5156
pub fn scratch_path(filename: &str) -> PathBuf {
@@ -129,6 +134,24 @@ fn decode_ecdsa_p256_openssh() {
129134
assert_eq!("[email protected]", key.comment());
130135
}
131136

137+
#[cfg(feature = "ecdsa")]
138+
#[test]
139+
fn decode_padless_wonder_openssh() {
140+
let key = PrivateKey::from_openssh(OPENSSH_PADLESS_WONDER_EXAMPLE).unwrap();
141+
assert_eq!(
142+
Algorithm::Ecdsa {
143+
curve: EcdsaCurve::NistP256
144+
},
145+
key.algorithm(),
146+
);
147+
assert_eq!(Cipher::None, key.cipher());
148+
assert_eq!(KdfAlg::None, key.kdf().algorithm());
149+
assert!(key.kdf().is_none());
150+
151+
#[cfg(feature = "alloc")]
152+
assert_eq!("", key.comment());
153+
}
154+
132155
#[cfg(feature = "ecdsa")]
133156
#[test]
134157
fn decode_ecdsa_p384_openssh() {

0 commit comments

Comments
 (0)