android – ISO 7816-4 APDU命令选择文件和写入记录
作者:互联网
我试图在Android上使用ISO 7816-4命令在PN532和HCE之间进行通信,我成功地选择了AID(DF),但是当我在该DF下选择EF时(EF不存在,所以我假设选择命令将创建该EF),然后将记录写入该EF,但它显示如下:
inList passive target
write: 4A 1 0
read: 4B 1 1 0 4 60 4 8 23 5A 4D 5 75 80 70 2
write: 40 1 0 A4 4 0 7 F0 1 2 3 4 5 6 0
read: 41 0 48 65 6C 6C 6F 20 44 65 73 6B 74 6F 70 21
Successfully hehe
48 65 6C 6C 6F 20 44 65 73 6B 74 6F 70 21 Hello Desktop!
write: 40 1 0 A4 2 C 1 1 0
read: 41 0 48 65 6C 6C 6F 20 44 65 73 6B 74 6F 70 21
Not enough space
write: 40 1 0 D2 0 0 7 42 41 4F 47 49 41 40 0
read: 41 0 4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 30
Not enough space
write: 40 1 0 D2 0 2 4 44 4F 41 4E 0
read: 41 0 4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 31
Not enough space
write: 40 1 0 B2 0 0 7 42 41 4F 47 49
read: 41 0 4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 32
Not enough space
write: 40 1 0 B2 0 2 4 44 4F 41 4E 0
read: 41 0 4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 33
Not enough space
我不知道我在这里做错了什么?
在Android上,日志是:
04-15 09:36:54.024: D/HostEmulationManager(929): notifyHostEmulationData
04-15 09:36:54.024: W/System.err(17710): [B@41ed5970
04-15 09:36:54.024: I/HCEDEMO(17710): Received: ???????BAOGI
04-15 09:36:54.024: D/HostEmulationManager(929): Sending data
04-15 09:36:54.164: D/BrcmNfcJni(929): RoutingManager::stackCallback: event=0x17
04-15 09:36:54.164: D/BrcmNfcJni(929): RoutingManager::stackCallback: NFA_CE_DATA_EVT; h=0x302; data len=10
04-15 09:36:54.164: D/HostEmulationManager(929): notifyHostEmulationData
04-15 09:36:54.164: W/System.err(17710): [B@41ed5e20
04-15 09:36:54.164: I/HCEDEMO(17710): Received: ?????DOAN??
04-15 09:36:54.174: D/HostEmulationManager(929): Sending data
04-15 09:36:54.885: D/BrcmNfcJni(929): RoutingManager::stackCallback: event=0x19
04-15 09:36:54.885: D/HostEmulationManager(929): notifyHostEmulationDeactivated
04-15 09:36:54.885: I/HCEDEMO(17710): Deactivated: 0
04-15 09:36:54.885: D/HostEmulationManager(929): Unbinding from service ComponentInfo{de.grundid.hcedemo/de.grundid.hcedemo.MyHostApduService}
04-15 09:36:54.895: E/BrcmNfcNfa(929): UICC[0x0] is not activated
它显示它可以接收一些数据,但它错过了我想传输的一些元素,但是,从PN532,当我使用读取记录时,它不显示这些数据?
解决方法:
您的Android HCE模拟智能卡应用程序理解和处理的命令完全取决于您(只要它们被格式化为有效的ISO 7816-4 APDU).
在您的情况下,您的Android HCE服务显然处理SELECT(通过DF名称)APDU,
00 A4 04 00 07 F0010203040506 00
并将此作为回应:
48 65 6C 6C 6F 20 44 65 73 6B 74 6F 70 21 ("Hello Desktop!" when interpreted as ASCII)
(请注意,根据ISO 7816-4,此响应不是有效的响应APDU,因为它缺少状态字.)
您发送的下一个命令是无效的SELECT(通过EF)命令:
00 A4 02 0C 01 01 00
对于该命令,Lc应为2,如果遵循ISO 7816-4,则EF标识符应包含两个字节.为此,您的Android HCE服务再次发送
48 65 6C 6C 6F 20 44 65 73 6B 74 6F 70 21 ("Hello Desktop!" when interpreted as ASCII)
(请注意,根据ISO 7816-4,此响应不是有效的响应APDU,因为它缺少状态字.)
所以我猜,你的Android HCE服务执行如下检查:
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (apdu[1] == (byte)0xA4) {
return "Hello Desktop!".getBytes("US-ASCII");
}
}
您发送的下一个命令是格式错误的WRITE RECORD命令,该命令尝试在经过精选的文件的第一个记录中写入“BAOGIA @”(格式错误,因为WRITE RECORD命令通常没有Le字段):
00 D2 00 00 07 42 41 4F 47 49 41 40 00
作为您的Android HCE服务发送的响应:
4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 30 ("Message from android: 0" when interpreted as ASCII)
(请注意,根据ISO 7816-4,此响应再次不是有效的响应APDU,因为它缺少状态字.)
然后,使用不同的记录有效负载重复WRITE RECORD命令,之后发送两个格式错误的READ RECORD命令:
00 D2 00 02 04 44 4F 41 4E 00
00 B2 00 00 07 42 41 4F 47 49
00 B2 00 02 04 44 4F 41 4E 00
作为您的Android HCE服务发送的响应:
4D 65 73 73 61 67 65 20 66 72 6F 6D 20 61 6E 64 72 6F 69 64 3A 20 xx ("Message from android: X" when interpreted as ASCII)
其中xx似乎是一个ASCII数字X,它为每个接收到的命令递增.
所以我猜,你的Android HCE服务看起来像这样:
private int mCommandCounter = 0;
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
String response;
if (apdu[1] == (byte)0xA4) {
response = "Hello Desktop!";
} else {
response = "Message from android: " + Integer.toString(mCommandCounter);
++mCommandCounter;
}
return response.getBytes("US-ASCII");
}
因此,总结一下,您的Android HCE服务将只理解和处理您(或任何开发它)实现的命令.因此,您可以将命令发送到HCE设备.它背后没有文件系统. ISO 7816-4仅建议智能卡应用程序的文件系统布局.
标签:apdu,android,nfc,contactless-smartcard,hce 来源: https://codeday.me/bug/20190830/1771355.html