@trustchex/react-native-sdk 1.486.0 → 1.488.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/module/Shared/Components/EIDScanner.js +97 -37
- package/lib/module/Shared/Components/IdentityDocumentCamera.constants.js +9 -1
- package/lib/module/Shared/EIDReader/cardFileInputStream.js +25 -2
- package/lib/module/Shared/EIDReader/defaultFileSystem.js +56 -6
- package/lib/module/Shared/EIDReader/eidReader.js +124 -14
- package/lib/module/Shared/EIDReader/lds/icao/dg11File.js +23 -6
- package/lib/module/Shared/EIDReader/nfcManagerCardService.js +79 -9
- package/lib/module/Shared/EIDReader/protocol/paceDH.js +101 -0
- package/lib/module/Shared/EIDReader/protocol/paceErrors.js +34 -0
- package/lib/module/Shared/EIDReader/protocol/paceProtocol.js +53 -4
- package/lib/module/Shared/EIDReader/protocol/readBinaryAPDUSender.js +7 -3
- package/lib/module/Shared/Libs/country-display.utils.js +13 -1
- package/lib/module/Shared/Libs/diagnosticReport.js +24 -1
- package/lib/module/Shared/Libs/diagnostics.js +30 -8
- package/lib/module/Shared/Libs/mrz.utils.js +172 -25
- package/lib/module/Shared/Libs/mrzFrameAggregator.js +29 -1
- package/lib/module/Shared/Libs/mrzTransliteration.js +14 -1
- package/lib/module/Translation/Resources/en.js +12 -0
- package/lib/module/Translation/Resources/tr.js +12 -0
- package/lib/module/version.js +1 -1
- package/lib/typescript/src/Shared/Components/EIDScanner.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Components/IdentityDocumentCamera.constants.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/cardFileInputStream.d.ts +3 -0
- package/lib/typescript/src/Shared/EIDReader/cardFileInputStream.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/defaultFileSystem.d.ts +4 -0
- package/lib/typescript/src/Shared/EIDReader/defaultFileSystem.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/eidReader.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/lds/icao/dg11File.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/nfcManagerCardService.d.ts +9 -1
- package/lib/typescript/src/Shared/EIDReader/nfcManagerCardService.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/protocol/paceDH.d.ts +56 -0
- package/lib/typescript/src/Shared/EIDReader/protocol/paceDH.d.ts.map +1 -0
- package/lib/typescript/src/Shared/EIDReader/protocol/paceErrors.d.ts +26 -0
- package/lib/typescript/src/Shared/EIDReader/protocol/paceErrors.d.ts.map +1 -0
- package/lib/typescript/src/Shared/EIDReader/protocol/paceProtocol.d.ts.map +1 -1
- package/lib/typescript/src/Shared/EIDReader/protocol/readBinaryAPDUSender.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/country-display.utils.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/diagnosticReport.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/diagnostics.d.ts +9 -1
- package/lib/typescript/src/Shared/Libs/diagnostics.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/mrz.utils.d.ts +1 -0
- package/lib/typescript/src/Shared/Libs/mrz.utils.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/mrzFrameAggregator.d.ts.map +1 -1
- package/lib/typescript/src/Shared/Libs/mrzTransliteration.d.ts.map +1 -1
- package/lib/typescript/src/Translation/Resources/en.d.ts +12 -0
- package/lib/typescript/src/Translation/Resources/en.d.ts.map +1 -1
- package/lib/typescript/src/Translation/Resources/tr.d.ts +12 -0
- package/lib/typescript/src/Translation/Resources/tr.d.ts.map +1 -1
- package/lib/typescript/src/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/Shared/Components/EIDScanner.tsx +149 -68
- package/src/Shared/Components/IdentityDocumentCamera.constants.ts +9 -1
- package/src/Shared/EIDReader/cardFileInputStream.ts +30 -6
- package/src/Shared/EIDReader/defaultFileSystem.ts +75 -17
- package/src/Shared/EIDReader/eidReader.ts +154 -21
- package/src/Shared/EIDReader/lds/icao/dg11File.ts +28 -13
- package/src/Shared/EIDReader/nfcManagerCardService.ts +91 -13
- package/src/Shared/EIDReader/protocol/paceDH.ts +183 -0
- package/src/Shared/EIDReader/protocol/paceErrors.ts +41 -0
- package/src/Shared/EIDReader/protocol/paceProtocol.ts +72 -6
- package/src/Shared/EIDReader/protocol/readBinaryAPDUSender.ts +7 -4
- package/src/Shared/Libs/country-display.utils.ts +12 -1
- package/src/Shared/Libs/diagnosticReport.ts +32 -1
- package/src/Shared/Libs/diagnostics.ts +38 -14
- package/src/Shared/Libs/mrz.utils.ts +193 -28
- package/src/Shared/Libs/mrzFrameAggregator.ts +38 -3
- package/src/Shared/Libs/mrzTransliteration.ts +16 -3
- package/src/Translation/Resources/en.ts +19 -0
- package/src/Translation/Resources/tr.ts +19 -0
- package/src/version.ts +1 -1
|
@@ -2,6 +2,7 @@ import { BACKey } from './bacKey';
|
|
|
2
2
|
import { PACEKeySpec } from './paceKeySpec';
|
|
3
3
|
import { NFCManagerCardService } from './nfcManagerCardService';
|
|
4
4
|
import { EIDService } from './eidService';
|
|
5
|
+
import { isPACEUnsupportedError } from './protocol/paceErrors';
|
|
5
6
|
import { DG1File } from './lds/icao/dg1File';
|
|
6
7
|
import { DG2File } from './lds/icao/dg2File';
|
|
7
8
|
import { DG11File } from './lds/icao/dg11File';
|
|
@@ -38,6 +39,54 @@ const categorizeNfcError = (msg: string): string => {
|
|
|
38
39
|
return 'reading_error';
|
|
39
40
|
};
|
|
40
41
|
|
|
42
|
+
/** Short, stable code per category for the user-facing reportable error code. */
|
|
43
|
+
const NFC_CATEGORY_CODE: Record<string, string> = {
|
|
44
|
+
user_cancelled: 'CANCEL',
|
|
45
|
+
tag_lost: 'TAGLOST',
|
|
46
|
+
timeout: 'TIMEOUT',
|
|
47
|
+
unsupported: 'UNSUPPORTED',
|
|
48
|
+
not_enabled: 'DISABLED',
|
|
49
|
+
auth_failed: 'AUTH',
|
|
50
|
+
reading_error: 'READ',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** Short, stable token per read step, so the error code names WHERE it failed.
|
|
54
|
+
* A generic NFC-READ is not actionable; NFC-READ-DG2-6982 tells support the
|
|
55
|
+
* read failed reading the face image (DG2) with APDU status 6982. */
|
|
56
|
+
const NFC_STEP_CODE: Record<string, string> = {
|
|
57
|
+
open: 'OPEN',
|
|
58
|
+
selectApplet: 'SELAPP',
|
|
59
|
+
selectMF: 'SELMF',
|
|
60
|
+
readCardAccess: 'CARDACCESS',
|
|
61
|
+
paceAuth: 'PACE',
|
|
62
|
+
bacAuth: 'BAC',
|
|
63
|
+
readDG1: 'DG1',
|
|
64
|
+
readDG2: 'DG2',
|
|
65
|
+
readDG11: 'DG11',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Build a stable, reportable error code from a categorised NFC failure that
|
|
70
|
+
* names the failing STEP, e.g. `NFC-READ-DG2-6982` (reading the face image
|
|
71
|
+
* failed, APDU status 6982) or `NFC-AUTH-PACE-6300` (PACE auth rejected). Falls
|
|
72
|
+
* back to `NFC-<category>` when neither step nor status word is known.
|
|
73
|
+
* Non-PII by construction: only the bucket + step name + the chip's 4-hex
|
|
74
|
+
* status word. A user can quote it to pinpoint exactly where the read failed.
|
|
75
|
+
*/
|
|
76
|
+
const buildNfcErrorCode = (
|
|
77
|
+
category: string,
|
|
78
|
+
msg: string,
|
|
79
|
+
step?: string,
|
|
80
|
+
stepSw?: string
|
|
81
|
+
): string => {
|
|
82
|
+
const part = NFC_CATEGORY_CODE[category] ?? 'READ';
|
|
83
|
+
const stepTok = step ? NFC_STEP_CODE[step] : undefined;
|
|
84
|
+
// Prefer the status word recorded against the failing step; fall back to one
|
|
85
|
+
// parsed from the error message.
|
|
86
|
+
const sw = stepSw ?? extractStatusWord(msg);
|
|
87
|
+
return [`NFC-${part}`, stepTok, sw].filter(Boolean).join('-');
|
|
88
|
+
};
|
|
89
|
+
|
|
41
90
|
// --- Minimal PNG encoder (pure JS, no native deps) ---
|
|
42
91
|
// Uses deflate stored (uncompressed) blocks for O(n) encoding speed.
|
|
43
92
|
// A naive DCT-based JPEG encoder would be O(n²) and freeze the JS thread
|
|
@@ -182,6 +231,14 @@ function pixelsToPng(
|
|
|
182
231
|
* If the image is JPEG 2000, decode and convert to PNG so React Native can display it.
|
|
183
232
|
* Returns { base64, mimeType } with the converted image, or the original if not JP2.
|
|
184
233
|
*/
|
|
234
|
+
// Largest JP2 face image we will attempt to decode in pure JS on the UI thread.
|
|
235
|
+
// The `jpeg2000` decoder's parse() is SYNCHRONOUS and roughly quadratic, so a
|
|
236
|
+
// large image (some passports store 20–30 KB+ JPEG2000 portraits) blocks the JS
|
|
237
|
+
// thread for many seconds or indefinitely — the German-passport "stuck at face
|
|
238
|
+
// conversion" hang. A JS timeout cannot interrupt a synchronous call, so we must
|
|
239
|
+
// gate BEFORE decoding. ~12 KB decodes fast; beyond that we keep the raw JP2.
|
|
240
|
+
const MAX_JP2_DECODE_BYTES = 12 * 1024;
|
|
241
|
+
|
|
185
242
|
function convertJP2IfNeeded(
|
|
186
243
|
imageBuffer: Buffer,
|
|
187
244
|
mimeType: string
|
|
@@ -190,6 +247,18 @@ function convertJP2IfNeeded(
|
|
|
190
247
|
return { base64: imageBuffer.toString('base64'), mimeType };
|
|
191
248
|
}
|
|
192
249
|
|
|
250
|
+
// Too large to decode synchronously without freezing the UI — keep the raw
|
|
251
|
+
// JP2. The face image is still captured (the verification backend / face-match
|
|
252
|
+
// accept JPEG2000); only the on-screen preview can't render it. This lets the
|
|
253
|
+
// NFC read COMPLETE instead of hanging.
|
|
254
|
+
if (imageBuffer.length > MAX_JP2_DECODE_BYTES) {
|
|
255
|
+
debugLog(
|
|
256
|
+
'EID',
|
|
257
|
+
`[EID] JP2 face image is ${imageBuffer.length} bytes (> ${MAX_JP2_DECODE_BYTES}); skipping in-JS decode to avoid blocking, keeping raw JP2`
|
|
258
|
+
);
|
|
259
|
+
return { base64: imageBuffer.toString('base64'), mimeType };
|
|
260
|
+
}
|
|
261
|
+
|
|
193
262
|
debugLog('EID', '[EID] Face image is JP2, attempting conversion…');
|
|
194
263
|
try {
|
|
195
264
|
// Lazy-load jpeg2000 to avoid crashing if it fails to import
|
|
@@ -349,7 +418,12 @@ const eidReader = async (
|
|
|
349
418
|
const stepTimer = () => {
|
|
350
419
|
const t0 = Date.now();
|
|
351
420
|
return (step: string, ok: boolean, error?: unknown) => {
|
|
352
|
-
const msg =
|
|
421
|
+
const msg =
|
|
422
|
+
error instanceof Error
|
|
423
|
+
? error.message
|
|
424
|
+
: error
|
|
425
|
+
? String(error)
|
|
426
|
+
: undefined;
|
|
353
427
|
// Store only the APDU status word and a COARSE category — never the raw
|
|
354
428
|
// error text, which could in theory embed field/PII values. The SW + step
|
|
355
429
|
// name + category carry all the actionable signal.
|
|
@@ -472,12 +546,38 @@ const eidReader = async (
|
|
|
472
546
|
|
|
473
547
|
const done = stepTimer();
|
|
474
548
|
try {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
)
|
|
549
|
+
// PACE runs many APDU round-trips; on iOS the CoreNFC tag connection
|
|
550
|
+
// can drop mid-handshake (empty/lost-tag error). A restart can't
|
|
551
|
+
// resume PACE (the on-chip state is gone), so on a CONNECTION-DROP
|
|
552
|
+
// (not a crypto/capability failure) restart the session and retry
|
|
553
|
+
// the WHOLE PACE handshake from scratch a few times. A real failure
|
|
554
|
+
// (wrong key, unsupported variant) is rethrown immediately.
|
|
555
|
+
const MAX_PACE_ATTEMPTS = 3;
|
|
556
|
+
for (let paceAttempt = 1; ; paceAttempt++) {
|
|
557
|
+
try {
|
|
558
|
+
await passportService.doPACE(
|
|
559
|
+
paceKey,
|
|
560
|
+
paceInfo.oid,
|
|
561
|
+
paceInfo.parameterId,
|
|
562
|
+
progressCallback
|
|
563
|
+
);
|
|
564
|
+
break;
|
|
565
|
+
} catch (attemptErr) {
|
|
566
|
+
const lost = nfcManagerCardService.isTagLost(attemptErr);
|
|
567
|
+
if (!lost || paceAttempt >= MAX_PACE_ATTEMPTS) {
|
|
568
|
+
throw attemptErr;
|
|
569
|
+
}
|
|
570
|
+
debugLog(
|
|
571
|
+
'EID',
|
|
572
|
+
`[EID] PACE attempt ${paceAttempt} lost the tag; restarting session and retrying`
|
|
573
|
+
);
|
|
574
|
+
await nfcManagerCardService.restartSession();
|
|
575
|
+
await new Promise((r) => setTimeout(r, 200 * paceAttempt));
|
|
576
|
+
// Re-select MF + re-read EF.CardAccess so the next PACE starts
|
|
577
|
+
// from the same clean state as the first attempt.
|
|
578
|
+
await passportService.sendSelectMF();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
481
581
|
hasPACESucceeded = true;
|
|
482
582
|
done('paceAuth', true);
|
|
483
583
|
debugLog('EID', '[EID] PACE succeeded');
|
|
@@ -493,13 +593,19 @@ const eidReader = async (
|
|
|
493
593
|
);
|
|
494
594
|
}
|
|
495
595
|
} catch (paceError) {
|
|
496
|
-
// PACE
|
|
497
|
-
|
|
596
|
+
// PACE could not complete — fall back to BAC. Distinguish a CAPABILITY
|
|
597
|
+
// gap (this build can't run the variant the chip advertises) from a
|
|
598
|
+
// RUNTIME failure (wrong key, tag lost, APDU error) so diagnostics show
|
|
599
|
+
// exactly why, e.g. pace_unsupported_curve vs pace_failed.
|
|
600
|
+
const reason = isPACEUnsupportedError(paceError)
|
|
601
|
+
? `pace_unsupported_${paceError.reason}`
|
|
602
|
+
: 'pace_failed';
|
|
603
|
+
diagnostics.nfcPaceFellBackToBac(reason);
|
|
498
604
|
const msg =
|
|
499
605
|
paceError instanceof Error ? paceError.message : String(paceError);
|
|
500
606
|
debugLog(
|
|
501
607
|
'EID',
|
|
502
|
-
`[EID] PACE
|
|
608
|
+
`[EID] PACE could not complete (${reason}: ${msg}), falling back to BAC`
|
|
503
609
|
);
|
|
504
610
|
if (paceError instanceof Error && paceError.stack) {
|
|
505
611
|
debugLog('EID', '[EID] PACE error stack:', paceError.stack);
|
|
@@ -555,6 +661,13 @@ const eidReader = async (
|
|
|
555
661
|
progressCallback(progress);
|
|
556
662
|
}
|
|
557
663
|
|
|
664
|
+
// Authentication is complete — enable iOS CoreNFC session-restart recovery
|
|
665
|
+
// for the data-group reads. It must stay OFF during PACE/BAC (the auth state
|
|
666
|
+
// lives on the chip; a mid-handshake restart destroys it). Reads are
|
|
667
|
+
// idempotent and their secure-messaging state lives in our JS wrapper, so a
|
|
668
|
+
// session restart between blocks is safe here.
|
|
669
|
+
nfcManagerCardService.setSessionRestartEnabled(true);
|
|
670
|
+
|
|
558
671
|
// Read DG1 (MRZ Info)
|
|
559
672
|
const dg1Done = stepTimer();
|
|
560
673
|
let mrzInfo: MRZInfo;
|
|
@@ -592,21 +705,23 @@ const eidReader = async (
|
|
|
592
705
|
|
|
593
706
|
// Read DG2 (Face Image)
|
|
594
707
|
const dg2Done = stepTimer();
|
|
595
|
-
const dg2InputStream = passportService.getInputStream(EIDService.EF_DG2);
|
|
596
|
-
await dg2InputStream.init();
|
|
597
|
-
InputStream.onRead((totalBytesRead, fileLength) => {
|
|
598
|
-
progress = 30 + (totalBytesRead / fileLength) * 70;
|
|
599
|
-
if (progressCallback) {
|
|
600
|
-
progressCallback(progress);
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
const dg2File = new DG2File([], dg2InputStream);
|
|
604
708
|
let imageAsBase64 = '';
|
|
605
709
|
let mimeType = '';
|
|
606
|
-
|
|
607
710
|
const allFaceImageInfos: FaceImageInfo[] = [];
|
|
608
711
|
let faceInfos;
|
|
609
712
|
try {
|
|
713
|
+
// init() selects EF.DG2 and reads its header over secure messaging; keep
|
|
714
|
+
// it INSIDE the try so a failure here is still recorded as the readDG2
|
|
715
|
+
// step (it was previously outside, so an early DG2 failure went unlabelled).
|
|
716
|
+
const dg2InputStream = passportService.getInputStream(EIDService.EF_DG2);
|
|
717
|
+
await dg2InputStream.init();
|
|
718
|
+
InputStream.onRead((totalBytesRead, fileLength) => {
|
|
719
|
+
progress = 30 + (totalBytesRead / fileLength) * 70;
|
|
720
|
+
if (progressCallback) {
|
|
721
|
+
progressCallback(progress);
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
const dg2File = new DG2File([], dg2InputStream);
|
|
610
725
|
faceInfos = await dg2File.getFaceInfos();
|
|
611
726
|
dg2Done('readDG2', true);
|
|
612
727
|
} catch (e) {
|
|
@@ -644,8 +759,26 @@ const eidReader = async (
|
|
|
644
759
|
};
|
|
645
760
|
} catch (error) {
|
|
646
761
|
const msg = error instanceof Error ? error.message : String(error);
|
|
647
|
-
|
|
762
|
+
const category = categorizeNfcError(msg);
|
|
763
|
+
diagnostics.nfcResult(false, category);
|
|
648
764
|
debugLog('EID', 'Error reading passport data', error);
|
|
765
|
+
// Rethrow so the caller can surface an accurate, actionable message. Swallowing
|
|
766
|
+
// here returned `undefined`, which the UI mis-reported as "invalid MRZ fields"
|
|
767
|
+
// (e.g. a German passport whose BAC/PACE auth or chip read actually failed).
|
|
768
|
+
// The category is attached for the UI to map to a specific message.
|
|
769
|
+
const rethrown = error instanceof Error ? error : new Error(msg);
|
|
770
|
+
const tagged = rethrown as Error & {
|
|
771
|
+
nfcCategory?: string;
|
|
772
|
+
nfcCode?: string;
|
|
773
|
+
};
|
|
774
|
+
tagged.nfcCategory = category;
|
|
775
|
+
tagged.nfcCode = buildNfcErrorCode(
|
|
776
|
+
category,
|
|
777
|
+
msg,
|
|
778
|
+
diagnostics.getFailureStep(),
|
|
779
|
+
diagnostics.getFailureStatusWord()
|
|
780
|
+
);
|
|
781
|
+
throw tagged;
|
|
649
782
|
} finally {
|
|
650
783
|
await passportService.close();
|
|
651
784
|
}
|
|
@@ -75,20 +75,35 @@ export class DG11File extends DataGroup {
|
|
|
75
75
|
* Strips trailing filler `<` within each segment.
|
|
76
76
|
*/
|
|
77
77
|
private parseName(nameString: string): void {
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
const fillerToSpace = (s: string): string =>
|
|
79
|
+
s
|
|
80
|
+
.replace(/<+$/, '') // drop trailing filler
|
|
81
|
+
.replace(/</g, ' ') // internal filler = word separator
|
|
82
|
+
.replace(/\s+/g, ' ')
|
|
83
|
+
.trim();
|
|
84
|
+
|
|
85
|
+
const dblIndex = nameString.indexOf('<<');
|
|
86
|
+
if (dblIndex >= 0) {
|
|
87
|
+
// Standard ICAO layout: PRIMARY<<SECONDARY.
|
|
88
|
+
this.lastName = fillerToSpace(nameString.substring(0, dblIndex));
|
|
89
|
+
this.firstName = fillerToSpace(nameString.substring(dblIndex + 2));
|
|
82
90
|
return;
|
|
83
91
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
|
|
93
|
+
// No `<<` separator. Some chips (e.g. certain Turkish PACE passports) store
|
|
94
|
+
// DG11 0x5F0E as SURNAME<GIVEN1<GIVEN2 with single fillers. Split on the
|
|
95
|
+
// FIRST single filler: surname = first token, given names = the rest. With
|
|
96
|
+
// no filler at all the field is a single token we cannot reliably split into
|
|
97
|
+
// surname/given, so leave firstName empty and let the caller decide (the MRZ
|
|
98
|
+
// split is then more trustworthy than a guess).
|
|
99
|
+
const singleIndex = nameString.replace(/<+$/, '').indexOf('<');
|
|
100
|
+
if (singleIndex >= 0) {
|
|
101
|
+
this.lastName = fillerToSpace(nameString.substring(0, singleIndex));
|
|
102
|
+
this.firstName = fillerToSpace(nameString.substring(singleIndex + 1));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.lastName = fillerToSpace(nameString);
|
|
107
|
+
this.firstName = '';
|
|
93
108
|
}
|
|
94
109
|
}
|
|
@@ -4,6 +4,36 @@ import { CommandAPDU } from './smartcards/commandAPDU';
|
|
|
4
4
|
import { ResponseAPDU } from './smartcards/responseAPDU';
|
|
5
5
|
import { Platform } from 'react-native';
|
|
6
6
|
|
|
7
|
+
// iOS CoreNFC "Tag connection lost" (NFCError 100) is a well-known issue whose
|
|
8
|
+
// likelihood scales with the number of APDU round-trips in a session — a large
|
|
9
|
+
// file like DG2 (face image) needs dozens, so the tag is regularly lost
|
|
10
|
+
// mid-read. The session itself can be re-established with
|
|
11
|
+
// restartTechnologyRequestIOS() WITHOUT losing our PACE/BAC secure-messaging
|
|
12
|
+
// state (keys + send-sequence-counter live in our JS wrapper), so a lost-tag
|
|
13
|
+
// transmit can recover by restarting the session and re-issuing the SAME APDU
|
|
14
|
+
// (idempotent — a failed read advances no SM state, and the caller rewinds the
|
|
15
|
+
// SSC). See react-native-nfc-manager#358.
|
|
16
|
+
const IOS_MAX_RECONNECTS = 3;
|
|
17
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
18
|
+
|
|
19
|
+
const isTagLostError = (e: unknown): boolean => {
|
|
20
|
+
const msg = (
|
|
21
|
+
e instanceof Error ? e.message : typeof e === 'string' ? e : ''
|
|
22
|
+
).toLowerCase();
|
|
23
|
+
// The native bridge surfaces these for a dropped/timed-out CoreNFC session;
|
|
24
|
+
// an empty message is also treated as a lost tag (the observed DG2 failure).
|
|
25
|
+
return (
|
|
26
|
+
msg === '' ||
|
|
27
|
+
msg.includes('lost') ||
|
|
28
|
+
msg.includes('not connected') ||
|
|
29
|
+
msg.includes('tag was') ||
|
|
30
|
+
msg.includes('session') ||
|
|
31
|
+
msg.includes('invalidated') ||
|
|
32
|
+
msg.includes('timeout') ||
|
|
33
|
+
msg.includes('100')
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
7
37
|
export class NFCManagerCardService extends CardService {
|
|
8
38
|
private isOpened = false;
|
|
9
39
|
public async open(): Promise<void> {
|
|
@@ -24,29 +54,77 @@ export class NFCManagerCardService extends CardService {
|
|
|
24
54
|
return this.isOpened;
|
|
25
55
|
}
|
|
26
56
|
|
|
57
|
+
// Session-restart recovery is SAFE only for idempotent data-group reads, NOT
|
|
58
|
+
// during PACE/BAC: the authentication state machine lives on the chip, so
|
|
59
|
+
// restarting the CoreNFC session mid-handshake destroys it ("Not even
|
|
60
|
+
// registered"). The reader turns this on AFTER authentication succeeds.
|
|
61
|
+
private sessionRestartEnabled = false;
|
|
62
|
+
|
|
63
|
+
public setSessionRestartEnabled(enabled: boolean): void {
|
|
64
|
+
this.sessionRestartEnabled = enabled;
|
|
65
|
+
}
|
|
66
|
+
|
|
27
67
|
public async transmit(commandAPDU: CommandAPDU): Promise<ResponseAPDU> {
|
|
28
|
-
|
|
68
|
+
const bytes = Array.from(commandAPDU.getBytes());
|
|
29
69
|
|
|
30
|
-
if (Platform.OS
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
70
|
+
if (Platform.OS !== 'ios') {
|
|
71
|
+
return new ResponseAPDU(await NFCManager.transceive(bytes));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// During auth (restart disabled) just send once — never restart mid-PACE.
|
|
75
|
+
if (!this.sessionRestartEnabled) {
|
|
76
|
+
const r = await NFCManager.sendCommandAPDUIOS(bytes);
|
|
77
|
+
return new ResponseAPDU([...r.response, r.sw1, r.sw2]);
|
|
39
78
|
}
|
|
40
79
|
|
|
41
|
-
|
|
80
|
+
// Post-auth data reads: on a lost-tag error restart the CoreNFC session and
|
|
81
|
+
// retry the same command (secure-messaging state is preserved by the caller).
|
|
82
|
+
let lastError: unknown;
|
|
83
|
+
for (let attempt = 0; attempt <= IOS_MAX_RECONNECTS; attempt++) {
|
|
84
|
+
try {
|
|
85
|
+
const r = await NFCManager.sendCommandAPDUIOS(bytes);
|
|
86
|
+
return new ResponseAPDU([...r.response, r.sw1, r.sw2]);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
lastError = e;
|
|
89
|
+
if (attempt >= IOS_MAX_RECONNECTS || !isTagLostError(e)) {
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
92
|
+
// Re-establish the session, then pause briefly so the RF field settles
|
|
93
|
+
// before re-issuing (immediate re-send after a drop usually fails again).
|
|
94
|
+
try {
|
|
95
|
+
await NFCManager.restartTechnologyRequestIOS();
|
|
96
|
+
} catch {
|
|
97
|
+
// If restart itself fails the session is unrecoverable for now —
|
|
98
|
+
// surface the original lost-tag error to the caller.
|
|
99
|
+
throw e;
|
|
100
|
+
}
|
|
101
|
+
await sleep(150 * (attempt + 1));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
throw lastError;
|
|
42
105
|
}
|
|
43
106
|
|
|
44
107
|
public getATR(): Uint8Array {
|
|
45
108
|
throw new Error('Method not implemented.');
|
|
46
109
|
}
|
|
47
110
|
|
|
48
|
-
public getIsConnectionLost(
|
|
49
|
-
|
|
111
|
+
public getIsConnectionLost(error: Error): boolean {
|
|
112
|
+
// Lost if the session was closed, or the error looks like a dropped tag.
|
|
113
|
+
return !this.isOpened || isTagLostError(error);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** True if the error looks like a dropped/timed-out CoreNFC tag connection
|
|
117
|
+
* (as opposed to a real APDU/crypto failure). */
|
|
118
|
+
public isTagLost(error: unknown): boolean {
|
|
119
|
+
return isTagLostError(error);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Re-establish the CoreNFC session (iOS) so a fresh handshake can be retried
|
|
123
|
+
* after a tag drop. No-op on Android. Best-effort. */
|
|
124
|
+
public async restartSession(): Promise<void> {
|
|
125
|
+
if (Platform.OS === 'ios') {
|
|
126
|
+
await NFCManager.restartTechnologyRequestIOS();
|
|
127
|
+
}
|
|
50
128
|
}
|
|
51
129
|
|
|
52
130
|
public async close(): Promise<void> {
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DH (modular exponentiation, non-EC) primitives for PACE Generic Mapping.
|
|
3
|
+
*
|
|
4
|
+
* Modern ePassports/eIDs use ECDH almost universally; a minority of documents
|
|
5
|
+
* negotiate classic DH PACE. This module provides the DH math as pure,
|
|
6
|
+
* unit-testable functions so it can be verified against the ICAO 9303 Part 11
|
|
7
|
+
* worked example before it ever touches a chip. It is deliberately separate from
|
|
8
|
+
* the EC path in paceProtocol.ts — the working ECDH handshake is not modified.
|
|
9
|
+
*
|
|
10
|
+
* References: ICAO Doc 9303 Part 11 §4.4.3.2 (Generic Mapping), BSI TR-03110
|
|
11
|
+
* Part 3 §3.3.1, RFC 5114 (standardized MODP groups), TR-03110 Table A.1
|
|
12
|
+
* (standardized domain parameter IDs 0–7).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** A DH group: safe-prime modulus p, generator g, and subgroup order q. */
|
|
16
|
+
export interface DHGroup {
|
|
17
|
+
/** prime modulus */
|
|
18
|
+
p: bigint;
|
|
19
|
+
/** generator */
|
|
20
|
+
g: bigint;
|
|
21
|
+
/** subgroup order (q) — used to size exponents */
|
|
22
|
+
q: bigint;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const hx = (s: string): bigint => BigInt('0x' + s.replace(/\s+/g, ''));
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Standardized DH domain parameters by TR-03110 parameter ID.
|
|
29
|
+
* 0 → RFC 5114 §2.1 1024-bit MODP Group with 160-bit Prime Order Subgroup
|
|
30
|
+
* 1 → RFC 5114 §2.2 2048-bit MODP Group with 224-bit Prime Order Subgroup
|
|
31
|
+
* 2 → RFC 5114 §2.3 2048-bit MODP Group with 256-bit Prime Order Subgroup
|
|
32
|
+
* (IDs 3–7 are reserved/unused in practice; EC params start at 8.)
|
|
33
|
+
*/
|
|
34
|
+
export const DH_GROUPS: Record<number, DHGroup> = {
|
|
35
|
+
// RFC 5114 §2.1 — 1024-bit MODP, 160-bit subgroup (the ICAO worked-example group).
|
|
36
|
+
0: {
|
|
37
|
+
p: hx(
|
|
38
|
+
'B10B8F96 A080E01D DE92DE5E AE5D54EC 52C99FBC FB06A3C6' +
|
|
39
|
+
'9A6A9DCA 52D23B61 6073E286 75A23D18 9838EF1E 2EE652C0' +
|
|
40
|
+
'13ECB4AE A9061123 24975C3C D49B83BF ACCBDD7D 90C4BD70' +
|
|
41
|
+
'98488E9C 219A7372 4EFFD6FA E5644738 FAA31A4F F55BCCC0' +
|
|
42
|
+
'A151AF5F 0DC8B4BD 45BF37DF 365C1A65 E68CFDA7 6D4DA708' +
|
|
43
|
+
'DF1FB2BC 2E4A4371'
|
|
44
|
+
),
|
|
45
|
+
g: hx(
|
|
46
|
+
'A4D1CBD5 C3FD3412 6765A442 EFB99905 F8104DD2 58AC507F' +
|
|
47
|
+
'D6406CFF 14266D31 266FEA1E 5C41564B 777E690F 5504F213' +
|
|
48
|
+
'160217B4 B01B886A 5E91547F 9E2749F4 D7FBD7D3 B9A92EE1' +
|
|
49
|
+
'909D0D22 63F80A76 A6A24C08 7A091F53 1DBF0A01 69B6A28A' +
|
|
50
|
+
'D662A4D1 8E73AFA3 2D779D59 18D08BC8 858F4DCE F97C2A24' +
|
|
51
|
+
'855E6EEB 22B3B2E5'
|
|
52
|
+
),
|
|
53
|
+
q: hx('F518AA87 81A8DF27 8ABA4E7D 64B7CB9D 49462353'),
|
|
54
|
+
},
|
|
55
|
+
// RFC 5114 §2.2 — 2048-bit MODP, 224-bit subgroup.
|
|
56
|
+
1: {
|
|
57
|
+
p: hx(
|
|
58
|
+
'AD107E1E 9123A9D0 D660FAA7 9559C51F A20D64E5 683B9FD1' +
|
|
59
|
+
'B54B1597 B61D0A75 E6FA141D F95A56DB AF9A3C40 7BA1DF15' +
|
|
60
|
+
'EB3D688A 309C180E 1DE6B85A 1274A0A6 6D3F8152 AD6AC212' +
|
|
61
|
+
'9037C9ED EFDA4DF8 D91E8FEF 55B7394B 7AD5B7D0 B6C12207' +
|
|
62
|
+
'C9F98D11 ED34DBF6 C6BA0B2C 8BBC27BE 6A00E0A0 B9C49708' +
|
|
63
|
+
'B3BF8A31 70918836 81286130 BC8985DB 1602E714 415D9330' +
|
|
64
|
+
'278273C7 DE31EFDC 7310F712 1FD5A074 15987D9A DC0A486D' +
|
|
65
|
+
'CDF93ACC 44328387 315D75E1 98C641A4 80CD86A1 B9E587E8' +
|
|
66
|
+
'BE60E69C C928B2B9 C52172E4 13042E9B 23F10B0E 16E79763' +
|
|
67
|
+
'C9B53DCF 4BA80A29 E3FB73C1 6B8E75B9 7EF363E2 FFA31F71' +
|
|
68
|
+
'CF9DE538 4E71B81C 0AC4DFFE 0C10E64F'
|
|
69
|
+
),
|
|
70
|
+
g: hx(
|
|
71
|
+
'AC4032EF 4F2D9AE3 9DF30B5C 8FFDAC50 6CDEBE7B 89998CAF' +
|
|
72
|
+
'74866A08 CFE4FFE3 A6824A4E 10B9A6F0 DD921F01 A70C4AFA' +
|
|
73
|
+
'AB739D77 00C29F52 C57DB17C 620A8652 BE5E9001 A8D66AD7' +
|
|
74
|
+
'C1766910 1999024A F4D02727 5AC1348B B8A762D0 521BC98A' +
|
|
75
|
+
'E2471504 22EA1ED4 09939D54 DA7460CD B5F6C6B2 50717CBE' +
|
|
76
|
+
'F180EB34 118E98D1 19529A45 D6F83456 6E3025E3 16A330EF' +
|
|
77
|
+
'BB77A86F 0C1AB15B 051AE3D4 28C8F8AC B70A8137 150B8EEB' +
|
|
78
|
+
'10E183ED D19963DD D9E263E4 770589EF 6AA21E7F 5F2FF381' +
|
|
79
|
+
'B539CCE3 409D13CD 566AFBB4 8D6C0191 81E1BCFE 94B30269' +
|
|
80
|
+
'EDFE72FE 9B6AA4BD 7B5A0F1C 71CFFF4C 19C418E1 F6EC0179' +
|
|
81
|
+
'81BC087F 2A7065B3 84B890D3 191F2BFA'
|
|
82
|
+
),
|
|
83
|
+
q: hx('801C0D34 C58D93FE 99717710 1F80535A 4738CEBC BF389A99' + 'B36371EB'),
|
|
84
|
+
},
|
|
85
|
+
// RFC 5114 §2.3 — 2048-bit MODP, 256-bit subgroup.
|
|
86
|
+
2: {
|
|
87
|
+
p: hx(
|
|
88
|
+
'87A8E61D B4B6663C FFBBD19C 65195999 8CEEF608 660DD0F2' +
|
|
89
|
+
'5D2CEED4 435E3B00 E00DF8F1 D61957D4 FAF7DF45 61B2AA30' +
|
|
90
|
+
'16C3D911 34096FAA 3BF4296D 830E9A7C 209E0C64 97517ABD' +
|
|
91
|
+
'5A8A9D30 6BCF67ED 91F9E672 5B4758C0 22E0B1EF 4275BF7B' +
|
|
92
|
+
'6C5BFC11 D45F9088 B941F54E B1E59BB8 BC39A0BF 12307F5C' +
|
|
93
|
+
'4FDB70C5 81B23F76 B63ACAE1 CAA6B790 2D525267 35488A0E' +
|
|
94
|
+
'F13C6D9A 51BFA4AB 3AD83477 96524D8E F6A167B5 A41825D9' +
|
|
95
|
+
'67E144E5 14056425 1CCACB83 E6B486F6 B3CA3F79 71506026' +
|
|
96
|
+
'C0B857F6 89962856 DED4010A BD0BE621 C3A3960A 54E710C3' +
|
|
97
|
+
'75F26375 D7014103 A4B54330 C198AF12 6116D227 6E11715F' +
|
|
98
|
+
'693877FA D7EF09CA DB094AE9 1E1A1597'
|
|
99
|
+
),
|
|
100
|
+
g: hx(
|
|
101
|
+
'3FB32C9B 73134D0B 2E775066 60EDBD48 4CA7B18F 21EF2054' +
|
|
102
|
+
'07F4793A 1A0BA125 10DBC150 77BE463F FF4FED4A AC0BB555' +
|
|
103
|
+
'BE3A6C1B 0C6B47B1 BC3773BF 7E8C6F62 901228F8 C28CBB18' +
|
|
104
|
+
'A55AE313 41000A65 0196F931 C77A57F2 DDF463E5 E9EC144B' +
|
|
105
|
+
'777DE62A AAB8A862 8AC376D2 82D6ED38 64E67982 428EBC83' +
|
|
106
|
+
'1D14348F 6F2F9193 B5045AF2 767164E1 DFC967C1 FB3F2E55' +
|
|
107
|
+
'A4BD1BFF E83B9C80 D052B985 D182EA0A DB2A3B73 13D3FE14' +
|
|
108
|
+
'C8484B1E 052588B9 B7D2BBD2 DF016199 ECD06E15 57CD0915' +
|
|
109
|
+
'B3353BBB 64E0EC37 7FD02837 0DF92B52 C7891428 CDC67EB6' +
|
|
110
|
+
'184B523D 1DB246C3 2F630784 90F00EF8 D647D148 D4795451' +
|
|
111
|
+
'5E2327CF EF98C582 664B4C0F 6CC41659'
|
|
112
|
+
),
|
|
113
|
+
q: hx(
|
|
114
|
+
'8CF83642 A709A097 B4479976 40129DA2 99B1A47D 1EB3750B' +
|
|
115
|
+
'A308B0FE 64F5FBD3'
|
|
116
|
+
),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* PACE DH Generic Mapping (ICAO 9303 Part 11 §4.4.3.2):
|
|
122
|
+
* shared secret h = PK_PICC ^ SK_PCD mod p
|
|
123
|
+
* mapped generator g~ = g^s · h mod p
|
|
124
|
+
* where s is the decrypted PICC nonce (a big-endian integer).
|
|
125
|
+
*
|
|
126
|
+
* Pure function over the group + the three inputs, so it can be checked against
|
|
127
|
+
* the ICAO worked example. `pkPicc` is the PICC mapping public key; `skPcd` the
|
|
128
|
+
* PCD mapping private key; `nonce` the decrypted nonce s.
|
|
129
|
+
*/
|
|
130
|
+
export const dhGenericMapping = (
|
|
131
|
+
group: DHGroup,
|
|
132
|
+
skPcd: bigint,
|
|
133
|
+
pkPicc: bigint,
|
|
134
|
+
nonce: bigint
|
|
135
|
+
): { sharedH: bigint; mappedGenerator: bigint } => {
|
|
136
|
+
const sharedH = modPow(pkPicc, skPcd, group.p);
|
|
137
|
+
const gPowS = modPow(group.g, nonce, group.p);
|
|
138
|
+
const mappedGenerator = (gPowS * sharedH) % group.p;
|
|
139
|
+
return { sharedH, mappedGenerator };
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* DH key agreement with a mapped generator (PACE step 3):
|
|
144
|
+
* PK = g~ ^ SK mod p (public key to send)
|
|
145
|
+
* K = PK_PICC ^ SK mod p (shared secret)
|
|
146
|
+
*/
|
|
147
|
+
export const dhPublicKey = (
|
|
148
|
+
group: DHGroup,
|
|
149
|
+
mappedGenerator: bigint,
|
|
150
|
+
sk: bigint
|
|
151
|
+
): bigint => modPow(mappedGenerator, sk, group.p);
|
|
152
|
+
|
|
153
|
+
export const dhSharedSecret = (
|
|
154
|
+
group: DHGroup,
|
|
155
|
+
pkPicc: bigint,
|
|
156
|
+
sk: bigint
|
|
157
|
+
): bigint => modPow(pkPicc, sk, group.p);
|
|
158
|
+
|
|
159
|
+
/** Square-and-multiply modular exponentiation (constant-shape, bigint). */
|
|
160
|
+
export function modPow(base: bigint, exp: bigint, mod: bigint): bigint {
|
|
161
|
+
if (mod === 1n) return 0n;
|
|
162
|
+
let result = 1n;
|
|
163
|
+
base = ((base % mod) + mod) % mod;
|
|
164
|
+
let e = exp;
|
|
165
|
+
while (e > 0n) {
|
|
166
|
+
if (e & 1n) result = (result * base) % mod;
|
|
167
|
+
e >>= 1n;
|
|
168
|
+
base = (base * base) % mod;
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Fixed-width big-endian byte encoding of a DH value (left-padded to p's size). */
|
|
174
|
+
export const dhEncode = (value: bigint, group: DHGroup): Buffer => {
|
|
175
|
+
const byteLen = Math.ceil(group.p.toString(16).length / 2);
|
|
176
|
+
let h = value.toString(16);
|
|
177
|
+
if (h.length % 2) h = '0' + h;
|
|
178
|
+
const buf = Buffer.from(h, 'hex');
|
|
179
|
+
if (buf.length === byteLen) return buf;
|
|
180
|
+
const out = Buffer.alloc(byteLen);
|
|
181
|
+
buf.copy(out, byteLen - buf.length);
|
|
182
|
+
return out;
|
|
183
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed PACE errors.
|
|
3
|
+
*
|
|
4
|
+
* The point is to let the caller distinguish a PACE CAPABILITY gap (this build
|
|
5
|
+
* cannot run the variant the chip advertises — e.g. DH agreement, CAM mapping,
|
|
6
|
+
* an unregistered curve) from a RUNTIME failure (wrong key, tag lost, APDU
|
|
7
|
+
* error). Both fall back to BAC, but the diagnostics reason must differ so we
|
|
8
|
+
* can tell "we don't support this document's PACE" from "the read went wrong".
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Why a PACE attempt could not even be run by this implementation. */
|
|
12
|
+
export type PACEUnsupportedReason =
|
|
13
|
+
| 'agreement' // key-agreement algorithm not implemented (e.g. DH when only ECDH built)
|
|
14
|
+
| 'mapping' // mapping type not implemented (e.g. CAM)
|
|
15
|
+
| 'curve' // standardized domain parameter / curve not registered
|
|
16
|
+
| 'cipher'; // cipher not implemented for the chosen mapping
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Thrown when the chip advertises a PACE variant this build cannot perform.
|
|
20
|
+
* Carries a coarse, non-PII reason that maps directly to a diagnostics fallback
|
|
21
|
+
* reason (`pace_unsupported_<reason>`), so support can see exactly which
|
|
22
|
+
* capability was missing for a given document.
|
|
23
|
+
*/
|
|
24
|
+
export class PACEUnsupportedError extends Error {
|
|
25
|
+
readonly paceUnsupported = true;
|
|
26
|
+
readonly reason: PACEUnsupportedReason;
|
|
27
|
+
|
|
28
|
+
constructor(reason: PACEUnsupportedReason, detail: string) {
|
|
29
|
+
super(`PACE variant not supported (${reason}): ${detail}`);
|
|
30
|
+
this.name = 'PACEUnsupportedError';
|
|
31
|
+
this.reason = reason;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Type guard usable across module boundaries (instanceof is unreliable after
|
|
36
|
+
* transpilation/bundling, so we also check the marker flag). */
|
|
37
|
+
export const isPACEUnsupportedError = (e: unknown): e is PACEUnsupportedError =>
|
|
38
|
+
e instanceof PACEUnsupportedError ||
|
|
39
|
+
(typeof e === 'object' &&
|
|
40
|
+
e !== null &&
|
|
41
|
+
(e as { paceUnsupported?: boolean }).paceUnsupported === true);
|