From 40e8a28ea92c4098cb35ab5c2074bc51634cc8fc Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 13 Jan 2021 18:29:26 +0100 Subject: [PATCH 1/4] Add test for JNI-117 - "Export Key" --- .../pEp/jniadapter/test/jni117/Makefile | 37 +++++++++++++++++++ .../pEp/jniadapter/test/jni117/TestAlice.java | 23 ++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni117/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni117/TestAlice.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni117/Makefile b/test/java/foundation/pEp/jniadapter/test/jni117/Makefile new file mode 100644 index 0000000..8c8748e --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni117/Makefile @@ -0,0 +1,37 @@ +include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni117 + +JAVA_CLASSES = \ + TestAlice.class \ + ../utils/AdapterBaseTestContext.class \ + ../utils/AdapterTestUtils.class \ + ../utils/TestCallbacks.class + +.PHONY: pitytest compile alice test clean + +all: alice compile + +pitytest: + $(MAKE) -C $(PITYTEST_DIR) + +alice: compile clean-pep-home-alice + cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR_ALICE) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestAlice + +compile: $(JAVA_CLASSES) pitytest + +%.class: %.java + cd $(JAVA_CWD);$(JAVAC_CMD) -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) + rm -f *.class + rm -f *.log + rm -Rf .gnupg + rm -Rf .lldb + +clean-pep-home: clean-pep-home-alice + +clean-pep-home-alice: + rm -rf $(PEP_HOME_DIR_ALICE)/.pEp diff --git a/test/java/foundation/pEp/jniadapter/test/jni117/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni117/TestAlice.java new file mode 100644 index 0000000..a2f856d --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni117/TestAlice.java @@ -0,0 +1,23 @@ +package foundation.pEp.jniadapter.test.jni117; +import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; +import foundation.pEp.pitytest.TestSuite; +import foundation.pEp.pitytest.TestUnit; +import foundation.pEp.pitytest.utils.TestUtils; + +import static foundation.pEp.pitytest.TestLogger.log; + +class TestAlice { + public static void main(String[] args) throws Exception { + TestSuite.getDefault().setVerbose(true); + TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); + + new TestUnit("Test Alice",new AdapterBaseTestContext() , ctx -> { + ctx.alice = ctx.engine.myself(ctx.alice); + log(new String(ctx.engine.export_key(ctx.alice.fpr))); + }); + + TestSuite.getDefault().run(); + } +} + + From 5e999db69f5732e68a30959cbbd10dc3f854de3a Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 13 Jan 2021 18:30:17 +0100 Subject: [PATCH 2/4] add export_key as "basic" method (gen on java handwritten in c++) --- src/codegen/pEp.yml2 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/codegen/pEp.yml2 b/src/codegen/pEp.yml2 index 7d0a645..14ef8b8 100644 --- a/src/codegen/pEp.yml2 +++ b/src/codegen/pEp.yml2 @@ -272,6 +272,7 @@ namespace pEp { basic string getMachineDirectory(); basic void config_passphrase(string passphrase); basic void config_passphrase_for_new_keys(bool enable, string passphrase); + basic bytearray export_key(string fpr); }; struct message { From 46ba24e930cd615450b7685b343ad0169ba08693 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 13 Jan 2021 18:30:57 +0100 Subject: [PATCH 3/4] codegen add support for "basic methods" returning bytearray --- src/codegen/gen_java_Engine.ysl2 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/codegen/gen_java_Engine.ysl2 b/src/codegen/gen_java_Engine.ysl2 index 10efac1..ea9e520 100644 --- a/src/codegen/gen_java_Engine.ysl2 +++ b/src/codegen/gen_java_Engine.ysl2 @@ -70,7 +70,9 @@ tstylesheet { } return ret; || - } otherwise + } when "@type = 'bytearray'" + |> return _«@name»(`apply "parm/*", mode=basic_parm_name`); + otherwise |> return Utils.toUTF16(_«@name»(`apply "parm/*", mode=basic_parm_name`)); } || From 866dc604394ef3a69017650c851ceb43834b8b87 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 13 Jan 2021 18:31:29 +0100 Subject: [PATCH 4/4] C++ impl of export_key() --- Makefile.conf | 5 +++-- src/cxx/basic_api.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Makefile.conf b/Makefile.conf index d202643..adf9ca8 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -48,9 +48,9 @@ ifndef JAVA_HOME endif # Guessing USE_JAVAH -# cant be guessed earlier, because we depend on JAVA_HOME which can be set in the local.conf +# cant be guessed earlier, because it depends on JAVA_HOME which can be set in the local.conf # Old versions of a Java distribution have a `javah` binary, new version do not and the replacement is "javac -h" -# TODO: dont check for presence of javah, but check if javac -h is supported, because some java (RHEL) has javah but supports javac -h +# TODO: dont check for presence of javah, but check if javac -h is supported, because some java (RHEL) has javah but supports javac -h (and javac -h is preferred) ifndef USE_JAVAH DUMMY:=$(shell which $(JAVA_HOME)/bin/javah) ifeq ($(.SHELLSTATUS),0) @@ -84,6 +84,7 @@ export YML_PATH=$(YML2_PATH) # BEGIN // kryptic hack to to replace a space with a newline # $(subst ${ },${space}, whatever) +# maybe not well known: define/endef is custom makefile function definition null := space := ${null} ${null} ${space} := ${space} diff --git a/src/cxx/basic_api.cc b/src/cxx/basic_api.cc index 49c3b2f..4f9960c 100644 --- a/src/cxx/basic_api.cc +++ b/src/cxx/basic_api.cc @@ -512,5 +512,32 @@ JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphras } } +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1export_1key (JNIEnv *env, + jobject obj, + jbyteArray fpr) +{ + 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); + + const char *_fpr = to_string(env, fpr); + char *buff = nullptr; + size_t size = 0; + + PEP_STATUS status = passphraseWrap(::export_key, session(), _fpr, &buff, &size); + + if (status != PEP_STATUS_OK) { + throw_pEp_Exception(env, status); + return NULL; + } + + return from_string(env, buff); +} + } // extern "C"