简体   繁体   中英

Communicate with applets using another logical channel

I build a .cap file using the below code from here :

package helloWorldPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class HelloWorldApplet extends Applet {
         private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};

         private static final byte HW_CLA = (byte)0x80;
         private static final byte HW_INS = (byte)0x00;

         public static void install(byte[] bArray, short bOffset, byte bLength) {
             new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
         }

         public void process(APDU apdu) {

             if (selectingApplet()) {
             return;
         }

         byte[] buffer = apdu.getBuffer();
         byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
         byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

         if (CLA != HW_CLA)
        {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

          switch ( INS ) {
             case HW_INS:
               getHelloWorld( apdu );
               break;
            default:
               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
         }
   }

  private void getHelloWorld( APDU apdu)
  {
      byte[] buffer = apdu.getBuffer();
      short length = (short) helloWorld.length;

      Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);

      apdu.setOutgoingAndSend((short)0, length);
  }
}

The above applet uploaded with AID = 0102030405060708091111 .And I successfully sent SELECT command And the 8000000B command,And receive HelloWorld in response.

OpenSC-Tool> opensc-tool -s 00a404000b0102030405060708091111 -s 800000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 80 00 00 00 0B
Received (SW1=0x90, SW2=0x00):
48 65 6C 6C 6F 20 57 6F 72 6C 64 Hello World

As far as I know, The low nibble in CLA of command indicate the number of channel,right? so why when I want to communicate with the applet using logical channel number 1, I receive 6E00 for second APDU command :

OpenSC-Tool> opensc-tool -s 01a404000b0102030405060708091111 -s 810000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 81 00 00 00 0B
Received (SW1=0x6E, SW2=0x00)

And :

OpenSC-Tool> opensc-tool -s 01a404000b0102030405060708091111 -s 800000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 80 00 00 00 0B
Received (SW1=0x6E, SW2=0x00)

Regarding the first command sequence:

<<< 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
>>> 90 00
>>> 81 00 00 00 0B
<<< 6E 00

You receive the status word 0x6E00 because that's what your applet code does:

byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);

if (CLA != HW_CLA)
{
    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}

So if the class byte of the APDU is not equal to 0x80 , which is the case if you use logical channel 1 (as it is 0x81 in that case), you throw an ISOException with error code 0x6E00 ( ISO7816.SW_CLA_NOT_SUPPORTED ).

Regarding the second command sequence:

<<< 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
>>> 90 00
>>> 80 00 00 00 0B
<<< 6E 00

You select the applet on logical channel 1 and then send the command 80 00 00 00 0B on the basic channel. As your applet is not selected on the basic channel, there is no recipient that understands commands in the proprietary class. THus, you receive the status word 0x6E00 ( ISO7816.SW_CLA_NOT_SUPPORTED ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM