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-07-01 14:33:18 +02:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2015-06-16 18:20:31 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
2015-07-26 13:09:55 +02:00
|
|
|
CryConfigLoader::CryConfigLoader(): CryConfigLoader(make_unique_ref<IOStreamConsole>()) {}
|
|
|
|
|
|
|
|
CryConfigLoader::CryConfigLoader(unique_ref<Console> console) : _console(std::move(console)) {}
|
|
|
|
|
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) {
|
2015-07-01 14:33:18 +02:00
|
|
|
_generateCipher(config);
|
2015-06-16 18:20:31 +02:00
|
|
|
_generateEncKey(config);
|
|
|
|
_generateRootBlobKey(config);
|
|
|
|
}
|
|
|
|
|
2015-06-17 12:28:18 +02:00
|
|
|
void CryConfigLoader::_initializeConfigWithWeakKey(CryConfig *config) {
|
2015-07-01 14:33:18 +02:00
|
|
|
_generateTestCipher(config);
|
2015-06-17 12:28:18 +02:00
|
|
|
_generateWeakEncKey(config);
|
|
|
|
_generateRootBlobKey(config);
|
|
|
|
}
|
|
|
|
|
2015-07-01 14:33:18 +02:00
|
|
|
void CryConfigLoader::_generateCipher(CryConfig *config) {
|
2015-09-04 16:02:55 +02:00
|
|
|
vector<string> ciphers = CryCiphers::supportedCipherNames();
|
|
|
|
string cipherName = "";
|
|
|
|
bool askAgain = true;
|
|
|
|
while(askAgain) {
|
|
|
|
int cipherIndex = _console->ask("Which block cipher do you want to use?", ciphers);
|
|
|
|
cipherName = ciphers[cipherIndex];
|
|
|
|
askAgain = !_showWarningForCipherAndReturnIfOk(cipherName);
|
|
|
|
};
|
|
|
|
config->SetCipher(cipherName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CryConfigLoader::_showWarningForCipherAndReturnIfOk(const string &cipherName) {
|
|
|
|
auto warning = CryCiphers::find(cipherName).warning();
|
|
|
|
if (warning == boost::none) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return _console->askYesNo(string() + (*warning) + " Do you want to take this cipher nevertheless?");
|
2015-07-01 14:33:18 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
void CryConfigLoader::_generateEncKey(CryConfig *config) {
|
2015-09-04 16:09:37 +02:00
|
|
|
_console->print("\nGenerating secure encryption key...");
|
2015-09-01 00:25:14 +02:00
|
|
|
config->SetEncryptionKey(CryCiphers::find(config->Cipher()).createKey());
|
2015-07-26 14:42:43 +02:00
|
|
|
_console->print("done\n");
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-01 00:25:14 +02:00
|
|
|
void CryConfigLoader::_generateTestCipher(CryConfig *config) {
|
|
|
|
config->SetCipher("aes-256-gcm");
|
|
|
|
}
|
|
|
|
|
2015-06-17 12:28:18 +02:00
|
|
|
void CryConfigLoader::_generateWeakEncKey(CryConfig *config) {
|
2015-09-01 00:25:14 +02:00
|
|
|
auto new_key = blockstore::encrypted::AES256_GCM::EncryptionKey::CreatePseudoRandom();
|
2015-06-17 12:28:18 +02:00
|
|
|
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
|
|
|
}
|