You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.8 KiB
140 lines
3.8 KiB
#include <cassert>
|
|
#include "passphrase_cache.hh"
|
|
|
|
pEp::PassphraseCache pEp::passphrase_cache;
|
|
|
|
namespace pEp {
|
|
PassphraseCache::cache_entry::cache_entry(const std::string& p, time_point t) :
|
|
passphrase{p, 0, PassphraseCache::cache_entry::max_len}, tp{t}
|
|
{ }
|
|
|
|
PassphraseCache::PassphraseCache(size_t max_size, duration timeout) :
|
|
_max_size{max_size}, _timeout{timeout}, first_time(true)
|
|
{ }
|
|
|
|
PassphraseCache::PassphraseCache(const PassphraseCache& second) :
|
|
_cache{second._cache}, _max_size{second._max_size},
|
|
_timeout{second._timeout}, first_time(true)
|
|
{
|
|
cleanup();
|
|
}
|
|
|
|
PassphraseCache& PassphraseCache::operator=(const PassphraseCache& second)
|
|
{
|
|
_cache = second._cache;
|
|
_max_size = second._max_size;
|
|
_timeout = second._timeout;
|
|
_which = _cache.end();
|
|
cleanup();
|
|
return *this;
|
|
}
|
|
|
|
const char *PassphraseCache::add(const std::string& passphrase)
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mtx);
|
|
|
|
if (!passphrase.empty()) {
|
|
while (_cache.size() >= _max_size)
|
|
_cache.pop_front();
|
|
|
|
_cache.emplace_back(passphrase, clock::now());
|
|
auto back = _cache.back(); // FIXME: In C++17 list::emplace_back()
|
|
// returns the just inserted element
|
|
// already.
|
|
return back.passphrase.c_str();
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
const char *PassphraseCache::add_stored(const std::string& passphrase)
|
|
{
|
|
std::lock_guard<std::mutex> lock(_stored_mtx);
|
|
_stored = passphrase;
|
|
return _stored.c_str();
|
|
}
|
|
|
|
bool PassphraseCache::for_each_passphrase(const passphrase_callee& callee)
|
|
{
|
|
if (callee(""))
|
|
return true;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(_stored_mtx);
|
|
if (!_stored.empty() && callee(_stored))
|
|
return true;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mtx);
|
|
cleanup();
|
|
|
|
for (auto entry=_cache.begin(); entry!=_cache.end(); ++entry) {
|
|
if (callee(entry->passphrase)) {
|
|
refresh(entry);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void PassphraseCache::cleanup()
|
|
{
|
|
while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout)
|
|
_cache.pop_front();
|
|
}
|
|
|
|
void PassphraseCache::refresh(cache::iterator entry)
|
|
{
|
|
entry->tp = clock::now();
|
|
_cache.splice(_cache.end(), _cache, entry);
|
|
}
|
|
|
|
const char *PassphraseCache::latest_passphrase()
|
|
{
|
|
if (first_time) {
|
|
cleanup();
|
|
_which = _cache.end();
|
|
first_time = false;
|
|
return _stored.c_str();
|
|
}
|
|
|
|
if (_cache.empty()) {
|
|
first_time = true;
|
|
throw Empty();
|
|
}
|
|
|
|
if (_which == _cache.begin()) {
|
|
first_time = true;
|
|
throw Exhausted();
|
|
}
|
|
|
|
--_which;
|
|
return _which->passphrase.c_str();
|
|
}
|
|
|
|
PEP_STATUS PassphraseCache::messageToSend(PassphraseCache& cache, PEP_SESSION session)
|
|
{
|
|
static pEp::PassphraseCache _copy;
|
|
static bool new_copy = true;
|
|
if (new_copy) {
|
|
_copy = cache;
|
|
new_copy = false;
|
|
}
|
|
try {
|
|
::config_passphrase(session, _copy.latest_passphrase());
|
|
return PEP_STATUS_OK;
|
|
}
|
|
catch (pEp::PassphraseCache::Empty&) {
|
|
new_copy = true;
|
|
return PEP_PASSPHRASE_REQUIRED;
|
|
}
|
|
catch (pEp::PassphraseCache::Exhausted&) {
|
|
new_copy = true;
|
|
return PEP_WRONG_PASSPHRASE;
|
|
}
|
|
}
|
|
};
|
|
|
|
|