Skip to content

Harden chain depth bounds and parser input validation#10209

Merged
douzzer merged 10 commits into
wolfSSL:masterfrom
ColtonWilley:harden-chain-depth-and-parser-bounds
Jul 3, 2026
Merged

Harden chain depth bounds and parser input validation#10209
douzzer merged 10 commits into
wolfSSL:masterfrom
ColtonWilley:harden-chain-depth-and-parser-bounds

Conversation

@ColtonWilley

@ColtonWilley ColtonWilley commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Harden certificate-chain depth handling and parser input validation.

  • Enforce MAX_CHAIN_DEPTH in certificate loading (ProcessUserChain), OCSP
    stapling (SendCertificateStatus), and TLS 1.3 certificate send
    (SendTls13Certificate). The load-time check runs after a cert is parsed,
    so a full-depth chain followed by trailing data still loads.
  • Add idx bounds checks to the peer-chain accessors in ssl.c.
  • SendCertificateStatus: bound the OCSP chain loops by MAX_CHAIN_DEPTH and
    error on an over-long chain rather than silently truncating the response.
  • Harden TLSX_SNI_GetFromBuffer length validation (require
    listLen == extLen - OPAQUE16_LEN, bound sniLen) against overreads on a
    malformed ClientHello.
  • Fix an off-by-one in TLSX_CSR_Free (<= to <).
  • Bounds-check wc_PKCS7_DecodeAuthEnvelopedData against truncated input
    (encryptedContentSz, the authTag tag read, authTagSz) under NO_PKCS7_STREAM.

Tests

  • Chain-depth boundary and chain-accessor index bounds in tests/api.c.
  • AuthEnvelopedData truncation cases in test_pkcs7.c.
  • Remove test_dtls13_oversized_cert_chain, superseded by the load-time cap.

@ColtonWilley
ColtonWilley marked this pull request as draft April 13, 2026 21:58
@ColtonWilley
ColtonWilley force-pushed the harden-chain-depth-and-parser-bounds branch from 9a2f68d to 3304644 Compare May 5, 2026 19:00
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

