Browse Source

Merge in JNI-111 - Passphrase support

JNI-96 Release_2.1.0-RC5
heck 5 years ago
parent
commit
9f0d86ed41
  1. 35
      src/basic_api.cc
  2. 11
      src/foundation_pEp_jniadapter_AbstractEngine.cc
  3. 18
      src/gen_cpp_Engine.ysl2
  4. 2
      src/jniutils.hh
  5. 22
      src/pEp.yml2
  6. 14
      test/java/foundation/pEp/jniadapter/test/jni111/TestAlice.java

35
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<std::mutex> l(global_mutex);
pEpLog("called with lock_guard");
mutex_local = get_engine_java_object_mutex(env, obj);
}
std::lock_guard<std::mutex> 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"

11
src/foundation_pEp_jniadapter_AbstractEngine.cc

@ -5,6 +5,7 @@
#include <pEp/sync_api.h>
#include <pEp/Adapter.hh>
#include <pEp/pEpLog.hh>
#include <pEp/passphrase_cache.hh>
#include "throw_pEp_exception.hh"
#include "jniutils.hh"
@ -95,8 +96,16 @@ PEP_STATUS messageToSend(message *msg)
std::lock_guard<std::mutex> 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);

18
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 <pEp/passphrase_cache.hh>
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»);
}

2
src/jniutils.hh

@ -8,6 +8,7 @@
#include <pEp/bloblist.h>
#include <pEp/message.h>
#include <pEp/sync_api.h>
#include <pEp/passphrase_cache.hh>
#if 0 // Enable if log needed
#include <android/log.h>
@ -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;

22
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
);
@ -250,10 +251,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 +276,7 @@ namespace pEp {
basic string getCrashdumpLog(int maxlines);
basic string getUserDirectory();
basic string getMachineDirectory();
basic void config_passphrase(string passphrase);
};
struct message {

14
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<AdapterBaseTestContext>("config_passphrase",new AdapterBaseTestContext() , ctx -> {
ctx.engine.config_passphrase("SUPERCOMPLICATEDPASSPHRASE");
AdapterBaseTestContext jni111Ctx = new AdapterBaseTestContext();
new TestUnit<AdapterBaseTestContext>("config_passphrase", jni111Ctx, ctx -> {
ctx.engine.config_passphrase("chocolate");
ctx.engine.config_passphrase("Bar");
ctx.engine.config_passphrase("Foo");
});
new TestUnit<AdapterBaseTestContext>("config_passphrase_for_new_keys",new AdapterBaseTestContext() , ctx -> {
ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE");
new TestUnit<AdapterBaseTestContext>("config_passphrase_for_new_keys", jni111Ctx, ctx -> {
ctx.engine.config_passphrase_for_new_keys(true, "SUPERCOMPLICATEDPASSPHRASE");
});
TestSuite.getDefault().run();

Loading…
Cancel
Save