简体   繁体   English

与Mifare DESFire EV1进行Android NFC通信

[英]Android NFC communication with Mifare DESFire EV1

Using a Nexus 4 and the latest Android API level 18 to communicate with a Mifare DESFire EV1 AES tag is giving me a headache. 使用Nexus 4和最新的Android API级别18与Mifare DESFire EV1 AES标签进行通信让我头疼。 Following the NXP native protocol in order to write and read this type of tag, these steps must be followed: 遵循NXP本机协议以编写和读取此类型的标记,必须遵循以下步骤:

  1. Select application 选择申请
  2. Authenticate 认证
  3. Write or Read 写或读

To do it so, I use Android's IsoDep class which provides access to ISO 14443-4 properties and I/O operations. 为此,我使用Android的IsoDep类,它提供对ISO 14443-4属性和I / O操作的访问。 The very weird thing about it is that once I send the select application native command I get an unexpected response. 关于它的一个非常奇怪的事情是,一旦我发送选择应用程序本机命令,我得到一个意外的响应。 Imagine I have the AID F4013D so I send: 想象一下,我有AID F4013D所以我发送:

-> 5AF4013D
<- 6E00

All possible responses must be one byte length (success 0x00 or error_code) and never two or more. 所有可能的响应必须是一个字节长度(成功0x00或error_code),而不是两个或更多。 Thus, the 0x6E before the success response is absolutely unexpected. 因此,成功响应之前的0x6E绝对是意外的。 It does not happen always, and when it does not and works fine, the select application and authentication processes work fine. 它并不总是发生,当它没有并且工作正常时,选择的应用程序和身份验证过程可以正常工作。 However once authenticated the write command does not have a correct behavior, all write commands finishes with a 0xAF from the PICC instead of a success 0x00 . 但是,一旦经过身份验证,写入命令就没有正确的行为,所有写入命令0xAF以PICC的0xAF结束而不是成功0x00 It seems like the PICC expect some extra data when it should not (I send the correct length payload). 似乎PICC不应该期待一些额外的数据(我发送正确长度的有效载荷)。 If I send any other command I get a 0xCA (Command Aborted) error code. 如果我发送任何其他命令,我会得到一个0xCA (命令中止)错误代码。

-> 5AF4013D
<- 00 /*Success*/
-> AA01
<- AFA8394ED57A5E83106B4EE72FD2BB0CC4
-> AF148F525E1DDE0AD6AB60B4B615552475C91F2E8D89B8523E4465113DD5BD19C6 
<- 0066D255C93F2F492AFE3715C88964F1BD /*Authentication success*/
-> 3D02000000030000222222 /*Write 3 bytes to file nº2*/
<- AF /*Unexpected, 0x00 was expected*/

As it is normal, if I send these type of commands with a personal reader (non Android NFC) it always works fine. 正常情况下,如果我使用个人阅读器(非Android NFC)发送这些类型的命令,它总能正常工作。 It seems that something in the Android NFC API is going strange, when it should just be a raw data transporter which never interprets or modifies data. 似乎Android NFC API中的某些内容很奇怪,它应该只是一个从不解释或修改数据的原始数据传输器。

I have also tried with ISO 7816-4 APDU structure with the same result. 我也尝试了ISO 7816-4 APDU结构,结果相同。 As a curiosity, with a Galaxy Nexus does not happen the select application strange response, but yes the write command one always. 作为一个好奇心,Galaxy Nexus不会发生选择应用程序奇怪的响应,但是一直是写命令。

(1) For the first part concerning the status code 6E00: (1)关于状态代码6E00的第一部分:

6E 00 is not a "strange byte 0x6E + success status code 0x00 ". 6E 00不是“奇怪的字节0x6E +成功状态码0x00 ”。 Instead it is a response APDU status word 6E 00 ("Class not supported"). 相反,它是响应APDU状态字6E 00 (“不支持类”)。 This indicates that there was previous communication with the card using APDU-based access (eg Android itself tried to read the card as Type 4 tag and did not reset the connection afterwards). 这表示先前使用基于APDU的访问与卡进行了通信(例如,Android本身尝试将卡读取为类型4标签,之后没有重置连接)。 Thus, the card will expect all further communication to be in ISO 7816-4 APDUs. 因此,该卡将期望所有进一步的通信都在ISO 7816-4 APDU中。 In that case (ie if you receive an ISO 7816-4 status code like 6E 00 ), you could continue using DESFire APDU wrapped commands by simply wrapping your native commands. 在这种情况下(即如果您收到ISO 7816-4状态代码,如6E 00 ),您可以通过简单地包装本机命令继续使用DESFire APDU包装的命令。

