-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpassport.dart
442 lines (406 loc) · 14.5 KB
/
passport.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Created by Crt Vavros, copyright © 2022 ZeroPass. All rights reserved.
import 'dart:typed_data';
import 'package:dmrtd/dmrtd.dart';
import 'package:dmrtd/extensions.dart';
import 'package:dmrtd/src/proto/access_key.dart';
import 'package:logging/logging.dart';
import 'proto/iso7816/icc.dart';
import 'proto/iso7816/response_apdu.dart';
import 'proto/mrtd_api.dart';
class PassportError implements Exception {
final String message;
final StatusWord? code;
PassportError(this.message, {this.code});
@override
String toString() => message;
}
enum _DF {
// ignore: constant_identifier_names
None,
// ignore: constant_identifier_names
MF,
// ignore: constant_identifier_names
DF1
}
class Passport {
static const aaChallengeLen = 8;
final _log = Logger("passport");
final MrtdApi _api;
_DF _dfSelected = _DF.None;
/// Constructs new [Passport] instance with communication [provider].
/// [provider] should be already connected.
Passport(final ComProvider provider) : _api = MrtdApi(provider);
/// Starts new Secure Messaging session with passport
/// using Document Basic Access [keys].
///
/// Can throw [ComProviderError] on connection failure.
/// Throws [PassportError] when provided [keys] are invalid or
/// if BAC session is not supported.
Future<void> startSession(final DBAKey keys) async {
_log.debug("Starting session");
await _selectDF1();
await _exec(() => _api.initSessionViaBAC(keys));
_log.debug("Session established");
}
/// Starts new Secure Messaging session with passport
/// using PACE (Password Authenticated Connection Establishment) protocol.
///
/// Can throw [ComProviderError] on connection failure.
/// Throws [PassportError] when provided [keys] are invalid or
/// if BAC session is not supported.
Future<void> startSessionPACE(final AccessKey accessKey, EfCardAccess efCardAccess) async {
_log.debug("Starting session");
await _exec(() => _api.initSessionViaPACE(accessKey, efCardAccess));
_log.debug("Session established");
}
/// Executes Active Authentication command with [challenge] and
/// returns signature bytes. The [challenge] should be 8 bytes long.
/// Session with passport should be already established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if invalid [challenge] length, AA is not supported
/// or if calling this function prior establishing session with passport.
///
/// Note: AA is not available if EF.DG15 file is missing from passport.
/// Read EF.COM file To determine if file EF.DG15.
Future<Uint8List> activeAuthenticate(final Uint8List challenge) async {
return await _exec(() =>
_api.activeAuthenticate(challenge)
);
}
/// Reads file EF.CardAccess from passport.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist.
///
/// Note: Might not be available if PACE is not supported
Future<EfCardAccess> readEfCardAccess() async {
_log.debug("Reading EF.CardAccess");
//
bool demo = false;
Uint8List data;
if (demo){
data =
Uint8List.fromList([49, 20, 48, 18, 6, 10, 4, 0, 127, 0, 7, 2, 2, 4, 2, 2, 2, 1, 2, 2, 1, 12]);//[49, 20, 48, 18, 6, 10, 4, 0, 127, 0, 7, 2, 2, 4, 2, 2, 2, 1, 2, 2, 1, 12]
// same data in hex format: 31 14 30 12 6 a 4 0 7f 0 7 2 2 4 2 2 2 1 2 2 1 c
return EfCardAccess.fromBytes(
data);
}
else {
await _selectMF();
return EfCardAccess.fromBytes(
await _exec(() => _api.readFileBySFI(EfCardAccess.SFI))
);
}
}
/// Reads file EF.CardSecurity from passport.
/// Session with passport via PACE protocol
/// should be established prior calling this function.
///
/// Note: PACE protocol is not supported yet.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if session was not established via PACE protocol.
Future<EfCardSecurity> readEfCardSecurity() async {
_log.debug("Reading EF.CardSecurity");
await _selectMF();
return EfCardSecurity.fromBytes(
await _exec(() => _api.readFileBySFI(EfCardSecurity.SFI))
);
}
/// Reads file EF.COM from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfCOM> readEfCOM() async {
_log.debug("Reading EF.COM");
await _selectDF1();
return EfCOM.fromBytes(
await _exec(() => _api.readFileBySFI(EfCOM.SFI))
);
}
/// Reads file EF.DG1 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG1> readEfDG1() async {
await _selectDF1();
_log.debug("Reading EF.DG1");
return EfDG1.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG1.SFI))
);
}
/// Reads file EF.DG2 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG2> readEfDG2() async {
_log.debug("Reading EF.DG2");
await _selectDF1();
return EfDG2.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG2.SFI))
);
}
/// Reads file EF.DG3 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
/// [PassportError] is also thrown if extended authentication is required
/// but wasn't successfully executed first.
///
/// Note: Extended authentication not supported.
Future<EfDG3> readEfDG3() async {
_log.debug("Reading EF.DG3");
await _selectDF1();
return EfDG3.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG3.SFI))
);
}
/// Reads file EF.DG4 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
/// [PassportError] is also thrown if extended authentication is required
/// but wasn't successfully executed first.
///
/// Note: Extended authentication not supported.
Future<EfDG4> readEfDG4() async {
_log.debug("Reading EF.DG4");
await _selectDF1();
return EfDG4.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG4.SFI))
);
}
/// Reads file EF.DG5 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG5> readEfDG5() async {
_log.debug("Reading EF.DG5");
await _selectDF1();
return EfDG5.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG5.SFI))
);
}
/// Reads file EF.DG6 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG6> readEfDG6() async {
_log.debug("Reading EF.DG6");
await _selectDF1();
return EfDG6.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG6.SFI))
);
}
/// Reads file EF.DG7 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG7> readEfDG7() async {
_log.debug("Reading EF.DG7");
await _selectDF1();
return EfDG7.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG7.SFI))
);
}
/// Reads file EF.DG8 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG8> readEfDG8() async {
_log.debug("Reading EF.DG8");
await _selectDF1();
return EfDG8.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG8.SFI))
);
}
/// Reads file EF.DG9 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG9> readEfDG9() async {
_log.debug("Reading EF.DG9");
await _selectDF1();
return EfDG9.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG9.SFI))
);
}
/// Reads file EF.DG10 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG10> readEfDG10() async {
_log.debug("Reading EF.DG10");
await _selectDF1();
return EfDG10.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG10.SFI))
);
}
/// Reads file EF.DG11 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG11> readEfDG11() async {
_log.debug("Reading EF.DG11");
await _selectDF1();
return EfDG11.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG11.SFI))
);
}
/// Reads file EF.DG12 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG12> readEfDG12() async {
_log.debug("Reading EF.DG12");
await _selectDF1();
return EfDG12.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG12.SFI))
);
}
/// Reads file EF.DG13 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG13> readEfDG13() async {
_log.debug("Reading EF.DG13");
await _selectDF1();
return EfDG13.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG13.SFI))
);
}
/// Reads file EF.DG14 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG14> readEfDG14() async {
await _selectDF1();
_log.debug("Reading EF.DG14");
return EfDG14.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG14.SFI))
);
}
/// Reads file EF.DG15 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG15> readEfDG15() async {
_log.debug("Reading EF.DG15");
await _selectDF1();
return EfDG15.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG15.SFI))
);
}
/// Reads file EF.DG16 from passport.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfDG16> readEfDG16() async {
_log.debug("Reading EF.DG16");
await _selectDF1();
return EfDG16.fromBytes(
await _exec(() => _api.readFileBySFI(EfDG16.SFI))
);
}
/// Reads file EF.SOD.
/// Session with passport should be already
/// established before calling this function.
///
/// Can throw [ComProviderError] on connection error.
/// Throws [PassportError] if file doesn't exist or
/// if calling this function prior establishing session with passport.
Future<EfSOD> readEfSOD() async {
_log.debug("Reading EF.SOD");
await _selectDF1();
return EfSOD.fromBytes(
await _exec(() => _api.readFileBySFI(EfSOD.SFI))
);
}
Future<void> _selectMF() async {
if(_dfSelected != _DF.MF) {
_log.debug("Selecting MF");
await _exec(() =>
_api.selectMasterFile()
);
_dfSelected = _DF.MF;
}
}
Future<void> _selectDF1() async {
if(_dfSelected != _DF.DF1) {
_log.debug("Selecting DF1");
await _exec(() =>
_api.selectEMrtdApplication()
);
_dfSelected = _DF.DF1;
}
}
Future<T> _exec<T>(Function f) async {
try {
return await f();
}
on ICCError catch(e) {
var msg = e.sw.description();
if(e.sw.sw1 == 0x63 && e.sw.sw2 == 0xcf) {
// some older passports return sw=63cf when data to establish session is wrong. (Wrong DBAKeys)
msg = StatusWord.securityStatusNotSatisfied.description();
}
throw PassportError(msg, code: e.sw);
}
on MrtdApiError catch(e) {
throw PassportError(e.message, code: e.code);
}
}
}