Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading PEM from slices without trailing newline #66

Merged
merged 2 commits into from
Dec 13, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustls-pki-types"
version = "1.10.0"
version = "1.10.1"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions src/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ fn from_slice(mut input: &[u8]) -> Result<Option<((SectionKind, Vec<u8>), &[u8])
let (line, newline_plus_remainder) = input.split_at(index);
input = &newline_plus_remainder[1..];
Some(line)
} else if !input.is_empty() {
let next_line = input;
input = &[];
Some(next_line)
} else {
None
};
Expand Down
28 changes: 28 additions & 0 deletions tests/data/rsa-key-no-trailing-newline.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCrOnJjNc/rt+EA
5V5w6Jq+d6WzukJqrFrEVogUI3ByMagTGx5YYABtbqnJXZ5Ad5gUxZzihqCvNzEP
IPS4OGIfhvzKhRno1qIK4u/luoVdc/kItZ5gNE7OIW6hmgD6X1JRQjnc6GNoOINX
2wmL7zePhD6mCqNIwlqQjjDh+GHkPdlhn/XXnv/LgRYpb52lfVmihqcAej5mIr6g
5xdyV1ql53QwdOEx0IJayOVuCvVSt2l7y8vqolHQ4CwNc4XN+rTYXdia8akysFiH
dMeUQi46TqWarvK+0s3hcq5NNzesg6p5miNyw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5yXQpFS
VKZaf26IBAAknew+4jgPkNCI2qlWgBki1GSpEJ0CgYBpksYOxy58fVySeKtxz7Vy
ckfVCsCsJKIRK3GU4M67GPiDUXGPJCb8l5QUcjwD1hte6aT6/z+DhW552kfcRd/w
d9UuI3uBcZosFBI5UQM4oQQ5gOLOcSmHfesRE0X5hQdcgjN025lUxUTGJqsrD6wj
gh5gNFkL4M5Hoktydw93eQ==
-----END PRIVATE KEY-----
21 changes: 21 additions & 0 deletions tests/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ fn any_private_key() {
));
}

#[test]
fn no_trailing_newline() {
let data = include_bytes!("data/rsa-key-no-trailing-newline.pem");
assert_eq!(
PrivatePkcs8KeyDer::pem_slice_iter(data)
.collect::<Result<Vec<_>, _>>()
.unwrap()
.len(),
1
);

assert_eq!(
PrivatePkcs8KeyDer::pem_file_iter("tests/data/rsa-key-no-trailing-newline.pem")
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
.len(),
1
);
}

#[test]
fn certificates() {
let data = include_bytes!("data/zen.pem");
Expand Down
Loading