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

Optimize name constraint matching #4047

Merged
merged 11 commits into from May 14, 2024
Merged

Optimize name constraint matching #4047

merged 11 commits into from May 14, 2024

Conversation

randombit
Copy link
Owner

Previously name constraint matching worked by smashing all of the constraints and names into std::string, then parsing the values back out of the strings for each match attempt. This is all quite slow and needless. Instead store the names in their best form for matching in a std::variant.

@randombit randombit requested a review from reneme May 10, 2024 12:02
@randombit randombit force-pushed the jack/opt-name-constraints branch 2 times, most recently from 43a4c83 to 0b70811 Compare May 10, 2024 12:13
@coveralls
Copy link

coveralls commented May 10, 2024

Coverage Status

coverage: 91.92% (-0.07%) from 91.993%
when pulling 6700358 on jack/opt-name-constraints
into 39535f1 on master.

Copy link
Collaborator

@FAlbertDev FAlbertDev left a comment

Choose a reason for hiding this comment

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

I like your optimizations :)

Comment on lines 265 to 270
m_excluded_contains_unknown = false;
for(const auto& c : m_permitted_subtrees) {
if(c.base().is_unknown_type()) {
m_excluded_contains_unknown = true;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
m_excluded_contains_unknown = false;
for(const auto& c : m_permitted_subtrees) {
if(c.base().is_unknown_type()) {
m_excluded_contains_unknown = true;
}
}
m_excluded_contains_unknown = false;
for(const auto& c : m_excluded_subtrees) {
if(c.base().is_unknown_type()) {
m_excluded_contains_unknown = true;
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

😭

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed using a lambda

m_permitted_contains_unknown = contains_unknown(m_permitted_subtrees);
m_excluded_contains_unknown = contains_unknown(m_excluded_subtrees);

src/lib/x509/name_constraint.cpp Outdated Show resolved Hide resolved
Previously name constraints flattened everything to a std::string and
then parsed it back to the desired form during matching. This is slow.
It also bounced through a std::function for each match, which has a
surprisingly high overhead.

This commit also removes some sketchy (untested, unused within
library, unclear purpose) string constructors for GeneralName and
GeneralSubtree. Technically a SemVer break but I'm pretty sure nobody
uses these.
DNS is case insensitive and forcing the DNS name to lowercase means we
don't have to tolower while comparing name constraints.
If we match on an excluded tree there is no reason to continue
checking the remainder of the constraints.
@randombit randombit force-pushed the jack/opt-name-constraints branch 2 times, most recently from f5d098f to 6700358 Compare May 14, 2024 08:01
If so we can immediately fail if the extension is critical.

Also this allows returning early from is_permitted since we don't have
to handle the case of looking for critical+unknown permitted subtrees,
which otherwise would force us to scan (and match against) the entire
list of permitted trees.
With the help of string_view we don't need to create any copies.
RFC 5280 states that minimum must be zero and maximum must be unset,
so keeping the fixed values in memory isn't that helpful.
@randombit randombit merged commit c326482 into master May 14, 2024
43 checks passed
@randombit randombit deleted the jack/opt-name-constraints branch May 14, 2024 08:37
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.

None yet

3 participants