java - 通过安全消息从智能卡中读取信息

标签 java encryption rsa apdu java-security

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

双方的连接是这样完成的:

行车记录仪<--can cable-->设备<---Bluetooth-->Android应用程序<--Socket API-->公司卡服务器<--USB电缆-->公司读卡器<-->公司卡片。

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

我的客户端程序<-->读卡器<-->公司卡

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

我已经从

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

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

公钥认证内容说明如下:

128 Byte Signature + 58 byte Public Key reminder + 8 byte Certification Authorithy Reference = 194 byte

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

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

enter image description here

enter image description here

***代码*

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";

最佳答案

tl;dr

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

My Client Programm <--> card reader <--> company card

如何获取卡号?

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

好的,但具体如何呢?

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

它应该用于第 4 字节(基于 0 的索引)和 16 字节长的公司卡片第 1 代应用程序

来自公司卡的最后一个 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

更多详情

要仅通过客户端应用程序和读卡器读取公司卡的卡号,您需要公钥和私钥对以及证书。

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

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

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

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

关于您下载的证书

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

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

关于java - 通过安全消息从智能卡中读取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117143/

相关文章:

java - 从 Fragment 调用方法

java - Log4j 如何阻止记录器打印到控制台?

python - pyinstaller: AttributeError: 'module' 对象没有属性 'RSA'

c - 如何在 c 中使用 tiny-aes 128 库?

rsa - 如何组合 n 和 e 来创建 RSA 中的公钥?

delphi - Delphi 和 PHP 中的安全 key 对加密解决方案?

java - 在 Java Swing 中创建 "Tips on StartUp"

jvm - sun.management.ThreadImpl#findDeadlockedThreads0 的 OpenJDK 实现在哪里

java - 带密码的 Zip 字节数组

java - 如何使用AES算法、Hadoop和Java加密大文本文件?