CharonVpnService provides a function to get trusted certificates via JNI
[strongswan.git] / src / frontends / android / jni / libandroidbridge / charonservice.c
index a9a3fe4..ac6df0d 100644 (file)
@@ -90,6 +90,105 @@ static void dbg_android(debug_t group, level_t level, char *fmt, ...)
        }
 }
 
+METHOD(charonservice_t, update_status, bool,
+       private_charonservice_t *this, android_vpn_state_t code)
+{
+       JNIEnv *env;
+       jmethodID method_id;
+       bool success = FALSE;
+
+       androidjni_attach_thread(&env);
+
+       method_id = (*env)->GetMethodID(env, android_charonvpnservice_class,
+                                                                       "updateStatus", "(I)V");
+       if (!method_id)
+       {
+               goto failed;
+       }
+       (*env)->CallVoidMethod(env, this->vpn_service, method_id, (jint)code);
+       success = !androidjni_exception_occurred(env);
+
+failed:
+       androidjni_exception_occurred(env);
+       androidjni_detach_thread();
+       return success;
+}
+
+METHOD(charonservice_t, bypass_socket, bool,
+       private_charonservice_t *this, int fd, int family)
+{
+       JNIEnv *env;
+       jmethodID method_id;
+
+       androidjni_attach_thread(&env);
+
+       method_id = (*env)->GetMethodID(env, android_charonvpnservice_class,
+                                                                       "protect", "(I)Z");
+       if (!method_id)
+       {
+               goto failed;
+       }
+       if (!(*env)->CallBooleanMethod(env, this->vpn_service, method_id, fd))
+       {
+               DBG1(DBG_CFG, "VpnService.protect() failed");
+               goto failed;
+       }
+       androidjni_detach_thread();
+       return TRUE;
+
+failed:
+       androidjni_exception_occurred(env);
+       androidjni_detach_thread();
+       return FALSE;
+}
+
+METHOD(charonservice_t, get_trusted_certificates, linked_list_t*,
+       private_charonservice_t *this)
+{
+       JNIEnv *env;
+       jmethodID method_id;
+       jobjectArray jcerts;
+       linked_list_t *list;
+       jsize i;
+
+       androidjni_attach_thread(&env);
+
+       method_id = (*env)->GetMethodID(env,
+                                               android_charonvpnservice_class,
+                                               "getTrustedCertificates", "(Ljava/lang/String;)[[B");
+       if (!method_id)
+       {
+               goto failed;
+       }
+       jcerts = (*env)->CallObjectMethod(env, this->vpn_service, method_id, NULL);
+       if (!jcerts)
+       {
+               goto failed;
+       }
+       list = linked_list_create();
+       for (i = 0; i < (*env)->GetArrayLength(env, jcerts); ++i)
+       {
+               chunk_t *ca_cert;
+               jbyteArray jcert;
+
+               ca_cert = malloc_thing(chunk_t);
+               list->insert_last(list, ca_cert);
+
+               jcert = (*env)->GetObjectArrayElement(env, jcerts, i);
+               *ca_cert = chunk_alloc((*env)->GetArrayLength(env, jcert));
+               (*env)->GetByteArrayRegion(env, jcert, 0, ca_cert->len, ca_cert->ptr);
+               (*env)->DeleteLocalRef(env, jcert);
+       }
+       (*env)->DeleteLocalRef(env, jcerts);
+       androidjni_detach_thread();
+       return list;
+
+failed:
+       androidjni_exception_occurred(env);
+       androidjni_detach_thread();
+       return NULL;
+}
+
 /**
  * Initialize the charonservice object
  */
@@ -105,6 +204,9 @@ static void charonservice_init(JNIEnv *env, jobject service)
 
        INIT(this,
                .public = {
+                       .update_status = _update_status,
+                       .bypass_socket = _bypass_socket,
+                       .get_trusted_certificates = _get_trusted_certificates,
                },
                .vpn_service = (*env)->NewGlobalRef(env, service),
        );