]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Expose method to retrieve a user password via JNI
authorTobias Brunner <tobias@strongswan.org>
Fri, 27 Mar 2020 10:10:43 +0000 (11:10 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 30 Oct 2020 14:34:07 +0000 (15:34 +0100)
src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java
src/frontends/android/app/src/main/jni/libandroidbridge/charonservice.c
src/frontends/android/app/src/main/jni/libandroidbridge/charonservice.h

index bee99cf103d4f85d4a36ed55c105998fa86d8476..c46605dfcbd8c9ac38a5455da14de9b573f0e021 100644 (file)
@@ -792,6 +792,22 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                return KeyChain.getPrivateKey(getApplicationContext(), mCurrentUserCertificateAlias);
        }
 
+       /**
+        * Function called via JNI to request a password from the user.
+        *
+        * Note that this method is called from a thread of charon's thread pool.
+        *
+        * @return the password
+        */
+       private String getPassword()
+       {
+               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+               {
+                       return mPasswordPrompt.getPassword();
+               }
+               return null;
+       }
+
        /**
         * Initialization of charon, provided by libandroidbridge.so
         *
index 16e31d84d6b1bb3792122c9eba4faa11495d7ee5..4fe0289bb25e506ef61165b7f466af1a78203699 100644 (file)
@@ -390,6 +390,35 @@ failed:
        return NULL;
 }
 
+METHOD(charonservice_t, get_password, char*,
+       private_charonservice_t *this)
+{
+       JNIEnv *env;
+       jmethodID method_id;
+       jstring jpassword;
+       char *pwd = NULL;
+
+       androidjni_attach_thread(&env);
+
+       method_id = (*env)->GetMethodID(env, android_charonvpnservice_class,
+                                                                       "getPassword", "()Ljava/lang/String;");
+       if (!method_id)
+       {
+               goto failed;
+       }
+       jpassword = (*env)->CallObjectMethod(env, this->vpn_service, method_id);
+       if (androidjni_exception_occurred(env) || !jpassword)
+       {
+               goto failed;
+       }
+       pwd = androidjni_convert_jstring(env, jpassword);
+
+failed:
+       androidjni_exception_occurred(env);
+       androidjni_detach_thread();
+       return pwd;
+}
+
 METHOD(charonservice_t, get_vpnservice_builder, vpnservice_builder_t*,
        private_charonservice_t *this)
 {
@@ -564,6 +593,7 @@ static void charonservice_init(JNIEnv *env, jobject service, jobject builder,
                        .get_trusted_certificates = _get_trusted_certificates,
                        .get_user_certificate = _get_user_certificate,
                        .get_user_key = _get_user_key,
+                       .get_password = _get_password,
                        .get_vpnservice_builder = _get_vpnservice_builder,
                        .get_network_manager = _get_network_manager,
                },
index 12353777beeeedc8a36e46343ecf3db0119d3f5e..d835b50406df1035657317aa356b4ee1a8086796 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2013 Tobias Brunner
+ * Copyright (C) 2012-2020 Tobias Brunner
  * Copyright (C) 2012 Giuliano Grassi
  * Copyright (C) 2012 Ralf Sager
  * HSR Hochschule fuer Technik Rapperswil
@@ -143,6 +143,13 @@ struct charonservice_t {
         */
        private_key_t *(*get_user_key)(charonservice_t *this, public_key_t *pubkey);
 
+       /**
+        * Get a password from the user via JNI
+        *
+        * @return                              allocated password, NULL on failure
+        */
+       char *(*get_password)(charonservice_t *this);
+
        /**
         * Get the current vpnservice_builder_t object
         *