Skip to content

Instantly share code, notes, and snippets.

@mrts
Created August 23, 2018 11:56
Show Gist options
  • Save mrts/327a8cceafc3823cb0f84336f90c6e6a to your computer and use it in GitHub Desktop.
Save mrts/327a8cceafc3823cb0f84336f90c6e6a to your computer and use it in GitHub Desktop.
Read cardholder name from Estonian ID card using the Python smart card framework pyscard
# Prerequisites:
# sudo apt install build-essential swig python-dev
# virtualenv venv
# source venv/bin/activate
# pip install pyscard
from __future__ import print_function
from smartcard.System import readers
SELECT_EE_FOLDER = [0x00, 0xA4, 0x01, 0x0C, 0x02, 0xEE, 0xEE]
SELECT_PERSONAL_DATA_FILE = [0x00, 0xA4, 0x02, 0x04, 0x02, 0x50, 0x44]
READ_FIRST_NAME_RECORD = [0x00, 0xB2, 0x02, 0x04]
READ_FIRST_NAME_APDUS = (
SELECT_EE_FOLDER,
SELECT_PERSONAL_DATA_FILE,
READ_FIRST_NAME_RECORD
)
# select OMNIKEY reader
reader = next((reader for reader in readers()
if reader.name.startswith('OMNIKEY')), None)
# establish connection to card
connection = reader.createConnection()
connection.connect()
# exchange APDUs to read first name from card
for apdu in READ_FIRST_NAME_APDUS:
response, sw1, sw2 = connection.transmit(apdu)
# print out first name stored in last response
print(''.join(chr(x) for x in response))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment