Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

java - Use READ BINARY to read more than 256 bytes

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 content should be a gzipped ISO5589-15 encoded XML string as specified here

As CommandAPDU I send

00 B0 00 00 00

to get the first 256 bytes. After sending

00 B0 00 FF 00

I get the next 256 bytes.

But how do I get the rest?

How will I know when the binary data ends?

German Specification Part 1 | German Specification Part 2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...