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

x509-cert/der regression #978

Closed
lumag opened this issue Apr 4, 2023 · 16 comments · Fixed by #987
Closed

x509-cert/der regression #978

lumag opened this issue Apr 4, 2023 · 16 comments · Fixed by #987

Comments

@lumag
Copy link
Contributor

lumag commented Apr 4, 2023

I found a regression after upgrading to x509-cert 0.2.1 / der 0.7.1. The attached certificate
test.txt can be parsed prefectly with x509-cert 0.1 / der 0.6.1, but fails after upgrade with the following error:

Error: Error { kind: Overlength, position: Some(Length(62)) }

I could not spot any obvious issue and the position seems lame (parsing TbsCertificate starting from byte 4 returns an error at position 29.

Reproducer (convert cert to DER for it to work):

use x509_cert::certificate::Certificate;
use x509_cert::der::Decode;

fn main() {
    let name = std::env::args().skip(1).next().unwrap();
    let bytes = std::fs::read(name).unwrap();
    let parsed_cert = &Certificate::from_der(&bytes).unwrap();
    println!("{parsed_cert:?}");
}
@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

git bisect is probably the easiest way to narrow down what change caused this.

Offhand I'm not sure what it would've been. The only major changes to decoding I can think of were related to integer/serial number handling.

@lumag
Copy link
Contributor Author

lumag commented Apr 4, 2023

git-bisect points to a8a0b04 (#795)

@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

Seems I guessed it then! cc @baloo

@lumag
Copy link
Contributor Author

lumag commented Apr 4, 2023

And going patch-by-patch (ugh), 62bbe0a is the problem.

@lumag
Copy link
Contributor Author

lumag commented Apr 4, 2023

And, I think I know the cause:

+const SERIAL_NUMBER_MAX_LEN: Length = Length::new(20);

@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

Yeah, looks like a 21-byte serial number:

$ openssl x509 -in /Users/tarcieri/Downloads/test.txt -inform pem -text                                           130 ↵
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            84:ce:0b:f6:a0:fe:82:4e:e5:e5:06:ca:a8:9d:78:c6:15:01:62:57:71

I don't think it meets the criteria for 21-byte serial numbers to be allowed, which would be adding a leading zero so the resulting number is positive.

@lumag
Copy link
Contributor Author

lumag commented Apr 4, 2023

Well, quoting RFC 5280:

Note: Non-conforming CAs may issue certificates with serial numbers
that are negative or zero. Certificate users SHOULD be prepared to
gracefully handle such certificates.

I think we should drop the SERIAL_NUMBER_MAX_LEN check from SertificateNumber. Instead this check should be a part of certificate builder.

@lumag
Copy link
Contributor Author

lumag commented Apr 4, 2023

In the end, rfc2459 did not contain such requirement. So we should be liberal in what we accept.

@baloo
Copy link
Member

baloo commented Apr 4, 2023

I thought we relaxed the check when parsing from DER (to allow any length) and enforced the 20bytes limitation on new?
Did I imagine that discussion?

@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

See lots of previous discussion on #823, #826, and #831

@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

@baloo you even quoted the exact same excerpt of RFC 5280: #823 (comment)

@baloo
Copy link
Member

baloo commented Apr 4, 2023

Looks like it fixed parsing of negative serials, but this one is just overlength?

@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

Yeah I think you're right. And RFC5280 doesn't seem to provide specific guidance for this case.

@baloo
Copy link
Member

baloo commented Apr 4, 2023

That was the comment I was thinking about: #826 (comment)

baloo added a commit to baloo/formats that referenced this issue Apr 4, 2023
@tarcieri
Copy link
Member

tarcieri commented Apr 4, 2023

also regardless, this seems like the wrong error, despite the name:

Error: Error { kind: Overlength, position: Some(Length(62)) }

That error is intended for TLV framing bugs which made this somewhat confusing.

A better one might be ErrorKind::Value with tag: Tag::Integer, which would at least make it clearer where the error was occurring.

baloo added a commit to baloo/formats that referenced this issue Apr 4, 2023
baloo added a commit to baloo/formats that referenced this issue Apr 5, 2023
This intends to allow user to relax checks when parsing certificate and
cover for non rfc5280 compliant x509 certificates.

Fixes RustCrypto#978
Fixes RustCrypto#984
baloo added a commit to baloo/formats that referenced this issue Apr 5, 2023
This allow the user to relax checks when parsing certificate and
cover for non rfc5280 compliant x509 certificates.

Fixes RustCrypto#978
Fixes RustCrypto#984
baloo added a commit to baloo/formats that referenced this issue Apr 5, 2023
This allow the user to relax checks when parsing certificate and
cover for non rfc5280 compliant x509 certificates.

Fixes RustCrypto#978
Fixes RustCrypto#984
@tarcieri
Copy link
Member

tarcieri commented Apr 5, 2023

I improved the error in #988.

Gonna close this in favor of #984 and #987

@tarcieri tarcieri closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants