Browse Source

fix: usb isd-r aid fallback (#188)

Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/188
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 10 months ago
parent
commit
6c774450ec

+ 25 - 16
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelFactory.kt

@@ -60,11 +60,11 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
                 Log.i(DefaultEuiccChannelManager.TAG, "Is OMAPI channel, setting MSS to 60")
                 Log.i(DefaultEuiccChannelManager.TAG, "Is OMAPI channel, setting MSS to 60")
                 it.lpa.setEs10xMss(60)
                 it.lpa.setEs10xMss(60)
             }
             }
-        } catch (e: IllegalArgumentException) {
+        } catch (_: IllegalArgumentException) {
             // Failed
             // Failed
             Log.w(
             Log.w(
                 DefaultEuiccChannelManager.TAG,
                 DefaultEuiccChannelManager.TAG,
-                "OMAPI APDU interface unavailable for physical slot ${port.card.physicalSlotIndex}."
+                "OMAPI APDU interface unavailable for physical slot ${port.card.physicalSlotIndex} with ISD-R AID: ${isdrAid.encodeHex()}."
             )
             )
         }
         }
 
 
@@ -80,20 +80,29 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha
         if (bulkIn == null || bulkOut == null) return null
         if (bulkIn == null || bulkOut == null) return null
         val conn = usbManager.openDevice(usbDevice) ?: return null
         val conn = usbManager.openDevice(usbDevice) ?: return null
         if (!conn.claimInterface(usbInterface, true)) return null
         if (!conn.claimInterface(usbInterface, true)) return null
-        return EuiccChannelImpl(
-            context.getString(R.string.usb),
-            FakeUiccPortInfoCompat(FakeUiccCardInfoCompat(EuiccChannelManager.USB_CHANNEL_ID)),
-            intrinsicChannelName = usbDevice.productName,
-            UsbApduInterface(
-                conn,
-                bulkIn,
-                bulkOut,
-                context.preferenceRepository.verboseLoggingFlow
-            ),
-            isdrAid,
-            context.preferenceRepository.verboseLoggingFlow,
-            context.preferenceRepository.ignoreTLSCertificateFlow,
-        )
+        try {
+            return EuiccChannelImpl(
+                context.getString(R.string.usb),
+                FakeUiccPortInfoCompat(FakeUiccCardInfoCompat(EuiccChannelManager.USB_CHANNEL_ID)),
+                intrinsicChannelName = usbDevice.productName,
+                UsbApduInterface(
+                    conn,
+                    bulkIn,
+                    bulkOut,
+                    context.preferenceRepository.verboseLoggingFlow
+                ),
+                isdrAid,
+                context.preferenceRepository.verboseLoggingFlow,
+                context.preferenceRepository.ignoreTLSCertificateFlow,
+            )
+        } catch (_: IllegalArgumentException) {
+            // Failed
+            Log.w(
+                DefaultEuiccChannelManager.TAG,
+                "USB APDU interface unavailable for ISD-R AID: ${isdrAid.encodeHex()}."
+            )
+        }
+        return null
     }
     }
 
 
     override fun cleanup() {
     override fun cleanup() {

+ 1 - 5
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -277,11 +277,7 @@ open class DefaultEuiccChannelManager(
                 )
                 )
                 try {
                 try {
                     val channel = tryOpenChannelFirstValidAid {
                     val channel = tryOpenChannelFirstValidAid {
-                        euiccChannelFactory.tryOpenUsbEuiccChannel(
-                            device,
-                            iface,
-                            it
-                        )
+                        euiccChannelFactory.tryOpenUsbEuiccChannel(device, iface, it)
                     }
                     }
                     if (channel != null && channel.lpa.valid) {
                     if (channel != null && channel.lpa.valid) {
                         usbChannel = channel
                         usbChannel = channel

+ 1 - 1
app-common/src/main/java/im/angry/openeuicc/core/usb/UsbApduInterface.kt

@@ -53,7 +53,7 @@ class UsbApduInterface(
             "A9088100820101830107".decodeHex(),
             "A9088100820101830107".decodeHex(),
             le = null,
             le = null,
         )
         )
-        transmitApduByChannel(terminalCapabilities, 0,)
+        transmitApduByChannel(terminalCapabilities, 0)
     }
     }
 
 
     override fun disconnect() {
     override fun disconnect() {

+ 5 - 8
app-common/src/main/java/im/angry/openeuicc/util/StringUtils.kt

@@ -34,15 +34,12 @@ fun formatFreeSpace(size: Int): String =
  * If none is found, at least EUICC_DEFAULT_ISDR_AID is returned
  * If none is found, at least EUICC_DEFAULT_ISDR_AID is returned
  */
  */
 fun parseIsdrAidList(s: String): List<ByteArray> =
 fun parseIsdrAidList(s: String): List<ByteArray> =
-    s.split('\n').map(String::trim).filter { !it.startsWith('#') }
+    s.split('\n')
         .map(String::trim)
         .map(String::trim)
-        .mapNotNull {
-            try {
-                it.decodeHex()
-            } catch (_: IllegalArgumentException) {
-                null
-            }
-        }
+        .filter { !it.startsWith('#') }
+        .map(String::trim)
+        .filter(String::isNotEmpty)
+        .mapNotNull { runCatching(it::decodeHex).getOrNull() }
         .ifEmpty { listOf(EUICC_DEFAULT_ISDR_AID.decodeHex()) }
         .ifEmpty { listOf(EUICC_DEFAULT_ISDR_AID.decodeHex()) }
 
 
 fun String.prettyPrintJson(): String {
 fun String.prettyPrintJson(): String {

+ 2 - 2
app/src/main/java/im/angry/openeuicc/core/PrivilegedEuiccChannelFactory.kt

@@ -44,11 +44,11 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto
                     context.preferenceRepository.verboseLoggingFlow,
                     context.preferenceRepository.verboseLoggingFlow,
                     context.preferenceRepository.ignoreTLSCertificateFlow,
                     context.preferenceRepository.ignoreTLSCertificateFlow,
                 )
                 )
-            } catch (e: IllegalArgumentException) {
+            } catch (_: IllegalArgumentException) {
                 // Failed
                 // Failed
                 Log.w(
                 Log.w(
                     DefaultEuiccChannelManager.TAG,
                     DefaultEuiccChannelManager.TAG,
-                    "TelephonyManager APDU interface unavailable for slot ${port.card.physicalSlotIndex} port ${port.portIndex}, falling back"
+                    "TelephonyManager APDU interface unavailable for slot ${port.card.physicalSlotIndex} port ${port.portIndex} with ISD-R AID: ${isdrAid.encodeHex()}."
                 )
                 )
             }
             }
         }
         }