Validate AIDs when registered.

Change-Id: Ib7140c30a8a7587f1809f529d3303e69933bb245
This commit is contained in:
Martijn Coenen
2014-06-27 13:23:41 -07:00
parent 061cfd0049
commit 1bfc3d6249
2 changed files with 29 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ import android.util.Log;
* The AidGroup class represents a group of Application Identifiers (AIDs).
*
* <p>An instance of this object can be used with
* {@link CardEmulation#registerAidGroupForService(android.content.ComponentName, AidGroup)}
* {@link CardEmulation#registerAidsForService(android.content.ComponentName, String, java.util.List)}
* to tell the OS which AIDs are handled by your HCE- or SE-based service.
*
* <p>The format of AIDs is defined in the ISO/IEC 7816-4 specification. This class
@@ -50,6 +50,11 @@ public final class AidGroup implements Parcelable {
if (aids.size() > MAX_NUM_AIDS) {
throw new IllegalArgumentException("Too many AIDs in AID group.");
}
for (String aid : aids) {
if (!ApduServiceInfo.isValidAid(aid)) {
throw new IllegalArgumentException("AID " + aid + " is not a valid AID.");
}
}
if (isValidCategory(category)) {
this.category = category;
} else {

View File

@@ -351,20 +351,37 @@ public final class ApduServiceInfo implements Parcelable {
}
}
/**
* A valid AID according to ISO/IEC 7816-4:
* <ul>
* <li>Has >= 5 bytes and <=16 bytes (>=10 hex chars and <= 32 hex chars)
* <li>Consist of only hex characters
* <li>Additionally, we allow an asterisk at the end, to indicate
* a prefix
* </ul>
*/
static boolean isValidAid(String aid) {
if (aid == null)
return false;
int aidLength = aid.length();
if (aidLength == 0 || (aidLength % 2) != 0) {
Log.e(TAG, "AID " + aid + " is not correctly formatted.");
// If a prefix AID, the total length must be odd (even # of AID chars + '*')
if (aid.endsWith("*") && ((aid.length() % 2) == 0)) {
Log.e(TAG, "AID " + aid + " is not a valid AID.");
return false;
}
// Minimum AID length is 5 bytes, 10 hex chars
if (aidLength < 10) {
Log.e(TAG, "AID " + aid + " is shorter than 5 bytes.");
// If not a prefix AID, the total length must be even (even # of AID chars)
if (!aid.endsWith("*") && ((aid.length() % 2) != 0)) {
Log.e(TAG, "AID " + aid + " is not a valid AID.");
return false;
}
// Verify hex characters
if (!aid.matches("[0-9A-Fa-f]{10,32}\\*?")) {
Log.e(TAG, "AID " + aid + " is not a valid AID.");
return false;
}
return true;
}