Use READ BINARY to read more than 256 bytes

后端 未结 5 2098
小鲜肉
小鲜肉 2020-12-10 15:40

I am trying to read a smartcard(German Gesundheitskarte) using javax.smartcardio

In the definition of the EF \"PD\" its length is specified as 850 bytes. The conten

5条回答
  •  旧巷少年郎
    2020-12-10 16:36

    READ BINARY APDUs allow 2 bytes for the file offset, coded in P1 and P2, and use Le for the length, for READ BINARY the number of bytes in the response. P1 is the high byte, or the most significant byte. The topmost bit of P1 is however reserved to indicate if P1 also contains a short file identifier. It should remain at value 0 if you are already reading a file, leaving you with a maximum offset of 32Ki - 1.

    I can't read the specs that you've linked but let's assume that the READ BINARY APDU on your card works the same way.

    Your command to read the first 256 bytes seems correct, noting that Le==0x00 indicates a read for 256 bytes.

    To read the bytes beginning at offset 256, 512, etc., start incrementing P1, e.g.:

    00 B0 01 00 00
    00 B0 02 00 00
    00 B0 03 00 00
    

    To read 256 bytes beginning at offset 257 (0x101):

    00 B0 01 01 00
    

    Offset 600 (0x258):

    00 B0 02 58 00
    

    In your code, if you're using Java int to store the offset, you'll usually end up incrementing P1 with something like this:

    int offset;
    int P1, P2;
    
    while (continueReading)
    {
        // ...
        P1 = (offset >> 8) & 0xFF;
        P2 = offset & 0x00FF;
        // ...
        // send APDU
    }
    

    How the size of a file is indicated depends on the implementation. Usually you can get the file size from the File Control Information (FCI) structure returned by a SELECT on the EF (00 A4 00 00 02 fileId). The size of the file may however also be embedded in the contents of the file. If possible you should not rely on status words to tell you the size of the file.


    Addition: Le, Ne and odd INS

    It's important that you only increase the offset with the amount of bytes that you actually receive within the response data (RDATA). Note that if P3 = Le that Le encodes Ne, which is the maximum size of the response data. You may receive less than that.

    If the file size is 32Ki or more then you need to use READ BINARY with odd INS (B7) to read the data above 32Ki. In that case the RDATA may also contain overhead. Obviously that - in turn - may influence the offset calculations and the calculations to read to then end of the file.

提交回复
热议问题