asn1parse: Require minimal-length encodings of lengths #216

Open
DemiMarie wants to merge 1 commit from DemiMarie/strict-len into development
DemiMarie commented 2025-03-21 00:22:25 -05:00 (Migrated from github.com)

Description

X.690 explicitly states that DER must use the shortest possible encoding of a length. For instance, 04 81 00 is not a valid DER encoding of the empty OCTET STRING. Instead, 04 00 must be used. With the exception of PKCS#7, Mbed TLS only parses DER, and it is reasonable to require that even PKCS#7 be DER-encoded.

PR checklist

Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line.
If the provided content is part of the present PR remove the # symbol.

  • changelog provided
  • framework PR not required
  • mbedtls development PR not required because: no change to mbedtls
  • mbedtls 3.6 PR not required because: it is unclear whether this should be backported to stable releases
  • mbedtls 2.28 PR not required because: it is unclear whether this should be backported to stable releases
  • tests provided
## Description X.690 explicitly states that DER must use the shortest possible encoding of a length. For instance, 04 81 00 is not a valid DER encoding of the empty OCTET STRING. Instead, 04 00 must be used. With the exception of PKCS#7, Mbed TLS only parses DER, and it is reasonable to require that even PKCS#7 be DER-encoded. ## PR checklist Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line. If the provided content is part of the present PR remove the # symbol. - [x] **changelog** provided - [x] **framework PR** not required - [x] **mbedtls development PR** not required because: no change to mbedtls - [x] **mbedtls 3.6 PR** not required because: it is unclear whether this should be backported to stable releases - [x] **mbedtls 2.28 PR** not required because: it is unclear whether this should be backported to stable releases - [x] **tests** provided
mpg (Migrated from github.com) reviewed 2025-03-21 00:22:25 -05:00
mpg commented 2025-06-10 05:47:07 -05:00 (Migrated from github.com)

As discussed elsewhere, removing from the 3.6.4 EPIC as it's not clear that we want to make this kind of change in an LTS branch in the absence of a pressing security reason. (Edit: if do you see a security concern here, please report it to mbed-tls-security@lists.trustedfirmware.org as usual.)

We are making this change in 1.0/4.0 though, as part of https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184 I think - @gilles-peskine-arm since you're reviewing it can you confirm?

As discussed elsewhere, removing from the 3.6.4 EPIC as it's not clear that we want to make this kind of change in an LTS branch in the absence of a pressing security reason. (Edit: if do you see a security concern here, please report it to mbed-tls-security@lists.trustedfirmware.org as usual.) We are making this change in 1.0/4.0 though, as part of https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184 I think - @gilles-peskine-arm since you're reviewing it can you confirm?
gilles-peskine-arm commented 2025-06-10 06:11:47 -05:00 (Migrated from github.com)

Indeed, we do not fix sloppiness in LTS branches if it looks harmless, which seems to be the case here. So we will not backport this patch to 3.6.

This patch is definitely desirable for TF-PSA-Crypto. It's unrelated to https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184: that issue is solely about INTEGER payload encoding, not about the length encoding.

Indeed, we do not fix sloppiness in LTS branches if it looks harmless, which seems to be the case here. So we will not backport this patch to 3.6. This patch is definitely desirable for TF-PSA-Crypto. It's unrelated to https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184: that issue is solely about INTEGER payload encoding, not about the length encoding.
DemiMarie commented 2025-06-10 11:01:23 -05:00 (Migrated from github.com)

PKCS#7 actually allows arbitrary BER, but that also includes indefinite-length encodings, which are already unsupported. A full BER parser would need to be implemented differently.

PKCS#7 actually allows arbitrary BER, but that also includes indefinite-length encodings, which are already unsupported. A full BER parser would need to be implemented differently.
mpg commented 2025-06-11 02:48:18 -05:00 (Migrated from github.com)

It's unrelated to https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184: that issue is solely about INTEGER payload encoding, not about the length encoding.

Ah, good point, I has missed that, thanks.

PKCS#7 actually allows arbitrary BER, but that also includes indefinite-length encodings, which are already unsupported. A full BER parser would need to be implemented differently.

Agreed, our current parser is not for BER, it's for "sloppy DER". I don't think we have or ever had any intention of parsing arbitrary BER.

> It's unrelated to https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/184: that issue is solely about INTEGER payload encoding, not about the length encoding. Ah, good point, I has missed that, thanks. > PKCS#7 actually allows arbitrary BER, but that also includes indefinite-length encodings, which are already unsupported. A full BER parser would need to be implemented differently. Agreed, our current parser is not for BER, it's for "sloppy DER". I don't think we have or ever had any intention of parsing arbitrary BER.
This pull request is broken due to missing fork information.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'DemiMarie/strict-len:DemiMarie/strict-len'
git switch 'DemiMarie/strict-len'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'development'
git merge --no-ff 'DemiMarie/strict-len'
git switch 'DemiMarie/strict-len'
git rebase 'development'
git switch 'development'
git merge --ff-only 'DemiMarie/strict-len'
git switch 'DemiMarie/strict-len'
git rebase 'development'
git switch 'development'
git merge --no-ff 'DemiMarie/strict-len'
git switch 'development'
git merge --squash 'DemiMarie/strict-len'
git switch 'development'
git merge --ff-only 'DemiMarie/strict-len'
git switch 'development'
git merge 'DemiMarie/strict-len'
git push origin 'development'
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-Mbed-TLS/TF-PSA-Crypto!216
No description provided.