| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef BACKEND_MEM_INCLUDED |
| 25 |
#define BACKEND_MEM_INCLUDED |
| 26 |
|
| 27 |
#include "components/keyrings/common/memstore/iterator.h" |
| 28 |
#include "components/keyrings/common/operations/operations.h" |
| 29 |
#include "components/keyrings/common/utils/utils.h" |
| 30 |
|
| 31 |
namespace keyring_common_unit { |
| 32 |
class Memory_backend final { |
| 33 |
public: |
| 34 |
Memory_backend() = default; |
| 35 |
|
| 36 |
virtual ~Memory_backend() = default; |
| 37 |
|
| 38 |
bool get(const keyring_common::meta::Metadata &metadata, |
| 39 |
keyring_common::data::Data &data) const { |
| 40 |
if (!metadata.valid()) return true; |
| 41 |
return !cache_.get(metadata, data); |
| 42 |
} |
| 43 |
|
| 44 |
bool store(const keyring_common::meta::Metadata &metadata, |
| 45 |
const keyring_common::data::Data &data) { |
| 46 |
if (!metadata.valid() || !data.valid()) return true; |
| 47 |
return !cache_.store(metadata, data); |
| 48 |
} |
| 49 |
|
| 50 |
bool erase(const keyring_common::meta::Metadata &metadata, |
| 51 |
const keyring_common::data::Data &data) { |
| 52 |
if (!metadata.valid()) return true; |
| 53 |
(void)data; |
| 54 |
return !cache_.erase(metadata); |
| 55 |
} |
| 56 |
|
| 57 |
bool generate(const keyring_common::meta::Metadata &metadata, |
| 58 |
keyring_common::data::Data &data, size_t length) { |
| 59 |
if (!metadata.valid()) return true; |
| 60 |
if (cache_.get(metadata, data)) return true; |
| 61 |
|
| 62 |
std::unique_ptr<unsigned char[]> key(new unsigned char[length]); |
| 63 |
if (!key) return true; |
| 64 |
if (!keyring_common::utils::get_random_data(key, length)) return true; |
| 65 |
|
| 66 |
std::string key_str; |
| 67 |
key_str.assign(reinterpret_cast<const char *>(key.get()), length); |
| 68 |
data.set_data(key_str); |
| 69 |
if (!cache_.store(metadata, data)) return true; |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
bool load_cache( |
| 75 |
keyring_common::operations::Keyring_operations<Memory_backend> &) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
size_t maximum_data_length() const { return 16384; } |
| 80 |
|
| 81 |
size_t size() const { return cache_.size(); } |
| 82 |
|
| 83 |
private: |
| 84 |
|
| 85 |
keyring_common::cache::Datacache<keyring_common::data::Data> cache_; |
| 86 |
}; |
| 87 |
} |
| 88 |
|
| 89 |
#endif |