gcc-arm-cortex-m3

  • FLASH: .text +28 B (+0.0%, 121,281 B / 262,144 B, total: 46% used)

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 198,894 B / 262,144 B, total: 76% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +64 B (+0.0%, 766,828 B / 1,048,576 B, total: 73% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +64 B (+0.0%, 277,592 B / 1,048,576 B, total: 26% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +64 B (+0.0%, 323,000 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +64 B (+0.1%, 122,061 B / 262,144 B, total: 47% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +64 B (+0.0%, 234,528 B / 262,144 B, total: 89% used)

gcc-arm-cortex-m7

  • FLASH: .text +64 B (+0.0%, 198,894 B / 262,144 B, total: 76% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +64 B (+0.0%, 278,168 B / 1,048,576 B, total: 27% used)

gcc-arm-cortex-m7-tls13

@ColtonWilley
ColtonWilley force-pushed the harden-chain-depth-and-parser-bounds branch 2 times, most recently from 089287c to d84c824 Compare May 27, 2026 21:13
@ColtonWilley
ColtonWilley marked this pull request as ready for review May 27, 2026 21:22
@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown

retest this please

Comment thread tests/api/test_dtls.c Outdated
Enforce MAX_CHAIN_DEPTH limits in OCSP chain processing
(SendCertificateStatus, ProcessChainOCSPRequest), certificate loading
(ProcessUserChain), and TLS 1.3 certificate sending
(SendTls13Certificate). Add idx bounds checks to chain accessors
in ssl.c.

Harden SNI extension parser (TLSX_SNI_GetFromBuffer) with length
checks preventing buffer overreads on malformed ClientHello.

Fix off-by-one in TLSX_CSR_Free where <= should be < since
csr->requests is a count, not a max index.

Add remaining-buffer bounds checks to PKCS7 decoders:
DecodeEnvelopedData, DecodeAuthEnvelopedData (encryptedContentSz
and authTagSz), DecodeEncryptedData, SignedData null signature tag,
and PwriKek_KeyUnWrap cekLen validation.
test_dtls13_oversized_cert_chain (added upstream in 1653ecd) loaded a >9-cert chain to exercise SendTls13Certificate's word16 overflow guard. This PR now rejects over-MAX_CHAIN_DEPTH chains at load in ProcessUserChain, so the chain never reaches the send path; the load-time rejection is covered by test_wolfSSL_CTX_use_certificate_chain_buffer_max_depth. The word16 send guard remains as defense-in-depth (now only reachable via large PQC chains).
@ColtonWilley
ColtonWilley force-pushed the harden-chain-depth-and-parser-bounds branch from d84c824 to 359fb7e Compare May 28, 2026 00:24
@julek-wolfssl
julek-wolfssl requested a review from Copilot May 29, 2026 13:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens certificate-chain depth handling and improves parser input validation to prevent out-of-bounds access and buffer overreads, with accompanying API/regression tests.

Changes:

  • Enforces MAX_CHAIN_DEPTH in certificate chain loading/sending paths and adds index bounds checks to chain accessors.
  • Hardens SNI extension parsing with stricter length validation and fixes an off-by-one in OCSP CSR cleanup.
  • Adds PKCS7 AuthEnvelopedData truncation regression coverage and updates API tests; removes a DTLS oversized-chain test.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
wolfcrypt/src/pkcs7.c Adds remaining-buffer bounds checks during AuthEnvelopedData parsing.
src/tls.c Tightens SNI extension length validation; fixes OCSP CSR free-loop off-by-one.
src/ssl.c Adds idx bounds checks for peer-chain accessors (length/cert/X509).
src/ssl_load.c Enforces MAX_CHAIN_DEPTH during user chain parsing/loading.
src/internal.c Bounds OCSP chain processing loops by MAX_CHAIN_DEPTH.
src/tls13.c Rejects TLS 1.3 certificate sends when chain count exceeds MAX_CHAIN_DEPTH.
tests/api/test_pkcs7.h Declares/registers new PKCS7 truncated AuthEnvelopedData test.
tests/api/test_pkcs7.c Adds new truncation regression test for AuthEnvelopedData decode.
tests/api/test_dtls.h Removes declaration/registration of oversized-chain DTLS 1.3 test.
tests/api/test_dtls.c Removes implementation of oversized-chain DTLS 1.3 test.
tests/api.c Adds tests for chain accessor idx bounds and max-depth chain buffer rejection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wolfcrypt/src/pkcs7.c
Comment thread tests/api/test_pkcs7.c
Comment thread wolfcrypt/src/pkcs7.c
Comment thread tests/api/test_pkcs7.c

@julek-wolfssl julek-wolfssl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copilot has some valid points

@dgarske
dgarske removed their request for review June 1, 2026 17:07
@dgarske dgarske assigned ColtonWilley and unassigned ColtonWilley Jun 1, 2026
- pkcs7.c: wrap the AuthEnvelopedData encryptedContentSz bounds check
  entirely in #ifdef NO_PKCS7_STREAM so the default streaming build
  carries no empty/no-op branch (behavior unchanged).
- test_pkcs7.c: assert the encoded size is >32 so the encSz-32 and
  encSz-1 truncations can't underflow.
Master's test reorg (c674cec) moved test_dtls13_oversized_cert_chain into
the new tests/api/test_dtls13.c. This branch removes that test, so resolve by
taking master's test_dtls.{c,h} and re-applying the removal in test_dtls13.{c,h}.
@ColtonWilley

Copy link
Copy Markdown
Contributor Author

Copilot has some valid points

Agreed, I have pushed updates to address the issues.

@ColtonWilley

ColtonWilley commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please

@dgarske
dgarske removed the request for review from wolfSSL-Bot June 8, 2026 19:23

@dgarske dgarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Skoll Code Review

Scan type: reviewOverall recommendation: COMMENT
Findings: 4 total — 4 posted, 0 skipped
4 finding(s) posted as inline comments (see file-level comments below)

Posted findings

  • [Medium] MAX_CHAIN_DEPTH check placement can reject a valid maximum-depth chain that has trailing datasrc/ssl_load.c:328-333
  • [Low] Max-depth load test overshoots the limit and omits the exact-boundary success casetests/api.c:3866-3915
  • [Info] PR description lists pkcs7.c changes that are not present in the diffwolfcrypt/src/pkcs7.c:14971-14975,15239-15243
  • [Info] SendTls13Certificate depth guard is now defense-in-depth only and its direct test path was removedsrc/tls13.c:9523-9526

Review generated by Skoll

Comment thread src/ssl_load.c Outdated
Comment thread tests/api.c
Comment thread wolfcrypt/src/pkcs7.c
Comment thread src/tls13.c
- ProcessUserChain: enforce MAX_CHAIN_DEPTH after a cert is parsed, so a
  full-depth chain followed by trailing data still loads instead of failing
  with MAX_CHAIN_ERROR.
- SendCertificateStatus: error on an over-long OCSP chain instead of silently
  truncating the stapled response; keep the chainOcspRequest[] index bound.
- wc_PKCS7_DecodeAuthEnvelopedData: bounds-check the authTag tag read under
  NO_PKCS7_STREAM.
- Tests: chain-depth test now pins the boundary (exactly MAX_CHAIN_DEPTH loads,
  one more rejected).
…h-and-parser-bounds

# Conflicts:
#	src/ssl.c
@dgarske
dgarske self-requested a review July 2, 2026 15:32
@douzzer
douzzer merged commit d638d2a into wolfSSL:master Jul 3, 2026
481 checks passed
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.

7 participants