selectMasterFile method

Future<void> selectMasterFile()

Selects Master File (MF). Can throw ICCError if command is sent to invalid MRTD document. Can throw ComProviderError in case connection with MRTD is lost.

Implementation

Future<void> selectMasterFile() async {
  _log.debug("Selecting root Master File");
  // In ICAO 9303 p10 doc, the command to select Master File is defined as sending select APDU
  // command with empty data field. On some passport this command doesn't work and MF is not selected,
  // although success status (9000) is returned. In doc ISO/IEC 7816-4 section 6 an alternative option
  // is specified by sending the same command as described in ICAO 9303 p10 doc but in this case
  // data field should be equal to '0x3F00'.
  // see: https://cardwerk.com/smart-card-standard-iso7816-4-section-6-basic-interindustry-commands
  //     'If P1-P2=’0000′ and if the data field is empty or equal to ‘3F00’, then select the MF.'
  //
  // To maximize our chance for MF to be selected we send select first command with P1-P2=’0000′ as
  // specified in doc ISO/IEC 7816-4 section 6.

  await icc
      .selectFile(cla: ISO7816_CLA.NO_SM, p1: 0, p2: 0)
      .onError<ICCError>((error, stackTrace) async {
    _log.warning(
        "Couldn't select MF by P1: 0, P2: 0 sw=${error.sw}, re-trying to select MF with FileID=3F00");
    return await icc
        .selectFile(
            cla: ISO7816_CLA.NO_SM,
            p1: 0,
            p2: 0,
            data: Uint8List.fromList([0x3F, 0x00]))
        .onError<ICCError>((error, stackTrace) async {
      _log.warning(
          "Couldn't select MF by P1=0, P2=0, FileID=3F00 sw=${error.sw}, re-trying to select MF with P2=0x0C and FileID=3F00");
      return await icc
          .selectFileById(
              p2: _defaultSelectP2, fileId: Uint8List.fromList([0x3F, 0x00]))
          .onError<ICCError>((error, stackTrace) async {
        _log.warning(
            "Couldn't select MF by P1=0, P2=0x0C, FileID=3F00 sw=${error.sw}, re-trying to select MF with P2=0x0C");
        return await icc.selectFile(
            cla: ISO7816_CLA.NO_SM, p1: 0, p2: _defaultSelectP2);
      });
    });
  });
}