Fix ePassport reporting of failed reads, and decode EF_CardAccess - #3565
Open
pkilar wants to merge 1 commit into
Open
Fix ePassport reporting of failed reads, and decode EF_CardAccess#3565pkilar wants to merge 1 commit into
pkilar wants to merge 1 commit into
Conversation
…ccess Reading a Polish passport produced an empty dump directory and the app answered with "EF_DG1 is missing - no MRZ to render", which sent us after one file when every file was absent. Chasing that turned up several reasons a failed read did not explain itself, and one wrong label in the SECURITY tab that the same document exposed. Nothing was reported as a success. _read_finished treated any read the classifier had not flagged as one: it set status to "success", loaded the empty record and switched to the data page, leaving the DG1 line in the log as the only clue. It now treats an empty record as a failure, and the loader no longer names DG1 when nothing at all was read. A failed read left no evidence, so pm3.log is kept beside the dump. The key material is redacted out of it: result.command is the whole built command line and the client echoes it back in its own output, so the MRZ, the document number and the CAN all reach the log unless both are scrubbed. The log does not count as a file when judging whether a read produced anything - otherwise a failed read looks like it holds one and the empty-dump prune, which uses rmdir, could never clear it again. The classifier missed a lot. Fifteen of the client's failure messages matched no rule and surfaced as the generic error; thirteen now classify, each verified to end in a return false in the client. Two are deliberately left alone with a comment saying why: "Secure select rejected" is answered 6A82 for an absent optional DG during a good read, and "PACE is not available" precedes a normal BAC fallback. It also contradicted itself twice. A superseded PACE attempt was reported as the outcome, so a dump with every file in it announced itself as "PACE authentication failed" - the client falls back to BAC, and detect_mechanism already knew BAC had got in. And "Did you supply the correct MRZ info?" is printed whenever external authentication fails, whatever the cause, so a chip that had stopped answering was reported as a bad MRZ; a missing APDU response now outranks it and says the key was never tested. EF_CardAccess was listed in the file table but never parsed, and DG14's protocols were mislabelled: 0.4.0.127.0.7.2.2.1.2 was called "PACE (ECDH, generic mapping)" when that arc is the Chip Authentication public key - the client calls the same constant oid_pk_ecdh - and PACE lives under 0.4.0.127.0.7.2.2.4.x.y, as the comment above the client's own table says. emrtd/securityinfos.py parses SecurityInfos for EF_CardAccess, EF_CardSecurity and DG14, taking its names and domain parameters from the client's pace_table and pacesdp_table so the two agree. Only the members of the SET count: walking every nested SEQUENCE also collected the X9.62 identifiers inside the public key, which are not protocols the chip supports. It uses the TLV reader already in the tree rather than asn1crypto, an optional dependency the old OID scan silently needed. Behaviour change: a read that writes no file reports failure and opens the LOG tab instead of an empty data page; every dump gains a redacted pm3.log, ignored when judging emptiness and removed with the directory; failures that surfaced as one generic error now name themselves; DG14 lists four named protocols where it listed six raw OIDs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
pkilar
force-pushed
the
epassport-diagnose-failed-reads
branch
from
August 31, 2026 20:32
2899d8d to
92a98b1
Compare
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.
Problem
Reading a Polish passport produced an empty dump directory, and the app answered with
EF_DG1 is missing - no MRZ to render. Every file was absent; DG1 was just the first one checked, so the message sent us after the wrong thing. Digging into that turned up several separate reasons a failed read did not explain itself, and one wrong label in the SECURITY tab.A read that dumped nothing was reported as a success.
_read_finishedtreated any read the classifier had not flagged as one — it set the status tosuccess, loaded the empty record and switched to the data page, leaving the DG1 line in the log as the only clue.A failed read left no evidence. The client output was not kept anywhere, so once the run was over there was nothing to say why the chip gave up nothing.
The classifier missed most of what the client reports. Auditing every failure message in
cmdhfemrtd.candemrtd/*.cagainst the rules found 15 unmatched, all of which surfaced as one generic error.It also contradicted itself twice. The client tries PACE and falls back to BAC, and every line was matched on its own — so a dump with every file in it announced itself as
PACE authentication failed, even thoughdetect_mechanism()already knew BAC had got in. AndDid you supply the correct MRZ info?is printed whenever external authentication fails, whatever the cause, so a chip that had stopped answering (APDU: no APDU response) was reported as a bad MRZ. That one cost real time on a live document.EF_CardAccesswas never decoded, and DG14's protocols were mislabelled. The tab called0.4.0.127.0.7.2.2.1.2"PACE (ECDH, generic mapping)". That arc is the Chip Authentication public key — the client calls the same constantoid_pk_ecdhinemrtd_pace.c:1158— and PACE lives under...2.2.4.x.y, as the comment above the client's own table says atemrtd_pace.c:40....2.2.3.xwas likewise labelled Terminal Authentication when it is Chip Authentication. So a PACE passport had its CA key announced as PACE while its actual PACE protocol went unlabelled. The generatedtd3sample shows the same thing.Change
One commit:
dg.pyandmodel.pyeach carry both halves of this, so splitting it would have meant cutting hunks rather than files.The decoding half adds
emrtd/securityinfos.py, parsing SecurityInfos forEF_CardAccess,EF_CardSecurityand DG14. Protocol names and standardised domain parameters are transcribed from the client's ownpace_tableandpacesdp_table, so the two agree on what a chip advertises. A Polish passport now reads:Only the members of the SET count as SecurityInfos — walking every nested SEQUENCE also collected the X9.62 identifiers inside the public key, which are not protocols the chip supports. It parses with the TLV reader already in the tree rather than
asn1crypto, which is an optional dependency: the old OID scan returned nothing without it, so these files now decode on a bare install.The reporting half covers the rest. An empty record is treated as a failure; the loader no longer names DG1 when nothing at all was read;
pm3.logis kept beside the dump; 13 of the 15 unmatched messages now classify; the superseded-attempt and MRZ-ranking contradictions are fixed.Two messages are deliberately left unclassified, with a comment in
RULESsaying why:Secure select rejected by the documentis answered6A82for an absent optional DG during a perfectly good read, andCouldn't parse EF_CardAccess, PACE is not availableprecedes a normal BAC fallback. A rule on either would fail working dumps, and there is a test pinning that.The log needed two things beyond writing it. The key material is redacted:
result.commandis the whole built command line and the client echoes it back in its own output, so the MRZ, document number and CAN reach the log unless both are scrubbed. And it does not count as a file when deciding whether a read produced anything — otherwise a failed read would look like it held one file, and the empty-dump prune (which usesrmdir) would never clear it again. Pruning now removes the log with the directory.Behaviour change
pm3.log, with the MRZ, document number and CAN redacted. It is ignored when judging whether a dump is empty, and removed along with the directory when clearing failed reads.Testing
python3 -m pytest tests/— 312 passed, 2 skipped (was 302 passed, 2 skipped).return false, and the two excluded ones were verified to occur during successful reads.black --checkclean.Not tested here: Windows/ProxSpace and macOS. Low risk — Python only, no platform-specific calls; the one filesystem addition is a
write_textguarded byOSError. Not tested: hardware beyond the reads above, which were taken on an RDV4.