Skip to content

fix: read certificates from PKCS#7 signature blocks and bundles, CSRs and TRUSTED CERTIFICATE - #49

Open
bitwize-music wants to merge 2 commits into
nmatt0:masterfrom
bitwize-music:fix/cert-container-unwrap
Open

bitwize-music wants to merge 2 commits into
nmatt0:masterfrom
bitwize-music:fix/cert-container-unwrap

Conversation

@bitwize-music

Copy link
Copy Markdown

Fixes #48. There are two commits, one for each part of the issue. Commit 1 stands alone if you'd rather land it first.

1. fix(boot): read certificates from a ContentInfo-wrapped PKCS#7

  • certs_in_pkcs7() replaces cert_from_pkcs7(). It steps into a ContentInfo (signedData only), still accepts a bare SignedData, and returns every certificate.
  • The boot check now tests each certificate, not just the first.

2. fix(keys): recover keys from PKCS#7/CMS, CSRs and TRUSTED CERTIFICATE

  • TRUSTED CERTIFICATE, X509 CERTIFICATE, CERTIFICATE REQUEST and NEW CERTIFICATE REQUEST go to parse_cert().
  • PKCS7, CMS, PKCS #7 SIGNED DATA and whole-file DER PKCS#7 go to certs_in_pkcs7(), then parse_cert() for each cert.
  • A CSR is labeled certificate-request (a new KeyOrigin). It is told apart from a v1 certificate by the Name that follows the leading INTEGER; a v1 cert has an AlgorithmIdentifier there. This also relabels DER CSRs, which were previously labeled certificate.
  • fp_der covers only the certificate's own TLV, so the trust settings after a TRUSTED CERTIFICATE no longer break the leaked-key corpus match.
  • Behavior change to note: DER PKCS#7 files, including APK META-INF/*.RSA, now feed --keys. A weak signing key is therefore reported on the signature block.

Test

  • test_boot covers the ContentInfo form, a known cert second in a two-cert chain, and an unknown cert alone (not flagged). On master the first two checks fail.
  • test_keys wraps a weak 1024-bit cert in each container and expects exactly one rsa-weak-modulus per file, with the right label. The containers are TRUSTED / X509 CERTIFICATE, a v1 cert, a CSR (both labels and DER), PEM and DER PKCS#7 bundles with a healthy 2048-bit cert first, and a CMS-signed META-INF/CERT.RSA. It also checks that an eMudhra intermediate as TRUSTED CERTIFICATE is still named by the corpus. The crash sweep adds mutations of the DER bundle. On master 9 checks fail.
  • Unit tests cover the bare and wrapped forms, certificate order, and a non-signedData ContentInfo (no certs returned).
  • Disabling any single rule makes a test fail. That covers the ContentInfo unwrap, the bare form, the content-type check, reading all certs, each label group, the PEM and DER PKCS#7 paths, the CSR label, the v1 check and the fingerprint scope.
  • ctest passes 15/15 at each commit and under -DMT_SANITIZE=ON. 4k mutated PKCS#7/CSR/cert blobs ran through both passes under ASan with no report.
  • Corpus (about 1.4M files): --keys adds 18 findings and removes 0, all from the newly read containers. --boot output is unchanged.
  • Independent of fix(keys): stop reading DH parameters, signatures and other non-key DER as RSA #47: the two PRs touch different lines of extract_public_keys. They merge cleanly in either order, and the merged tree passes ctest.

🤖 Generated with Claude Code

bitwize-music and others added 2 commits November 20, 2026 16:57
…ure block

cert_from_pkcs7 looked for the [0] certificates field among the outer
SEQUENCE's children. That is right for a bare SignedData. But an APK/JAR
signature block (META-INF/*.RSA), an OTA signature or a .p7b bundle is
stored as ContentInfo { OID signedData, [0] EXPLICIT SignedData }. There
the first [0] is the EXPLICIT wrapper, so the whole SignedData came back
as the "certificate". Its fingerprint never matches the test-cert corpus,
so apk-test-signing-cert could not fire on a real signature block. The
boot test passed only because its fixture (AMI_P7) is a bare SignedData,
a form `openssl pkcs7 -inform DER` itself rejects.

Replace it with certs_in_pkcs7(). It steps into a ContentInfo when one
is present (signedData only), still accepts a bare SignedData, and
returns every certificate in the set. The boot check now tests each of
them, because a block carrying a chain need not list the signer first
(JAR blocks often put the CA first).

test_boot checks the AMI block in its ContentInfo form, and with the
known cert second in a two-cert chain. It also checks that an unknown
cert alone is not flagged. The unit tests check the bare and wrapped
forms, certificate order, and that a ContentInfo of another content
type yields nothing.

Part of nmatt0#48

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…TIFICATE blocks

extract_public_keys sent only the exact PEM label CERTIFICATE to
parse_cert. Everything below fell through to the public-key parser,
which cannot read it, so the key was never analyzed:
- a certificate under OpenSSL's TRUSTED CERTIFICATE label (a cert
  followed by trust settings, the format of RHEL/Fedora's
  ca-bundle.trust.crt) or its legacy X509 CERTIFICATE label;
- a PEM certificate request;
- every certificate in a PKCS7 / CMS block.
DER PKCS#7 (a .p7b bundle, an APK/JAR META-INF/*.RSA) was not unwrapped
either, although pubkey.hpp lists PKCS#7 as a certificate source.

Route those labels to parse_cert, and unwrap PKCS#7/CMS (PEM, or a
whole-file DER blob) with certs_in_pkcs7, taking every certificate in
the set. parse_cert already finds the SPKI in a CSR's
CertificationRequestInfo; a DER CSR worked, but was labeled
"certificate". It now labels a request "certificate-request", telling
it apart from a v1 certificate by the Name that follows the leading
INTEGER. The DER fingerprint now covers only the certificate's own TLV,
so trailing bytes (the trust settings) do not break the leaked-key
corpus match.

test_keys wraps a weak 1024-bit cert in each of these containers:
TRUSTED and X509 CERTIFICATE, a v1 certificate, a CSR (both labels and
DER), PEM and DER PKCS#7 bundles with a healthy 2048-bit cert first, and
a CMS-signed META-INF/CERT.RSA. It asserts exactly one rsa-weak-modulus
for each, with the right label. It also checks that an eMudhra
intermediate as a TRUSTED CERTIFICATE is still named by the corpus. The
crash sweep adds mutations of the DER bundle.

Closes nmatt0#48

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

apk-test-signing-cert misses real APK/OTA signature blocks; --keys skips PKCS#7, CSR and TRUSTED CERTIFICATE

1 participant