Skip to content

refactor(tpm2_getekcertificate): harden Intel EK cert parsing#3574

Merged
JuergenReppSIT merged 1 commit into
tpm2-software:masterfrom
hyperfinitism:fix/intel-ek-parser
Apr 22, 2026
Merged

refactor(tpm2_getekcertificate): harden Intel EK cert parsing#3574
JuergenReppSIT merged 1 commit into
tpm2-software:masterfrom
hyperfinitism:fix/intel-ek-parser

Conversation

@hyperfinitism
Copy link
Copy Markdown
Contributor

@hyperfinitism hyperfinitism commented Apr 15, 2026

This PR replaces fragile ad-hoc string parsing of Intel EK certificate responses with a small reusable helper, get_json_field().

Previously, the code relied on prefix matching (e.g. {"pubhash) and fixed-offset string manipulation to extract fields from JSON responses. This is brittle and can break with minor variations such as whitespace changes or different field ordering.

This change introduces a minimal JSON field lookup helper and uses it to:

  • Detect Intel responses by checking for the presence of the "pubhash" field, instead of relying on a raw string prefix.
  • Extract the "certificate" field explicitly, and then perform Base64URL-to-Base64 conversion via convert_base64url_to_base64().
  • (Remove trailing spaces)

This makes the parsing more robust while keeping the implementation lightweight, and also makes the code semantically clear.

In addition to improving robustness, the new code avoids the previous curl_easy_unescape() + strdup() sequence when extracting the certificate field. That reduces unnecessary heap allocation/copying in this path (2 alloc + 2 copy → 1 alloc + 1 copy).

No new external dependencies (e.g. json-c) are introduced.

Related issue/PR

Once PR #3573 is merged, issue #3559 will be resolved. This PR makes the parsing logic more robust and refactors the code to ensure maintainability.

Comment thread tools/tpm2_getekcertificate.c Fixed
Extract a reusable get_json_field() helper to look up named fields in
JSON strings, and use it to replace fragile ad-hoc parsing:

- Check for "pubhash" field presence instead of matching a raw prefix
  string ("{\"pubhash"), making detection resilient to whitespace and
  field ordering variations.
- Replace base64_decode() with convert_base64url_to_base64(): extract
  the "certificate" field via get_json_field(), then perform Base64URL
  to Base64 conversion separately.

No new external dependencies (e.g. json-c) are introduced.

Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
@JuergenReppSIT
Copy link
Copy Markdown
Member

Hi @hyperfinitism That looks good. If you remove the draft marking, I would close my PR and merge yours.

@hyperfinitism hyperfinitism marked this pull request as ready for review April 22, 2026 14:18
@JuergenReppSIT JuergenReppSIT merged commit afff1f0 into tpm2-software:master Apr 22, 2026
23 checks passed
@hyperfinitism hyperfinitism deleted the fix/intel-ek-parser branch April 22, 2026 14:46
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.

3 participants