Skip to content

fix: check the Mbed TLS client key against its certificate - #790

Merged
DavidCozens merged 4 commits into
feature/tls-reworkfrom
fix/mbedtls-key-certificate-pairing
Aug 23, 2026
Merged

fix: check the Mbed TLS client key against its certificate#790
DavidCozens merged 4 commits into
feature/tls-reworkfrom
fix/mbedtls-key-certificate-pairing

Conversation

@DavidCozens

@DavidCozens DavidCozens commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Purpose

Addresses #719. SolidSyslogMbedTlsStream installed the client credential
without checking that the key belonged to the certificate, so a mismatched pair
was accepted at Open and surfaced only as a handshake rejection from the
collector. On an embedded target that puts the fault on the device and the
evidence at the far end of the connection. The OpenSSL adapter has checked this
locally since it shipped, and the two are meant to be substitutable.

No Closes keyword, per the integration-branch convention — the issue is closed
by the final feature-to-main pull request.

Change Description

mbedtls_pk_check_pair is the counterpart to SSL_CTX_check_private_key that
the issue's original framing said did not exist. It takes &crt->pk, and for an
RSA key compares the modulus and public exponent, for an EC key the exported
public keys — no signature round trip, so there is no code-space argument for
leaving the two adapters different. mbedtls_ssl_conf_own_cert names it in its
own documentation as the way to check the pair it is being handed. The RNG it
takes is the Rng handle the stream configuration already requires, so the
check needs nothing new from the integrator.

The check runs before mbedtls_ssl_conf_own_cert and a mismatch skips the
install. Reporting while installing anyway would still present a credential the
collector rejects at CertificateVerify, which is the fault this exists to
remove.

A mismatch reports WARNING with CAT_BAD_CONFIG under a new detail code,
SOLIDSYSLOG_MBEDTLS_STREAM_ERROR_CLIENT_CREDENTIAL_MISMATCHED, and Open
succeeds server-authenticated. That follows the credential rule in docs/tls.md:
a fault in the material we present never stops delivery. The code is placed
between _INCOMPLETE and _NOT_INSTALLED to match the order the OpenSSL adapter
already uses, which shifts _NOT_INSTALLED by one — handlers match by name, and
nothing outside this branch has consumed either value.

The pairing check moves into the Mbed TLS credentials backend when the
credentials role lands later in #782. That relocation is known and accepted, so
this defect is fixed now rather than after the API work.

Test Evidence

Four commits, red confirmed before each:

  1. OpenChecksClientKeyAgainstItsCertificate — red as a compile failure on the
    absent fake accessors. Green by adding mbedtls_pk_check_pair to
    MbedTlsFake and calling it with the certificate's public key, the private
    key, mbedtls_ctr_drbg_random and the configured RNG. The result is not read
    at this step.
  2. OpenReportsMismatchedClientCredentialAndStillConnects — red on the absent
    detail code. Green by reporting it and keeping Open true.
  3. OpenSkipsOwnCertWhenClientKeyDoesNotMatchCertificate — the skip had slipped
    into step 2 as an else if, so the production code was reduced back to the
    minimum to get a genuine red (expected <0> but was <1>) before the else if
    was restored.
  4. Refactor under green — the condition becomes
    MbedTlsStream_ClientKeyMatchesCertificate.

No test was added for the credential-absent case: the check sits inside the
existing MbedTlsStream_HasClientCredential branch, and
OpenSkipsOwnCertWhenClientCertChainIsNull / ...WhenClientKeyIsNull already
drive Open with a NULL certificate, so moving the dereference outside the guard
fails them.

Full local run in the freertos-host service: 22/22 suites, including
MbedTlsIntegrationTests and OpenSslIntegrationTests against the real
libraries. SolidSyslogMbedTlsStreamTest is 61 tests, 182 checks.
clang-format --dry-run --Werror clean over the whole tree,
misra_renumber.py --apply produced six line-number updates and no new findings,
markdownlint clean on the changed page.

Areas Affected

Platform/MbedTls/ — the stream adapter and its error enum. Tests/Support/
gains one interposed mbedTLS entry point, used only by the mbedTLS test
executables. The divergence note comes off docs/platforms/mbedtls/index.md,
leaving five. Nothing in Core/ and no public API outside the Mbed TLS pack.

Summary by CodeRabbit

  • Bug Fixes

    • Client certificates are now checked against their private keys before use.
    • Mismatched credentials are rejected with a configuration warning, while the connection can still proceed without the invalid certificate.
    • Added a dedicated error code for client credential mismatches.
  • Documentation

    • Updated the Mbed TLS platform documentation to reflect the completed credential validation support.
  • Tests

    • Added coverage for matching and mismatched client certificate and private-key combinations.

