Merge in JNI-114 (new callback param PassphraseType) Release_2.1.0 Release_2.1.0-RC12
authorheck <heck@pep.foundation>
Tue, 18 Aug 2020 21:32:56 +0200
changeset 9245d4ad9577e68
parent 922 c99c2d43b14f
parent 923 2850d0285d3d
child 925 7a68e5e07267
child 964 4a272644d3a3
child 966 7e5b637aaf46
Merge in JNI-114 (new callback param PassphraseType)
     1.1 --- a/.hgignore	Thu Aug 13 00:38:03 2020 +0200
     1.2 +++ b/.hgignore	Tue Aug 18 21:32:56 2020 +0200
     1.3 @@ -39,6 +39,7 @@
     1.4  SyncHandshakeSignal.java
     1.5  CipherSuite.java
     1.6  
     1.7 +passphrase_status_list.yml2
     1.8  status_list.yml2
     1.9  
    1.10  ndk*/
     2.1 --- a/src/Makefile	Thu Aug 13 00:38:03 2020 +0200
     2.2 +++ b/src/Makefile	Tue Aug 18 21:32:56 2020 +0200
     2.3 @@ -69,7 +69,7 @@
     2.4  .PHONY: all
     2.5  all: $(JAR) $(SHARED)
     2.6  
     2.7 -$(JAR): status_list.yml2 $(JAVA_SOURCES) $(C_SOURCES)
     2.8 +$(JAR): status_list.yml2 passphrase_status_list.yml2 $(JAVA_SOURCES) $(C_SOURCES)
     2.9  	$(JP)/javac foundation/pEp/jniadapter/*.java
    2.10  	$(JP)/jar cf $@ foundation/pEp/jniadapter/*.class
    2.11  
    2.12 @@ -96,6 +96,9 @@
    2.13  status_list.yml2: pEp.yml2
    2.14  	bash ../utils/extract_pEp_status_codes_from_engine.sh "$(PEP_HEADER)" $@
    2.15  
    2.16 +passphrase_status_list.yml2: status_list.yml2
    2.17 +	grep passphrase $< > $@
    2.18 +
    2.19  foundation/pEp/jniadapter/pEpException.java: pEp.yml2 gen_java_exceptions.ysl2 pEp.yml2
    2.20  	$(YML2_PROC) -y gen_java_exceptions.ysl2 $< -o $@
    2.21  
    2.22 @@ -139,6 +142,8 @@
    2.23  	rm -f foundation/pEp/jniadapter/SyncHandshakeResult.java
    2.24  	rm -f foundation/pEp/jniadapter/SyncHandshakeSignal.java
    2.25  	rm -f foundation/pEp/jniadapter/CipherSuite.java
    2.26 +	rm -f foundation/pEp/jniadapter/PassphraseType.java
    2.27  	rm -f throw_pEp_exception.*
    2.28  	rm -f foundation_pEp_jniadapter_Message.cc foundation_pEp_jniadapter_Engine.cc
    2.29  	rm -f status_list.yml2
    2.30 +	rm -f passphrase_status_list.yml2
     3.1 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java	Thu Aug 13 00:38:03 2020 +0200
     3.2 +++ b/src/foundation/pEp/jniadapter/AbstractEngine.java	Tue Aug 18 21:32:56 2020 +0200
     3.3 @@ -193,11 +193,11 @@
     3.4          return 0;
     3.5      }
     3.6  
     3.7 -    public byte[] passphraseRequiredFromC() {
     3.8 +    public byte[] passphraseRequiredFromC(final PassphraseType passphraseType) {
     3.9          String ret = "";
    3.10          if (passphraseRequiredCallback != null) {
    3.11              System.out.println("calling passphraseRequiredCallback on engine ObjID:" + getId());
    3.12 -            ret = passphraseRequiredCallback.passphraseRequired();
    3.13 +            ret = passphraseRequiredCallback.passphraseRequired(passphraseType);
    3.14          } else {
    3.15              System.out.println("no callback registered on engine ObjID:" + getId());
    3.16              // if this happens (no callback registered
     4.1 --- a/src/foundation/pEp/jniadapter/Sync.java	Thu Aug 13 00:38:03 2020 +0200
     4.2 +++ b/src/foundation/pEp/jniadapter/Sync.java	Tue Aug 18 21:32:56 2020 +0200
     4.3 @@ -20,7 +20,7 @@
     4.4      }
     4.5  
     4.6      interface PassphraseRequiredCallback {
     4.7 -        String passphraseRequired();
     4.8 +        String passphraseRequired(final PassphraseType pEpStatus);
     4.9      }
    4.10  
    4.11      public class DefaultCallback 
     5.1 --- a/src/foundation_pEp_jniadapter_AbstractEngine.cc	Thu Aug 13 00:38:03 2020 +0200
     5.2 +++ b/src/foundation_pEp_jniadapter_AbstractEngine.cc	Tue Aug 18 21:32:56 2020 +0200
     5.3 @@ -21,20 +21,24 @@
     5.4  
     5.5  std::mutex mutex_obj;
     5.6  
     5.7 -jfieldID field_value = nullptr;
     5.8 +jfieldID signal_field_value = nullptr;
     5.9 +jfieldID passphrase_type_field_value = nullptr;
    5.10  jmethodID messageConstructorMethodID = nullptr;
    5.11  jmethodID messageToSendMethodID = nullptr;
    5.12  jmethodID notifyHandShakeMethodID = nullptr;
    5.13  jmethodID needsFastPollMethodID = nullptr;
    5.14  jmethodID passphraseRequiredMethodID = nullptr;
    5.15 -jmethodID method_values = nullptr;
    5.16 +jmethodID sync_handshake_signal_values = nullptr;
    5.17 +jmethodID passphrase_status_values = nullptr;
    5.18 +jmethodID passphrase_callback_values = nullptr;
    5.19  
    5.20  jobject objj = nullptr;
    5.21  
    5.22  jclass messageClass = nullptr;
    5.23 -jclass identityClass = nullptr;;
    5.24 +jclass identityClass = nullptr;
    5.25  jclass signalClass = nullptr;
    5.26  jclass engineClass = nullptr;
    5.27 +jclass passphraseTypeClass = nullptr;
    5.28  
    5.29  namespace JNISync {
    5.30      JNIEnv* env() {
    5.31 @@ -72,6 +76,8 @@
    5.32          _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/_Identity")));
    5.33      signalClass = reinterpret_cast<jclass>(
    5.34              _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/SyncHandshakeSignal")));
    5.35 +    passphraseTypeClass = reinterpret_cast<jclass>(
    5.36 +            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/PassphraseType")));
    5.37      engineClass = reinterpret_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Engine")));
    5.38  
    5.39      messageConstructorMethodID = _env->GetMethodID(messageClass, "<init>", "(J)V");
    5.40 @@ -90,19 +96,47 @@
    5.41      passphraseRequiredMethodID = _env->GetMethodID(
    5.42          engineClass,
    5.43          "passphraseRequiredFromC",
    5.44 -        "()[B");
    5.45 +        "(Lfoundation/pEp/jniadapter/PassphraseType;)[B");
    5.46  
    5.47 -    method_values = JNISync::env()->GetStaticMethodID(signalClass, "values",
    5.48 +    sync_handshake_signal_values = JNISync::env()->GetStaticMethodID(signalClass, "values",
    5.49                  "()[Lfoundation/pEp/jniadapter/SyncHandshakeSignal;");
    5.50 -    field_value = JNISync::env()->GetFieldID(signalClass, "value", "I");
    5.51 +    passphrase_status_values = JNISync::env()->GetStaticMethodID(passphraseTypeClass, "values",
    5.52 +                "()[Lfoundation/pEp/jniadapter/PassphraseType;");
    5.53 +    signal_field_value = JNISync::env()->GetFieldID(signalClass, "value", "I");
    5.54 +    passphrase_type_field_value = JNISync::env()->GetFieldID(passphraseTypeClass, "value", "I");
    5.55  }
    5.56  
    5.57 -char* JNIAdapter::passphraseRequiredCallback() {
    5.58 +char* JNIAdapter::passphraseRequiredCallback(const PEP_STATUS status) {
    5.59      pEpLog("called");
    5.60 +    jobject status_ = nullptr;
    5.61 +    {
    5.62 +        assert(passphraseTypeClass);
    5.63 +        assert(passphrase_status_values);
    5.64 +        assert(passphrase_type_field_value);
    5.65  
    5.66 +        jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(passphraseTypeClass,
    5.67 +                passphrase_status_values);
    5.68 +
    5.69 +        if (JNISync::env()->ExceptionCheck()) {
    5.70 +            JNISync::env()->ExceptionClear();
    5.71 +            throw_pEp_Exception(JNISync::env(), PEP_UNKNOWN_ERROR);
    5.72 +        }
    5.73 +
    5.74 +        jsize values_size = JNISync::env()->GetArrayLength(values);
    5.75 +        for (jsize i = 0; i < values_size; i++) {
    5.76 +            jobject element = JNISync::env()->GetObjectArrayElement(values, i);
    5.77 +            assert(element);
    5.78 +            jint value = JNISync::env()->GetIntField(element, passphrase_type_field_value);
    5.79 +            if (value == (jint) status) {
    5.80 +                status_ = element;
    5.81 +                break;
    5.82 +            }
    5.83 +            JNISync::env()->DeleteLocalRef(element);
    5.84 +        }
    5.85 +    }
    5.86      assert(objj && passphraseRequiredMethodID);
    5.87  
    5.88 -    jobject ppJO = JNISync::env()->CallObjectMethod(objj, passphraseRequiredMethodID);
    5.89 +    jobject ppJO = JNISync::env()->CallObjectMethod(objj, passphraseRequiredMethodID, status_);
    5.90      if (JNISync::env()->ExceptionCheck()) {
    5.91          JNISync::env()->ExceptionDescribe();
    5.92          JNISync::env()->ExceptionClear();
    5.93 @@ -160,11 +194,11 @@
    5.94      jobject signal_ = nullptr;
    5.95      {
    5.96          assert(signalClass);
    5.97 -        assert(method_values);
    5.98 -        assert(field_value);
    5.99 +        assert(sync_handshake_signal_values);
   5.100 +        assert(signal_field_value);
   5.101  
   5.102          jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(signalClass,
   5.103 -                method_values);
   5.104 +                sync_handshake_signal_values);
   5.105          if (JNISync::env()->ExceptionCheck()) {
   5.106              JNISync::env()->ExceptionClear();
   5.107              return PEP_UNKNOWN_ERROR;
   5.108 @@ -174,7 +208,7 @@
   5.109          for (jsize i = 0; i < values_size; i++) {
   5.110              jobject element = JNISync::env()->GetObjectArrayElement(values, i);
   5.111              assert(element);
   5.112 -            jint value = JNISync::env()->GetIntField(element, field_value);
   5.113 +            jint value = JNISync::env()->GetIntField(element, signal_field_value);
   5.114              if (value == (jint) signal) {
   5.115                  signal_ = element;
   5.116                  break;
     6.1 --- a/src/gen_cpp_Engine.ysl2	Thu Aug 13 00:38:03 2020 +0200
     6.2 +++ b/src/gen_cpp_Engine.ysl2	Tue Aug 18 21:32:56 2020 +0200
     6.3 @@ -80,11 +80,11 @@
     6.4                      pEpLog("calling passphrase_cache.api(::«@name»())");
     6.5                      status = passphrase_cache.api(::«@name»,session()`apply "parm", mode=call`);
     6.6                      pEpLog("PEP_STATUS:" << status);
     6.7 -                    if(status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE ) {
     6.8 +                    if(status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE || status == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED) {
     6.9                          pEpLog("none of the cached passphrases worked");
    6.10                          if(retryCount < maxRetries) {
    6.11                              // call the app
    6.12 -                            char* _passphrase = passphraseRequiredCallback();
    6.13 +                            char* _passphrase = passphraseRequiredCallback(status);
    6.14                              pEpLog("callback returned, config_passphrase() with new passphrase");
    6.15                              PEP_STATUS status = ::config_passphrase(session(),passphrase_cache.add(_passphrase));
    6.16                              retryAgain = true;
     7.1 --- a/src/pEp.yml2	Thu Aug 13 00:38:03 2020 +0200
     7.2 +++ b/src/pEp.yml2	Tue Aug 18 21:32:56 2020 +0200
     7.3 @@ -10,7 +10,11 @@
     7.4      exception Status {
     7.5         include ./status_list.yml2
     7.6      };
     7.7 - 
     7.8 +
     7.9 +    enum PassphraseType {
    7.10 +        include ./passphrase_status_list.yml2
    7.11 +    };
    7.12 +
    7.13      enum Color {
    7.14          PEP_color_no_color > 0
    7.15          PEP_color_yellow > 1
     8.1 --- a/src/passphrase_callback.hh	Thu Aug 13 00:38:03 2020 +0200
     8.2 +++ b/src/passphrase_callback.hh	Tue Aug 18 21:32:56 2020 +0200
     8.3 @@ -5,11 +5,10 @@
     8.4  namespace pEp {
     8.5      namespace JNIAdapter {
     8.6  
     8.7 -    char* passphraseRequiredCallback();
     8.8 +        char* passphraseRequiredCallback(const PEP_STATUS status);
     8.9  
    8.10 -        template<typename... A> PEP_STATUS passphraseWrap(
    8.11 -                PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a);
    8.12 -    };
    8.13 -};
    8.14 +        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a);
    8.15 +    }
    8.16 +}
    8.17  
    8.18  #include "passphrase_callback.hxx"
    8.19 \ No newline at end of file
     9.1 --- a/src/passphrase_callback.hxx	Thu Aug 13 00:38:03 2020 +0200
     9.2 +++ b/src/passphrase_callback.hxx	Tue Aug 18 21:32:56 2020 +0200
     9.3 @@ -5,8 +5,7 @@
     9.4  namespace pEp {
     9.5      namespace JNIAdapter {
     9.6  
     9.7 -        template<typename... A> PEP_STATUS passphraseWrap(
     9.8 -                PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a) {
     9.9 +        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a) {
    9.10              pEpLog("cached passphrase mode");
    9.11              bool retryAgain = false;
    9.12              int maxRetries = 3;
    9.13 @@ -17,14 +16,16 @@
    9.14                  pEpLog("calling passphrase_cache.api from basic_api");
    9.15                  status = passphrase_cache.api(f, session, a...);
    9.16                  pEpLog("PEP_STATUS:" << status);
    9.17 -                if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) {
    9.18 +                if (status == PEP_PASSPHRASE_REQUIRED ||
    9.19 +                    status == PEP_WRONG_PASSPHRASE ||
    9.20 +                    status == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
    9.21 +                {
    9.22                      pEpLog("none of the cached passphrases worked");
    9.23                      if (retryCount < maxRetries) {
    9.24                          // call the app
    9.25 -                        char *_passphrase = passphraseRequiredCallback();
    9.26 +                        char *_passphrase = passphraseRequiredCallback(status);
    9.27                          pEpLog("callback returned, config_passphrase() with new passphrase");
    9.28 -                        PEP_STATUS status = ::config_passphrase(session,
    9.29 -                                                                passphrase_cache.add(_passphrase));
    9.30 +                        PEP_STATUS status = ::config_passphrase(session, passphrase_cache.add(_passphrase));
    9.31                          retryAgain = true;
    9.32                          retryCount++;
    9.33                      } else {
    10.1 --- a/test/java/foundation/pEp/jniadapter/test/jni114/TestAlice.java	Thu Aug 13 00:38:03 2020 +0200
    10.2 +++ b/test/java/foundation/pEp/jniadapter/test/jni114/TestAlice.java	Tue Aug 18 21:32:56 2020 +0200
    10.3 @@ -50,10 +50,11 @@
    10.4              // Register callback passphraseRequired()
    10.5              ctx.engine.setPassphraseRequiredCallback(new Sync.PassphraseRequiredCallback() {
    10.6                  @Override
    10.7 -                public String passphraseRequired() {
    10.8 +                public String passphraseRequired(PassphraseType type) {
    10.9                      log("passphraseRequired() called");
   10.10                      log("Please Enter Passphrase...");
   10.11                      sleep(2000);
   10.12 +                    assert type == PassphraseType.pEpPassphraseRequired;
   10.13                      return "passphrase_alice";
   10.14                  }
   10.15              });