Refactored creation of new config files - this happens in a CryConfigLoader now

This commit is contained in:
Sebastian Messmer 2015-06-16 18:20:31 +02:00
parent fbb16915d3
commit aed30a0ec1
6 changed files with 91 additions and 20 deletions

View File

@ -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;

51
src/CryConfigLoader.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "CryConfigLoader.h"
#include <boost/filesystem.hpp>
namespace bf = boost::filesystem;
using std::unique_ptr;
using std::make_unique;
namespace cryfs {
unique_ptr<CryConfig> CryConfigLoader::loadOrCreate(const bf::path &filename) {
auto config = loadExisting(filename);
if (config.get() != nullptr) {
return config;
}
return createNew(filename);
}
unique_ptr<CryConfig> CryConfigLoader::createNew(const bf::path &filename) {
auto config = make_unique<CryConfig>(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<CryConfig> CryConfigLoader::loadExisting(const bf::path &filename) {
if (bf::exists(filename)) {
return make_unique<CryConfig>(filename);
}
return nullptr;
}
}

29
src/CryConfigLoader.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#ifndef MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_
#define MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_
#include <memory>
#include <boost/filesystem/path.hpp>
#include "CryConfig.h"
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
namespace cryfs {
class CryConfigLoader {
public:
using Cipher = blockstore::encrypted::AES256_GCM;
static std::unique_ptr<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> createNew(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> loadExisting(const boost::filesystem::path &filename);
private:
static void _initializeConfig(CryConfig *config);
static void _generateEncKey(CryConfig *config);
static void _generateRootBlobKey(CryConfig *config);
};
}
#endif

View File

@ -35,7 +35,7 @@ namespace cryfs {
constexpr uint32_t CryDevice::BLOCKSIZE_BYTES;
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _blobStore(make_unique<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore<Cipher>>(std::move(blockStore), GetOrCreateEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
: _blobStore(make_unique<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore<Cipher>>(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() {

View File

@ -4,10 +4,9 @@
#include <messmer/blockstore/interface/BlockStore.h>
#include <messmer/blobstore/interface/BlobStore.h>
#include "CryConfig.h"
#include "CryConfigLoader.h"
#include <boost/filesystem.hpp>
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
#include <messmer/fspp/fs_interface/Device.h>
#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<CryConfig> config, std::unique_ptr<blockstore::BlockStore> 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::BlobStore> _blobStore;

View File

@ -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<OnDiskBlockStore>(bf::path("/home/heinzi/cryfstest/root"));
auto config = make_unique<cryfs::CryConfig>(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);