【问题标题】:Reading information from the smart card by secure messaging通过安全消息从智能卡中读取信息
【发布时间】:2017-12-20 09:23:57
【问题描述】:

我有行驶记录仪公司卡,用于在允许客户通过远程下载下载行驶记录仪数据之前对客户进行身份验证。使用的 APDU 命令 在我下面的代码中是在行驶记录仪和公司卡之间成功验证的命令。

双方的连接如下:

Tachograph device Android app Company Card Server company card reader company卡。

如上所述的通信运行良好,应用用户正在通过身份验证。现在我正在尝试在没有应用程序的情况下直接从公司卡中读取一些信息,如下所示:

我的客户程序 读卡器 公司卡

在我的客户端程序中,我使用从 Android 应用程序发送到公司卡的相同 APDU 命令。目前,我面临外部身份验证问题 带有 INS 标签“82”。我收到错误66 88,这意味着wrong certification

我已经从

下载了一个认证文件D__TCC40-1.bin

https://dtc.jrc.ec.europa.eu/dtc_public_key_certificates.php

公钥认证内容描述如下:

128 字节签名 + 58 字节公钥提醒 + 8 字节证书颁发机构参考 = 194 字节

在读取文件D__TCC40-1.bin 时,它的长度为194 字节(公司卡也使用长度为194 的证明)。外部身份验证基本上从“84” INS 命令开始。 该指令由终端发送到智能卡,下发8位随机数。随后,终端接收到随机数并用一些加密 使用公钥进行加密的加密算法。然后,终端通过算法将加密后的数字发送到带有82INS标签的智能卡。

现在我正在尝试从项目根目录加载下载的证书D__TCC40-1.bin 并使用它来加密我得到的 8 位随机数 GET CHALLANGE 84 INS 标记,但我不知道如何使用算法对其进行加密以将其结果发送到智能卡。 如何使用Public Key Certificate 加密随机数以将其发送到智能卡以获取90 00 作为响应?目前,正如我之前提到的,我收到了 66 88 作为回复。

***代码*

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
import javax.xml.bind.DatatypeConverter;


public class CardIdTest {

    static CardChannel channel = null;
    static byte[] signature = null;


