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/signed_data: introduce support for larger DER values. #73

Merged
merged 6 commits into from
Jun 15, 2023
Merged

der/signed_data: introduce support for larger DER values. #73

merged 6 commits into from
Jun 15, 2023

Conversation

cpu
Copy link
Member

@cpu cpu commented May 30, 2023

This branch updates the Webpki der module to offer some function variants that allow reading a DER encoded tag and value where the value's length can be expressed as a long-form length of up to four bytes.

To do this we have to lift the ring::io::der module's read_tag_and_get_value function into webpki, since it is hardcoded to support long-form lengths no longer than two bytes and the upstream is not responsive to patches at this time. Once the function is within the webpki code and can be modified we tweak it to support lengths up to five bytes, and to allow the caller to specify a maximum usize to read.

The primary use-case for larger values is for supporting using parse_signed_data on large CRLs. To enable this the parse_signed_data function is updated to accept a size_limit. The one existing caller of this function (in cert.rs, for parsing certificates) was previously using the implicit limit imposed by ring to read no more than (2^16)-1 bytes. This branch makes that implicit limit into an explicit limit so that there's no risk of DOS from an extremely large certificate. The CertificateRevocationList::try_from fn that implements TryFrom is updated to allow the maximum supported tagged DER value size (based on the four-byte long-form limit). This will allow loading large CRLs where there is no DOS risk.

Unit test coverage is added both for the new size range and for existing error paths (e.g. high tag number form, non-canonical encodings).

Resolves #58

@cpu cpu self-assigned this May 30, 2023
@codecov
Copy link

codecov bot commented May 30, 2023

Codecov Report

Merging #73 (6e2c881) into main (4b0f266) will increase coverage by 0.30%.
The diff coverage is 99.48%.

❗ Current head 6e2c881 differs from pull request most recent head 3760bdf. Consider uploading reports for the commit 3760bdf to get more accurate results

@@            Coverage Diff             @@
##             main      #73      +/-   ##
==========================================
+ Coverage   95.59%   95.90%   +0.30%     
==========================================
  Files          15       15              
  Lines        2953     3078     +125     
==========================================
+ Hits         2823     2952     +129     
+ Misses        130      126       -4     
Impacted Files Coverage Δ
src/der.rs 98.02% <99.43%> (+1.37%) ⬆️
src/cert.rs 98.42% <100.00%> (+4.09%) ⬆️
src/crl.rs 99.53% <100.00%> (-0.03%) ⬇️
src/error.rs 57.14% <100.00%> (+32.14%) ⬆️
src/signed_data.rs 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like a bunch of the commits in the middle here should be squashed together since they aren't quite meaningful in isolation.

src/der.rs Outdated Show resolved Hide resolved
src/der.rs Show resolved Hide resolved
src/der.rs Outdated Show resolved Hide resolved
@cpu
Copy link
Member Author

cpu commented Jun 9, 2023

I feel like a bunch of the commits in the middle here should be squashed together since they aren't quite meaningful in isolation.

I reduced the intermediate commits. Hopefully it's a little bit better now?

I also realized I was mistakenly referring to things as "short form" lengths when they were in fact the long-form lengths and had miscalculated some boundaries based on thinking part of the leading byte represented length content when it was not the case. All of the above have been fixed throughout.

Writing this comment I also realized I dropped the unit test coverage commit. I will fix that in a moment. Please hold review until that's in :-)

@cpu
Copy link
Member Author

cpu commented Jun 9, 2023

Writing this comment I also realized I dropped the unit test coverage commit. I will fix that in a moment. Please hold review until that's in :-)

Ok, re-added and ready for another review pass. Thanks!

src/der.rs Outdated Show resolved Hide resolved
src/der.rs Outdated Show resolved Hide resolved
@ctz
Copy link
Member

ctz commented Jun 12, 2023

Given webpki does no-copy parsing and doesn't have a streaming interface, I think the amount of DOS attack surface we are avoiding by limiting the size of certificates is hopefully limited. In other words, to process a 24MB certificate the caller must already have the entire thing in contiguous memory. But keeping the existing constraint is a conservative choice, and one that won't be a problem until post-quantum certificates start being a thing.

