2015-06-16 18:20:31 +02:00
|
|
|
#include "CryConfigLoader.h"
|
2015-10-19 02:46:47 +02:00
|
|
|
#include "CryConfigFile.h"
|
2015-06-16 18:20:31 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-10-22 18:48:04 +02:00
|
|
|
#include <messmer/cpp-utils/random/Random.h>
|
2015-10-26 16:36:57 +01:00
|
|
|
#include <messmer/cpp-utils/logging/logging.h>
|
2015-06-16 18:20:31 +02:00
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
2015-06-18 13:45:08 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2015-09-12 20:16:13 +02:00
|
|
|
using cpputils::Console;
|
|
|
|
using cpputils::IOStreamConsole;
|
2015-10-24 19:35:37 +02:00
|
|
|
using cpputils::Random;
|
|
|
|
using cpputils::RandomGenerator;
|
2015-11-04 05:27:00 +01:00
|
|
|
using cpputils::SCryptSettings;
|
2015-06-18 13:45:08 +02:00
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
2015-07-01 14:33:18 +02:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2015-10-24 19:35:37 +02:00
|
|
|
using std::function;
|
2015-11-04 05:27:00 +01:00
|
|
|
using std::shared_ptr;
|
2015-10-26 16:36:57 +01:00
|
|
|
using namespace cpputils::logging;
|
2015-06-16 18:20:31 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
2015-11-04 05:27:00 +01:00
|
|
|
CryConfigLoader::CryConfigLoader(unique_ref<Console> console, RandomGenerator &keyGenerator, const SCryptSettings &scryptSettings, function<string()> askPassword, const optional<string> &cipher)
|
|
|
|
: _creator(std::move(console), keyGenerator), _scryptSettings(scryptSettings), _askPassword(askPassword), _cipher(cipher) {
|
2015-10-24 19:35:37 +02:00
|
|
|
}
|
2015-07-26 13:09:55 +02:00
|
|
|
|
2015-10-26 16:36:57 +01:00
|
|
|
optional<CryConfigFile> CryConfigLoader::_loadConfig(const bf::path &filename) {
|
2015-10-24 19:35:37 +02:00
|
|
|
string password = _askPassword();
|
|
|
|
auto config = CryConfigFile::load(filename, password);
|
|
|
|
if (config == none) {
|
2015-10-26 16:36:57 +01:00
|
|
|
LOG(ERROR) << "Could not load config file. Wrong password?";
|
|
|
|
return none;
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
2015-10-30 19:53:15 +01:00
|
|
|
if (_cipher != none && config->config()->Cipher() != *_cipher) {
|
|
|
|
throw std::runtime_error("Filesystem uses "+config->config()->Cipher()+" cipher and not "+*_cipher+" as specified.");
|
|
|
|
}
|
2015-10-24 19:35:37 +02:00
|
|
|
return std::move(*config);
|
|
|
|
}
|
|
|
|
|
2015-11-04 05:27:00 +01:00
|
|
|
optional<CryConfigFile> CryConfigLoader::loadOrCreate(const bf::path &filename) {
|
|
|
|
if (bf::exists(filename)) {
|
|
|
|
return _loadConfig(filename);
|
|
|
|
} else {
|
|
|
|
return _createConfig(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CryConfigFile CryConfigLoader::_createConfig(const bf::path &filename) {
|
|
|
|
auto config = _creator.create(_cipher);
|
|
|
|
//TODO Ask confirmation if using insecure password (<8 characters)
|
|
|
|
string password = _askPassword();
|
|
|
|
return CryConfigFile::create(filename, std::move(config), password, _scryptSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|