From 49d4ac3b0d33b07e80b440621f402d8d52ea8b50 Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 29 Jun 2020 22:56:52 +0200 Subject: [PATCH 1/3] 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(); From e84feed11ec129a17b59a2c202049c0cd463318d Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 30 Jun 2020 02:31:31 +0200 Subject: [PATCH 2/3] call API functions like encrypt_message() or decrypt_message() using cache.api() --- src/gen_cpp_Engine.ysl2 | 18 +++++++++++++++++- src/pEp.yml2 | 17 +++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/gen_cpp_Engine.ysl2 b/src/gen_cpp_Engine.ysl2 index eef8111..9f50823 100644 --- a/src/gen_cpp_Engine.ysl2 +++ b/src/gen_cpp_Engine.ysl2 @@ -17,6 +17,7 @@ tstylesheet { #include "foundation_pEp_jniadapter_«@name».h" #include "throw_pEp_exception.hh" #include "jniutils.hh" + #include using pEp::Adapter::session; using namespace pEp::JNIAdapter; @@ -62,7 +63,20 @@ tstylesheet { apply "parm[creates|returns]", mode=out; || - PEP_STATUS status = ::«@name»(session()`apply "parm", mode=call`); + || + choose { + when "@cached = 'true'" { + || + pEpLog("cached passphrase"); + PEP_STATUS status = cache.api(::«@name»,session()`apply "parm", mode=call`); + || + } otherwise { + || + PEP_STATUS status = ::«@name»(session()`apply "parm", mode=call`); + || + } + } + || if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) |`> |` status < PEP_STATUS_OK |`> |` status >= PEP_TRUSTWORD_NOT_FOUND) { @@ -237,6 +251,8 @@ tstylesheet { | bool _«$name» = (bool) «$name»; when "$type='int'" | int _«$name» = (int) «$name»; + when "$type='string'" + | const char *_«$name» = to_string(env, «$name»); otherwise | √$type *_«$name» = to_«$type»(env, «$name»); } diff --git a/src/pEp.yml2 b/src/pEp.yml2 index e9d0309..603f4fe 100644 --- a/src/pEp.yml2 +++ b/src/pEp.yml2 @@ -85,32 +85,32 @@ namespace pEp { }; interface Engine { - method encrypt_message( + method cached=true encrypt_message( in message src, in stringlist extra, creates message dst, in EncFormat format, - Cconst PEP_encrypt_flags flags "0" + Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0" ); - method encrypt_message_and_add_priv_key( + method cached=true encrypt_message_and_add_priv_key( in message src, creates message dst, in string fpr, Cconst PEP_enc_format encformat "PEP_enc_PEP", - Cconst PEP_encrypt_flags flags "0" + Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0" ); - method encrypt_message_for_self( + method cached=true encrypt_message_for_self( in identity target, in message src, in stringlist extra, creates message dst, Cconst PEP_enc_format encformat "PEP_enc_PEP", - Cconst PEP_encrypt_flags flags "0" + Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0" ); - method decrypt_message( + method cached=true decrypt_message( inout message src, creates message dst, inout stringlist keylist, @@ -150,7 +150,8 @@ namespace pEp { returns stringlist blacklist ); - method own_message_private_key_details( + // TODO: WTF is... + method cached=true own_message_private_key_details( in message msg, returns identity ident ); From 6099e26bca99b9460134dd9ed5c792d8c317ee90 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 30 Jun 2020 02:50:34 +0200 Subject: [PATCH 3/3] Passphrase support for messageToSend() --- src/foundation_pEp_jniadapter_AbstractEngine.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/foundation_pEp_jniadapter_AbstractEngine.cc b/src/foundation_pEp_jniadapter_AbstractEngine.cc index beb06ef..b4e038b 100644 --- a/src/foundation_pEp_jniadapter_AbstractEngine.cc +++ b/src/foundation_pEp_jniadapter_AbstractEngine.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include "throw_pEp_exception.hh" #include "jniutils.hh" @@ -95,8 +96,16 @@ PEP_STATUS messageToSend(message *msg) std::lock_guard l(mutex_obj); pEpLog("############### messageToSend() called"); - jobject msg_ = nullptr; + // Passphrase + // When a protocol implementation of the p≡p engine using messageToSend() cannot sign or encrypt with an + // empty passphrase and not with the configured passphrase it is calling messageToSend() with a NULL instead + // of a struct _message object. + if (Adapter::on_sync_thread() && !msg) { + return pEp::PassphraseCache::messageToSend(cache, Adapter::session()); + } + + jobject msg_ = nullptr; assert(messageClass && messageConstructorMethodID && objj && messageToSendMethodID); msg_ = o.env()->NewObject(messageClass, messageConstructorMethodID, (jlong) msg);