Package javax.smartcardio

Examples of javax.smartcardio.CommandAPDU


  }

  @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


     * Test of fromCommandAPDU method, of class Apdu.
     */
    @Test
    public void testFromCommandAPDU() {
        System.out.println("fromCommandAPDU");
        CommandAPDU apdu = null;
        Apdu instance = new Apdu();
        instance.fromCommandAPDU(apdu);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
View Full Code Here

     */
    @Test
    public void testGetCommandAPDU() {
        System.out.println("getCommandAPDU");
        Apdu instance = new Apdu();
        CommandAPDU expResult = null;
        CommandAPDU result = instance.getCommandAPDU();
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
View Full Code Here

     * Test of CommandAPDU2String method, of class Apdu.
     */
    @Test
    public void testCommandAPDU2String_CommandAPDU() {
        System.out.println("CommandAPDU2String");
        CommandAPDU command = null;
        String expResult = "";
        String result = Apdu.CommandAPDU2String(command);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
View Full Code Here

     * Test of CommandAPDU2String method, of class Apdu.
     */
    @Test
    public void testCommandAPDU2String_3args() {
        System.out.println("CommandAPDU2String");
        CommandAPDU command = null;
        String toInsertAfterNewLine = "";
        int dataLimit = 0;
        String expResult = "";
        String result = Apdu.CommandAPDU2String(command, toInsertAfterNewLine, dataLimit);
        assertEquals(expResult, result);
View Full Code Here

    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while(length > 0)
        {
          int block = Math.min(length, BLOCK_SIZE);
            ResponseAPDU r = channel.transmit(new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, block));
            if(r.getSW() != 0x9000) {
              throw new CardException("Read binary failed: " + RSIDUtils.int2Hex(r.getSW()));
            }

            try {
View Full Code Here

      cmd[4] = LC;
      System.arraycopy(pin, 0, cmd, 5, 8);
      cmd[13] 0x00;
      ResponseAPDU r = null;
    try {
      r = channel.transmit(new CommandAPDU(cmd));
    } catch (CardException e) {
      throw new RSIDCardException("Greska pri autorizaciji korisnika. Pogresna loznika?");
    }
    if(r.getSW() != 0x9000) {
      throw new RSIDCardException("Greska pri autorizaciji korisnika. Pogresna loznika?");
View Full Code Here

      if(terminal==null ||(terminal!=null && terminal.isCardPresent()==false)){
        Utilities.writeToLogs(home, "[E] Card was teared! Card ID:"+cID);
        System.out.println("Card was teared. Terminal will now reboot.");
        System.exit(-1);
      }
      CommandAPDU apduBalance = new CommandAPDU((byte) 0x00, INS_GET_BALANCE,(byte) 0x00, (byte) 0x00,16+8);
      ResponseAPDU res        = ch.transmit(apduBalance);
      double balance          = 0;

      if (res.getSW() !=  0x9000){
        Utilities.writeToLogs(home,"[E] Error during operation. Remove card from terminal !=  0x9000 "+cID);
View Full Code Here

        data=Arrays.copyOf(data, 16);
        byte[] encdata=encryptAES128(data, 0, 16);
        // Expansion of array from 16 bytes to 16+8 bytes(for MAC signature to fit)
        encdata=Arrays.copyOf(encdata, 24);
        signMessage(encdata, 16, encdata, 16);
        CommandAPDU apduInc = new CommandAPDU((byte) 0x00,INS_MODIFY_BALANCE,(byte) 0x00, (byte) 0x00,encdata,16+8);
        ResponseAPDU res           = ch.transmit(apduInc);

        if (res.getSW() !=  0x9000){
          if (res.getSW() == 0x6984){
            Utilities.writeToLogs(home,"[E] Requested topup amount surpasses card limit. Try it with less! MAX 250.00€ "+cID);
View Full Code Here

    byte[] nonceT = new byte[8];
    byte[] nonceT2 = new byte[8];
    sr.nextBytes(nonceT);
    sr.nextBytes(nonceT2);
    //Card handshake INS=0x20 ; response length=2bytes cardID+16bytes nonceR+8bytes signature
    CommandAPDU challenge1 = new CommandAPDU((byte) 0x00, (byte) 0x20,(byte) 0x00, (byte) 0x00,nonceT,18+8);
    if(ch==null) throw new CardException("Card not present!");
    ResponseAPDU res= ch.transmit(challenge1);
    byte[] buff= res.getData();
    if (res.getSW() !=  0x9000){
      Utilities.writeToLogs(home,"[E] Error reading the card. Aborting operation. Error while handshake - step 1 !=0x9000 "+cID);
      System.out.println("[!] Error reading the card. Aborting operation. "); throw new CardException("Error while handshake - step 1");
    }
    else{
      //Step 2: recover data from response APDU = cardID, nonceR
      //----------------------------------------------------------

      // CardID goes in plaintext
      byte[] cardID=Arrays.copyOfRange(buff, 16, 18);
      cID=Integer.toString((int)Utilities.getShort(cardID, 0)); //We put the read CardID in the cID variable for logging purposes

      //We check if the card is blacklisted; if it is, block it && break handshake
      if(isBlacklisted(cardID)){
        Utilities.writeToLogs(home,"[E] Error: Blacklisted card handshake attempt "+cID);
        System.out.println("[!] Error: your card is blocked. Go to the closest CheapKnip Customer Service Point");
        CARD_LEGIT=false;
        CommandAPDU blockCard = new CommandAPDU((byte) 0x00, INS_BLOCK_CARD,(byte) 0x00, (byte) 0x00);
        res= ch.transmit(blockCard);
        System.exit(-1);
      }

      // Compute & init card keys
      if (AES_KEY_CARD==null)
        AES_KEY_CARD=getKeyFromCardID();
      if (sk==null)
        initSignatureKey(cardID);
      if(!verifySignature(buff, 18)){
        Utilities.writeToLogs(home,"[E] Error: check that your card is a CheapKnip card(SIGNATURE CHECK FAILED) "+cID);
        System.out.println("[!] Error: check that your card is a CheapKnip card");
        CARD_LEGIT=false;
        return;
      }

      // Decrypt nonceR --> AES(nonceR)K
      byte[] cryptoBuff = decryptAES128(buff,0,16);
      byte[] nonceR    = Arrays.copyOf(cryptoBuff,8);

      byte[] nonceC_T2=new byte[16];
      //Recover nonceC from Card: nonceR XOR nonceT
      //We put nonceC in the first 8 bytes of the array
      for(int i=0;i<8;i++){
        nonceC_T2[i]=(byte)((nonceR[i])^(nonceT[i]));
      }
      for(int i=8;i<16;i++){
        nonceC_T2[i]=nonceT2[i-8];
      }
      //encrypt properly nonceC_T2

      //Step3: we send AES(nonceC,nonceT2)K back to the card and wait for response AES(nonceR2)K
      //----------------------------------------------------------
      byte[] enc_nonceC_T2=encryptAES128(nonceC_T2, 0, 16);
      //Note in step3: state is a parameter; P1 is 0x10!!
      CommandAPDU challenge2 = new CommandAPDU((byte) 0x00, INS_HANDSHAKE,(byte) 0x10, (byte) 0x00,enc_nonceC_T2,16+8);
      res= ch.transmit(challenge2);
      buff= res.getData();
      if(!verifySignature(buff, 16)){
        Utilities.writeToLogs(home,"[E] Card error. Remove your card & try again(SIGNATURE FAILED) "+cID);
        System.out.println("[!] Card error. Remove your card & try again");
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.