Let ACardEmulator provide installed AIDs

This commit is contained in:
Jan-Christopher Pien
2016-10-12 00:17:16 +02:00
committed by Frank Morgner
parent dae5622992
commit 3501843159
6 changed files with 118 additions and 45 deletions

View File

@@ -23,12 +23,6 @@
package com.vsmartcard.acardemulator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import java.util.Arrays;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
@@ -37,13 +31,25 @@ import android.content.pm.Signature;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.samsung.android.sdk.SsdkUnsupportedException;
import com.samsung.android.sdk.accessory.*;
import com.samsung.android.sdk.accessory.SA;
import com.samsung.android.sdk.accessory.SAAgent;
import com.samsung.android.sdk.accessory.SAAuthenticationToken;
import com.samsung.android.sdk.accessory.SAPeerAgent;
import com.samsung.android.sdk.accessory.SASocket;
import com.vsmartcard.acardemulator.emulators.EmulatorSingleton;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
public class SmartcardProviderService extends SAAgent {
private static final String TAG = "ACardEmulator";
private static final int ACCESSORY_CHANNEL_ID = 104;
@@ -199,14 +205,34 @@ public class SmartcardProviderService extends SAAgent {
if (mConnectionHandler == null) {
return;
}
final byte[] response = EmulatorSingleton.process(getApplicationContext(), data);
final byte[] sendResponse;
if (data[0] == 'd') {
// APDU is sent
byte[] newData = new byte[data.length - 1];
System.arraycopy(data, 1, newData, 0, data.length - 1);
byte[] response = EmulatorSingleton.process(getApplicationContext(), newData);
sendResponse = new byte[response.length + 1];
sendResponse[0] = 'd';
System.arraycopy(response, 0, sendResponse, 1, response.length);
} else if (data[0] == 'a') {
// request for aids
String[] aidList = EmulatorSingleton.getRegisteredAids(getApplicationContext());
String aidListJoined = TextUtils.join(",", aidList);
sendResponse = new byte[aidListJoined.length() + 1];
sendResponse[0] = 'a';
byte[] aids = aidListJoined.getBytes();
System.arraycopy(aids, 0, sendResponse, 1, aids.length);
} else {
Log.e(TAG, "unknown message from consumer: " + new String(data));
return;
}
new Thread(new Runnable() {
public void run() {
try {
mConnectionHandler.secureSend(ACCESSORY_CHANNEL_ID, response);
} catch (IOException e) {
e.printStackTrace();
}
try {
mConnectionHandler.secureSend(ACCESSORY_CHANNEL_ID, sendResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

View File

@@ -22,13 +22,25 @@ package com.vsmartcard.acardemulator.emulators;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.CardEmulation;
import android.preference.PreferenceManager;
import android.support.annotation.XmlRes;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.vsmartcard.acardemulator.R;
import com.vsmartcard.acardemulator.Util;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EmulatorSingleton {
public static final String TAG = "com.vsmartcard.acardemulator.EmulatorService";
public static final String EXTRA_CAPDU = "MSG_CAPDU";
@@ -90,6 +102,32 @@ public class EmulatorSingleton {
return rapdu;
}
public static String[] getRegisteredAids(Context context) {
List<String> aidList = new ArrayList<>();
XmlResourceParser aidXmlParser = context.getResources().getXml(R.xml.aid_list);
try {
while (aidXmlParser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (aidXmlParser.getEventType() == XmlPullParser.START_TAG) {
if (aidXmlParser.getName().equals("aid-filter")) {
int aid = aidXmlParser.getAttributeResourceValue(0, -1);
if (aid != -1) {
aidList.add(context.getResources().getString(aid));
}
}
}
aidXmlParser.next();
}
aidXmlParser.close();
} catch (XmlPullParserException | IOException e) {
Log.e(TAG.substring(0, 23), "Couldn't parse aid xml list.");
}
return aidList.toArray(new String[0]);
}
public static void deactivate() {
emulator.deactivate();
}