Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Nov 3, 2021

This adds a Decoder struct which supports one configurable setting: the Base64 line-wrapping width.

From RFC7468 Section 2:

Parsers MAY handle other line sizes.

Closes #176

@tarcieri
Copy link
Member Author

tarcieri commented Nov 3, 2021

@kjvalencik this is what I propose for addressing #176. It would allow you to do:

pem_rfc7468::Decoder { wrap_width: 70 }.decode_vec(input)

...or for a fully futureproofed version:

pem_rfc7468::Decoder { wrap_width: 70, ..Default::default() }.decode_vec(input)

@kjvalencik
Copy link

@tarcieri I really like this API with the Decoder. Unfortunately, this only works when line-length is at a 4:3 ratio, otherwise base64 decode fails.

For example, this works with 64 and 72. But, if I try to decode with 70 (what OpenSSH uses), it fails to decode base64 (since it's incomplete input).

    #[test]
    fn openssh_example() {
        let pem = include_bytes!("../tests/examples/openssh.pem");
        let result = Encapsulation::parse(pem).unwrap();
        assert_eq!(result.label, "OPENSSH PRIVATE KEY");

        let decoder = Decoder { wrap_width: 70 };
        let mut buf = [0; 2048];
        result.decode(&decoder, &mut buf).unwrap();
    }
---- decoder::tests::openssh_example stdout ----
thread 'decoder::tests::openssh_example' panicked at 'called `Result::unwrap()` on an `Err` value: Base64', pem-rfc7468/src/decoder.rs:308:43

@tarcieri
Copy link
Member Author

tarcieri commented Nov 3, 2021

Aah wow, that's really annoying. Supporting anything else would require a buffered Base64 decoder, which isn't exactly trivial to implement, and something I'd like to avoid in proper PKCS/PKIX use cases.

I suppose I could add an error case for if wrap_width % 4 != 0 for now, and if someone would like to contribute a buffered decoder, it could eventually just work.

This adds a `Decoder` struct which supports one configurable setting:
the Base64 line-wrapping width.

From RFC7468 Section 2:

> Parsers MAY handle other line sizes.

Closes #176
@tarcieri tarcieri force-pushed the pem-rfc7468/decoder-struct branch from b21641f to b2d46a2 Compare November 4, 2021 17:04
@tarcieri tarcieri merged commit 46b8a08 into master Nov 4, 2021
@tarcieri tarcieri deleted the pem-rfc7468/decoder-struct branch November 4, 2021 17:09
@tarcieri tarcieri mentioned this pull request Nov 14, 2021
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.

pem-rfc7468: Handle non-standard line sizes when decoding

3 participants