[英]Communicate with applets using another logical channel
I build a .cap file using the below code from here : 我从这里使用以下代码构建一个.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);
}
}
The above applet uploaded with AID = 0102030405060708091111
.And I successfully sent SELECT command And the 8000000B
command,And receive HelloWorld in response. 上面的小程序以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
As far as I know, The low nibble in CLA of command indicate the number of channel,right? 据我所知,命令CLA中的低半字节表示通道数,对吗? so why when I want to communicate with the applet using logical channel number 1, I receive 6E00
for second APDU command : 所以为什么当我想使用逻辑通道号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)
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: 您收到状态字0x6E00
因为这是您的applet代码执行的操作:
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
). 因此,如果APDU的类字节不等于0x80
(如果您使用逻辑通道1(在这种情况下为0x81
))就是这种情况,则会抛出ISOException
错误代码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. 您选择逻辑通道1上的applet,然后在基本通道上发送命令80 00 00 00 0B
。 As your applet is not selected on the basic channel, there is no recipient that understands commands in the proprietary class. 由于没有在基本通道上选择您的applet,因此没有收件人可以理解专有类中的命令。 THus, you receive the status word 0x6E00
( ISO7816.SW_CLA_NOT_SUPPORTED
). 因此,您将收到状态字0x6E00
( ISO7816.SW_CLA_NOT_SUPPORTED
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.