1 // 2 // Copyright (C) 2015 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef ATTESTATION_SERVER_DATABASE_IMPL_H_ 18 #define ATTESTATION_SERVER_DATABASE_IMPL_H_ 19 20 #include "attestation/server/database.h" 21 22 #include <string> 23 24 #include <base/callback_forward.h> 25 #include <base/files/file_path_watcher.h> 26 #include <base/threading/thread_checker.h> 27 28 #include "attestation/common/crypto_utility.h" 29 30 namespace attestation { 31 32 // An I/O abstraction to help with testing. 33 class DatabaseIO { 34 public: 35 // Reads the persistent database blob. 36 virtual bool Read(std::string* data) = 0; 37 // Writes the persistent database blob. 38 virtual bool Write(const std::string& data) = 0; 39 // Watch for external changes to the database. 40 virtual void Watch(const base::Closure& callback) = 0; 41 }; 42 43 // An implementation of Database backed by an ordinary file. Not thread safe. 44 // All methods must be called on the same thread as the Initialize() call. 45 class DatabaseImpl : public Database, public DatabaseIO { 46 public: 47 // Does not take ownership of pointers. 48 explicit DatabaseImpl(CryptoUtility* crypto); 49 ~DatabaseImpl() override; 50 51 // Reads and decrypts any existing database on disk synchronously. Must be 52 // called before calling other methods. 53 void Initialize(); 54 55 // Database methods. 56 const AttestationDatabase& GetProtobuf() const override; 57 AttestationDatabase* GetMutableProtobuf() override; 58 bool SaveChanges() override; 59 bool Reload() override; 60 61 // DatabaseIO methods. 62 bool Read(std::string* data) override; 63 bool Write(const std::string& data) override; 64 void Watch(const base::Closure& callback) override; 65 66 // Useful for testing. set_io(DatabaseIO * io)67 void set_io(DatabaseIO* io) { io_ = io; } 68 69 private: 70 // Encrypts |protobuf_| into |encrypted_output|. Returns true on success. 71 bool EncryptProtobuf(std::string* encrypted_output); 72 73 // Decrypts |encrypted_input| as output by EncryptProtobuf into |protobuf_|. 74 // Returns true on success. 75 bool DecryptProtobuf(const std::string& encrypted_input); 76 77 AttestationDatabase protobuf_; 78 DatabaseIO* io_; 79 CryptoUtility* crypto_; 80 std::string database_key_; 81 std::string sealed_database_key_; 82 std::unique_ptr<base::FilePathWatcher> file_watcher_; 83 base::ThreadChecker thread_checker_; 84 }; 85 86 } // namespace attestation 87 88 #endif // ATTESTATION_SERVER_DATABASE_IMPL_H_ 89