Repository navigation
Conversation
9e40254 to
c3493b2
Compare
There was a problem hiding this comment.
🟢 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
|
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 |
|
/cc @monnerat you might be interested |
I'm afraid I do not endorse this kind of changes. There is a comment in 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 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. |
|
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 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 @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. |
Many thanks for the details. Calling 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
5d61eb0 to
7a07c66
Compare
|
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 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. |
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. |
|
@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? |
|
And maybe can we add @monnerat in the H1 report as viewer so he get more context ? I don’t know how it works :) |
I disclosed that report, so it is publicly accessible now. |
|
Perfect |
|
Yeah, a test case would be great but I will proceed and merge this anyway. A test can be added later. |

Curl_wssl_verify_pinned()usedCurl_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--pinnedpubkeycould 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.