Search code examples
uwpsmartcardapdusmartcard-readereid

Reading image from Belgian EID card


When I am reading EF(ID#RN) file using APDU command from card - everything is fine. But when I am trying to read image file EF(ID#Photo), then image is cropped. I can read correctly only first 510-512 bytes.

File read operation is always returning 90-00 - success response. So, I am setting file size limited (to 3064 bytes) to stop read operation.

Have read in google group that it could be related to transactions. But my platform is UWP and transactions are not supported in Windows.Devices.SmartCards namespace. Can be sure that image on card is not broken (as it could be readed by third-party software).

My code is very similar to readBinary method in next one example:

Here it is:

        List<byte> resultBytes = new List<byte>();
        byte offset = 0x00;
        byte size = 0xFF;

        var fileBytes = await GetBytes(card, new byte[] { 0x00, 0xB0, (byte)(offset >> 8), (byte)(offset & 0xFF), size });

        if (fileBytes[0] == 0x6C)
        {
            size = fileBytes[1];
            fileBytes = await GetBytes(card, new byte[] { 0x00, 0xB0, (byte)(offset >> 8), (byte)(offset & 0xFF), size });
        }

        while (fileBytes[fileBytes.Length - 2] == 0x90 && fileBytes[fileBytes.Length - 1] == 0x00)
        {
            if (resultBytes.Count >= maxSize && maxSize > 0) break;

            var newBytes = fileBytes.ToList();
            newBytes.RemoveRange(fileBytes.Length - 2, 2);
            resultBytes.AddRange(newBytes);

            if (maxSize > 0 && resultBytes.Count > maxSize - size) size = (byte)(maxSize - resultBytes.Count);

            offset = (byte)(offset + size);

            fileBytes = await GetBytes(card, new byte[] { 0x00, 0xB0, (byte)(offset >> 8), (byte)(offset & 0xFF), size });

            if (fileBytes[0] == 0x6C)
            {
                size = fileBytes[1];
                fileBytes = await GetBytes(card, new byte[] { 0x00, 0xB0, (byte)(offset >> 8), (byte)(offset & 0xFF), size });
            }
        }

        return resultBytes;

Solution

  • I guess, the line:

    offset =  (byte)(offset + size) 
    

    is the problem. If you cast to byte, how can offset increase?