    public static void main(String[] args) {

        try {

            try {
                FileInputStream ecPubKeyFIS = new FileInputStream("D__TCC40-1.bin");

                try {
                    int certificateLength = ecPubKeyFIS.available();
                    byte[] certificate = new byte[certificateLength];
                    ecPubKeyFIS.read(certificate);

                    CardIdTest.signature = new byte[128];
                    System.arraycopy(certificate, 0, CardIdTest.signature, 0, 128);
                    int sigLength = CardIdTest.signature.length;

                    byte[] publicKeyReminder = new byte[58];
                    System.arraycopy(certificate, 128, publicKeyReminder, 0, 58);
                    int PKLength = publicKeyReminder.length;

                    byte[] certificationAuthorithyReference = new byte[8];
                    System.arraycopy(certificate, 186, certificationAuthorithyReference, 0, 8);
                    int referenceLength = certificationAuthorithyReference.length;
                    System.out.println("End");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            TerminalFactory factory = TerminalFactory.getDefault();
            List<CardTerminal> terminals = factory.terminals().list();
            CardTerminal terminal = terminals.get(0);
            Card card = terminal.connect("T=1");
            System.out.println("Terminals list: " + terminals);
            ATR atr = card.getATR();
            byte[] atrArray = atr.getBytes();
            String atrHex = CardIdTest.byteArrayToHexString(atrArray);
            System.out.println("ATR: " + atrHex);
            CardIdTest.channel = card.getBasicChannel();

            String command1 = "00 a4 02 0c 02 00 02"; // select EF_ICC file.
            CardIdTest.execute(command1, 1);
            String command2 = "00 b0 00 00 09";  // Read binary
            String cardExtendedSerialNumberTemp = CardIdTest.execute(command2, 2);
            String cardExtendedSerialNumber = cardExtendedSerialNumberTemp.substring(4);
            String command3 = "00 a4 04 0c 06 ff 54 41 43 48 4f"; // select DF file or master file.
            CardIdTest.execute(command3, 3);
            String command4 = "00 a4 02 0c 02 05 01"; // select 05 01 elementary file.
            CardIdTest.execute(command4, 4);
            String command5 = "00 b0 00 00 01"; // read the binary byte.
            CardIdTest.execute(command5, 5);
            String command6 = "00 22 c1 b6 0a 83 08 00 00 00 05 09 02 ff a1"; // issse security managment environment.
            CardIdTest.execute(command6, 6);
            String command8 = "00 88 00 00 10 e9 96 79 ec 74 27 e6 50 00 00 00 05 09 02 ff a1 80"; // internal authentication.
            CardIdTest.execute(command8, 8);
            String command9 = "00 84 00 00 08"; // Get Challange / 8 digits random number
            String exteranlAuthenticationChallange = CardIdTest.execute(command9, 9);
            String digitalSignature = CardIdTest.byteArrayToHexString(CardIdTest.signature);
            String command10 = "00 82 00 00 80 " + digitalSignature; // eternal authentication.
            CardIdTest.execute(command10, 10);
            String command11 = "00 a4 02 0c 02 05 01"; // select 05 01 file
            String command12 = "0c b0 00 00 09 97 01 01 8e 04 1e ee 49 a1 00"; // read the birnay of the selected file.

            card.disconnect(true); // reset

        } catch (CardException e) {

            e.printStackTrace();
        }

    }


    private static String execute(String commandWithSpace, int number) throws CardException {
        String commandWithoutSpace = commandWithSpace.replace(" ", "");
        byte[] apdu = DatatypeConverter.parseHexBinary(commandWithoutSpace);
        CommandAPDU command = new CommandAPDU(apdu);
        ResponseAPDU responseAPDU = CardIdTest.channel.transmit(command);
        byte[] reponseData = responseAPDU.getData();
        String response = responseAPDU.toString();
        if (reponseData.length > 0) {
            String msg = new String(reponseData);
            String dataHex = CardIdTest.byteArrayToHexString(reponseData);
            System.out.println("command       (" + number + ")   (*" + apdu.length + "): " + commandWithSpace);

            System.out.println("response with data:  ( #" + msg.length() + ") :" + dataHex);
            System.out.println("msg: " + msg);
            return dataHex;

        } else {
            byte[] bytes = responseAPDU.getBytes();
            String responseHex = CardIdTest.byteArrayToHexString(bytes);
            System.out.println("command        (" + number + ")   (*" + apdu.length + "): " + commandWithSpace);
            System.out.println("response without data :" + responseHex);
            return responseHex;
        }
    }


    public static String byteArrayToHexString(byte[] byteArray) {
        StringBuilder sb = new StringBuilder();

        for (byte b : byteArray) {
            sb.append(String.format(" %02x", b));
        }
        return sb.toString();

    }


    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String byteArrayToHex(byte[] byteArray) {
        StringBuilder sb = new StringBuilder();
        for (byte b : byteArray) {
            sb.append(String.format(" %02x", b));
        }
        return sb.toString();

    }

}

66 88 错误的输出

Terminals list: [PC/SC terminal Generic Smart Card Reader Interface 0]
ATR:  3b 9f 96 c0 0a 31 fe 45 43 54 31 69 0b 01 00 01 00 00 00 00 00 00 00 0d
command        (1)   (*7): 00 a4 02 0c 02 00 02
response without data : 90 00
command       (2)   (*5): 00 b0 00 00 09
response with data:  ( #9) : 00 00 01 98 fa 03 16 14 ad
command        (3)   (*11): 00 a4 04 0c 06 ff 54 41 43 48 4f
response without data : 90 00
command        (4)   (*7): 00 a4 02 0c 02 05 01
response without data : 90 00
command       (5)   (*5): 00 b0 00 00 01
response with data:  ( #1) : 04
command        (6)   (*15): 00 22 c1 b6 0a 83 08 00 00 00 05 09 02 ff a1
response without data : 90 00
command       (8)   (*22): 00 88 00 00 10 e9 96 79 ec 74 27 e6 50 00 00 00 05 09 02 ff a1 80
response with data:  ( #128) : 7f 96 43 f2 ee d9 44 34 2d 09 b6 c3 47 a0 08 28 6d 3f 3d 30 e8 3d 82 fb 21 e0 5f 7a 3e bd 99 f9 ba 4c 2c c5 56 df fc cc b1 7e 66 bc 9a 26 b0 0e 53 52 fe d7 51 a3 84 75 f6 7d 3a 24 48 d1 a4 fe 8d 82 0e a8 bb 10 2d f2 51 8d 0c 6c 96 0f 0b 2a e3 a7 ce 5c d9 27 91 8f 7c 2b 21 1c f5 fa 65 cd 5f 5c e6 6f 1c a5 ad 27 4a 57 c3 16 76 0b 06 e1 d8 fc af 20 ce 48 61 1d 53 48 f9 78 5b b9 3a c1
command       (9)   (*5): 00 84 00 00 08
response with data:  ( #8) : 06 ba 52 a6 34 7a fe 30
command        (10)   (*133): 00 82 00 00 80  9b 33 b2 68 9a 71 93 3b 50 c2 4d 65 95 e7 84 59 db 40 77 e1 40 a1 b1 a3 b9 8c a8 a1 fb 36 ac b5 a6 2b 60 b3 63 f8 dd 77 de ad a1 4d ab 39 ab cc c8 79 51 aa d1 7a 97 bd 16 c3 d8 2d dd 74 cf 98 47 89 b9 36 d0 02 43 29 f6 69 2d a5 1f f2 27 89 ad fb 81 3d 47 93 08 e4 56 7c 4f 0d a1 b8 07 4e cb 9b 18 80 73 33 75 2b c9 dc de c4 ce 96 71 07 d8 5f 6d 20 f6 a3 09 88 87 8c 69 ec 6f de 51 ca
response without data : 66 88

顺便说一句,读取驱动卡信息的代码在没有安全消息的情况下工作,如下所示:

        // String masterFile = "00 A4 04 0C 06 FF544143484F";
        // String elementaryFile = "00 A4 02 0C 02 0520";
        // String readBinary = "00 B0 00 01 10";

【问题讨论】:

  • 你确定你得到的是6688而不是6988吗?也许您指的是“INS”(“INstruction”的缩写)而不是“INC”(通常是“INCrease”的缩写)?
  • 是的,我收到了6688。是的,我的意思是 INS 而不是 INC
  • 你有没有让它工作?我也面临同样的问题

标签: java encryption rsa apdu java-security


【解决方案1】:

tl;博士

您不能只读取公司卡的卡号,因为您永远无法通过这种方式对卡进行身份验证:

我的客户程序 读卡器 公司卡

如何获取卡号?

利用远程下载会话的身份验证过程来获取卡号。

好的,但具体如何?

在身份验证过程的最后一个 APDU 中,公司卡将带有请求数据的卡号发送到行驶记录仪,只需从中提取号码。

它应该用于公司卡应用程序生成 1,字节数为 4(基于 0 的索引),长度为 16 个字节。

公司卡的最后一个 APDU 示例(字节已更改,因为这张卡属于我的雇主):

81 81 8B 12 53 32 30 30 30 34 30 30 30 34 36 33 34 30 30 45 01 43 6F 6E 74
69 6E 65 6E 74 61 6C 20 28 64 65 29 20 37 32 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 55 67 AC 00 55 67 AC 00 65 B2 D5 80 01 43 6F 6D 70 61 6E
79 20 37 32 20 20 20 20 20 20 20 20 20 20 20 60 20 20 20 20 20 20 20 20 20
20 20 20 20 01 61 64 64 72 65 74 73 20 37 32 20 20 24 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 40 20 20 26 20 20 20 63 65 8E 04 05 C9 D2 2F 90 00

本例中的卡号为:53 32 30 30 30 34 30 30 30 34 36 33 34 30 30 45,转换为ASCII码为:S20004000463400E

更多详情

为了让您仅使用客户端应用程序和读卡器读取公司卡的卡号,您需要一个公钥和私钥对以及一个证书。

要生成它们,您可以从欧洲委员会数字行驶记录仪联合研究中心下载智能行驶记录仪密钥和证书生成工具here

使用此工具,您可以生成一个密钥对,并使用它们为您的应用程序生成和签署证书。

然后您需要让您的会员国使用他们的私钥签署您的证书。

不幸的是,这不会发生,我与 Kraftfahrt-Bundesamt(KBA,或德国联邦机动车辆和驾驶员局)负责证书签署的人交谈,我被告知 欧洲委员会数字行驶记录仪联合研究中心 出于明显的安全原因,不会为软件签署证书,只签署诸如行驶记录仪或Download key device 之类的设备和公司卡之类的卡。

关于您下载的证书

此证书对您没有帮助,因为您没有与之关联的私钥。

您需要与证书关联的外部身份验证的私钥,其中包含用于内部身份验证的公钥。

【讨论】:

  • 卡的有效期如何?有没有办法把它弄出来?
  • @Piotr 是的。您可以在 EF Identification (0520h) 下找到它,然后命名为 cardExpiryDate(起始字节 61,长度为 4 个字节)。我从未尝试过将其取出,但根据文档,这是可能的。
  • 我成功了。事实证明,您可以在远程身份验证完成后立即阅读EF Identification。就是这样。
猜你喜欢
  • 2023-03-21
  • 2011-10-12
  • 1970-01-01
  • 2018-06-28
  • 2011-09-24
  • 2021-11-07
  • 1970-01-01
  • 2017-04-19
  • 2012-07-15
相关资源
最近更新 更多