blob: 077ec05df38c4f18e25c688f6de238b25462019c [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATTESTATION_SERVER_DATABASE_IMPL_H_
#define ATTESTATION_SERVER_DATABASE_IMPL_H_
#include "attestation/server/database.h"
#include <string>
#include <base/callback_forward.h>
#include <base/files/file_path_watcher.h>
#include <base/threading/thread_checker.h>
#include "attestation/common/crypto_utility.h"
namespace attestation {
// An I/O abstraction to help with testing.
class DatabaseIO {
public:
// Reads the persistent database blob.
virtual bool Read(std::string* data) = 0;
// Writes the persistent database blob.
virtual bool Write(const std::string& data) = 0;
// Watch for external changes to the database.
virtual void Watch(const base::Closure& callback) = 0;
};
// An implementation of Database backed by an ordinary file. Not thread safe.
// All methods must be called on the same thread as the Initialize() call.
class DatabaseImpl : public Database,
public DatabaseIO {
public:
// Does not take ownership of pointers.
explicit DatabaseImpl(CryptoUtility* crypto);
~DatabaseImpl() override;
// Reads and decrypts any existing database on disk synchronously. Must be
// called before calling other methods.
void Initialize();
// Database methods.
const AttestationDatabase& GetProtobuf() const override;
AttestationDatabase* GetMutableProtobuf() override;
bool SaveChanges() override;
bool Reload() override;
// DatabaseIO methods.
bool Read(std::string* data) override;
bool Write(const std::string& data) override;
void Watch(const base::Closure& callback) override;
// Useful for testing.
void set_io(DatabaseIO* io) {
io_ = io;
}
private:
// Encrypts |protobuf_| into |encrypted_output|. Returns true on success.
bool EncryptProtobuf(std::string* encrypted_output);
// Decrypts |encrypted_input| as output by EncryptProtobuf into |protobuf_|.
// Returns true on success.
bool DecryptProtobuf(const std::string& encrypted_input);
AttestationDatabase protobuf_;
DatabaseIO* io_;
CryptoUtility* crypto_;
std::string database_key_;
std::string sealed_database_key_;
std::unique_ptr<base::FilePathWatcher> file_watcher_;
base::ThreadChecker thread_checker_;
};
} // namespace attestation
#endif // ATTESTATION_SERVER_DATABASE_IMPL_H_