2015-06-16 18:20:31 +02:00
|
|
|
#include "CryConfigLoader.h"
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
2015-06-18 13:45:08 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2015-06-16 18:20:31 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
2015-06-18 13:45:08 +02:00
|
|
|
unique_ref<CryConfig> CryConfigLoader::loadOrCreate(const bf::path &filename) {
|
2015-06-16 18:20:31 +02:00
|
|
|
auto config = loadExisting(filename);
|
2015-06-18 13:45:08 +02:00
|
|
|
if (config != none) {
|
|
|
|
return std::move(*config);
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
|
|
|
return createNew(filename);
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:45:08 +02:00
|
|
|
unique_ref<CryConfig> CryConfigLoader::createNew(const bf::path &filename) {
|
|
|
|
auto config = make_unique_ref<CryConfig>(filename);
|
2015-06-16 18:20:31 +02:00
|
|
|
_initializeConfig(config.get());
|
|
|
|
config->save();
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CryConfigLoader::_initializeConfig(CryConfig *config) {
|
|
|
|
_generateEncKey(config);
|
|
|
|
_generateRootBlobKey(config);
|
|
|
|
}
|
|
|
|
|
2015-06-17 12:28:18 +02:00
|
|
|
void CryConfigLoader::_initializeConfigWithWeakKey(CryConfig *config) {
|
|
|
|
_generateWeakEncKey(config);
|
|
|
|
_generateRootBlobKey(config);
|
|
|
|
}
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-17 12:28:18 +02:00
|
|
|
void CryConfigLoader::_generateWeakEncKey(CryConfig *config) {
|
|
|
|
auto new_key = Cipher::EncryptionKey::CreatePseudoRandom();
|
|
|
|
config->SetEncryptionKey(new_key.ToString());
|
|
|
|
}
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
void CryConfigLoader::_generateRootBlobKey(CryConfig *config) {
|
|
|
|
//An empty root blob entry will tell CryDevice to create a new root blob
|
|
|
|
config->SetRootBlob("");
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:45:08 +02:00
|
|
|
optional<unique_ref<CryConfig>> CryConfigLoader::loadExisting(const bf::path &filename) {
|
2015-06-16 18:20:31 +02:00
|
|
|
if (bf::exists(filename)) {
|
2015-06-18 13:45:08 +02:00
|
|
|
return make_unique_ref<CryConfig>(filename);
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
2015-06-18 13:45:08 +02:00
|
|
|
return none;
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
|
|
|
|
2015-06-18 13:45:08 +02:00
|
|
|
unique_ref<CryConfig> CryConfigLoader::loadOrCreateWithWeakKey(const bf::path &filename) {
|
2015-06-17 12:28:18 +02:00
|
|
|
auto config = loadExisting(filename);
|
2015-06-18 13:45:08 +02:00
|
|
|
if (config != none) {
|
|
|
|
return std::move(*config);
|
2015-06-17 12:28:18 +02:00
|
|
|
}
|
|
|
|
return createNewWithWeakKey(filename);
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:45:08 +02:00
|
|
|
unique_ref<CryConfig> CryConfigLoader::createNewWithWeakKey(const bf::path &filename) {
|
|
|
|
auto config = make_unique_ref<CryConfig>(filename);
|
2015-06-17 12:28:18 +02:00
|
|
|
_initializeConfigWithWeakKey(config.get());
|
|
|
|
config->save();
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|