From aed30a0ec1f6e0c86895806014da4229d9a7a7f0 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 16 Jun 2015 18:20:31 +0200 Subject: [PATCH] Refactored creation of new config files - this happens in a CryConfigLoader now --- src/CryConfig.h | 3 ++- src/CryConfigLoader.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ src/CryConfigLoader.h | 29 +++++++++++++++++++++++ src/CryDevice.cpp | 18 ++++----------- src/CryDevice.h | 7 +++--- src/main.cpp | 3 ++- 6 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 src/CryConfigLoader.cpp create mode 100644 src/CryConfigLoader.h diff --git a/src/CryConfig.h b/src/CryConfig.h index 1a75b5d2..32df8379 100644 --- a/src/CryConfig.h +++ b/src/CryConfig.h @@ -19,11 +19,12 @@ public: const std::string &EncryptionKey() const; void SetEncryptionKey(const std::string &value); + void save() const; + private: boost::filesystem::path _configfile; void load(); - void save() const; std::string _rootBlob; std::string _encKey; diff --git a/src/CryConfigLoader.cpp b/src/CryConfigLoader.cpp new file mode 100644 index 00000000..7bc58542 --- /dev/null +++ b/src/CryConfigLoader.cpp @@ -0,0 +1,51 @@ +#include "CryConfigLoader.h" +#include + +namespace bf = boost::filesystem; +using std::unique_ptr; +using std::make_unique; + +namespace cryfs { + +unique_ptr CryConfigLoader::loadOrCreate(const bf::path &filename) { + auto config = loadExisting(filename); + if (config.get() != nullptr) { + return config; + } + return createNew(filename); +} + +unique_ptr CryConfigLoader::createNew(const bf::path &filename) { + auto config = make_unique(filename); + _initializeConfig(config.get()); + config->save(); + return config; +} + +void CryConfigLoader::_initializeConfig(CryConfig *config) { + _generateEncKey(config); + _generateRootBlobKey(config); +} + +void CryConfigLoader::_generateEncKey(CryConfig *config) { + printf("Generating secure encryption key..."); + fflush(stdout); + auto new_key = Cipher::EncryptionKey::CreateOSRandom(); + config->SetEncryptionKey(new_key.ToString()); + printf("done\n"); + fflush(stdout); +} + +void CryConfigLoader::_generateRootBlobKey(CryConfig *config) { + //An empty root blob entry will tell CryDevice to create a new root blob + config->SetRootBlob(""); +} + +unique_ptr CryConfigLoader::loadExisting(const bf::path &filename) { + if (bf::exists(filename)) { + return make_unique(filename); + } + return nullptr; +} + +} diff --git a/src/CryConfigLoader.h b/src/CryConfigLoader.h new file mode 100644 index 00000000..6c14e6d1 --- /dev/null +++ b/src/CryConfigLoader.h @@ -0,0 +1,29 @@ +#pragma once +#ifndef MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_ +#define MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_ + +#include +#include +#include "CryConfig.h" +#include + +namespace cryfs { + +class CryConfigLoader { +public: + using Cipher = blockstore::encrypted::AES256_GCM; + + static std::unique_ptr loadOrCreate(const boost::filesystem::path &filename); + + static std::unique_ptr createNew(const boost::filesystem::path &filename); + static std::unique_ptr loadExisting(const boost::filesystem::path &filename); + +private: + static void _initializeConfig(CryConfig *config); + static void _generateEncKey(CryConfig *config); + static void _generateRootBlobKey(CryConfig *config); +}; + +} + +#endif diff --git a/src/CryDevice.cpp b/src/CryDevice.cpp index 3c41724e..7a50e141 100644 --- a/src/CryDevice.cpp +++ b/src/CryDevice.cpp @@ -35,7 +35,7 @@ namespace cryfs { constexpr uint32_t CryDevice::BLOCKSIZE_BYTES; CryDevice::CryDevice(unique_ptr config, unique_ptr blockStore) -: _blobStore(make_unique(make_unique(make_unique>(std::move(blockStore), GetOrCreateEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) { +: _blobStore(make_unique(make_unique(make_unique>(std::move(blockStore), GetEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) { } Key CryDevice::GetOrCreateRootKey(CryConfig *config) { @@ -43,25 +43,15 @@ Key CryDevice::GetOrCreateRootKey(CryConfig *config) { if (root_key == "") { auto new_key = CreateRootBlobAndReturnKey(); config->SetRootBlob(new_key.ToString()); + config->save(); return new_key; } return Key::FromString(root_key); } -CryDevice::Cipher::EncryptionKey CryDevice::GetOrCreateEncryptionKey(CryConfig *config) { - string encryption_key = config->EncryptionKey(); - if (encryption_key == "") { - printf("Generating secure encryption key..."); - fflush(stdout); - auto new_key = Cipher::EncryptionKey::CreateOSRandom(); - printf("done\n"); - fflush(stdout); - config->SetEncryptionKey(new_key.ToString()); - return new_key; - } - - return Cipher::EncryptionKey::FromString(encryption_key); +CryDevice::Cipher::EncryptionKey CryDevice::GetEncryptionKey(CryConfig *config) { + return Cipher::EncryptionKey::FromString(config->EncryptionKey()); } Key CryDevice::CreateRootBlobAndReturnKey() { diff --git a/src/CryDevice.h b/src/CryDevice.h index 0363bcf8..8a56c40e 100644 --- a/src/CryDevice.h +++ b/src/CryDevice.h @@ -4,10 +4,9 @@ #include #include -#include "CryConfig.h" +#include "CryConfigLoader.h" #include -#include #include #include "messmer/cpp-utils/macros.h" @@ -19,7 +18,7 @@ class CryDevice: public fspp::Device { public: static constexpr uint32_t BLOCKSIZE_BYTES = 32 * 1024; - using Cipher = blockstore::encrypted::AES256_GCM; + using Cipher = CryConfigLoader::Cipher; CryDevice(std::unique_ptr config, std::unique_ptr blockStore); virtual ~CryDevice(); @@ -36,7 +35,7 @@ public: private: blockstore::Key GetOrCreateRootKey(CryConfig *config); - Cipher::EncryptionKey GetOrCreateEncryptionKey(CryConfig *config); + Cipher::EncryptionKey GetEncryptionKey(CryConfig *config); blockstore::Key CreateRootBlobAndReturnKey(); std::unique_ptr _blobStore; diff --git a/src/main.cpp b/src/main.cpp index 27415309..edbcee53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "messmer/fspp/fuse/Fuse.h" #include "messmer/fspp/impl/FilesystemImpl.h" #include "CryDevice.h" +#include "CryConfigLoader.h" namespace bf = boost::filesystem; @@ -19,7 +20,7 @@ using std::make_unique; int main (int argc, char *argv[]) { auto blockStore = make_unique(bf::path("/home/heinzi/cryfstest/root")); - auto config = make_unique(bf::path("/home/heinzi/cryfstest/config.json")); + auto config = cryfs::CryConfigLoader::loadOrCreate(bf::path("/home/heinzi/cryfstest/config.json")); cryfs::CryDevice device(std::move(config), std::move(blockStore)); fspp::FilesystemImpl fsimpl(&device); fspp::fuse::Fuse fuse(&fsimpl);