Home

Awesome

Smartcard Android Emulator Extension

Download and run the emulator

mkdir pcsc-emulator && cd pcsc-emulator
wget https://github.com/bertrandmartel/pcsc-android-emulator/releases/download/v1.0/emulator.tar.gz
tar -xvzf emulator.tar.gz && rm emulator.tar.gz
export ANDROID_SDK_ROOT=~/Android/Sdk #change this
export LD_LIBRARY_PATH=$PWD/pcsc-lite:$LD_LIBRARY_PATH
./emulator/emulator @smartcard -verbose -system system.img -pcsc

Build instructions

Instructions for building and using the emulator extension for smartcard from seek-for-android instructions

Init

AOSP_DIR=seek-for-android
PCSC_DIR=~/pcsc-lite

Build AOSP

building & patching AOSP

mkdir $AOSP_DIR && cd $AOSP_DIR
repo init -u https://github.com/seek-for-android/platform_manifest.git -b scapi-4.0.0
repo sync
. build/envsetup.sh 
lunch aosp_x86-eng
make update-api
make -j32

Build pcsc

mkdir -p $PCSC_DIR && cd $PCSC_DIR
wget https://alioth.debian.org/frs/download.php/file/4225/pcsc-lite-1.8.22.tar.bz2
current=$(pwd)
mkdir -p 32lib
tar -C 32lib -xvjf pcsc-lite-1.8.22.tar.bz2
cd 32lib/pcsc-lite-1.8.22
sudo apt-get install libudev-dev:i386
CFLAGS="-m32" LDFLAGS="-m32" ./configure; make

cd $current
mkdir -p 64lib
tar -C 64lib -xvjf pcsc-lite-1.8.22.tar.bz2
cd 64lib/pcsc-lite-1.8.22
CFLAGS="-m64" LDFLAGS="-m64" ./configure; make

Build emulator

build emulator with pcsc support

cd $AOSP_DIR
cd external/qemu
export PCSC_INCPATH=$PCSC_DIR/32lib/pcsc-lite-1.8.22/src/PCSC
export PCSC32_LIBPATH=$PCSC_DIR/32lib/pcsc-lite-1.8.22/src/.libs/
export PCSC64_LIBPATH=$PCSC_DIR/64lib/pcsc-lite-1.8.22/src/.libs/
./android-configure.sh
make -j32

run emulator

Prior to the following step, assure to create an avd in Android Studio with system-images 21 x86

In my case the avd is named "smartcard" :

cd $AOSP_DIR
export ANDROID_SDK_ROOT=/path/to/Sdk
./external/qemu/objs/emulator @smartcard -verbose -pcsc -system out/target/product/generic_x86/system.img

If you have to name the reader :

./external/qemu/objs/emulator @smartcard -verbose -pcsc "Gemalto Prox Dual USB PC Link Reader [Prox-DU Contact_10800061] 01 00" -system out/target/product/generic_x86/system.img

Disable SIM pin code check

I had some troubles with the SIM pin code keyguard that won't return successfull pincode. Instead it will return "SIM operation failed" and exhaust the pin code retry.

To disable the pin code check (but still have the keyguard view), apply the patch :

cd $AOSP_DIR
cd frameworks/base
git apply /path/to/0001-disable-sim-pin-code-check-in-keyguard.patch

Troubleshoot

no APDU access allowed

09-10 00:48:49.396 2116-2116/fr.bmartel.smartcardapi E/MainActivity: Error occured:
     java.lang.SecurityException: Access Control Enforcer: no APDU access allowed!
         at org.simalliance.openmobileapi.service.SmartcardError.throwException(SmartcardError.java:134)
         at org.simalliance.openmobileapi.Session.openLogicalChannel(Session.java:339)
         at org.simalliance.openmobileapi.Session.openLogicalChannel(Session.java:378)

This error occurs because the Access control enforcer refuses to send apdu for your app. You have to add an access rule to authorize your app to send apdu to a specific applet. A rule defines :

To list, add and delete rules, clone this fork of GlobalPlatformPro on the access-control branch :

git git@github.com:bertrandmartel/GlobalPlatformPro.git
cd GlobalPlatformPro
git checkout access-control
ant -f build.xml
java -jar gp.jar -acr-list
java -jar gp.jar -acr-add -app D2760001180002FF49502589C0019B01 -hash 1FA8CC6CE448894C7011E23BCF56DB9BD9097432 -rule 01

Note that this will issue an install for personalization which requires authentication (eg setting the right keys)

Note that you will have to restart the emulator to apply those rule (emulator will retrieve all rules at boot)

iccOpenLogicalChannel failed

09-10 01:01:57.837 2121-2121/fr.bmartel.smartcardapi E/MainActivity: Error occured:
     java.io.IOException: iccOpenLogicalChannel failed

smartcard has been disconnected

Secure Element is not presented

09-10 01:05:31.855 2098-2098/fr.bmartel.smartcardapi E/MainActivity: Error occured:
	java.io.IOException: Secure Element is not presented.

No SIM inserted