Package org.nfctools.mf.block

Examples of org.nfctools.mf.block.TrailerBlock


  public void doWithReaderWriter(MfClassicReaderWriter readerWriter) throws IOException {
    DataBlock emptyDataBlock = new DataBlock();
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();

    for (int sectorId = 0; sectorId < memoryLayout.getSectors(); sectorId++) {
      TrailerBlock trailerBlock = readTrailerBlock(readerWriter, sectorId);
      if (trailerBlock != null) {
        Key keyToWrite = trailerBlock.isKeyBReadable() ? Key.A : Key.B;
        for (byte[] key : knownKeys) {
          try {
            initTransportConfig(readerWriter, sectorId, new KeyValue(keyToWrite, key));
            for (int blockId = 0; blockId < memoryLayout.getBlocksPerSector(sectorId); blockId++) {
              if (!memoryLayout.isTrailerBlock(sectorId, blockId) && !(blockId == 0 && sectorId == 0)) {
View Full Code Here


  }

  public static void initTransportConfig(MfClassicReaderWriter readerWriter, int sector, KeyValue keyValue)
      throws IOException {
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    TrailerBlock transportTrailer = new TrailerBlock();
    MfClassicAccess access = new MfClassicAccess(keyValue, sector,
        memoryLayout.getTrailerBlockNumberForSector(sector));
    readerWriter.writeBlock(access, transportTrailer);
  }
View Full Code Here

      throws IOException {
    byte[] writeKeyValue = keyConfig == null ? null : keyConfig.getWriteKeyValue();
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    MfClassicAccess accessTrailer = new MfClassicAccess(MfClassicConstants.MAD_KEY, 0,
        memoryLayout.getTrailerBlockNumberForSector(0));
    TrailerBlock madTrailer = (TrailerBlock)readerWriter.readBlock(accessTrailer)[0];

    if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_AVAILABLE) != 0) {
      if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_V1) == MadConstants.GPB_MAD_V1) {
        madTrailer.setKey(Key.A, MadConstants.DEFAULT_MAD_KEY);
        if (writeKeyValue != null)
          madTrailer.setKey(Key.B, writeKeyValue);
        Mad1 mad1 = new Mad1(readerWriter, keyConfig, madTrailer);
        mad1.readMad();
        if (writeKeyValue == null)
          mad1.setReadonly();
        return mad1;
      }
      else if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_V2) == MadConstants.GPB_MAD_V2) {
        madTrailer.setKey(Key.A, MadConstants.DEFAULT_MAD_KEY);
        if (writeKeyValue != null)
          madTrailer.setKey(Key.B, writeKeyValue);
        Mad2 mad = new Mad2(readerWriter, keyConfig, madTrailer);
        mad.readMad();
        if (writeKeyValue == null)
          mad.setReadonly();
        return mad;
View Full Code Here

  private Application createNewApplication() {
    try {
      ApplicationDirectory applicationDirectory = readerWriter.createApplicationDirectory(new MadKeyConfig(Key.A,
          MfConstants.TRANSPORT_KEY, writeKey));
      TrailerBlock readWriteTrailerBlock = ClassicHandler.createReadWriteDataTrailerBlock();
      readWriteTrailerBlock.setKey(Key.A, writeKey);
      readWriteTrailerBlock.setKey(Key.B, writeKey);
      Application application = applicationDirectory.createApplication(MfConstants.NDEF_APP_ID,
          applicationDirectory.getMaxContinousSize(), writeKey, readWriteTrailerBlock);
      return application;
    }
    catch (IOException e) {
View Full Code Here

  public static boolean isBlank(MfClassicReaderWriter readerWriter) throws IOException {
    try {
      MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
      for (int sector = 0; sector < memoryLayout.getSectors(); sector++) {
        TrailerBlock trailerBlock = readTrailerBlock(readerWriter, sector, MfClassicConstants.TRANSPORT_KEY);
        if (!Arrays.equals(MfConstants.TRANSPORT_ACCESS_CONDITIONS, trailerBlock.getAccessConditions()))
          return false;
        if (trailerBlock.getGeneralPurposeByte() != MfConstants.TRANSPORT_GPB)
          return false;
      }
      return true;
    }
    catch (MfLoginException e) {
View Full Code Here

    }
  }

  public static boolean isFormattedWritable(Application application, KeyValue keyValue) throws IOException {
    try {
      TrailerBlock trailerBlock = application.readTrailer(keyValue);
      for (int dataArea = 0; dataArea < 3; dataArea++) {
        if (!trailerBlock.canWriteDataBlock(keyValue.getKey(), dataArea))
          return false;
      }
      if (NfcUtils.getLeastSignificantNibble(trailerBlock.getGeneralPurposeByte()) != 0)
        return false;
      return true;
    }
    catch (MfLoginException e) {
      return false;
View Full Code Here

    }
  }

  public static TrailerBlock createReadWriteDataTrailerBlock() {
    try {
      TrailerBlock trailerBlock = new TrailerBlock();
      trailerBlock.setAccessConditions(MfConstants.NDEF_READ_WRITE_ACCESS_CONDITIONS);
      trailerBlock.setGeneralPurposeByte(MfConstants.NDEF_GPB_V10_READ_WRITE);
      return trailerBlock;
    }
    catch (MfException e) {
      throw new RuntimeException(e);
    }
View Full Code Here

    for (KeyValue keyValue : keys) {
      try {
        MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
        int trailerBlockNumberForSector = memoryLayout.getTrailerBlockNumberForSector(sector);
        MfClassicAccess access = new MfClassicAccess(keyValue, sector, trailerBlockNumberForSector);
        TrailerBlock trailerBlock = (TrailerBlock)readerWriter.readBlock(access)[0];
        return trailerBlock;
      }
      catch (MfLoginException e) {
        lastException = e;
      }
View Full Code Here

  private void checkReadAccess(MfAccess mfAccess) throws IOException {
    int trailerBlockId = mfAccess.getCard().getTrailerBlockNumberForSector(mfAccess.getSector());
    Map<Integer, MfBlock> blockMap = cardBlockMap.get(mfAccess.getCard());

    TrailerBlock trailerBlock = (TrailerBlock)blockMap.get(mfAccess.getCard().getBlockNumber(mfAccess.getSector(),
        trailerBlockId));

    byte[] key = trailerBlock.getKey(mfAccess.getKey());

    if (Key.B.equals(mfAccess.getKey()) && trailerBlock.isKeyBReadable())
      throw new MfException("Cannot login with Key B. Key B is readable");

    if (!NfcUtils.isEqualArray(key, mfAccess.getKeyValue())) {
      throw new MfLoginException("Login failed. Sector: " + mfAccess.getSector() + ", Block: "
          + mfAccess.getBlock() + " Key: " + mfAccess.getKey().name() + " Given: "
View Full Code Here

  private void checkWriteAccess(MfAccess mfAccess, int blockId) throws IOException {
    int trailerBlockId = mfAccess.getCard().getTrailerBlockNumberForSector(mfAccess.getSector());
    Map<Integer, MfBlock> blockMap = cardBlockMap.get(mfAccess.getCard());

    TrailerBlock trailerBlock = (TrailerBlock)blockMap.get(mfAccess.getCard().getBlockNumber(mfAccess.getSector(),
        trailerBlockId));

    byte[] key = trailerBlock.getKey(mfAccess.getKey());

    if (Key.B.equals(mfAccess.getKey()) && trailerBlock.isKeyBReadable())
      throw new MfLoginException("Cannot login with Key B. Key B is readable. Sector: " + mfAccess.getSector()
          + ", Block: " + blockId);

    int dataArea = (int)(mfAccess.getCard().getBlocksPerSector(mfAccess.getSector()) > 3 ? Math
        .floor((double)mfAccess.getCard().getBlocksPerSector(mfAccess.getSector()) / 5.0) : blockId);

    if ((blockId == trailerBlockId && !trailerBlock.canWriteTrailerBlock(mfAccess.getKey()))
        || (blockId != trailerBlockId && !trailerBlock.canWriteDataBlock(mfAccess.getKey(), dataArea))) {
      throw new MfLoginException("Write Access Denied. Sector: " + mfAccess.getSector() + ", Block: " + blockId
          + " Key: " + mfAccess.getKey().name());

    }
View Full Code Here

TOP

Related Classes of org.nfctools.mf.block.TrailerBlock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.