diff --git a/Adapter.cc b/Adapter.cc index aa5bdb3..b545f54 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -7,6 +7,7 @@ #include #include "status_to_string.hh" #include "pEpLog.hh" +#include "passphrase_cache.hh" using namespace std; @@ -82,6 +83,11 @@ namespace pEp { return 0; } + PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) + { + return passphrase_cache.ensure_passphrase(session, fpr); + } + // threshold: max waiting time in seconds SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold) { @@ -120,7 +126,7 @@ namespace pEp { case init: if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event); + status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); break; default: diff --git a/Adapter.hh b/Adapter.hh index d5faac2..d71fb33 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -24,6 +24,7 @@ namespace pEp { namespace Adapter { int _inject_sync_event(SYNC_EVENT ev, void *management); + PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); template void startup( diff --git a/passphrase_cache.cc b/passphrase_cache.cc index 6c5a806..3d1815b 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -153,5 +153,21 @@ namespace pEp { return PEP_WRONG_PASSPHRASE; } } + + PEP_STATUS PassphraseCache::ensure_passphrase(PEP_SESSION session, std::string fpr) + { + PEP_STATUS status = PEP_STATUS_OK; + + for_each_passphrase([&](std::string passphrase) { + status = ::config_passphrase(session, passphrase.c_str()); + if (status) + return true; + + status = ::probe_encrypt(session, fpr.c_str()); + return status == PEP_STATUS_OK; + }); + + return status; + } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 08cc3a0..b857590 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -73,6 +73,7 @@ namespace pEp { static const char *latest_passphrase(PassphraseCache& _cache); using passphrase_callee = std::function; bool for_each_passphrase(const passphrase_callee& callee); + PEP_STATUS ensure_passphrase(PEP_SESSION session, std::string fpr); protected: void cleanup();