Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read/write SLE4442 memory card with WinSCard API in c#

Tags:

c#

A bit of background information:

Inorder to read/write to SLE4442 memory cards, my app is currently using an Omnikey Cardman 3021 USB card reader, a Sumbsembly Smartcard API (external dll) which is capable of wrapping CT-API calls (directed to omnikey's dll) so that I can read/write the memory card in my c# app. The only problem here is that Omnikey only provides a 32-bit dll of their CT-API. I asked if they are going to produce a 64-bit version, but they couldn't be bothered.

Current situation:

Inorder to make my application 64-bit capable, I must rewrite it using Windows WinSCard API. The problem here is that there are no specific examples on the web how to do it. Also getting hold of working APDU commands is nearly impossible, but I've managed to aquire two slightly different versions that sort of work. I have googled this a hundred times over many months and with what I have managed to gobble together I can finally read the SLE4442 memory card. But for the life of me I can't get writing to work.

The code:

I'm not going to post the entire code into this first post (if need be I can do it later or provide a link to the source code). But I'll outline the basic steps.

1) SCardEstablishContext

2) Get the reader name via SCardListReaders

3) SCardConnect

4) Read entire memory with SCardTransmit and APDU new byte[] { 0xFF, 0xB0, 0, 0, 0 };

5) Verify pin with SCardTransmit and APDU new byte[] { 0xFF, 0x20, 0, 0, 3, 0xFF, 0xFF, 0xFF }; (Note that this does return 0x90;0x00 as a response, which means the verification should have been succesful)

6) Try to write with ScardTransmit and APDU new byte[] { 0xFF, 0xD6, 0, 0, 50, 1 }; (try to write value 1 at memory position 50) - I have also tried using an APDU with the first parameter being 0x00 and/or the second byte being 0xD0. The response has never been 0x90;0x00 so I assume there is an error during writing, but I haven't been able to find any meaning to the error codes returned.

Possible causes:

Because I can read a memory card with the WinSCard API then it must be possible to also write to one (side note - the memory card(s) that I try to write to are in in working condition, I haven't locked them down by failing to verify the PIN 3 times).

1) Maybe the write APDU command is wrong. Could be that the instruction byte (second byte) is incorrect, or the memory location uses some sort of an extended coding scheme.

2) Maybe the verify command didn't actually verify. As in the command itself is fine, which is why 0x90 was returned, but I must call or setup something first.

3) Just a hunch, but I think that this is the real culprit. While googling I did find some vague references to having to call the SCardControl method with parameter IOCTL_SMARTCARD_SET_CARD_TYPE and setting the card type to SLE4442. But again no working examples anywhere and my trial-and-error testing resulted in failures. I got "One or more of the supplied parameters could not be properly interpreted." and some other error messages as well, can't remember what they all were. Assuming the code I copy-pasted from google code has the right descriptions for the error codes.

What I need:

What I need is someone to post or direct me to a site that has full+working code in c# for read/write SLE4442 using WinSCard API and it must work in both 32-bit and 64-bit enviroments. The code doesn't have to be foolproof - eg. handling every possible error situation nicely. I should be able to do that myself. But if it is (including the APDU command result descriptions - eg. 0x90;0x00 is success, but 0x6B;0x4D is... etc...) then all the better.

like image 508
Marko Avatar asked Jul 14 '10 22:07

Marko


People also ask

How does the winscard API work?

The WinSCard API makes direct exchange of data between readers and cards possible by using APDUs. The WinSCard API’s C functions are declared in the header file, winscard.h, and the return codes are declared in winerror.h.

What kind of memory cards are supported by winscard?

It exposes native, Windows smart card API (winscard.dll) through a set of managed, event-driven .NET classes. sample applications and extensive documentation. Contact and contactless memory cards is supported. Contactless: Mifare Classic, Ultralight, DESFire EV1, iCODE, … Support for legacy HID iCLASS cards and iCLASS SEOS cards upon request.

Can I read/write storage cards from within the sample code?

You can easily read/write storage cards from within your C# or VB.NET sample code. It supports I2C and 2-wire protocols. Xchip-based and Aviator-based OMNIKEY readers can be integrated through the same C# API.

What is winscard DLL?

It exposes native, Windows smart card API (winscard.dll) through a set of managed, event-driven .NET classes. sample applications and extensive documentation.


2 Answers

APDU for writing to card, in your example, should be:

FF D6 00 50 01 01
like image 151
Johnny Avatar answered Sep 21 '22 09:09

Johnny


Our Omnikey (3121) terminal writes data on SLE4442/SLE4442 cards perfectly, try this APDU: FF D6 00 04 10 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E AA see also: http://acs.com.hk/drivers/eng/API_ACR122U.pdf, chapter 5.4

like image 20
Albert Mulder Avatar answered Sep 24 '22 09:09

Albert Mulder