If we run this let’s have a look at the output we get, Print("Returned Status Word 2: " + str(sw2)) Print("Returned Status Word 1: " + str(sw1))
So let’s send that to the card, building on our code from before: #!/usr/bin/env python3ĭata, sw1, sw2 = ansmit(toBytes('00a40004022fe2')) So that’s our APDU encoded, it’s final value will be A0 A4 00 04 02 2FE2 Parameter 1 – Selection Control (Limit search options) Going back to what we learned in the last post about structuring APDUs, let’s create the APDU to SELECT 2FE2. To select it we will need it’s identifier aka File ID (FID), for us the FID of the ICCID EF is 2FE2, so we’ll SELECT file 2FE2. Information about EF-ICCID from TS 102 221 The APDU we’ll send will SELECT (using the INS byte value of A4 as per the above table) the file that contains the ICCID.Įach file on a smart card has been pre-created and in the case of SIM cards at least, is defined in a specification.įor this post we’ll be selecting the EF ICCID, which is defined in TS 102 221. Table of Instruction bytes from TS 102 221 APDU to select EF ICCID We’ll set the instruction byte to A4 to SELECT, and B0 to READ BINARY. This means we’ll need to create two APDUs, one to SELECT the file, and the other to READ BINARY to get the file’s contents. Today we’ll select the EF that contains the ICCID of the card, and then we will read that file’s binary contents. So now we have an object for interfacing with our smart card reader, let’s try sending an APDU to it. So to make life a bit easier we’ll store the list of smart card readers and access the one we want from the list #!/usr/bin/env python3 I want to use my USB SIM reader (The one identified by Identiv SCR35xx USB Smart Card Reader CCID Interface 00 00), so the next step will be to start a connection with this reader, which is the first in the list. So we can see when we run readers() we’re returned a list of readers on the system. (If your device doesn’t show up in this list, double check it’s PCSC compatible, and you can see it in your OS.) Here we can see the two readers that are present on my system (To add some confusion I have two readers connected – One built in Smart Card reader and one USB SIM reader): Running this will output a list of the readers on the system: So let’s get started by getting pyscard to list the readers we have available on our system: #!/usr/bin/env python3
pyscard supports Windows and Linux and you can install it using PIP with: pip install pyscard We’ll use pyscard to interface with the PCSC interface. I’m going to use Python3 to interface with these cards, but keep in mind you can find similar smart card libraries in most common programming languages.Īt this stage as we’re just interfacing with Smart Cards, our library won’t have anything SIM-specific (yet).
Throughout this series we’ll be using a few Python libraries to interface with the Smart Cards, but under the hood all will be using PCSC to communicate. To abstract away some complexity we’re going to use the industry-standard PCSC (PC – Smart Card) interface to communicate with our SIM card. To keep it simple, we’re not going to concern ourselves too much with the physical layer side of things for interfacing with the card, so we’ll start with sending raw APDUs to the cards, and then we’ll use some handy libraries to make life easier. USB SIM / Smart Card reader supports all the standard form factors makes life a lot easier! You may end up fiddling around with the plastic adapters to change the SIM form factor between regular smart card, SIM card (standard), micro and nano. A SIM card – No need to worry about ADM keys or anything fancy, one of those old SIM cards you kept in the draw because you didn’t know what to do with them is fine, or the SIM in our phone if you can find the pokey pin thing.
So in our last post we took a whirlwind tour of what an APDU does, is, and contains. This is part 3 of an n part tutorial series on working with SIM cards.