3

I want to read fundamental data like name, address and such from a german insurance health card (egK). I got a scm sdi011 card reader and i use c#. I already get the ATR String from card/cardreader but I don't know how to go on. Would be nice if someone can tell me what to do now.

Here is my code:

    WinSCard card = new WinSCard();
    try {
       card.EstablishContext();
       card.ListReaders();
       string szReader = card.ReaderNames[1];
       card.Connect(szReader);
       string ATRStr = card.AtrString;
       ATRBox.Text = ATRStr;
    } catch (WinSCardException exception) {
       richTextBox1.Text = exception.WinSCardFunctionName + " Error 0x" + exception.Status.ToString("X08") + ": " + exception.Message;
    } finally {
       card.Disconnect();
    }
guidot
  • 5,095
  • 2
  • 25
  • 37
Manuel Weitzel
  • 101
  • 1
  • 1
  • 13
  • 1
    That you are *already* able to get the ATR makes me think, that you consider yourself close to the target. Unfortunately this is not the case. Get the eGK specification of the generation you are interested in (in addition to ISO 7816-4), and check where the data is stored (EF.Personendaten). This is a lot of work with significant details unlikely to be found here ready-made. The problem is not *C#* but the command sequence in general with the peculiar smart card philosophy. – guidot Jul 21 '15 at 11:38
  • I already found out, that the data i want is stored in EF.PD (or EF.PSD) - the problem is that i don't have an idea how to read out this store. – Manuel Weitzel Jul 21 '15 at 11:48
  • literature recommendation is Rankl/Effing: Handbuch der Chipkarten and ISO 7816, part 4, especially sections concerning SELECT, READ BINARY. Afterwards you are welcome to return with specific questions. – guidot Jul 21 '15 at 14:12
  • okay thank you a lot. I readed a lot about that stuff now, but what i don't get is how to build sendBuffer to get EF.PD back in readBuffer from Card. I use it like follow: byte[] buffer3 = new byte[1]; buffer3[0] = 0x00; byte[] sendBuffer = buffer3; byte[] responseBuffer = new byte[0x256]; int length = responseBuffer.Length; card.Transmit(sendBuffer, sendBuffer.Length, responseBuffer, ref length); but i get nothing back (just one number and rest are zeros)…. – Manuel Weitzel Jul 22 '15 at 12:13

1 Answers1

1

The rough sequence is as follows (I leave out extended length and access rights):

  • select the appropriate DF
  • select the file EF.PD
  • send a sequence of Read Binary commands with increasing offset encoded in P1/P2

In the following square brackets denote optional parts.

The sendbuffer has to contain the command APDU, i. e. CLA, INS, P1, P2, [LC, data], [LE] Its easiest, to specify LE=0, since then the card sends as much as possible and you have an idea, by which value to increment P1/P2.

The answer contains [data] SW1/SW2, so at least two bytes should be returned. If you get less, this might indicate, that your command was malformed and rejected by winscard directly, without having been sent to the card.

guidot
  • 5,095
  • 2
  • 25
  • 37
  • Hi again, so i now have CLA=0x00; INS=0xB0; INS = 0x00; P1 = 0x00; P2 = 0x00; LC = 0x00; LE = 0x00; and SendBuffer = CLA, INS, P1, P2, LC, LE; but the only "data" i get is this stuff "103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" i readed the ISO 7816-4 pdf and found a lot about CLA and 0x00 seems the best choice ("The command is the last or only command of a chain"). INS i tooked B0 for ReadBinary. The parameter bytes i set 0x00 - is it wrong? lc and le i also set 0x00.... what's my mistake? – Manuel Weitzel Jul 22 '15 at 13:09
  • The card responds 67 00 -> length error (Decimal base is VERY unusual, since ISO only uses hex). This is correct, since specifying LC=0 and no data is illegal, see my optional parentheses. – guidot Jul 22 '15 at 19:16
  • okay now (when i do LC=0x01) i get 6A 86 = Incorrect parameters P1-P2... P1=0xD0; P2=0x01; i set this P1 and P2 because in spec of egk from gematik i found that D0 01 is file identifier for EF.PD - but what is wrong now? :-( – Manuel Weitzel Jul 22 '15 at 19:47
  • @ManuelWeitzel: I have no idea, which command you are talking about. I'm not aware of **any** command, where the file identifier is specified in P1/P2. Apparently more reading of ISO 7816-4 is necessary. – guidot Jul 23 '15 at 07:08