随机数据生成器小程序 returns `0x6F00`

Random data generator applet returns `0x6F00`

我编写了以下程序来生成不同长度的随机数,使用两种不同的算法(ALG_SECURE_RANDOMALG_PSEUDO_RANDOM)。

APDU命令中的

P1P2依次指定算法和随机长度。

CAP 文件成功生成并上传到卡上,但是当我向卡发送 APDU 命令时,我收到 0X6F00 状态字:

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000202
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 02 02
Received (SW1=0x90, SW2=0x00)

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000102
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 01 02
Received (SW1=0x6F, SW2=0x00) 

我的小程序有问题吗?


更新:

根据亲爱的@Vojta 的回答,我替换了

Util.arrayCopyNonAtomic(generatedRandom, (short) 0, buffer, (short) 0,
        (short) ISO7816.OFFSET_P2);
apdu.setOutgoingAndSend((short) 0, (short) ISO7816.OFFSET_P2);

process() 方法中使用以下行:

    Util.arrayCopyNonAtomic(generatedRandom, (short) 0, buffer, (short) 0,
            (short) buffer[ISO7816.OFFSET_P2]);
    apdu.setOutgoingAndSend((short) 0, (short) buffer[ISO7816.OFFSET_P2]);

现在我在 OpenSC-Tool 输出中有一个奇怪的输出:

安全随机生成器:

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000110
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 01 10
Received (SW1=0x90, SW2=0x00):
B8 1F 80 25 A2 8E 25 30 F8 22 F8 40 0F AE B0 6C ...%..%0.".@...l
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00                                  .....

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000110
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 01 10
Received (SW1=0x6F, SW2=0x00)

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000110
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 01 10
Received (SW1=0x90, SW2=0x00):
F6 45 A9 0C 0C 3B 3A 5A 5F DC A8 36 .E...;:Z_..6

伪随机生成器:

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000210
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 02 10
Received (SW1=0x90, SW2=0x00):
37 FD FC 67 EB 9E 21 00 6B E9 44 A7 21 3F 31 9A 7..g..!.k.D.!?1.
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00                            .......

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000210
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 02 10
Received (SW1=0x6F, SW2=0x00)

OSC: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00000210
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 00 00
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 02 10
Received (SW1=0x90, SW2=0x00):
72 FE 48 1B 9A A0 BD 2D DF F9 E7 F8 58 CF B7 C0 r.H....-....X...
00 00 00 00 00 00 00 00 00 00 00                ...........

为什么我对单个命令有不同的输出?

您的代码中有一个小错误。你要

Util.arrayCopyNonAtomic(generatedRandom, (short) 0, buffer, (short) 0,
            (short) buffer[ISO7816.OFFSET_P2]);

而不是

Util.arrayCopyNonAtomic(generatedRandom, (short) 0, buffer, (short) 0,
            (short) ISO7816.OFFSET_P2);

一般规则:始终用 try-catch 块包围 process 方法的内容,并根据异常的类型和原因设置状态词。否则你只会得到 6F00 而你不知道到底发生了什么。如果您遵循此规则,您就会知道 ArrayIndexOutOfBoundsException 被抛出。

更新答案:

奇怪的输出是由于

Util.arrayCopyNonAtomic(generatedRandom, (short) 0, buffer, (short) 0,
            (short) buffer[ISO7816.OFFSET_P2]);
apdu.setOutgoingAndSend((short) 0, (short) buffer[ISO7816.OFFSET_P2]);

用一些随机值覆盖 buffer[ISO7816.OFFSET_P2],然后在下一行使用该值。您应该在 process 方法的开头将 buffer[ISO7816.OFFSET_P2] 存储在 RAM 中:

final byte p2 = buffer[ISO7816.OFFSET_P2];

回复下方评论:

你遇到 P2 >= 0x80 的问题,因为将 byte 转换为 short。不幸的是,JavaCard 处理带符号的字节,这就是为什么 P2 >= 0x80 的长度为负数的原因。您可以通过以下方式轻松避免这种情况:

final short outputLen = (short) (buffer[ISO7816.OFFSET_P2] & 0xFF);

命令不支持 你有错误的指令连接在一起。