Blu*_*oke 5 c# apdu smartcard javacard
我正在开发一个 C# Java 卡(智能卡)程序,并且我正在尝试利用github 上的PCSC-sharp库。
这是我的请求的“short / tl;dr”版本: PCSC-sharp 示例涵盖了 Iso7816 Case2Short。有人可以纠正我下面的示例,或者为我提供 Iso7816 Case3Short(命令数据。无响应数据)和 Case4Short(命令数据。预期响应数据)的示例吗?
这是我的请求的“长”版本: 在 Java 卡上,我使用标准和自定义 APDU 命令,并且我可以使用 python 脚本(使用智能卡库)成功调用这些命令。换句话说,我已经证明 Java Card 命令有效。
我现在的目标是使用 PCSC-sharp 执行相同的命令,但当我包含数据时(即 APDU LC > 0),我失败了。
这是 github 上的示例。请注意,我只需更改阅读器名称、指令(选择文件而不是获取挑战)和 Le 即可使用此功能。
var contextFactory = ContextFactory.Instance;
using (var ctx = contextFactory.Establish(SCardScope.System)) {
using (var isoReader = new IsoReader(ctx, "ACME Smartcard reader",
SCardShareMode.Shared, SCardProtocol.Any, false)) {
var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) {
CLA = 0x00, // Class
Instruction = InstructionCode.GetChallenge,
P1 = 0x00, // Parameter 1
P2 = 0x00, // Parameter 2
Le = 0x08 // Expected length of the returned data
};
var response = isoReader.Transmit(apdu);
Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2);
// ..
}
}
Run Code Online (Sandbox Code Playgroud)
我的示例版本中的 APDU 转换为:
Send: 00 A4 00 00 00 00
Resp: 90 00
Run Code Online (Sandbox Code Playgroud)
...这对我有用并且适合 Case2Short (没有数据,没有响应)
我试图重新创建两种不同类型的传输:
Case3Short (命令数据。无响应数据)
Send: 00 A4 04 00 06 01 02 03 04 05 06 00
Resp 90 00
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private bool SendApduCase3()
{
var success = false;
DebugWriteLine("case3 start");
using (var isoReader = new IsoReader(td.context, td.name, SCardShareMode.Shared, SCardProtocol.T1, false))
{
DebugWriteLine("case3 command");
var commandApdu = new CommandApdu(IsoCase.Case3Short, isoReader.ActiveProtocol)
{
CLA = 0x00,
INS = 0xA4,
P1 = 0x04,
P2 = 0x00,
Data = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
Le = 0x00
};
DebugWriteLine("case3 transmit");
var response = isoReader.Transmit(commandApdu);
if ((response.SW1 == 0x90) && (response.SW2 == 0x00)) success = true;
}
DebugWriteLine("case3 " + success.ToString());
return (success);
}
Run Code Online (Sandbox Code Playgroud)
...但它捕获命令并且从未将其发送到传输,而是返回以下错误:
System.ArgumentException:
Iso7816-4 Case3Short does not expect any data fields in its return value and therefore has no bytes for Le
at PCSC.Iso7816.CommandApdu.set_Le(Int32 value)
Run Code Online (Sandbox Code Playgroud)
if= 0x00被删除,则命令已发送,但现在它在传输上挂起,这很奇怪,因为不应该有返回数据。
ReaderPresent: catch case3
PCSC.Exceptions.InsufficientBufferException: The data buffer to receive returned data is too small for the returned data."
Run Code Online (Sandbox Code Playgroud)
Case4Short (命令数据。预期响应数据)
Send: 80 11 00 00 04
Resp: 01 02 03 04 90 00
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private bool SendApduCase4()
{
var success = false;
DebugWriteLine("case4 start");
using (var isoReader = new IsoReader(td.context, td.name, SCardShareMode.Shared, SCardProtocol.T1, false))
{
DebugWriteLine("case4 command");
var commandApdu = new CommandApdu(IsoCase.Case3Short, isoReader.ActiveProtocol)
{
CLA = 0x00,
INS = 0xA4,
P1 = 0x00,
P2 = 0x00,
Data = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
Le = 0x00
};
DebugWriteLine("case4 transmit");
var response = isoReader.Transmit(commandApdu);
if (response.HasData) DebugWriteLine(response.GetData().ToString());
if ((response.SW1 == 0x90) && (response.SW2 == 0x00))
{
if (response.HasData)
{
byte[] expected = new byte[] { 0x51, 0x01, 0x02, 0x03 };
if (response.GetData() == expected) success = true;
}
}
}
DebugWriteLine("case4 " + success.ToString());
return (success);
}
Run Code Online (Sandbox Code Playgroud)
...类似地,它捕获命令但从未将其发送到传输
我在这里做错了什么?
哦,我在搜索中找到的最接近的答案就在这里,但没有涵盖我需要的内容。
情况 3 不需要任何返回数据。如果您不期望数据,则Le不应出现。Le是预期数据量的编码Ne,称为。Le = 00意思是:给我最大256 个字节的返回值:Ne = 256。因此,包含任何Le值都会使其成为 ISO case 4 命令。如果Le不存在则Ne = 0。
Le一般来说,即使设置了,智能卡仍会发送正确的响应。不幸的是,PCSC-sharp 库做出了不同的决定,不允许您发送命令 - 这可能是一件好事,因为智能卡实现仍然可能返回错误。
当您删除时,Le = 00您遇到了另一个问题。按名称选择(INS = A4并P1 = 04使用P2 = 00实际上可能返回文件控制数据,使其成为 ISO case 4 命令。这次该命令被正确构造为 ISO case 3,但卡返回数据,库中没有为此保留缓冲区空间 -收到响应后给你一个错误。
要测试案例 3,您需要使用和INS = A4,这表明不需要响应数据。当然,或者任何其他实际的 ISO case 3 命令,例如按文件 ID 进行选择。P1 = 04P2 = 0C
笔记: