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

DER canonical encoding for INTEGERs #1395

Closed
mkbanchi opened this issue May 3, 2024 · 6 comments
Closed

DER canonical encoding for INTEGERs #1395

mkbanchi opened this issue May 3, 2024 · 6 comments

Comments

@mkbanchi
Copy link
Contributor

mkbanchi commented May 3, 2024

Trying to decode a simple DER payload [0x02, 0x00] as an INTEGER, I get an ASN.1 INTEGER not canonically encoded as DER error. Obviously [0x02, 0x01, 0x00] works correctly and decode as 0.

I took a look at ITU-T X.690 specification and I was not able to find a section describing this restriction, neither in paragraph 9 (Canonical Encoding Rules), nor in paragraph 10 (Distinguished encoding rules). There are some restrictions on Length form, but it seems there is nothing that doesn't allow to have only one byte for length with value 0x00, as is specified in section 8.1.3.4 (short form) instead.

Now, thinking about DER, i suppose that this could make sense, but i would not be wondered if next code succeded:

assert_eq!(i32::from_der(&[0x02, 0x00]), Ok(0));
@tarcieri
Copy link
Member

tarcieri commented May 3, 2024

From X.690:

8.3 Encoding of an integer value
8.3.1 The encoding of an integer value shall be primitive. The contents octets shall consist of one or more octets.

@tarcieri tarcieri closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2024
@mkbanchi
Copy link
Contributor Author

mkbanchi commented May 3, 2024

Oh, I missed that :(, thanks.

@tarcieri
Copy link
Member

tarcieri commented May 3, 2024

No worries, it's an elephantine specification

@tarcieri
Copy link
Member

tarcieri commented May 3, 2024

This error might be better surfaced as Length rather than Noncanonical, since this wouldn't be a valid BER production either

@mkbanchi
Copy link
Contributor Author

mkbanchi commented May 4, 2024

I agree, maybe Length or similar would help to explain the error better. Thanks

@tarcieri
Copy link
Member

tarcieri commented May 8, 2024

Changed to Length in #1400

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

No branches or pull requests

2 participants