Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e65c6978 authored by Martijn Coenen's avatar Martijn Coenen Committed by Android (Google) Code Review
Browse files

Merge "Validate AIDs when registered."

parents 1e98a77f 1bfc3d62
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -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 {
+23 −6
Original line number Diff line number Diff line
@@ -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;
    }