diff --git a/core/java/android/nfc/cardemulation/AidGroup.java b/core/java/android/nfc/cardemulation/AidGroup.java index cabda5de6a254..34568c258dcbf 100644 --- a/core/java/android/nfc/cardemulation/AidGroup.java +++ b/core/java/android/nfc/cardemulation/AidGroup.java @@ -16,7 +16,7 @@ import android.util.Log; * The AidGroup class represents a group of Application Identifiers (AIDs). * *

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. * *

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 { diff --git a/core/java/android/nfc/cardemulation/ApduServiceInfo.java b/core/java/android/nfc/cardemulation/ApduServiceInfo.java index f379ee8b7f0a5..930cf113c1b7d 100644 --- a/core/java/android/nfc/cardemulation/ApduServiceInfo.java +++ b/core/java/android/nfc/cardemulation/ApduServiceInfo.java @@ -351,20 +351,37 @@ public final class ApduServiceInfo implements Parcelable { } } + /** + * A valid AID according to ISO/IEC 7816-4: + *

+ */ 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; }