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

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

Merge "Move AID validation to CardEmulation."

parents 287554ca b5144116
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public final class AidGroup implements Parcelable {
            throw new IllegalArgumentException("Too many AIDs in AID group.");
        }
        for (String aid : aids) {
            if (!ApduServiceInfo.isValidAid(aid)) {
            if (!CardEmulation.isValidAid(aid)) {
                throw new IllegalArgumentException("AID " + aid + " is not a valid AID.");
            }
        }
+1 −35
Original line number Diff line number Diff line
@@ -218,7 +218,7 @@ public final class ApduServiceInfo implements Parcelable {
                            com.android.internal.R.styleable.AidFilter);
                    String aid = a.getString(com.android.internal.R.styleable.AidFilter_name).
                            toUpperCase();
                    if (isValidAid(aid) && !currentGroup.aids.contains(aid)) {
                    if (CardEmulation.isValidAid(aid) && !currentGroup.aids.contains(aid)) {
                        currentGroup.aids.add(aid);
                    } else {
                        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
    public String toString() {
        StringBuilder out = new StringBuilder("ApduService: ");
+37 −0
Original line number 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() {
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
        sService = adapter.getCardEmulationService();
    }

}