@cpu
Copy link
Member Author

cpu commented Jun 12, 2023

Working on test failures ⏳

@cpu
Copy link
Member Author

cpu commented Jun 12, 2023

@ctz I think this is ready for another pass when you have a chance.

Since #44 landed I updated cc2a4a4 to apply the changes in this branch to CRL parsing. That's the only meaningful change outside of the tweaks suggested in your prev. review.

Previously the only way to construct a CRL was through TryFrom - I've maintained that as the "default" path for users that don't want to think about size limits. It defers to a new CertificateRevocationList::new constructor that allows specifying an explicit size limit. I think that's the right balance but I'm open to suggestions.

src/crl.rs Outdated Show resolved Hide resolved
src/crl.rs Outdated Show resolved Hide resolved
cpu added 6 commits June 15, 2023 09:35
This commit lifts out the `read_tag_and_get_value` implementation from
Ring's `io::der` package so that it can be customized for webpki's
needs. In anticipation of modifying this function to allow specifying
a maximum length for the DER value, we name it
`read_tag_and_get_value_limited`. For now, as this `fn` is unused we
allow `dead_code`.
Also updates the lifted `read_tag_and_get_value_limited` to use this
convenient translation in place of a wrapper fn.
This commit introduces some constants for misc. DER values that are used
when reading a tag and value. Additionally, some variable names are
reworded to better clarify that what bytes are signifiers of the length
encoding and what bytes constitute the encoded length data.
This commit updates the lifted `read_tag_and_get_value_limited` function
to support reading long-form DER encoded lengths of three and four bytes
in addition to *ring*'s existing support for short-form lengths and one
and two byte long-form lengths.

A two-byte long-form DER encoded length (the largest supported
previously) allows values of up to 65,536 bytes (2^16) which is
insufficient for some larger DER encoded objects such as signed CRL
data. Four byte long-form DER lengths will allow reading values up to
up to 4096MB (2^32).

In a subsequent commit we will introduce a limit argument to this
function to allow the caller to control how large the read value can be.
This commit adds a `size_limit: usize` parameter to
`read_tag_and_get_value_limited`, allowing the caller to decide
a maximum length for a value to be read. When the encoded length exceeds
this limit `Error::BadDer` is returned and the reader is not used to
fetch the tag's value.

With the size limit in place, we can introduce a variant of
`expect_tag_and_get_value` that allows specifying a maximum size limit.
The existing `expect_tag_and_get_value` is limited to lengths that can
be expressed in the long-form DER encoding of at most two bytes. This
version can be used by callers that wish to control the maximum value
size with their own limit.

Next, we update `parse_signed_data` to allow the caller to specify
a size limit for the outermost SEQUENCE of signed data. In the context
of processing certificates, we maintain the existing implicit limit that
was imposed by ring. Similarly `CertificateRevocationList::try_from`
is updated to use the same default size limit as used for certificates.
This allows keeping the `TryFrom` impl and provides a sensible default
for users that don't know what to use.

For CRLs where there is no risk of DOS attack we update the CRL
`TryFrom` implementation to use the maximum supported DER size (based on
the four-byte long-form encoding) as the limit. This allows parsing
large CRLs that would exceed the upstream *ring* two-byte long-form
limit.
@cpu
Copy link
Member Author

cpu commented Jun 15, 2023

cpu force-pushed the cpu-58-adjustable-der-size-limit branch from 6e2c881 to 3760bdf

Rebased on main, updated to incorporate @ctz's feedback.

@djc If this gets a +1 from @ctz would you like me to hold merge for you to re-review? The last push introduced functional changes w.r.t the CRL size limit.

@djc
Copy link
Member

djc commented Jun 15, 2023

Nope, I think I'm good.

@cpu cpu merged commit 4badcf3 into rustls:main Jun 15, 2023
17 of 18 checks passed
@cpu cpu deleted the cpu-58-adjustable-der-size-limit branch June 15, 2023 18:53
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.

Support for parsing large ASN.1 values
3 participants