DavidCozens and others added 4 commits August 22, 2026 23:52
Drives mbedtls_pk_check_pair into MbedTlsStream_ApplyTlsPolicy with the
certificate's public key, the private key and the RNG the configuration
already carries. The result is not read yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A key that does not match its certificate reached the collector as a
handshake rejection, putting the evidence at the far end of the connection.
mbedtls_pk_check_pair sees it locally, so it is now reported as WARNING with
CAT_BAD_CONFIG and its own detail code, and the stream connects
server-authenticated as it does for every other credential fault.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Reporting while installing anyway would still present a certificate the
collector rejects at CertificateVerify, which is the diagnosis-at-the-wrong-end
fault the check exists to remove.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Extracts MbedTlsStream_ClientKeyMatchesCertificate so the condition reads as
its intent, takes the divergence note off the Mbed TLS platform page, and
renumbers the MISRA suppressions the added lines moved.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: a0ada83c-b00a-4047-af27-d03529482ae2

📥 Commits

Reviewing files that changed from the base of the PR and between 21cb9ab and bb93d38.

📒 Files selected for processing (7)
  • Platform/MbedTls/Interface/SolidSyslogMbedTlsStreamErrors.h
  • Platform/MbedTls/Source/SolidSyslogMbedTlsStream.c
  • Tests/MbedTls/SolidSyslogMbedTlsStreamTest.cpp
  • Tests/Support/MbedTlsFake.c
  • Tests/Support/MbedTlsFake.h
  • docs/platforms/mbedtls/index.md
  • misra_suppressions.txt

Included review availability: 4 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.


Walkthrough

The Mbed TLS stream now validates that the configured client certificate matches its private key before installation. Tests mock and inspect mbedtls_pk_check_pair, and a new error code identifies mismatches.

Changes

Client credential validation

Layer / File(s) Summary
Credential validation and error contract
Platform/MbedTls/Interface/SolidSyslogMbedTlsStreamErrors.h, Platform/MbedTls/Source/SolidSyslogMbedTlsStream.c, docs/platforms/mbedtls/index.md, misra_suppressions.txt
The stream validates client certificate and private-key compatibility with the configured RNG. It emits a bad-configuration warning and skips credential installation when the pair does not match.
Credential validation test support
Tests/MbedTls/SolidSyslogMbedTlsStreamTest.cpp, Tests/Support/MbedTlsFake.c, Tests/Support/MbedTlsFake.h
The fake captures mbedtls_pk_check_pair arguments and return values. Tests verify validation, mismatch handling, continued connection setup, and skipped certificate installation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to bb93d

The PR adds local validation for mismatched client keys and certificates while preserving delivery behavior; no actionable merge-blocking risk remains after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant SolidSyslogMbedTlsStream
  participant mbedtls_pk_check_pair
  participant mbedtls_ssl_conf_own_cert
  SolidSyslogMbedTlsStream->>mbedtls_pk_check_pair: Check client certificate and private key with configured RNG
  alt Pair matches
    SolidSyslogMbedTlsStream->>mbedtls_ssl_conf_own_cert: Install client credentials
  else Pair does not match
    SolidSyslogMbedTlsStream->>SolidSyslogMbedTlsStream: Emit mismatch warning
  end
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.32% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 5 files. (2 skipped: 2 unsupported.) Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses Conventional Commits format and clearly summarises the main change: checking the Mbed TLS client key against its certificate.
Description check ✅ Passed The description includes all required sections and provides clear purpose, implementation details, test evidence, and affected areas.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mbedtls-key-certificate-pairing

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

☀️   Quality Summary

   JUnit   build-linux-gcc (Whole Project): ✅ successful — 1530 passed
   JUnit   build-freertos-host-tdd-plustcp (Whole Project): ✅ successful — 1888 passed
   JUnit   build-linux-clang (Whole Project): ✅ successful — 1461 passed
   JUnit   sanitize-linux-gcc (Whole Project): ✅ successful — 1461 passed
   JUnit   integration-linux-openssl (Whole Project): ✅ successful — 16 passed
   JUnit   integration-linux-mbedtls (Whole Project): ✅ successful — 14 passed
   JUnit   integration-windows-openssl (Whole Project): ✅ successful — 16 passed
   JUnit   bdd-linux-syslog-ng (Whole Project): ✅ successful — 49 passed, 3 skipped
   JUnit   bdd-windows-otel (Whole Project): ✅ successful — 46 passed, 6 skipped
   JUnit   bdd-freertos-qemu-plustcp (Whole Project): ✅ successful — 45 passed, 7 skipped
   JUnit   bdd-freertos-qemu-lwip (Whole Project): ✅ successful — 45 passed, 7 skipped
   JUnit   build-windows-msvc (Whole Project): ✅ successful — 1303 passed
   JUnit   build-linux-tunable-override (Whole Project): ✅ successful — 1461 passed
   ⚠️   Clang-Tidy (Whole Project): No warnings
   ⚠️   CPPCheck (Whole Project): No warnings


Created by Quality Monitor v4.15.0 (#82d77af). More details are shown in the GitHub Checks Result.

@DavidCozens
DavidCozens merged commit 05e8e68 into feature/tls-rework Aug 23, 2026
38 checks passed
@DavidCozens
DavidCozens deleted the fix/mbedtls-key-certificate-pairing branch August 23, 2026 07:02
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.

1 participant