Java Card发送数据而不获取数据APDU [英] Java Card to send data without get data APDU

查看:26
本文介绍了Java Card发送数据而不获取数据APDU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个领域的新手,如果我的问题很幼稚,请原谅我.

I'm new to this field, so forgive me if my question is naive.

我想发行一个带有自动选择小程序的 Java 卡,几乎所有的 APDU 都将在这个小程序中得到处理.我需要这个小程序将数据发送到 CAD,而不使用 Java 卡标准中的常用格式(即不发送 0x61 0xbytesToRead 并等待 0x00 0xc0).
例如,我想发送 0x23 个字节来回答 0xA0A40000027F20 这几乎是一个 SELECT 命令,但第一个字节错误!

I want to issue a Java card which has an auto-select applet and almost all APDUs are going to get handled in this applet. I need this applet to send data to CAD not using the usual format in Java card standard (i.e. without sending 0x61 0xbytesToRead and waiting for 0x00 0xc0).
For example I'd like to send 0x23 bytes in answer to 0xA0A40000027F20 which is almost a SELECT command but with wrong first byte!

那么这有可能吗?如果可能,请告诉我如何操作.

So is this possible to do this? and if it's possible please tell me how.

谢谢.

推荐答案

是的,这是可能的.要实现目标,您需要执行以下两个步骤:

Yes, it is possible. To aim your goal, you have two steps as below :

  1. 您必须将小程序设为默认选择.
  2. 您必须在接收到此命令和/或接收到 SELECT APDU 命令时返回一些数据.

对于第一步,正如它所回答的 这里:

For the first step, as it is answered here:

这取决于卡 - 并非所有卡似乎都支持在安装后将小程序设为默认值.但是您可以使用具有 --make-default 选项的 Java 开源 GlobalPlatform 工具:

It depends on cards - not all of them seem to support making an applet default after installation. But you can use the open source GlobalPlatform tool for Java that has --make-default option:

java -jar gp.jar --make-default A000100201100001

IIRC JCOP 是实际支持它的卡之一.

IIRC JCOP was one of the cards that actually supported it.

对于第二步,正如所回答的 这里 :

And for the second step, as answered here :

我猜你在处理如果选择Applet()然后返回"的良好做法"吗?您需要处理传入的 APDU 而不是简单的返回.可以正常方式返回数据选择,但注意选择成功返回0x9000.

I guess you do the "good practice" of "if selectingApplet() then return" in process? You need to process the incoming APDU instead of simple return. You can return data to select the normal way, but be careful to return 0x9000 if the select was successful.

它必须是这样的:

public void process(APDU apdu)
    { 
       byte[] buf = apdu.getBuffer();
       if (selectingApplet())
          { 
          //send the data in buffer return;
          }
    } 

更新:

要在此答案下方回答您的评论:

To answer your comment below this answer :

我编写了以下程序:

package test;

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

public class Test extends Applet {

    public static final byte[] res = { (byte) 0x00, (byte) 0x00, (byte) 0x3B,
            (byte) 0xAD, (byte) 0x3F, (byte) 0x00, (byte) 0x01, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x16,
            (byte) 0xB3, (byte) 0x03, (byte) 0x06, (byte) 0x04, (byte) 0x00,
            (byte) 0x83, (byte) 0x8A, (byte) 0x83, (byte) 0x8A, (byte) 0x00,
            (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x3B, (byte) 0xAD,
            (byte) 0x00, (byte) 0x00, (byte) 0x3B, (byte) 0xAD, (byte) 0x2F,
            (byte) 0x06, (byte) 0x02 };

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

    public void process(APDU apdu) {
        if (selectingApplet()) {
            return;
        }

        byte[] buf = apdu.getBuffer();

        if (buf[ISO7816.OFFSET_CLA] == (byte)0xA0 && buf[ISO7816.OFFSET_INS] == (byte) 0xA4 && buf[ISO7816.OFFSET_P1] == (byte) 0x00&& buf[ISO7816.OFFSET_P2] == (byte) 0x00 
                && buf[ISO7816.OFFSET_LC] == (byte) 0x02 && buf[ISO7816.OFFSET_LC + 1] == (byte) 0x7F  && buf[ISO7816.OFFSET_LC + 2] == (byte) 0x20) {
            ISOException.throwIt((short) 0x9F23);
        } else if (buf[ISO7816.OFFSET_CLA] == (byte) 0xA0 && buf[ISO7816.OFFSET_INS] == (byte) 0xC0  && buf[ISO7816.OFFSET_P1] == (byte) 0x00 
                && buf[ISO7816.OFFSET_P2] == (byte) 0x00  && buf[ISO7816.OFFSET_P2+1] == (byte) 0x23 ) {
            Util.arrayCopyNonAtomic(res, (short) 0, buf, (short) 0, (short) 35);
            apdu.setOutgoingAndSend((short) 0, (short) 35);

        } else {
            ISOException.throwIt((short) 0x9090);
        }
    }
}

然后我将它安装为默认选择的小程序:

Then I installed it as default selected applet :

CommandLine> gp -install e:\soq.cap --default

CommandLine>

然后我向它发送 APDU 命令:

And then I send the APDU commands to it :

CommandLine> OSC.exe -s A0A40000027F20 -s a0c0000023
Using reader with a card: ACS CCID USB Reader 0
Sending: A0 A4 00 00 02 7F 20
Received (SW1=0x9F, SW2=0x23)
Sending: A0 C0 00 00 23
Received (SW1=0x90, SW2=0x00):
00 00 3B AD 3F 00 01 00 00 00 00 00 16 B3 03 06 ..;.?...........
04 00 83 8A 83 8A 00 03 00 00 3B AD 00 00 3B AD ..........;...;.
2F 06 02                                        /..

它似乎可以如您所愿.

这篇关于Java Card发送数据而不获取数据APDU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