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

Mr *_*ker 5 java encryption rsa apdu java-security

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

双方之间的联系如下:

行车记录仪< - 可以连线 - >设备<---蓝牙 - > Android应用程序< - 套接字API - >公司卡服务器< - USB电缆 - >公司读卡器< - >公司卡.

如上所述的通信运行良好,并且app用户正在进行身份验证.现在我试图直接从公司卡上读取一些信息而不使用以下应用程序:

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

在我的客户端程序中,我使用从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字节证书Authorithy Reference = 194字节

在读取文件时,D__TCC40-1.bin它具有194字节长度(公司卡也使用长度为194的认证).外部认证从'84'INS命令开始低音开始.该命令由终端发送到智能卡,以提供8位数的随机数.随后,终端接收随机数并使用一些使用公钥进行加密的加密算法对其进行加密.然后,终端通过算法将带有82INS标签的加密号码发送到智能卡.

现在我正在尝试D__TCC40-1.bin从项目根目录加载下载的证书并使用它来加密我使用GET CHALLANGE 84INS标记获得的8位数随机数,但我不知道如何使用alogrithm 加密它以发送其结果到智能卡.我如何使用Public Key Certificateencyrpte随机数将其发送到智能卡以获得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();

    }

}
Run Code Online (Sandbox Code Playgroud)

输出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
Run Code Online (Sandbox Code Playgroud)

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

        // String masterFile = "00 A4 04 0C 06 FF544143484F";
        // String elementaryFile = "00 A4 02 0C 02 0520";
        // String readBinary = "00 B0 00 01 10";
Run Code Online (Sandbox Code Playgroud)

ibo*_*ali 1

太长了;博士

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

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

如何获取卡号?

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

好的,但是具体如何呢?

在认证过程的最后一个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
Run Code Online (Sandbox Code Playgroud)

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

更多细节

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

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

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

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

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

关于您下载的证书

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

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