Skip to content

wolfssl: get the public key to pin from wolfSSL - #23516

Closed
matty69v wants to merge 1 commit into
curl:masterfrom
matty69v:x509asn1-tbs-lengths
Closed

matty69v wants to merge 1 commit into
curl:masterfrom
matty69v:x509asn1-tbs-lengths

Conversation

@matty69v

@matty69v matty69v commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Curl_wssl_verify_pinned() used Curl_parseX509() to find the SubjectPublicKeyInfo to pin. That parser is a lightweight one meant for certinfo and does not reject everything wolfSSL rejects. With wolfSSL 5.9.2 and older, a CA-signed certificate with a malformed version or signature element in the TBSCertificate made curl read the public key from different bytes than wolfSSL did, so --pinnedpubkey could match a key the server does not hold. wolfSSL 5.9.4 rejects such certificates.

This takes the key from wolfSSL_X509_get_pubkey_buffer() instead, so the pinned key comes from wolfSSL's own parse. x509asn1.c is left as it is.

Tested against wolfSSL 5.9.2 with the reproducer from https://hackerone.com/reports/4128701: with both crafted certificates, a pin for the fake key is now refused and the pin for the real server key matches. A well-formed certificate still pins as before.

Schannel also uses Curl_parseX509() for pinning, but there the certificate has already been parsed by CryptoAPI, and I cannot build or test on Windows, so it is not changed here.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 17:58
@github-actions github-actions Bot added the tests label Oct 20, 2026
@matty69v
matty69v force-pushed the x509asn1-tbs-lengths branch from 9e40254 to c3493b2 Compare October 20, 2026 18:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused validation changes enforce the intended field boundaries, with regression coverage and no unresolved findings.

No open issues 🎉

What changed in this PR

Tightens Curl_parseX509() validation to reject malformed certificate fields that could cause curl and the TLS library to interpret different data.

Changes:

  • Requires the version wrapper to contain exactly one element.
  • Validates signature parsing and requires an OID followed by at most one parameter.
  • Adds three malformed-certificate regression cases.
File Description
tests/​unit/​unit1657.c Adds regression cases for trailing data and a missing signature OID.
lib/​vtls/​x509asn1.c Adds version and signature structure checks.

Review effort: Balanced

@testclutch

Copy link
Copy Markdown

Analysis of PR #23516 at c3493b2d:

Test 3236 failed, but it has been permanently failing lately, so it's probably NOT a fault of the PR. Note that this test has failed in 688 different CI jobs (the link just goes to one of them).

Test 3235 failed, but it has been permanently failing lately, so it's probably NOT a fault of the PR. Note that this test has failed in 153 different CI jobs (the link just goes to one of them).

Test 2100 failed, but it has been 12.2% flaky lately, so it's probably NOT a fault of the PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them). Note that this CI job has had a number of other flaky tests recently (2, to be specific) so it may be that this failure is rather a systemic issue with this job and not with this specific PR.

Generated by Testclutch

@bagder

bagder commented Oct 20, 2026

Copy link
Copy Markdown
Member

/cc @monnerat you might be interested

@monnerat

Copy link
Copy Markdown
Contributor

you might be interested

I'm afraid I do not endorse this kind of changes.
As already previously discussed in another PR/issue, this targets discrepancies that the crypto backend in use accepts. My mind did not change about the subject.

There is a comment in x509asn1.c:

* Lightweight ASN.1 parser.
* In particular, it does not check for syntactic/lexical errors.
* It is intended to support certificate information gathering for SSL backends
* that offer a mean to get certificates as a whole, but do not supply
* entry points to get particular certificate sub-fields.
* Please note there is no pretension here to rewrite a full SSL library.

That means what it says: it has been written initially to help extracting cert info when using backends that do not offer an API for that. This cert info is used for verbose output or returned by curl_easy_getinfo(). It should not be used for anything else and even more important, parsed data must have been previously accepted by the backend. If an ASN.1 element diverges but has been (wrongly) accepted by the backend, it is not curl's role to mitigate the backend's bug.

By being designed with the above in mind, the code neither implements special care nor global support about enforcing compliance with standards and fully relies on the backend for this. IMO, adding such "wart" checks is counterproductive, goes in the opposite direction of the initial intention (lightweight, not a backend by itself), redundant, bloating code and suffers from a lack of generalisation.

I do not have access to the mentioned hackerone discussion and may be don't know the exact target, but if the problem could have been pre-detected by the backend, I'm not in favor of this PR.

Comment thread tests/unit/unit1657.c Resolved
@matty69v

Copy link
Copy Markdown
Contributor Author

Thanks for the review, that's fair, and I agree the parser was never meant to be a validating one.

The thing that made me look at it is that it is not only used for certinfo today: wolfssl.c and schannel.c call Curl_parseX509() to extract the SPKI for CURLOPT_PINNEDPUBLICKEY. So when the backend is lenient (wolfSSL <= 5.9.2 here), the key curl pins is not necessarily the key the TLS library used.

If you'd rather keep x509asn1 lightweight, I think the cleaner fix is to stop relying on it for pinning, and get the public key from the backend instead (for wolfSSL via wolfSSL_X509_get_pubkey_buffer(), which returns the SPKI from wolfSSL's own parse, for Schannel from pCertInfo->SubjectPublicKeyInfo). Then any parser discrepancy only affects informational output, which matches the comment in the file. I'm happy to rework the PR in that direction, or close this one if you think neither is worth it.

