Package javax.smartcardio

Examples of javax.smartcardio.CommandAPDU


    for (int currentBlock = 0; currentBlock < mfAccess.getBlocksToRead(); currentBlock++) {

      blockNumber = (byte)mfAccess.getCard().getBlockNumber(mfAccess.getSector(),
          mfAccess.getBlock() + currentBlock);

      CommandAPDU readBlock = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_READ_BINARY, 0x00, blockNumber, 16);
      ResponseAPDU readBlockResponse;
      try {
        readBlockResponse = cardChannel.transmit(readBlock);
        if (!ApduUtils.isSuccess(readBlockResponse)) {
          throw new MfException("Reading block failed. Sector: " + mfAccess.getSector() + ", Block: "
View Full Code Here


  protected abstract void loginIntoSector(MfAccess mfAccess, CardChannel cardChannel) throws IOException;

  protected void loginIntoSector(MfAccess mfAccess, CardChannel cardChannel, byte memoryKeyId) throws IOException {
    try {
      CommandAPDU loadKey = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_EXTERNAL_AUTHENTICATE,
          Acs.P1_LOAD_KEY_INTO_VOLATILE_MEM, memoryKeyId, mfAccess.getKeyValue());
      ResponseAPDU loadKeyResponse = cardChannel.transmit(loadKey);
      if (!ApduUtils.isSuccess(loadKeyResponse)) {
        throw new MfLoginException("Loading key failed. Sector: " + mfAccess.getSector() + ", Block: "
            + mfAccess.getBlock() + " Key: " + mfAccess.getKey().name() + ", Response: " + loadKeyResponse);
      }

      byte blockNumber = (byte)mfAccess.getCard().getBlockNumber(mfAccess.getSector(), mfAccess.getBlock());

      byte keyTypeToUse = mfAccess.getKey() == Key.A ? Acs.KEY_A : Acs.KEY_B;

      CommandAPDU auth = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_INTERNAL_AUTHENTICATE_ACS, 0, 0, new byte[] {
          0x01, 0x00, blockNumber, keyTypeToUse, memoryKeyId });
      ResponseAPDU authResponse = cardChannel.transmit(auth);
      if (!ApduUtils.isSuccess(authResponse)) {
        throw new MfLoginException("Login failed. Sector: " + mfAccess.getSector() + ", Block: "
            + mfAccess.getBlock() + " Key: " + mfAccess.getKey().name() + ", Response: " + authResponse);
View Full Code Here

      if (mfAccess.getCard().isTrailerBlock(mfAccess.getSector(), mfAccess.getBlock() + currentBlock)) {
        if (!(mfBlock[currentBlock] instanceof TrailerBlock))
          throw new MfException("invalid block for trailer");
      }

      CommandAPDU writeBlock = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_UPDATE_BINARY, 0x00, blockNumber,
          mfBlock[currentBlock].getData());
      ResponseAPDU writeBlockResponse;
      try {
        writeBlockResponse = cardChannel.transmit(writeBlock);
        if (!ApduUtils.isSuccess(writeBlockResponse)) {
View Full Code Here

  }

  @Override
  public Response transmit(Command command) {
    try {
      CommandAPDU commandAPDU = null;
      if (command.isDataOnly()) {
        commandAPDU = new CommandAPDU(0xff, 0, 0, 0, command.getData(), command.getOffset(),
            command.getLength());
      }
      else if (command.hasData()) {
        commandAPDU = new CommandAPDU(Apdu.CLS_PTS, command.getInstruction(), command.getP1(), command.getP2(),
            command.getData());
      }
      else {
        commandAPDU = new CommandAPDU(Apdu.CLS_PTS, command.getInstruction(), command.getP1(), command.getP2(),
            command.getLength());
      }
      if (log.isDebugEnabled())
        log.debug("command: " + NfcUtils.convertBinToASCII(commandAPDU.getBytes()));
      ResponseAPDU responseAPDU = cardChannel.transmit(commandAPDU);
      if (log.isDebugEnabled())
        log.debug("response: " + NfcUtils.convertBinToASCII(responseAPDU.getBytes()));
      return new Response(responseAPDU.getSW1(), responseAPDU.getSW2(), responseAPDU.getData());
    }
View Full Code Here

  }

  @Override
  public Response transmit(Command command) {
    try {
      CommandAPDU commandAPDU = null;
      if (command.isDataOnly()) {
        commandAPDU = new CommandAPDU(0xff, 0, 0, 0, command.getData(), command.getOffset(),
            command.getLength());
      }
      else
        throw new NfcException("Only data mode supported");

      if (log.isDebugEnabled())
        log.debug("command: " + NfcUtils.convertBinToASCII(commandAPDU.getBytes()));

      byte[] transmitControlResponse = card.transmitControlCommand(Acs.IOCTL_SMARTCARD_ACR122_ESCAPE_COMMAND,
          commandAPDU.getBytes());
      ResponseAPDU responseAPDU = new ResponseAPDU(transmitControlResponse);
      if (log.isDebugEnabled())
        log.debug("response: " + NfcUtils.convertBinToASCII(responseAPDU.getBytes()));

      return new Response(responseAPDU.getSW1(), responseAPDU.getSW2(), responseAPDU.getData());
View Full Code Here

        }
    }

    public static void execTestScript(CardChannel channel) throws Exception {

        CommandAPDU select = new CommandAPDU(
            0x00// class
            0xA4// command
            0x04// op1
            0x00// op2
            new byte[] {
                //AID of applet
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x07 //SOC
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x01 //MED
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x02 //DLO
                //(byte)0x32, 0x50, 0x41, 0x59, 0x2E, (byte)0x53, (byte)0x59, (byte)0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31 //PSE
                (byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x10, (byte)0x10 //MASTERCARD
            }
        );
        ResponseAPDU r = channel.transmit(select);
        Messages msg = Messages.findMessage(r);
        if (!msg.isOk()) {
            System.out.println("Cannot select applet");
            return;
        }

        for(int file = 0; file<16; file++) {
            System.out.println("READ FILE:" + file);
            for(int rec = 0; rec<16; rec++) {
                int EF = getFile(file);
                CommandAPDU command = new CommandAPDU(0x00, 0xB2, rec, EF, 256);
                r = channel.transmit(command);
                msg = Messages.findMessage(r);
                if (msg.isOk()) {
                    System.out.println("\nFOUND RECORD OK: file:" + file + "rec:" + rec);
                    //System.out.println("REQUEST: " + Converter.hexDump(command.getBytes()));
View Full Code Here

        commandSelect();
        byte[] command = ByteArrayHelper.concat(COMMAND_HEAD, new BCD(id).getValue());
        command[14] = (byte) (command.length - 15);
        command = ByteArrayHelper.expand(command, 28, (byte) 0xff);
        command = ByteArrayHelper.concat(WRITE_COMMAND, command);
        exec(new CommandAPDU(command), "Write id error");
    }
View Full Code Here

        exec(new CommandAPDU(command), "Write id error");
    }

    public int readID() throws CardException {
        commandSelect();
        ResponseAPDU resp = exec(new CommandAPDU(READ_COMMAND), "Read id error");
        if (Arrays.equals(Arrays.copyOf(COMMAND_HEAD, 14), Arrays.copyOf(resp.getData(), 14))) {
            return new BCD(resp.getData()).toInteger(16);
        } else {
            return 0;
        }
View Full Code Here

TOP

Related Classes of javax.smartcardio.CommandAPDU

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.