Home | History | Annotate | Download | only in server
      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,
     46                      public DatabaseIO {
     47  public:
     48   // Does not take ownership of pointers.
     49   explicit DatabaseImpl(CryptoUtility* crypto);
     50   ~DatabaseImpl() override;
     51 
     52   // Reads and decrypts any existing database on disk synchronously. Must be
     53   // called before calling other methods.
     54   void Initialize();
     55 
     56   // Database methods.
     57   const AttestationDatabase& GetProtobuf() const override;
     58   AttestationDatabase* GetMutableProtobuf() override;
     59   bool SaveChanges() override;
     60   bool Reload() override;
     61 
     62   // DatabaseIO methods.
     63   bool Read(std::string* data) override;
     64   bool Write(const std::string& data) override;
     65   void Watch(const base::Closure& callback) override;
     66 
     67   // Useful for testing.
     68   void set_io(DatabaseIO* io) {
     69     io_ = io;
     70   }
     71 
     72  private:
     73   // Encrypts |protobuf_| into |encrypted_output|. Returns true on success.
     74   bool EncryptProtobuf(std::string* encrypted_output);
     75 
     76   // Decrypts |encrypted_input| as output by EncryptProtobuf into |protobuf_|.
     77   // Returns true on success.
     78   bool DecryptProtobuf(const std::string& encrypted_input);
     79 
     80   AttestationDatabase protobuf_;
     81   DatabaseIO* io_;
     82   CryptoUtility* crypto_;
     83   std::string database_key_;
     84   std::string sealed_database_key_;
     85   std::unique_ptr<base::FilePathWatcher> file_watcher_;
     86   base::ThreadChecker thread_checker_;
     87 };
     88 
     89 }  // namespace attestation
     90 
     91 #endif  // ATTESTATION_SERVER_DATABASE_IMPL_H_
     92