2015-06-16 18:20:31 +02:00
|
|
|
#pragma once
|
2015-10-15 13:06:51 +02:00
|
|
|
#ifndef MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGLOADER_H_
|
|
|
|
#define MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGLOADER_H_
|
2015-06-16 18:20:31 +02:00
|
|
|
|
2015-06-21 17:44:45 +02:00
|
|
|
#include <messmer/cpp-utils/pointer/unique_ref.h>
|
2015-10-27 13:28:42 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-10-19 02:46:47 +02:00
|
|
|
#include "CryConfigFile.h"
|
2015-09-01 00:25:14 +02:00
|
|
|
#include "CryCipher.h"
|
2015-10-19 02:46:47 +02:00
|
|
|
#include "CryConfigCreator.h"
|
2015-10-27 13:28:42 +01:00
|
|
|
#include "crypto/kdf/Scrypt.h"
|
2015-06-16 18:20:31 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
|
|
|
class CryConfigLoader {
|
|
|
|
public:
|
2015-10-24 19:35:37 +02:00
|
|
|
CryConfigLoader(cpputils::unique_ref<cpputils::Console> console, cpputils::RandomGenerator &keyGenerator, std::function<std::string()> askPassword);
|
|
|
|
CryConfigLoader(CryConfigLoader &&rhs) = default;
|
2015-06-16 18:20:31 +02:00
|
|
|
|
2015-10-27 13:28:42 +01:00
|
|
|
template<class SCryptSettings = SCryptDefaultSettings>
|
2015-10-26 16:36:57 +01:00
|
|
|
boost::optional<CryConfigFile> loadOrCreate(const boost::filesystem::path &filename);
|
2015-07-26 13:09:55 +02:00
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
private:
|
2015-10-26 16:36:57 +01:00
|
|
|
boost::optional<CryConfigFile> _loadConfig(const boost::filesystem::path &filename);
|
2015-10-27 13:28:42 +01:00
|
|
|
template<class SCryptSettings>
|
2015-10-24 19:35:37 +02:00
|
|
|
CryConfigFile _createConfig(const boost::filesystem::path &filename);
|
|
|
|
|
|
|
|
CryConfigCreator _creator;
|
|
|
|
std::function<std::string()> _askPassword;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CryConfigLoader);
|
2015-06-16 18:20:31 +02:00
|
|
|
};
|
|
|
|
|
2015-10-27 13:28:42 +01:00
|
|
|
template<class SCryptSettings>
|
|
|
|
boost::optional<CryConfigFile> CryConfigLoader::loadOrCreate(const boost::filesystem::path &filename) {
|
|
|
|
if (boost::filesystem::exists(filename)) {
|
|
|
|
return _loadConfig(filename);
|
|
|
|
} else {
|
|
|
|
return _createConfig<SCryptSettings>(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class SCryptSettings>
|
|
|
|
CryConfigFile CryConfigLoader::_createConfig(const boost::filesystem::path &filename) {
|
|
|
|
auto config = _creator.create();
|
|
|
|
//TODO Ask confirmation if using insecure password (<8 characters)
|
|
|
|
std::string password = _askPassword();
|
|
|
|
return CryConfigFile::create<SCryptSettings>(filename, std::move(config), password);
|
|
|
|
}
|
|
|
|
|
2015-06-16 18:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|