CharonVpnService provides a function to get trusted certificates via JNI
[strongswan.git] / src / frontends / android / jni / libandroidbridge / charonservice.c
index 75d0da6..ac6df0d 100644 (file)
@@ -21,6 +21,8 @@
 
 #include "charonservice.h"
 #include "android_jni.h"
+#include "kernel/android_ipsec.h"
+#include "kernel/android_net.h"
 
 #include <daemon.h>
 #include <hydra.h>
@@ -88,20 +90,131 @@ 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
  */
 static void charonservice_init(JNIEnv *env, jobject service)
 {
        private_charonservice_t *this;
+       static plugin_feature_t features[] = {
+               PLUGIN_CALLBACK(kernel_net_register, kernel_android_net_create),
+                       PLUGIN_PROVIDE(CUSTOM, "kernel-net"),
+               PLUGIN_CALLBACK(kernel_ipsec_register, kernel_android_ipsec_create),
+                       PLUGIN_PROVIDE(CUSTOM, "kernel-ipsec"),
+       };
 
        INIT(this,
                .public = {
+                       .update_status = _update_status,
+                       .bypass_socket = _bypass_socket,
+                       .get_trusted_certificates = _get_trusted_certificates,
                },
                .vpn_service = (*env)->NewGlobalRef(env, service),
        );
        charonservice = &this->public;
 
+       lib->plugins->add_static_features(lib->plugins, "androidbridge", features,
+                                                                         countof(features), TRUE);
+
        lib->settings->set_int(lib->settings,
                                        "charon.plugins.android_log.loglevel", ANDROID_DEBUG_LEVEL);
 }