EDIT: In fact, this is somewhat expected behavior on an NFC device. 编辑:事实上,这在NFC设备上有些预期的行为。 The idea is that an NFC device will automatically scan detected tags for NDEF messages. 这个想法是NFC设备将自动扫描检测到的NDEF消息标签。 In the case of a DESFire card, the NFC device will detect the card as potential Type 4 tag. 在DESFire卡的情况下,NFC设备将检测到卡作为潜在的Type 4标签。 Thus the NFC device will send ISO 7816-4 APDUs as it would send to any other Type 4 tag. 因此,NFC设备将发送ISO 7816-4 APDU,因为它将发送到任何其他类型4标签。 Hence, if the NFC device doesn't reset the communication with the tag before handing the detected tag to the app, the app can only communicate using ISO 7816-4 APDUs. 因此,如果NFC设备在将检测到的标签交给应用程序之前没有重置与标签的通信,则应用程序只能使用ISO 7816-4 APDU进行通信。 Note, however, that I would consider it a bug that this happens only for some activations on the same device. 但请注意,我认为这只是在同一设备上进行某些激活时才会发生的错误。 In my opinion, the behavior on one specific device model should be consistent. 在我看来, 一个特定设备模型上的行为应该是一致的。

EDIT: While I would not consider this behavior a bug, it is actually caused by a known bug ( #58773 ) in Android's NFC stack for devices with Broadcom NFC controller. 编辑:虽然我不认为这种行为是一个错误,但它实际上是由Android的NFC堆栈中的已知错误( #58773 )引起的,这些错误适用于具有Broadcom NFC控制器的设备。 On affected devices, the automatic presence check sends ISO 7816-4 APDUs at timed intervals that cause DESFire cards to switch into ISO 7816-4 APDU mode. 在受影响的设备上,自动存在检查以定时间隔发送ISO 7816-4 APDU,导致DESFire卡切换到ISO 7816-4 APDU模式。


(2) For the second part concerning the (unexpected) response code 0xAF: (2)关于(意外)响应代码0xAF的第二部分:

Could it be that your file's communication settings are setup for either "plain communication secured by MACing" or "fully enciphered communication"? 可能是您的文件的通信设置是为“通过MACing保护的普通通信”还是“完全加密的通信”设置的? In that case, simply sending the three data bytes would not be enough. 在这种情况下,仅发送三个数据字节是不够的。 Instead you would need to send either the plain data plus MAC or the padded, CRCed and encyrypted data. 相反,您需要发送普通数据加MAC或填充,CRC化和加密数据。 Hence the 0xAF indicating that the card expects further data. 因此, 0xAF表示该卡期望进一步的数据。

EDIT: So to summarize the comments below. 编辑:所以总结下面的评论。 After sending further bytes (one byte at a time for each received 0xAF status code: AF FF ) it turned out that exactly 8 more bytes were expected by the card. 在发送了更多字节后(每个接收到的0xAF状态代码一次一个字节: AF FF ),结果证明该卡预计还有8个字节。 8 bytes is exactly the size of the CMAC for AES authentication. 8个字节正好是AES身份验证的CMAC大小。 Thus, the communication settings were set to "plain communication secured by MACing". 因此,通信设置被设置为“通过MACing保证的普通通信”。

You can find a good library with some examples here: https://github.com/grundid/nfctools 您可以在这里找到一个包含一些示例的好库: https//github.com/grundid/nfctools

There is also another one with some examples here: https://code.google.com/p/ismb-npp-java/ 另外还有一些例子: https : //code.google.com/p/ismb-npp-java/

I hope this help for your purpose! 我希望这有助于您的目的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM