Browse Source

lpac-jni: Expose customizable ISDR AIDs from lpac

...so that we could expose an option to the user going forward.
Peter Cai 11 months ago
parent
commit
03bfdf373c

+ 10 - 1
app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelImpl.kt

@@ -15,12 +15,21 @@ class EuiccChannelImpl(
     verboseLoggingFlow: Flow<Boolean>,
     verboseLoggingFlow: Flow<Boolean>,
     ignoreTLSCertificateFlow: Flow<Boolean>
     ignoreTLSCertificateFlow: Flow<Boolean>
 ) : EuiccChannel {
 ) : EuiccChannel {
+    companion object {
+        // TODO: This needs to go somewhere else.
+        val ISDR_AID = "A0000005591010FFFFFFFF8900000100".decodeHex()
+    }
+
     override val slotId = port.card.physicalSlotIndex
     override val slotId = port.card.physicalSlotIndex
     override val logicalSlotId = port.logicalSlotIndex
     override val logicalSlotId = port.logicalSlotIndex
     override val portId = port.portIndex
     override val portId = port.portIndex
 
 
     override val lpa: LocalProfileAssistant =
     override val lpa: LocalProfileAssistant =
-        LocalProfileAssistantImpl(apduInterface, HttpInterfaceImpl(verboseLoggingFlow, ignoreTLSCertificateFlow))
+        LocalProfileAssistantImpl(
+            ISDR_AID,
+            apduInterface,
+            HttpInterfaceImpl(verboseLoggingFlow, ignoreTLSCertificateFlow)
+        )
 
 
     override val atr: ByteArray?
     override val atr: ByteArray?
         get() = (apduInterface as? ApduInterfaceAtrProvider)?.atr
         get() = (apduInterface as? ApduInterfaceAtrProvider)?.atr

+ 5 - 1
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt

@@ -5,7 +5,11 @@ internal object LpacJni {
         System.loadLibrary("lpac-jni")
         System.loadLibrary("lpac-jni")
     }
     }
 
 
-    external fun createContext(apduInterface: ApduInterface, httpInterface: HttpInterface): Long
+    external fun createContext(
+        isdrAid: ByteArray,
+        apduInterface: ApduInterface,
+        httpInterface: HttpInterface
+    ): Long
     external fun destroyContext(handle: Long)
     external fun destroyContext(handle: Long)
 
 
     external fun euiccInit(handle: Long): Int
     external fun euiccInit(handle: Long): Int

+ 2 - 1
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt

@@ -12,6 +12,7 @@ import net.typeblog.lpac_jni.LocalProfileNotification
 import net.typeblog.lpac_jni.ProfileDownloadCallback
 import net.typeblog.lpac_jni.ProfileDownloadCallback
 
 
 class LocalProfileAssistantImpl(
 class LocalProfileAssistantImpl(
+    isdrAid: ByteArray,
     rawApduInterface: ApduInterface,
     rawApduInterface: ApduInterface,
     rawHttpInterface: HttpInterface
     rawHttpInterface: HttpInterface
 ): LocalProfileAssistant {
 ): LocalProfileAssistant {
@@ -76,7 +77,7 @@ class LocalProfileAssistantImpl(
     private val httpInterface = HttpInterfaceWrapper(rawHttpInterface)
     private val httpInterface = HttpInterfaceWrapper(rawHttpInterface)
 
 
     private var finalized = false
     private var finalized = false
-    private var contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)
+    private var contextHandle: Long = LpacJni.createContext(isdrAid, apduInterface, httpInterface)
 
 
     init {
     init {
         if (LpacJni.euiccInit(contextHandle) < 0) {
         if (LpacJni.euiccInit(contextHandle) < 0) {

+ 14 - 0
libs/lpac-jni/src/main/jni/lpac-jni/lpac-jni.c

@@ -37,17 +37,30 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
 
 
 JNIEXPORT jlong JNICALL
 JNIEXPORT jlong JNICALL
 Java_net_typeblog_lpac_1jni_LpacJni_createContext(JNIEnv *env, jobject thiz,
 Java_net_typeblog_lpac_1jni_LpacJni_createContext(JNIEnv *env, jobject thiz,
+                                                  jbyteArray isdr_aid,
                                                   jobject apdu_interface,
                                                   jobject apdu_interface,
                                                   jobject http_interface) {
                                                   jobject http_interface) {
     struct lpac_jni_ctx *jni_ctx = NULL;
     struct lpac_jni_ctx *jni_ctx = NULL;
     struct euicc_ctx *ctx = NULL;
     struct euicc_ctx *ctx = NULL;
+    jbyte *isdr_java = NULL;
+    uint32_t isdr_len = 0;
+    uint8_t *isdr_c = NULL;
 
 
     ctx = calloc(1, sizeof(struct euicc_ctx));
     ctx = calloc(1, sizeof(struct euicc_ctx));
     jni_ctx = calloc(1, sizeof(struct lpac_jni_ctx));
     jni_ctx = calloc(1, sizeof(struct lpac_jni_ctx));
+
+    isdr_java = (*env)->GetByteArrayElements(env, isdr_aid, JNI_FALSE);
+    isdr_len = (*env)->GetArrayLength(env, isdr_aid);
+    isdr_c = calloc(isdr_len, sizeof(uint8_t));
+    memcpy(isdr_c, isdr_java, isdr_len);
+    (*env)->ReleaseByteArrayElements(env, isdr_aid, isdr_java, JNI_ABORT);
+
     ctx->apdu.interface = &lpac_jni_apdu_interface;
     ctx->apdu.interface = &lpac_jni_apdu_interface;
     ctx->http.interface = &lpac_jni_http_interface;
     ctx->http.interface = &lpac_jni_http_interface;
     jni_ctx->apdu_interface = (*env)->NewGlobalRef(env, apdu_interface);
     jni_ctx->apdu_interface = (*env)->NewGlobalRef(env, apdu_interface);
     jni_ctx->http_interface = (*env)->NewGlobalRef(env, http_interface);
     jni_ctx->http_interface = (*env)->NewGlobalRef(env, http_interface);
+    ctx->aid = (const uint8_t *) isdr_c;
+    ctx->aid_len = isdr_len;
     ctx->userdata = (void *) jni_ctx;
     ctx->userdata = (void *) jni_ctx;
     return (jlong) ctx;
     return (jlong) ctx;
 }
 }
@@ -60,6 +73,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(JNIEnv *env, jobject thiz, jl
     (*env)->DeleteGlobalRef(env, jni_ctx->apdu_interface);
     (*env)->DeleteGlobalRef(env, jni_ctx->apdu_interface);
     (*env)->DeleteGlobalRef(env, jni_ctx->http_interface);
     (*env)->DeleteGlobalRef(env, jni_ctx->http_interface);
     free(jni_ctx);
     free(jni_ctx);
+    free((void *) ctx->aid);
     free(ctx);
     free(ctx);
 }
 }