From a9bf54b8466f99e3618f9b176a7818b2a4b18185 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 13 Dec 2024 13:21:42 +0100 Subject: [PATCH 1/2] Fix reading PEM from slices without trailing newline --- src/pem.rs | 4 ++++ tests/data/rsa-key-no-trailing-newline.pem | 28 ++++++++++++++++++++++ tests/pem.rs | 21 ++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/data/rsa-key-no-trailing-newline.pem diff --git a/src/pem.rs b/src/pem.rs index 5955387..243979a 100644 --- a/src/pem.rs +++ b/src/pem.rs @@ -203,6 +203,10 @@ fn from_slice(mut input: &[u8]) -> Result), &[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 }; diff --git a/tests/data/rsa-key-no-trailing-newline.pem b/tests/data/rsa-key-no-trailing-newline.pem new file mode 100644 index 0000000..54eafa3 --- /dev/null +++ b/tests/data/rsa-key-no-trailing-newline.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCrOnJjNc/rt+EA +5V5w6Jq+d6WzukJqrFrEVogUI3ByMagTGx5YYABtbqnJXZ5Ad5gUxZzihqCvNzEP +IPS4OGIfhvzKhRno1qIK4u/luoVdc/kItZ5gNE7OIW6hmgD6X1JRQjnc6GNoOINX +2wmL7zePhD6mCqNIwlqQjjDh+GHkPdlhn/XXnv/LgRYpb52lfVmihqcAej5mIr6g +5xdyV1ql53QwdOEx0IJayOVuCvVSt2l7y8vqolHQ4CwNc4XN+rTYXdia8akysFiH +dMeUQi46TqWarvK+0s3hcq5NNzesg6p5miNywpUVffAYLmd6Rr9vDOkFTwQzIX8U +3Pi05TNtAgMBAAECggEALEZWoZSeiMLpKUQl4Wgj6zAg5pI47kBW0AHX+e0X/E8e +uKIAfLWGJsXAnVLZwq2p42Ud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1hte6aT6/z+DhW552kfcRd/w +d9UuI3uBcZosFBI5UQM4oQQ5gOLOcSmHfesRE0X5hQdcgjN025lUxUTGJqsrD6wj +gh5gNFkL4M5Hoktydw93eQ== +-----END PRIVATE KEY----- \ No newline at end of file diff --git a/tests/pem.rs b/tests/pem.rs index fbea816..f8eaab7 100644 --- a/tests/pem.rs +++ b/tests/pem.rs @@ -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::, _>>() + .unwrap() + .len(), + 1 + ); + + assert_eq!( + PrivatePkcs8KeyDer::pem_file_iter("tests/data/rsa-key-no-trailing-newline.pem") + .unwrap() + .collect::, _>>() + .unwrap() + .len(), + 1 + ); +} + #[test] fn certificates() { let data = include_bytes!("data/zen.pem"); From cd01b0f993a1a2508b56f58b20526abb56cff307 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 13 Dec 2024 13:23:45 +0100 Subject: [PATCH 2/2] Bump version to 1.10.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 98175a2..051e55f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"