簡體   English   中英

使用另一個邏輯通道與小程序進行通信

[英]Communicate with applets using another logical channel

我從這里使用以下代碼構建一個.cap文件:

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);
  }
}

上面的小程序以AID = 0102030405060708091111上傳。我成功發送了SELECT命令和8000000B命令,並收到HelloWorld的響應。

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

據我所知,命令CLA中的低半字節表示通道數,對嗎? 所以為什么當我想使用邏輯通道號1與applet通信時,我收到第二個APDU命令的6E00

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)

和:

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)

關於第一個命令序列:

<<< 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

您收到狀態字0x6E00因為這是您的applet代碼執行的操作:

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

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

因此,如果APDU的類字節不等於0x80 (如果您使用邏輯通道1(在這種情況下為0x81 ))就是這種情況,則會拋出ISOException錯誤代碼0x6E00ISO7816.SW_CLA_NOT_SUPPORTED )。

關於第二個命令序列:

<<< 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

您選擇邏輯通道1上的applet,然后在基本通道上發送命令80 00 00 00 0B 由於沒有在基本通道上選擇您的applet,因此沒有收件人可以理解專有類中的命令。 因此,您將收到狀態字0x6E00ISO7816.SW_CLA_NOT_SUPPORTED )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM