Skip to content

About

Android library that reads and verifies ePassport NFC chips (ICAO 9303): PACE/BAC, passive and chip authentication, CSCA certificate verification, MRZ scanning. Kotlin and on Maven Central.

Topics

Resources

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Android NFC Passport Reader

An Android library that reads the contactless chip in an ePassport and tells you what can be trusted about it, with a sample app that shows it working.

Result screen: the holder's details and the verification panel, on the ICAO specimen The same result screen in light mode Reading screen: where to place the phone on the passport

The sample app on the ICAO 9303 specimen document; no real passport data.

What it does

  • Reads ICAO 9303 passports (TD3) over NFC, authenticating with PACE or BAC from the three MRZ fields (document number, date of birth, expiry).
  • Reads the data groups the chip lists: the MRZ (DG1), the face (DG2, in JPEG or JPEG 2000, ISO/IEC 19794 or 39794), and where present the portrait, signature, personal and document details, and persons to notify (DG5, DG7, DG11, DG12, DG13, DG16).
  • Verifies what it read, check by check: the document signer's signature, the certificate chain to a country's CSCA, the hash of every data group it read, and whether the chip is genuine (Chip Authentication, PACE-CAM or Active Authentication). Each check is reported as passed, failed, not present or not checked, and each data group's hash says whether it was compared.
  • Copes with real hardware: ReaderMode handling, adaptive settle delays, PACE retries decided by status word, and chip-gated extended-length APDUs, all tunable through one timing config.
  • Ships CSCA trust anchors from the German BSI master list, with a script to refresh them.
  • Logs personal data only in debuggable builds: MRZ fields, names and document numbers go through a gate tied to the host app's debuggable flag. It also keeps every third-party type off its public API.

The sample app adds camera MRZ scanning (ML Kit), manual entry, a reading screen showing where to hold the phone, and a result screen with one panel per data group.

Modules

Module What it is
:passport-reader Android library. Chip protocols, data model, verification report, and the NFC reliability layer. No DI, no UI, no analytics.
:sample-app Small Compose app demonstrating the library. Camera MRZ scan or manual entry, read, result.

Using the library

The library is on Maven Central, for apps with minSdk 29 or higher:

dependencies {
    implementation("io.github.munkchunk:android-passport-reader:0.1.6")
}

android {
    // BouncyCastle's three jars each carry the same licence file.
    packaging {
        resources {
            pickFirsts += "META-INF/LICENSE.md"
        }
    }
}

The library carries its own R8 rules, so a minified app needs nothing more.

Two levels of API, depending on how much you want to own.

The whole flow, including ReaderMode and retries:

val manager = NfcPassportReaderManager(context)

// From your Activity's lifecycle
override fun onResume() { super.onResume(); manager.attachActivity(this) }
override fun onPause()  { super.onPause();  manager.detachActivity() }

// Observe progress
manager.readState.collect { state -> /* NfcReadState */ }

// Read. Dates are the raw 6-digit MRZ form, YYMMDD.
val passport = manager.readPassport(
    passportNumber = "L898902C<",
    dateOfBirth    = "740812",
    expiryDate     = "120415"
)

A single read, if you already have the Tag:

val reader = NfcPassportReader(context)
val result: Result<Passport> = reader.readPassport(tag, passportNumber, dateOfBirth, expiryDate)

Failures arrive as PassportReadException subtypes — WrongMrz, AuthenticationFailed, PaceFailed, TagLost, NfcIo, NoTagDetected, TrustOrPassiveAuthFailed, Unknown.

Each carries titleRes and messageRes — string resource ids — plus messageArgs and a plain-text technicalDetails for a bug report:

val text = context.getString(error.messageRes, *error.messageArgs.toTypedArray())

The wording lives in this library's res/values/strings.xml, so it can be read in one place and overridden by declaring the same name in your own strings.xml. An app that would rather write its own wording can ignore the resources entirely and switch on the exception type. technicalDetails stays a String on purpose: status words and PACE steps are diagnostics, not copy, and are easier to search for untranslated.

Images

The chip's images (face, portrait, signature, fingerprints, proof of citizenship and document scans) are decoded only when they are shown to be the issuer's: the bytes they come from are the bytes whose hash matches EF.SOd, and EF.SOd's signature and certificate chain hold. Any image left undecoded is listed in Passport.undecodedImages with its reason (UNVERIFIED, TOO_LARGE or DOES_NOT_DECODE), so an app can tell a chip with no photograph from one whose photograph is not shown.

An app that must show a chip's images whatever its verification, such as a diagnostic tool, can ask for unverified images too:

NfcPassportReaderManager(context, decodeUnverifiedImages = true)

The sample app does this. An app that acts on the result should not.

Timing

Real-world reads live or die on RF timing, so it is configurable:

NfcPassportReaderManager(context, timingConfig = NfcTimingPresets.CONSERVATIVE)

DEFAULT is tuned for PACE passports. CONSERVATIVE raises the settle delays for flaky hardware or stubborn documents. FAST is quicker but will likely fail on PACE. The manager also adapts at runtime, lengthening the settle delay after a lost tag and shortening it again after a success.

NfcReadTimingConfig covers the whole read, not just the part before the tag is detected: awaitTagTimeoutMs and the settle delays govern tag detection, while isoDepTimeoutMs, cardStabilizationMs and the pace* values govern what happens once the chip is in hand. Every delay in the library comes from here.

Setting awaitTagTimeoutMs to Long.MAX_VALUE removes the limit on waiting for a passport, and the sample app does this (see docs/sample-app.md). The wait then ends only when the read is cancelled, so cancel it when the Activity pauses and when NFC is switched off (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED): a wait left running holds the manager's read lock, and every later read queues behind it.

Trust anchors

Passive authentication checks the document signer against CSCA certificates bundled with the library, taken from the German BSI master list (see NOTICE.md). An app can add its own: put them in a PKCS12 key store named csca-certificates.p12, with an empty password, in the app's external files Download directory (context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)). Each certificate there is used as a trust anchor alongside the bundled ones, which is how a country missing from the master list can be added.

Error reporting

Optional, and an interface rather than a dependency:

NfcPassportReaderManager(context, errorReporter = object : NfcErrorReporter {
    override fun report(where: String, ex: PassportReadException) { /* ... */ }
})

Building

Needs JDK 17 and the Android SDK (API 36). Point Gradle at your SDK:

echo "sdk.dir=/path/to/android-sdk" > local.properties
./gradlew :passport-reader:assembleDebug
./gradlew :sample-app:assembleDebug
./gradlew :passport-reader:testDebugUnitTest :sample-app:testDebugUnitTest

CONTRIBUTING.md has the full set of checks.

Verifying it works

An emulator cannot do this. You need an NFC-capable Android phone and a real ePassport. Install the sample app from the APK on the Releases page, or build it with ./gradlew :sample-app:installDebug, then enter or scan the passport's details and hold it against the back of the phone. docs/testing.md has the log tags to watch, what a good read looks like, and what to check first when one fails.

Limitations

  • TD3 passports only. No TD1 or TD2 identity cards.
  • Six countries have no trust anchor in the bundled CSCA list — GH, ID, IR, KZ, NG, YE — so their passports will fail passive authentication. See NOTICE.md.
  • Most data groups have never been read from a real passport. DG4 (iris) is not read at all, nor is DG3 (fingerprints): both need Terminal Authentication, which the public API cannot be given credentials for yet. PACE-CAM is verified only against ICAO's worked example. See docs/data-groups.md for every file the chip can hold and what the library does with it.
  • The chip read path has no automated tests. Verifying a change means a phone and a real passport.

The full list is in docs/architecture.md.

Documentation

Contributing

Reports of what your passport did, especially from countries the library has not seen, are as valuable as code. See CONTRIBUTING.md. To report a vulnerability, see SECURITY.md.

Licence

Apache License 2.0 — see LICENSE. Free to use, modify and redistribute, commercially or otherwise, by individuals and organisations alike.

One thing to be aware of if you ship an app built on this library. The library links against JMRTD, SCUBA and cert-cvc, which are LGPL. Nothing of theirs is modified, vendored or re-exported here — they are implementation dependencies and no third-party type appears on this library's public API — so this project's own Apache-2.0 terms are unaffected. But the LGPL obligations travel with the combined work you distribute: preserve their notices, make their licences available, and allow your recipients to relink against a modified JMRTD. NOTICE.md records every dependency and its licence.

About

Android library that reads and verifies ePassport NFC chips (ICAO 9303): PACE/BAC, passive and chip authentication, CSCA certificate verification, MRZ scanning. Kotlin and on Maven Central.

Topics

Resources

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages