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

Commit b5144116 authored by Martijn Coenen's avatar Martijn Coenen
Browse files

Move AID validation to CardEmulation.

More logical place since it's a generic utility
function.

Change-Id: I3381f10d6720e415d091dbd475ef0df8733e7e75
parent 69ea13ea
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -51,7 +51,7 @@ public final class AidGroup implements Parcelable {
            throw new IllegalArgumentException("Too many AIDs in AID group.");
            throw new IllegalArgumentException("Too many AIDs in AID group.");
        }
        }
        for (String aid : aids) {
        for (String aid : aids) {
            if (!ApduServiceInfo.isValidAid(aid)) {
            if (!CardEmulation.isValidAid(aid)) {
                throw new IllegalArgumentException("AID " + aid + " is not a valid AID.");
                throw new IllegalArgumentException("AID " + aid + " is not a valid AID.");
            }
            }
        }
        }
+1 −35
Original line number Original line Diff line number Diff line
@@ -218,7 +218,7 @@ public final class ApduServiceInfo implements Parcelable {
                            com.android.internal.R.styleable.AidFilter);
                            com.android.internal.R.styleable.AidFilter);
                    String aid = a.getString(com.android.internal.R.styleable.AidFilter_name).
                    String aid = a.getString(com.android.internal.R.styleable.AidFilter_name).
                            toUpperCase();
                            toUpperCase();
                    if (isValidAid(aid) && !currentGroup.aids.contains(aid)) {
                    if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) {
                        currentGroup.aids.add(aid);
                        currentGroup.aids.add(aid);
                    } else {
                    } else {
                        Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid);
                        Log.e(TAG, "Ignoring invalid or duplicate aid: " + aid);
@@ -351,40 +351,6 @@ 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;

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

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

    @Override
    @Override
    public String toString() {
    public String toString() {
        StringBuilder out = new StringBuilder("ApduService: ");
        StringBuilder out = new StringBuilder("ApduService: ");
+37 −0
Original line number Original line Diff line number Diff line
@@ -571,8 +571,45 @@ public final class CardEmulation {
        }
        }
    }
    }


    /**
     * 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>
     *
     * @hide
     */
    public static boolean isValidAid(String aid) {
        if (aid == null)
            return false;

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

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

    void recoverService() {
    void recoverService() {
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
        sService = adapter.getCardEmulationService();
        sService = adapter.getCardEmulationService();
    }
    }

}
}