Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing throw specifier #29

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 78 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,87 +37,88 @@ dependencies:
import 'package:dmrtd/dmrtd.dart';

try {
final nfc = NfcProvider();
await nfc.connect(iosAlertMessage: "Hold your iPhone near Passport");

final passport = Passport(nfc);

nfc.setIosAlertMessage("Reading EF.CardAccess ...");
final cardAccess = await passport.readEfCardAccess();

_nfc.setIosAlertMessage("Initiating session with PACE or BAC...");
//set MrtdData
mrtdData.isPACE = true; //initialize with PACE(set false if you want to do with DBA)
mrtdData.isDBA = accessKey.PACE_REF_KEY_TAG == 0x01 ? true : false;

if (isPace) {
//PACE session
await passport.startSessionPACE(accessKey, mrtdData.cardAccess!);
} else {
//BAC session
await passport.startSession(accessKey as DBAKey);
}

nfc.setIosAlertMessage(formatProgressMsg("Reading EF.COM ...", 0));
final efcom = await passport.readEfCOM();

nfc.setIosAlertMessage(formatProgressMsg("Reading Data Groups ...", 20));
EfDG1 dg1;
if(efcom.dgTags.contains(EfDG1.TAG)) {
dg1 = await passport.readEfDG1();
}

EfDG2 dg2;
if(efcom.dgTags.contains(EfDG2.TAG)) {
dg2 = await passport.readEfDG2();
}

EfDG14 dg14;
if(efcom.dgTags.contains(EfDG14.TAG)) {
dg14 = await passport.readEfDG14();
}

EfDG15 dg15;
Uint8List sig;
if(efcom.dgTags.contains(EfDG15.TAG)) {
dg15 = await passport.readEfDG15();
nfc.setIosAlertMessage(formatProgressMsg("Doing AA ...", 60));
sig = await passport.activeAuthenticate(Uint8List(8));
}

nfc.setIosAlertMessage(formatProgressMsg("Reading EF.SOD ...", 80));
final sod = await passport.readEfSOD();
final nfc = NfcProvider();
await nfc.connect(iosAlertMessage: "Hold your iPhone near Passport");

final passport = Passport(nfc);

nfc.setIosAlertMessage("Reading EF.CardAccess ...");
final cardAccess = await passport.readEfCardAccess();

_nfc.setIosAlertMessage("Initiating session with PACE or BAC...");
//set MrtdData
mrtdData.isPACE = true; //initialize with PACE(set false if you want to do with DBA)
mrtdData.isDBA = accessKey.PACE_REF_KEY_TAG == 0x01 ? true : false;

if (isPace) {
//PACE session
await passport.startSessionPACE(accessKey, mrtdData.cardAccess!);
} else {
//BAC session
await passport.startSession(accessKey as DBAKey);
}

nfc.setIosAlertMessage(formatProgressMsg("Reading EF.COM ...", 0));
final efcom = await passport.readEfCOM();

nfc.setIosAlertMessage(formatProgressMsg("Reading Data Groups ...", 20));
EfDG1 dg1;
if (efcom.dgTags.contains(EfDG1.TAG)) {
dg1 = await passport.readEfDG1();
}

EfDG2 dg2;
if (efcom.dgTags.contains(EfDG2.TAG)) {
dg2 = await passport.readEfDG2();
}

EfDG14 dg14;
if (efcom.dgTags.contains(EfDG14.TAG)) {
dg14 = await passport.readEfDG14();
}

EfDG15 dg15;
Uint8List sig;
if (efcom.dgTags.contains(EfDG15.TAG)) {
dg15 = await passport.readEfDG15();
nfc.setIosAlertMessage(formatProgressMsg("Doing AA ...", 60));
sig = await passport.activeAuthenticate(Uint8List(8));
}

nfc.setIosAlertMessage(formatProgressMsg("Reading EF.SOD ...", 80));
final sod = await passport.readEfSOD();
}
on Exception catch(e) {
final se = e.toString().toLowerCase();
String alertMsg = "An error has occurred while reading Passport!";
if(e is PassportError) {
if(se.contains("security status not satisfied")) {
alertMsg = "Failed to initiate session with passport.\nCheck input data!";
}
}

if(se.contains('timeout')){
alertMsg = "Timeout while waiting for Passport tag";
}
else if(se.contains("tag was lost")){
alertMsg = "Tag was lost. Please try again!";
}
else if(se.contains("invalidated by user")){
alertMsg = "";
}
errorAlertMsg = alertMsg;
final se = e.toString().toLowerCase();
String alertMsg = "An error has occurred while reading Passport!";
if (e is PassportError) {
if (se.contains("security status not satisfied")) {
alertMsg = "Failed to initiate session with passport.\nCheck input data!";
}
}

if (se.contains('timeout')){
alertMsg = "Timeout while waiting for Passport tag";
}
else if (se.contains("tag was lost")) {
alertMsg = "Tag was lost. Please try again!";
}
else if (se.contains("invalidated by user")) {
alertMsg = "";
}

errorAlertMsg = alertMsg;
}
finally {
if(errorAlertMsg?.isNotEmpty){
await _nfc.disconnect(iosErrorMessage: errorAlertMsg);
if(!Platform.isIOS) {
// Show error to the user
}
}
else {
await _nfc.disconnect(iosAlertMessage: formatProgressMsg("Finished", 100);
}
if (errorAlertMsg?.isNotEmpty) {
await _nfc.disconnect(iosErrorMessage: errorAlertMsg);
if (!Platform.isIOS) {
// Show error to the user
}
}
else {
await _nfc.disconnect(iosAlertMessage: formatProgressMsg("Finished", 100));
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
flutter_localizations:
sdk: flutter
flutter_platform_widgets: ^3.3.5
intl: ^0.18.0
intl: ^0.19.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
21 changes: 12 additions & 9 deletions lib/src/crypto/aa_pubkey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,41 @@ class AAPublicKey {

AAPublicKeyType get type => _type;

/// Parses AAPublicKey from [encPubKey] bytes which should be DER encoded ASN.1 bytes.
///
/// Throws [Exception] when provided bytes doesn't contain correct DER encoded ASN.1 AAPublicKey format.
AAPublicKey.fromBytes(final Uint8List encPubKey) : _encPubKey = encPubKey {
// Parse key type and SubjectPublicKey bytes

final tvPubKeyInfo = TLV.decode(encPubKey);
if(tvPubKeyInfo.tag.value != 0x30) { // Sequence
EfParseError(
if (tvPubKeyInfo.tag.value != 0x30) { // Sequence
throw Exception(
"Invalid SubjectPublicKeyInfo tag=${tvPubKeyInfo.tag.value.hex()}, expected tag=30"
);
}

final tvAlg = TLV.decode(tvPubKeyInfo.value);
if(tvAlg.tag.value != 0x30) { // Sequence
EfParseError(
if (tvAlg.tag.value != 0x30) { // Sequence
throw Exception(
"Invalid AlgorithmIdentifier tag=${tvAlg.tag.value.hex()}, expected tag=30"
);
}

final tvAlgOID = TLV.decode(tvAlg.value);
if(tvAlg.tag.value != 0x06) { // OID
EfParseError(
if (tvAlg.tag.value != 0x06) { // OID
throw Exception(
"Invalid Algorithm OID object tag=${tvAlgOID.tag.value.hex()}, expected tag=06"
);
}

final rsaOID = "2A864886F70D010101".parseHex();
if(ListEquality().equals(tvAlgOID.value, rsaOID)) {
if (ListEquality().equals(tvAlgOID.value, rsaOID)) {
_type = AAPublicKeyType.RSA;
}

_subPubKeyBytes = tvPubKeyInfo.value.sublist(tvAlg.encodedLen);
if(_subPubKeyBytes[0] != 0x03) { // Bit String
EfParseError(
if (_subPubKeyBytes[0] != 0x03) { // Bit String
throw Exception(
"Invalid SubjectPublicKey object tag=${_subPubKeyBytes[0].hex()}, expected tag=03"
);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/src/lds/df1/efdg15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import 'dart:typed_data';
import 'dg.dart';
import '../../crypto/aa_pubkey.dart';
import 'package:dmrtd/dmrtd.dart';

class EfDG15 extends DataGroup {
static const FID = 0x010F;
Expand All @@ -26,6 +26,10 @@ class EfDG15 extends DataGroup {

@override
void parseContent(final Uint8List content) {
_pubkey = AAPublicKey.fromBytes(content);
try {
_pubkey = AAPublicKey.fromBytes(content);
} on Exception catch(e) {
throw EfParseError("Failed to parse AAPublicKey from EF.DG15: $e");
}
}
}
10 changes: 5 additions & 5 deletions lib/src/passport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Passport {

final _log = Logger("passport");
final MrtdApi _api;
_DF _dfSelectd = _DF.None;
_DF _dfSelected = _DF.None;

/// Constructs new [Passport] instance with communication [provider].
/// [provider] should be already connected.
Expand Down Expand Up @@ -404,22 +404,22 @@ class Passport {
}

Future<void> _selectMF() async {
if(_dfSelectd != _DF.MF) {
if(_dfSelected != _DF.MF) {
_log.debug("Selecting MF");
await _exec(() =>
_api.selectMasterFile()
);
_dfSelectd = _DF.MF;
_dfSelected = _DF.MF;
}
}

Future<void> _selectDF1() async {
if(_dfSelectd != _DF.DF1) {
if(_dfSelected != _DF.DF1) {
_log.debug("Selecting DF1");
await _exec(() =>
_api.selectEMrtdApplication()
);
_dfSelectd = _DF.DF1;
_dfSelected = _DF.DF1;
}
}

Expand Down
Loading