Retrieve ICC-ID (printed on SIM card) using SIM APIs

0.00 avg. rating (0% score) - 0 votes
The ICCID (also known as the SIM Serial Number, SSN) is the 19-digit number printed on the plastic housing surrounding the SIM module.


It’s not too difficult to retrieve this number and you can use the SIM Manager API to do it. The 3 functions in the SIM Manager API that we are interested in are SimInitialize, SimDeinitialize, and SimReadRecord.


SimReadRecord is a privileged function. You either need to lower the security configuration on your
device to
Prompt One Tier or lower using the Device Security Manager, or sign your code with a privileged certificate.


The P/Invoke prototypes for these functions are pretty self-explanatory and don’t require any
complex marshaling. Here they are in all their glory:

<DllImport(“cellcore.dll”)> _
Shared Function SimInitialize( _
ByVal dwFlags As Integer, _
ByVal lpfnCallback As IntPtr, _
ByVal dwParam As Integer, _
ByRef lphSim As IntPtr) As Integer
End Function

<DllImport(“cellcore.dll”)> _
Shared Function SimDeinitialize( _
ByVal hSim As IntPtr) As Integer
End Function

<DllImport(“cellcore.dll”)> _
Shared Function SimReadRecord( _
ByVal hSim As IntPtr, _
ByVal dwAddress As Integer, _
ByVal dwRecordType As Integer, _
ByVal dwIndex As Integer, _
ByVal lpData() As Byte, _
ByVal dwBufferSize As Integer, _
ByRef dwSize As Integer) As Integer
End Function

The key to retrieving
the ICCID is the call to
SimReadRecord.
This will allow us to retrieve a elementary file from the SIM’s EEPROM.
SimReadRecord takes an address of the record we want to read. This address is
defined as
EF_ICCID below:


Dim EF_ICCID As Integer = &H2FE2

Dim SIM_RECORDTYPE_TRANSPARENT As Integer = 1

With the infrastructure code out of the way, we’re now ready to execute the sequence to
retrieve the ICCID.


Dim hSim As IntPtr
Dim iccid(9)As Byte
SimInitialize(0, IntPtr.Zero, 0, hSim)
SimReadRecord(hSim, EF_ICCID, SIM_RECORDTYPE_TRANSPARENT, 0, iccid,
iccid.Length, 0)
SimDeinitialize(hSim)
Dim SimSerialNumber AsString = FormatAsSimString(iccid)


FormatAsSimString is a method that I wrote to convert the ICCID byte array into the same format as printed on the SIM card. SimSerialNumber will be formatted string that looks something like this:

111111 22222 3333 4444


If you compare the bytes in the ICCID array to the digits printed on the SIM card, you’ll note they don’t quite match up. This is because the ICCID array is an array of 4-bit unsigned integers in little endian order. For example, if the first 6 digits of your ICCID is 894412, they will appear as 0x98, 0x44, 0x21 in the byte array. The helper method below will convert a pair of 4-bit integers (i.e, a byte) into

a string.

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>