Skip to content

Fix off-by-one in pem_find_pattern rejecting PEM without a trailing newline - #11259

Open
gasbytes wants to merge 1 commit into
wolfSSL:masterfrom
gasbytes:pem-parser-new-line
Open

Fix off-by-one in pem_find_pattern rejecting PEM without a trailing newline#11259
gasbytes wants to merge 1 commit into
wolfSSL:masterfrom
gasbytes:pem-parser-new-line

Conversation

@gasbytes

Copy link
Copy Markdown
Contributor

Description

Changed both scans loops in pem_find_pattern to <= so PEM ending at the footer's final dash parses.

Testing

Updated the general test test_wolfssl_pem_read to test this edge case too.

Checklist

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

Copilot AI lite review requested due to automatic review settings August 25, 2026 15:40
@gasbytes gasbytes self-assigned this Aug 25, 2026
@wolfSSL-Bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@gasbytes
gasbytes force-pushed the pem-parser-new-line branch from 380cdda to 48a29f1 Compare August 25, 2026 17:41
@gasbytes
gasbytes marked this pull request as ready for review August 25, 2026 17:41
@github-actions

Copy link
Copy Markdown

retest this please

@gasbytes

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please (logs lost)

@gasbytes
gasbytes force-pushed the pem-parser-new-line branch from 48a29f1 to 7e054bc Compare August 26, 2026 10:23
@gasbytes

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please (timeout)

1 similar comment
@gasbytes

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please (timeout)

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot 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.

Fenrir Automated Review — PR #11259

Scan targets checked: wolfssl-bugs, wolfssl-src

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread src/pk.c
}
/* Find postfix part. */
for (; idx < pemLen - postfixLen; idx++) {
for (; idx <= pemLen - postfixLen; idx++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Widened postfix scan makes PEM ending in a blank line fail to parse · Logic errors

The header search at src/pk.c:6905 uses postfix "\n\n" and is not bounded by the footer. With idx <= pemLen - postfixLen, a trailing blank line at the very end of the input now matches, so hdrLen swallows the body and footer, start advances to pemLen, the footer search finds nothing, and pem_read_data returns ASN_NO_PEM_HEADER. Unencrypted PEM files ending in "\n\n" that parsed before this PR now fail in PEM_read/PEM_read_bio.

Related known finding #11685 (similar but distinct): Both affect boundary handling in pem_find_pattern, but #11685 concerns excluding a valid footer match at the buffer end due to a strict bound. This candidate instead lets the header postfix scan match a blank line after the footer because its search is not footer-bounded. The operations, root causes, and required fixes differ.

Fix: Bound the encryption-header search so it cannot match past the footer — locate PEM_END first and pass that offset as the search limit.

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.

this feels like a false positive but not sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

seems legit imo, should be addressed now.

@philljj
philljj self-requested a review August 26, 2026 20:59
@philljj

philljj commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

skoll locally ran clean, no comments.

@philljj philljj 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.

Please review if Fenrir suggestion is worth incorporating. Feels marginal

Looks good otherwise.

@philljj philljj assigned gasbytes and unassigned wolfSSL-Bot Aug 26, 2026
@gasbytes
gasbytes force-pushed the pem-parser-new-line branch from 7e054bc to a71e104 Compare August 27, 2026 12:28
@gasbytes

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please

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