ledger-core
PreferencesBackend.hpp
1 /*
2  *
3  * PreferencesBackend
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 10/01/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 
32 #ifndef LEDGER_CORE_PREFERENCESBACKEND_HPP
33 #define LEDGER_CORE_PREFERENCESBACKEND_HPP
34 
35 #include "../api/Preferences.hpp"
36 #include "../api/PreferencesEditor.hpp"
37 #include <leveldb/db.h>
38 #include <memory>
39 #include "../api/ThreadDispatcher.hpp"
40 #include "../api/ExecutionContext.hpp"
41 #include "../api/PathResolver.hpp"
42 #include "../api/Lock.hpp"
43 #include <string>
44 #include <functional>
45 #include "../utils/optional.hpp"
46 #include "Preferences.hpp"
47 #include <unordered_map>
48 #include <mutex>
49 #include <api/RandomNumberGenerator.hpp>
50 #include <utils/Option.hpp>
51 #include <crypto/AESCipher.hpp>
52 
53 namespace ledger {
54  namespace core {
55  class Preferences;
56 
57  enum PreferencesChangeType {
58  PUT_TYPE, DELETE_TYPE
59  };
60 
62  PreferencesChangeType type;
63  std::vector<uint8_t> key;
64  std::vector<uint8_t> value;
65 
66  PreferencesChange() = default;
67  PreferencesChange(PreferencesChangeType t, std::vector<uint8_t> k, std::vector<uint8_t> v);
68  };
69 
71  public:
73  const std::string& path,
74  const std::shared_ptr<api::ExecutionContext>& writingContext,
75  const std::shared_ptr<api::PathResolver>& resolver
76  );
77 
78  ~PreferencesBackend() = default;
79 
80  std::shared_ptr<Preferences> getPreferences(const std::string& name);
81  void iterate(const std::vector<uint8_t>& keyPrefix, std::function<bool (leveldb::Slice&&, leveldb::Slice&&)>);
82  optional<std::string> get(const std::vector<uint8_t>& key);
83 
86  bool commit(const std::vector<PreferencesChange>& changes);
87 
93  void setEncryption(
94  const std::shared_ptr<api::RandomNumberGenerator>& rng,
95  const std::string& password
96  );
97 
104  void unsetEncryption();
105 
114  bool resetEncryption(
115  const std::shared_ptr<api::RandomNumberGenerator>& rng,
116  const std::string& oldPassword,
117  const std::string& newPassword
118  );
119 
121  std::string getEncryptionSalt();
122 
124  void clear();
125 
126  private:
127  std::shared_ptr<api::ExecutionContext> _context;
128  std::weak_ptr<leveldb::DB> _db;
129  std::string _dbName;
130  Option<AESCipher> _cipher;
131 
132  // Get a raw entry from the key-value store.
133  optional<std::string> getRaw(const std::vector<uint8_t>& key) const;
134 
135  // Drop a database instance.
136  void dropInstance(const std::string &path);
137 
138  // Put a single PreferencesChange.
139  void putPreferencesChange(
140  leveldb::WriteBatch& batch,
141  Option<AESCipher>& cipher,
142  const PreferencesChange& change
143  );
144 
145  // Create a new salt to use with an AESCipher.
146  std::string createNewSalt(const std::shared_ptr<api::RandomNumberGenerator>& rng);
147 
148  // helper method used to encrypt things we want to put in leveldb
149  std::vector<uint8_t> encrypt_preferences_change(
150  const PreferencesChange& change,
151  AESCipher& cipher
152  );
153 
154  // helper method used to decrypt things we want to retrieve from leveldb
155  std::vector<uint8_t> decrypt_preferences_change(
156  const std::vector<uint8_t>& data,
157  AESCipher& cipher
158  );
159 
160  // an owning table that holds connection opened
161  static std::unordered_map<std::string, std::shared_ptr<leveldb::DB>> LEVELDB_INSTANCE_POOL;
162  static std::mutex LEVELDB_INSTANCE_POOL_MUTEX;
163 
164  static std::weak_ptr<leveldb::DB> obtainInstance(const std::string& path);
165  };
166  }
167 }
168 
169 #endif //LEDGER_CORE_PREFERENCESBACKEND_HPP
Definition: Option.hpp:49
Definition: PreferencesBackend.hpp:61
Definition: PreferencesBackend.hpp:70
Definition: AESCipher.hpp:44
Definition: Account.cpp:8
Definition: optional.hpp:177