@vszakats: I've replaced the new blobs. The test now keeps the one existing valid certificate and builds each bad variant as a small byte patch, with the ASN.1 element it changes written out in a comment.

@matty69v
matty69v requested review from vszakats and a balanced review from Copilot October 21, 2026 18:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused validation changes match the intended constraints, with regression coverage and no identified blocking issues.

No open issues 🎉

Review effort: Balanced

@monnerat

Copy link
Copy Markdown
Contributor

The thing that made me look at it is that it is not only used for certinfo today: wolfssl.c and schannel.c call Curl_parseX509() to extract the SPKI for CURLOPT_PINNEDPUBLICKEY. So when the backend is lenient (wolfSSL <= 5.9.2 here), the key curl pins is not necessarily the key the TLS library used.

Many thanks for the details.

Calling Curl_parseX509() without prior real crypto-backend validation is definitively a misuse.

Unless there is some plan from the curl project management to turn this parser into a validating one, the incriminated backend interfaces should be changed rather than piggy-patching the parser. This should not be a problem since you already identified the certificate extraction functions. I therefore consider the targeted problem as a backend interface problem rather than a parser one.

I still argue for a lightweight curl's parser: maintaining redundant sensitive code has little value added, while expensive. We don't need another n+1'th backend. Its presence is only justified by missing specific capabilities in the real crypto backend: the latter should never be worked-around.

Should such a validating parser be implemented anyway, validation should be driven by ASN.1 syntax tables rather than piggy-patches at some places: the latter lengthens the code and affects its readability. In addition, this is a Pandora box: there will always be temptations to perform deeper (useless) checks in sub-structures.

Curl_wssl_verify_pinned() used Curl_parseX509() to find the
SubjectPublicKeyInfo in the peer certificate. That parser is a
lightweight one meant for certinfo, and it does not reject everything
wolfSSL rejects. With wolfSSL 5.9.2 and older, a certificate with a
malformed version or signature element in its TBSCertificate made curl
read the public key from different bytes than the ones wolfSSL used,
so --pinnedpubkey compared against a key the server did not hold.

Use wolfSSL_X509_get_pubkey_buffer() instead, so the pinned key comes
from wolfSSL's own parse of the certificate.

Ref: https://hackerone.com/reports/4128701
@matty69v
matty69v force-pushed the x509asn1-tbs-lengths branch from 5d61eb0 to 7a07c66 Compare October 22, 2026 05:58
@matty69v matty69v changed the title x509asn1: check version and signature element lengths in TBSCertificate wolfssl: get the public key to pin from wolfSSL Oct 22, 2026
@matty69v

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I've reworked the PR as you suggested: x509asn1.c and the test are back to master, and wolfssl.c now takes the SPKI for pinning from wolfSSL_X509_get_pubkey_buffer(), so Curl_parseX509() is no longer used for it there.

Checked with wolfSSL 5.9.2 and the H1 reproducer: with the crafted certificates curl now pins the key wolfSSL actually used, and a normal certificate pins as before.

I left Schannel alone since CryptoAPI has already parsed the certificate there and I can't test on Windows. Happy to look at it in a separate PR if you'd like.

Ready for another look, @monnerat @vszakats.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Automated regression coverage is needed for both malformed-certificate variants on an affected wolfSSL version.

1 open issue

Review effort: Balanced

Comment thread lib/vtls/wolfssl.c Resolved
@bagder

bagder commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Calling Curl_parseX509() without prior real crypto-backend validation is definitively a misuse.

This PR is the second time within a short period when a problem in this parser was discovered because "the real crypto-backend" accepted it, and then curl code runs into problem. It shows that we cannot rely totally on the protection of the TLS libraries. We need to also cover our bases, at least in the cases where there are proven situations when the problems leak through to us.

@matty69v

Copy link
Copy Markdown
Contributor Author

@bagder thanks. To keep things separate:

This PR now only changes wolfssl.c to get the key to pin from wolfSSL with wolfSSL_X509_get_pubkey_buffer(), so curl pins exactly the key the handshake used. That fixes the reported issue on its own, and follows @monnerat's suggestion to fix the backend side rather than the parser.

If you still want the extra checks in Curl_parseX509() as a safety net, I can send them as a separate PR so they can be discussed on their own without blocking this fix. What do you prefer?

@matty69v

Copy link
Copy Markdown
Contributor Author

And maybe can we add @monnerat in the H1 report as viewer so he get more context ?

I don’t know how it works :)

@bagder

bagder commented Oct 22, 2026

Copy link
Copy Markdown
Member

And maybe can we add @monnerat in the H1 report as viewer so he get more context ?

I disclosed that report, so it is publicly accessible now.

@matty69v

Copy link
Copy Markdown
Contributor Author

Perfect

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Automated regression coverage for the malformed-certificate pinning bypass remains unresolved.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@bagder

bagder commented Oct 23, 2026

Copy link
Copy Markdown
Member

Yeah, a test case would be great but I will proceed and merge this anyway. A test can be added later.

@bagder bagder closed this in 4f4988c Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

6 participants