Skip to content

Fix GetSafeContent to check length#10018

Merged
dgarske merged 2 commits intowolfSSL:masterfrom
embhorn:zd21389
Mar 20, 2026
Merged

Fix GetSafeContent to check length#10018
dgarske merged 2 commits intowolfSSL:masterfrom
embhorn:zd21389

Conversation

@embhorn
Copy link
Copy Markdown
Member

@embhorn embhorn commented Mar 19, 2026

Description

Added bounds check after GetObjectId() in GetSafeContent() to verify the OID didn't consume more bytes than the ContentInfo SEQUENCE declared. Returns ASN_PARSE_E if (localIdx - curIdx) > (word32)curSz, preventing the unsigned integer underflow in the ci->dataSz calculation.

Credit to Muhammad Arya Arjuna (pelioro) for reporting this issue and confirming the fix.

Fixes zd21389

Testing

Added test_wc_d2i_PKCS12_oid_underflow

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@embhorn embhorn self-assigned this Mar 19, 2026
Copilot AI review requested due to automatic review settings March 19, 2026 20:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a defensive bounds check in PKCS#12 parsing to prevent an unsigned underflow when an encoded OID exceeds the declared ContentInfo SEQUENCE length, and introduces a regression test for the crafted input case.

Changes:

  • Add a post-GetObjectId() bounds check in GetSafeContent() to reject OIDs longer than the declared SEQUENCE length.
  • Add a new API test exercising the OID-length vs SEQUENCE-length underflow scenario.
  • Register the new test in the PKCS#12 API test declarations.

Reviewed changes

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

File Description
wolfcrypt/src/pkcs12.c Adds bounds validation to prevent size underflow during ContentInfo parsing.
tests/api/test_pkcs12.h Registers the new regression test in the PKCS#12 test list.
tests/api/test_pkcs12.c Adds a crafted DER test case to ensure the parser rejects the underflow scenario.

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

Comment thread tests/api/test_pkcs12.c
Comment thread tests/api/test_pkcs12.c
Comment thread wolfcrypt/src/pkcs12.c
@embhorn embhorn assigned wolfSSL-Bot and unassigned embhorn Mar 20, 2026
@embhorn
Copy link
Copy Markdown
Member Author

embhorn commented Mar 20, 2026

Fix confirmed by reporter

@dgarske dgarske merged commit 82b6b9c into wolfSSL:master Mar 20, 2026
487 checks passed
TristanInSec pushed a commit to TristanInSec/wolfssl that referenced this pull request Apr 15, 2026
Bound GetObjectId() by the ContentInfo SEQUENCE end
(curIdx + curSz) instead of the full buffer size. This
prevents the OID TLV from being parsed past the SEQUENCE
boundary in the first place, complementing the post-check
added in PR wolfSSL#10018.

Previously, GetObjectId received (word32)size as maxIdx,
allowing it to read OID data beyond the ContentInfo SEQUENCE.
The post-check then caught this after the fact. With this
change, GetObjectId itself rejects an OID that would exceed
the SEQUENCE, so the over-read never occurs.
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.

4 participants