From 49d4ac3b0d33b07e80b440621f402d8d52ea8b50 Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 29 Jun 2020 22:56:52 +0200 Subject: [PATCH] config_passphrase() - implemented using cache.add() --- src/basic_api.cc | 35 +++++++++++++++++++ src/jniutils.hh | 2 ++ src/pEp.yml2 | 5 +-- .../pEp/jniadapter/test/jni111/TestAlice.java | 14 +++++--- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/basic_api.cc b/src/basic_api.cc index 43273f9..3fe1e63 100644 --- a/src/basic_api.cc +++ b/src/basic_api.cc @@ -454,6 +454,41 @@ JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineD return from_string(env, ::per_machine_directory()); } +void logPassphraseCache() { + try { + while(true) { + pEpLog("Cache: '" << cache.latest_passphrase() << "'"); + } + } catch(pEp::PassphraseCache::Empty e) { + pEpLog(e.what()); + } catch(pEp::PassphraseCache::Exhausted ex) { + pEpLog(ex.what()); + } +} + +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase + (JNIEnv * env, + jobject obj, + jbyteArray passphrase) +{ + std::mutex *mutex_local = nullptr; + { + std::lock_guard l(global_mutex); + pEpLog("called with lock_guard"); + mutex_local = get_engine_java_object_mutex(env, obj); + } + std::lock_guard l(*mutex_local); + + logPassphraseCache(); + char *_passphrase = to_string(env, passphrase); + + PEP_STATUS status = ::config_passphrase(session(),cache.add(_passphrase)); + if (status != 0) { + throw_pEp_Exception(env, status); + return; + } + logPassphraseCache(); +} } // extern "C" diff --git a/src/jniutils.hh b/src/jniutils.hh index e8ee16b..4b620d6 100644 --- a/src/jniutils.hh +++ b/src/jniutils.hh @@ -8,6 +8,7 @@ #include #include #include +#include #if 0 // Enable if log needed #include @@ -20,6 +21,7 @@ namespace pEp { namespace JNIAdapter { + static pEp::PassphraseCache cache; // Global mutex needs to be locked in all constructors which insert their own mutex object // into the unordered_map (which is thread safe for read, but not for write) extern std::mutex global_mutex; diff --git a/src/pEp.yml2 b/src/pEp.yml2 index ca4533a..e9d0309 100644 --- a/src/pEp.yml2 +++ b/src/pEp.yml2 @@ -250,10 +250,6 @@ namespace pEp { in CipherSuite suite ); - method config_passphrase( - in string passphrase - ); - method config_passphrase_for_new_keys( in bool enable, in string passphrase @@ -279,6 +275,7 @@ namespace pEp { basic string getCrashdumpLog(int maxlines); basic string getUserDirectory(); basic string getMachineDirectory(); + basic void config_passphrase(string passphrase); }; struct message { diff --git a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java index 2bf50db..74bbf98 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java +++ b/test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java @@ -1,5 +1,7 @@ package foundation.pEp.jniadapter.test.jni111; + import static foundation.pEp.pitytest.TestLogger.*; + import foundation.pEp.pitytest.*; import foundation.pEp.pitytest.utils.TestUtils; import foundation.pEp.jniadapter.test.utils.*; @@ -12,12 +14,16 @@ class TestAlice { TestSuite.getDefault().setVerbose(true); TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); - new TestUnit("config_passphrase",new AdapterBaseTestContext() , ctx -> { - ctx.engine.config_passphrase("SUPERCOMPLICATEDPASSPHRASE"); + AdapterBaseTestContext jni111Ctx = new AdapterBaseTestContext(); + + new TestUnit("config_passphrase", jni111Ctx, ctx -> { + ctx.engine.config_passphrase("chocolate"); + ctx.engine.config_passphrase("Bar"); + ctx.engine.config_passphrase("Foo"); }); - new TestUnit("config_passphrase_for_new_keys",new AdapterBaseTestContext() , ctx -> { - ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE"); + new TestUnit("config_passphrase_for_new_keys", jni111Ctx, ctx -> { + ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE"); }); TestSuite.getDefault().run();