Repository navigation
fix: read certificates from PKCS#7 signature blocks and bundles, CSRs and TRUSTED CERTIFICATE - #49
Open
bitwize-music wants to merge 2 commits into
Open
bitwize-music wants to merge 2 commits into
bitwize-music wants to merge 2 commits into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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#7certs_in_pkcs7()replacescert_from_pkcs7(). It steps into aContentInfo(signedData only), still accepts a bareSignedData, and returns every certificate.2.
fix(keys): recover keys from PKCS#7/CMS, CSRs andTRUSTED CERTIFICATETRUSTED CERTIFICATE,X509 CERTIFICATE,CERTIFICATE REQUESTandNEW CERTIFICATE REQUESTgo toparse_cert().PKCS7,CMS,PKCS #7 SIGNED DATAand whole-file DER PKCS#7 go tocerts_in_pkcs7(), thenparse_cert()for each cert.certificate-request(a newKeyOrigin). 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 labeledcertificate.fp_dercovers only the certificate's own TLV, so the trust settings after aTRUSTED CERTIFICATEno longer break the leaked-key corpus match.META-INF/*.RSA, now feed--keys. A weak signing key is therefore reported on the signature block.Test
test_bootcovers 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_keyswraps a weak 1024-bit cert in each container and expects exactly onersa-weak-modulusper file, with the right label. The containers areTRUSTED/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-signedMETA-INF/CERT.RSA. It also checks that an eMudhra intermediate asTRUSTED CERTIFICATEis still named by the corpus. The crash sweep adds mutations of the DER bundle. On master 9 checks fail.ContentInfo(no certs returned).-DMT_SANITIZE=ON. 4k mutated PKCS#7/CSR/cert blobs ran through both passes under ASan with no report.--keysadds 18 findings and removes 0, all from the newly read containers.--bootoutput is unchanged.extract_public_keys. They merge cleanly in either order, and the merged tree passes ctest.🤖 Generated with Claude Code