MyClientId is generated outside of CryDevice to give the CryConfigCreator access to it.

This commit is contained in:
Sebastian Messmer 2016-06-26 16:53:10 -07:00
parent 7b56706c1a
commit be8dffb24d
20 changed files with 132 additions and 81 deletions

View File

@ -191,7 +191,7 @@ namespace cryfs {
return *configFile; return *configFile;
} }
CryConfigFile Cli::_loadOrCreateConfig(const ProgramOptions &options) { CryConfigLoader::ConfigLoadResult Cli::_loadOrCreateConfig(const ProgramOptions &options) {
try { try {
auto configFile = _determineConfigFile(options); auto configFile = _determineConfigFile(options);
auto config = _loadOrCreateConfigFile(configFile, options.cipher(), options.blocksizeBytes()); auto config = _loadOrCreateConfigFile(configFile, options.cipher(), options.blocksizeBytes());
@ -206,7 +206,7 @@ namespace cryfs {
} }
} }
optional<CryConfigFile> Cli::_loadOrCreateConfigFile(const bf::path &configFilePath, const optional<string> &cipher, const optional<uint32_t> &blocksizeBytes) { optional<CryConfigLoader::ConfigLoadResult> Cli::_loadOrCreateConfigFile(const bf::path &configFilePath, const optional<string> &cipher, const optional<uint32_t> &blocksizeBytes) {
if (_noninteractive) { if (_noninteractive) {
return CryConfigLoader(_console, _keyGenerator, _scryptSettings, return CryConfigLoader(_console, _keyGenerator, _scryptSettings,
&Cli::_askPasswordNoninteractive, &Cli::_askPasswordNoninteractive,
@ -224,7 +224,7 @@ namespace cryfs {
try { try {
auto blockStore = make_unique_ref<OnDiskBlockStore>(options.baseDir()); auto blockStore = make_unique_ref<OnDiskBlockStore>(options.baseDir());
auto config = _loadOrCreateConfig(options); auto config = _loadOrCreateConfig(options);
CryDevice device(std::move(config), std::move(blockStore)); CryDevice device(std::move(config.configFile), std::move(blockStore), config.myClientId);
_sanityCheckFilesystem(&device); _sanityCheckFilesystem(&device);
fspp::FilesystemImpl fsimpl(&device); fspp::FilesystemImpl fsimpl(&device);
fspp::fuse::Fuse fuse(&fsimpl, "cryfs", "cryfs@"+options.baseDir().native()); fspp::fuse::Fuse fuse(&fsimpl, "cryfs", "cryfs@"+options.baseDir().native());

View File

@ -11,6 +11,7 @@
#include <cpp-utils/network/HttpClient.h> #include <cpp-utils/network/HttpClient.h>
#include <cryfs/filesystem/CryDevice.h> #include <cryfs/filesystem/CryDevice.h>
#include "CallAfterTimeout.h" #include "CallAfterTimeout.h"
#include <cryfs/config/CryConfigLoader.h>
namespace cryfs { namespace cryfs {
class Cli final { class Cli final {
@ -21,8 +22,8 @@ namespace cryfs {
private: private:
void _checkForUpdates(); void _checkForUpdates();
void _runFilesystem(const program_options::ProgramOptions &options); void _runFilesystem(const program_options::ProgramOptions &options);
CryConfigFile _loadOrCreateConfig(const program_options::ProgramOptions &options); CryConfigLoader::ConfigLoadResult _loadOrCreateConfig(const program_options::ProgramOptions &options);
boost::optional<CryConfigFile> _loadOrCreateConfigFile(const boost::filesystem::path &configFilePath, const boost::optional<std::string> &cipher, const boost::optional<uint32_t> &blocksizeBytes); boost::optional<CryConfigLoader::ConfigLoadResult> _loadOrCreateConfigFile(const boost::filesystem::path &configFilePath, const boost::optional<std::string> &cipher, const boost::optional<uint32_t> &blocksizeBytes);
boost::filesystem::path _determineConfigFile(const program_options::ProgramOptions &options); boost::filesystem::path _determineConfigFile(const program_options::ProgramOptions &options);
static std::string _askPasswordForExistingFilesystem(); static std::string _askPasswordForExistingFilesystem();
static std::string _askPasswordForNewFilesystem(); static std::string _askPasswordForNewFilesystem();

View File

@ -40,7 +40,8 @@ set(LIB_SOURCES
filesystem/cachingfsblobstore/SymlinkBlobRef.cpp filesystem/cachingfsblobstore/SymlinkBlobRef.cpp
filesystem/CryFile.cpp filesystem/CryFile.cpp
filesystem/CryDevice.cpp filesystem/CryDevice.cpp
filesystem/MyClientId.cpp localstate/MyClientId.cpp
localstate/LocalStateDir.cpp
) )
add_library(${PROJECT_NAME} STATIC ${LIB_SOURCES}) add_library(${PROJECT_NAME} STATIC ${LIB_SOURCES})

View File

@ -2,6 +2,8 @@
#include "CryCipher.h" #include "CryCipher.h"
#include <gitversion/gitversion.h> #include <gitversion/gitversion.h>
#include <cpp-utils/random/Random.h> #include <cpp-utils/random/Random.h>
#include <cryfs/localstate/LocalStateDir.h>
#include <cryfs/localstate/MyClientId.h>
using cpputils::Console; using cpputils::Console;
using cpputils::unique_ref; using cpputils::unique_ref;
@ -19,7 +21,7 @@ namespace cryfs {
:_console(console), _configConsole(console, noninteractive), _encryptionKeyGenerator(encryptionKeyGenerator) { :_console(console), _configConsole(console, noninteractive), _encryptionKeyGenerator(encryptionKeyGenerator) {
} }
CryConfig CryConfigCreator::create(const optional<string> &cipherFromCommandLine, const optional<uint32_t> &blocksizeBytesFromCommandLine) { CryConfigCreator::ConfigCreateResult CryConfigCreator::create(const optional<string> &cipherFromCommandLine, const optional<uint32_t> &blocksizeBytesFromCommandLine) {
CryConfig config; CryConfig config;
config.SetCipher(_generateCipher(cipherFromCommandLine)); config.SetCipher(_generateCipher(cipherFromCommandLine));
config.SetVersion(gitversion::VersionString()); config.SetVersion(gitversion::VersionString());
@ -31,7 +33,8 @@ namespace cryfs {
#ifndef CRYFS_NO_COMPATIBILITY #ifndef CRYFS_NO_COMPATIBILITY
config.SetHasVersionNumbers(true); config.SetHasVersionNumbers(true);
#endif #endif
return config; uint32_t myClientId = MyClientId(LocalStateDir::forFilesystemId(config.FilesystemId())).loadOrGenerate();
return ConfigCreateResult {std::move(config), myClientId};
} }
uint32_t CryConfigCreator::_generateBlocksizeBytes(const optional<uint32_t> &blocksizeBytesFromCommandLine) { uint32_t CryConfigCreator::_generateBlocksizeBytes(const optional<uint32_t> &blocksizeBytesFromCommandLine) {

View File

@ -14,7 +14,12 @@ namespace cryfs {
CryConfigCreator(std::shared_ptr<cpputils::Console> console, cpputils::RandomGenerator &encryptionKeyGenerator, bool noninteractive); CryConfigCreator(std::shared_ptr<cpputils::Console> console, cpputils::RandomGenerator &encryptionKeyGenerator, bool noninteractive);
CryConfigCreator(CryConfigCreator &&rhs) = default; CryConfigCreator(CryConfigCreator &&rhs) = default;
CryConfig create(const boost::optional<std::string> &cipherFromCommandLine, const boost::optional<uint32_t> &blocksizeBytesFromCommandLine); struct ConfigCreateResult {
CryConfig config;
uint32_t myClientId;
};
ConfigCreateResult create(const boost::optional<std::string> &cipherFromCommandLine, const boost::optional<uint32_t> &blocksizeBytesFromCommandLine);
private: private:
std::string _generateCipher(const boost::optional<std::string> &cipherFromCommandLine); std::string _generateCipher(const boost::optional<std::string> &cipherFromCommandLine);
std::string _generateEncKey(const std::string &cipher); std::string _generateEncKey(const std::string &cipher);

View File

@ -6,6 +6,8 @@
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <gitversion/gitversion.h> #include <gitversion/gitversion.h>
#include <gitversion/VersionCompare.h> #include <gitversion/VersionCompare.h>
#include "../localstate/LocalStateDir.h"
#include "../localstate/MyClientId.h"
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
using cpputils::unique_ref; using cpputils::unique_ref;
@ -22,6 +24,8 @@ using std::vector;
using std::string; using std::string;
using std::function; using std::function;
using std::shared_ptr; using std::shared_ptr;
using std::unique_ptr;
using std::make_unique;
using gitversion::VersionCompare; using gitversion::VersionCompare;
using namespace cpputils::logging; using namespace cpputils::logging;
@ -33,7 +37,7 @@ CryConfigLoader::CryConfigLoader(shared_ptr<Console> console, RandomGenerator &k
_cipherFromCommandLine(cipherFromCommandLine), _blocksizeBytesFromCommandLine(blocksizeBytesFromCommandLine) { _cipherFromCommandLine(cipherFromCommandLine), _blocksizeBytesFromCommandLine(blocksizeBytesFromCommandLine) {
} }
optional<CryConfigFile> CryConfigLoader::_loadConfig(const bf::path &filename) { optional<CryConfigLoader::ConfigLoadResult> CryConfigLoader::_loadConfig(const bf::path &filename) {
string password = _askPasswordForExistingFilesystem(); string password = _askPasswordForExistingFilesystem();
std::cout << "Loading config file (this can take some time)..." << std::flush; std::cout << "Loading config file (this can take some time)..." << std::flush;
auto config = CryConfigFile::load(filename, password); auto config = CryConfigFile::load(filename, password);
@ -53,7 +57,8 @@ optional<CryConfigFile> CryConfigLoader::_loadConfig(const bf::path &filename) {
config->save(); config->save();
} }
_checkCipher(*config->config()); _checkCipher(*config->config());
return std::move(*config); uint32_t myClientId = MyClientId(LocalStateDir::forFilesystemId(config->config()->FilesystemId())).loadOrGenerate();
return ConfigLoadResult {std::move(*config), myClientId};
} }
void CryConfigLoader::_checkVersion(const CryConfig &config) { void CryConfigLoader::_checkVersion(const CryConfig &config) {
@ -75,7 +80,7 @@ void CryConfigLoader::_checkCipher(const CryConfig &config) const {
} }
} }
optional<CryConfigFile> CryConfigLoader::loadOrCreate(const bf::path &filename) { optional<CryConfigLoader::ConfigLoadResult> CryConfigLoader::loadOrCreate(const bf::path &filename) {
if (bf::exists(filename)) { if (bf::exists(filename)) {
return _loadConfig(filename); return _loadConfig(filename);
} else { } else {
@ -83,14 +88,14 @@ optional<CryConfigFile> CryConfigLoader::loadOrCreate(const bf::path &filename)
} }
} }
CryConfigFile CryConfigLoader::_createConfig(const bf::path &filename) { CryConfigLoader::ConfigLoadResult CryConfigLoader::_createConfig(const bf::path &filename) {
auto config = _creator.create(_cipherFromCommandLine, _blocksizeBytesFromCommandLine); auto config = _creator.create(_cipherFromCommandLine, _blocksizeBytesFromCommandLine);
//TODO Ask confirmation if using insecure password (<8 characters) //TODO Ask confirmation if using insecure password (<8 characters)
string password = _askPasswordForNewFilesystem(); string password = _askPasswordForNewFilesystem();
std::cout << "Creating config file (this can take some time)..." << std::flush; std::cout << "Creating config file (this can take some time)..." << std::flush;
auto result = CryConfigFile::create(filename, std::move(config), password, _scryptSettings); auto result = CryConfigFile::create(filename, std::move(config.config), password, _scryptSettings);
std::cout << "done" << std::endl; std::cout << "done" << std::endl;
return result; return ConfigLoadResult {std::move(result), config.myClientId};
} }

View File

@ -16,11 +16,16 @@ public:
CryConfigLoader(std::shared_ptr<cpputils::Console> console, cpputils::RandomGenerator &keyGenerator, const cpputils::SCryptSettings &scryptSettings, std::function<std::string()> askPasswordForExistingFilesystem, std::function<std::string()> askPasswordForNewFilesystem, const boost::optional<std::string> &cipherFromCommandLine, const boost::optional<uint32_t> &blocksizeBytesFromCommandLine, bool noninteractive); CryConfigLoader(std::shared_ptr<cpputils::Console> console, cpputils::RandomGenerator &keyGenerator, const cpputils::SCryptSettings &scryptSettings, std::function<std::string()> askPasswordForExistingFilesystem, std::function<std::string()> askPasswordForNewFilesystem, const boost::optional<std::string> &cipherFromCommandLine, const boost::optional<uint32_t> &blocksizeBytesFromCommandLine, bool noninteractive);
CryConfigLoader(CryConfigLoader &&rhs) = default; CryConfigLoader(CryConfigLoader &&rhs) = default;
boost::optional<CryConfigFile> loadOrCreate(const boost::filesystem::path &filename); struct ConfigLoadResult {
CryConfigFile configFile;
uint32_t myClientId;
};
boost::optional<ConfigLoadResult> loadOrCreate(const boost::filesystem::path &filename);
private: private:
boost::optional<CryConfigFile> _loadConfig(const boost::filesystem::path &filename); boost::optional<ConfigLoadResult> _loadConfig(const boost::filesystem::path &filename);
CryConfigFile _createConfig(const boost::filesystem::path &filename); ConfigLoadResult _createConfig(const boost::filesystem::path &filename);
void _checkVersion(const CryConfig &config); void _checkVersion(const CryConfig &config);
void _checkCipher(const CryConfig &config) const; void _checkCipher(const CryConfig &config) const;

View File

@ -17,7 +17,8 @@
#include "../config/CryCipher.h" #include "../config/CryCipher.h"
#include <cpp-utils/system/homedir.h> #include <cpp-utils/system/homedir.h>
#include <gitversion/VersionCompare.h> #include <gitversion/VersionCompare.h>
#include "MyClientId.h" #include "cryfs/localstate/MyClientId.h"
#include "cryfs/localstate/LocalStateDir.h"
using std::string; using std::string;
@ -52,12 +53,12 @@ namespace bf = boost::filesystem;
namespace cryfs { namespace cryfs {
CryDevice::CryDevice(CryConfigFile configFile, unique_ref<BlockStore> blockStore) CryDevice::CryDevice(CryConfigFile configFile, unique_ref<BlockStore> blockStore, uint32_t myClientId)
: _fsBlobStore( : _fsBlobStore(
make_unique_ref<ParallelAccessFsBlobStore>( make_unique_ref<ParallelAccessFsBlobStore>(
make_unique_ref<CachingFsBlobStore>( make_unique_ref<CachingFsBlobStore>(
make_unique_ref<FsBlobStore>( make_unique_ref<FsBlobStore>(
CreateBlobStore(std::move(blockStore), &configFile) CreateBlobStore(std::move(blockStore), &configFile, myClientId)
) )
) )
) )
@ -66,8 +67,8 @@ CryDevice::CryDevice(CryConfigFile configFile, unique_ref<BlockStore> blockStore
_onFsAction() { _onFsAction() {
} }
unique_ref<blobstore::BlobStore> CryDevice::CreateBlobStore(unique_ref<BlockStore> blockStore, CryConfigFile *configFile) { unique_ref<blobstore::BlobStore> CryDevice::CreateBlobStore(unique_ref<BlockStore> blockStore, CryConfigFile *configFile, uint32_t myClientId) {
auto versionCountingEncryptedBlockStore = CreateVersionCountingEncryptedBlockStore(std::move(blockStore), configFile); auto versionCountingEncryptedBlockStore = CreateVersionCountingEncryptedBlockStore(std::move(blockStore), configFile, myClientId);
// Create versionCountingEncryptedBlockStore not in the same line as BlobStoreOnBlocks, because it can modify BlocksizeBytes // Create versionCountingEncryptedBlockStore not in the same line as BlobStoreOnBlocks, because it can modify BlocksizeBytes
// in the configFile and therefore has to be run before the second parameter to the BlobStoreOnBlocks parameter is evaluated. // in the configFile and therefore has to be run before the second parameter to the BlobStoreOnBlocks parameter is evaluated.
return make_unique_ref<BlobStoreOnBlocks>( return make_unique_ref<BlobStoreOnBlocks>(
@ -77,11 +78,10 @@ unique_ref<blobstore::BlobStore> CryDevice::CreateBlobStore(unique_ref<BlockStor
configFile->config()->BlocksizeBytes()); configFile->config()->BlocksizeBytes());
} }
unique_ref<BlockStore> CryDevice::CreateVersionCountingEncryptedBlockStore(unique_ref<BlockStore> blockStore, CryConfigFile *configFile) { unique_ref<BlockStore> CryDevice::CreateVersionCountingEncryptedBlockStore(unique_ref<BlockStore> blockStore, CryConfigFile *configFile, uint32_t myClientId) {
auto encryptedBlockStore = CreateEncryptedBlockStore(*configFile->config(), std::move(blockStore)); auto encryptedBlockStore = CreateEncryptedBlockStore(*configFile->config(), std::move(blockStore));
auto statePath = _statePath(configFile->config()->FilesystemId()); auto statePath = LocalStateDir::forFilesystemId(configFile->config()->FilesystemId());
auto integrityFilePath = statePath / "integritydata"; auto integrityFilePath = statePath / "integritydata";
auto myClientId = MyClientId(statePath).loadOrGenerate();
#ifndef CRYFS_NO_COMPATIBILITY #ifndef CRYFS_NO_COMPATIBILITY
if (!configFile->config()->HasVersionNumbers()) { if (!configFile->config()->HasVersionNumbers()) {
@ -101,22 +101,6 @@ Key CryDevice::CreateRootBlobAndReturnKey() {
return rootBlob->key(); return rootBlob->key();
} }
bf::path CryDevice::_statePath(const CryConfig::FilesystemID &filesystemId) {
bf::path app_dir = cpputils::system::HomeDirectory::get() / ".cryfs";
_createDirIfNotExists(app_dir);
bf::path filesystems_dir = app_dir / "filesystems";
_createDirIfNotExists(filesystems_dir);
bf::path this_filesystem_dir = filesystems_dir / filesystemId.ToString();
_createDirIfNotExists(this_filesystem_dir);
return this_filesystem_dir;
}
void CryDevice::_createDirIfNotExists(const bf::path &path) {
if (!bf::exists(path)) {
bf::create_directory(path);
}
}
optional<unique_ref<fspp::Node>> CryDevice::Load(const bf::path &path) { optional<unique_ref<fspp::Node>> CryDevice::Load(const bf::path &path) {
// TODO Split into smaller functions // TODO Split into smaller functions
ASSERT(path.is_absolute(), "Non absolute path given"); ASSERT(path.is_absolute(), "Non absolute path given");

View File

@ -17,7 +17,7 @@ namespace cryfs {
class CryDevice final: public fspp::Device { class CryDevice final: public fspp::Device {
public: public:
CryDevice(CryConfigFile config, cpputils::unique_ref<blockstore::BlockStore> blockStore); CryDevice(CryConfigFile config, cpputils::unique_ref<blockstore::BlockStore> blockStore, uint32_t myClientId);
void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override;
@ -47,13 +47,10 @@ private:
blockstore::Key _rootKey; blockstore::Key _rootKey;
std::vector<std::function<void()>> _onFsAction; std::vector<std::function<void()>> _onFsAction;
static boost::filesystem::path _statePath(const CryConfig::FilesystemID &filesystemId);
static void _createDirIfNotExists(const boost::filesystem::path &path);
blockstore::Key GetOrCreateRootKey(CryConfigFile *config); blockstore::Key GetOrCreateRootKey(CryConfigFile *config);
blockstore::Key CreateRootBlobAndReturnKey(); blockstore::Key CreateRootBlobAndReturnKey();
static cpputils::unique_ref<blobstore::BlobStore> CreateBlobStore(cpputils::unique_ref<blockstore::BlockStore> blockStore, CryConfigFile *configFile); static cpputils::unique_ref<blobstore::BlobStore> CreateBlobStore(cpputils::unique_ref<blockstore::BlockStore> blockStore, CryConfigFile *configFile, uint32_t myClientId);
static cpputils::unique_ref<blockstore::BlockStore> CreateVersionCountingEncryptedBlockStore(cpputils::unique_ref<blockstore::BlockStore> blockStore, CryConfigFile *configFile); static cpputils::unique_ref<blockstore::BlockStore> CreateVersionCountingEncryptedBlockStore(cpputils::unique_ref<blockstore::BlockStore> blockStore, CryConfigFile *configFile, uint32_t myClientId);
static cpputils::unique_ref<blockstore::BlockStore> CreateEncryptedBlockStore(const CryConfig &config, cpputils::unique_ref<blockstore::BlockStore> baseBlockStore); static cpputils::unique_ref<blockstore::BlockStore> CreateEncryptedBlockStore(const CryConfig &config, cpputils::unique_ref<blockstore::BlockStore> baseBlockStore);
struct BlobWithParent { struct BlobWithParent {

View File

@ -0,0 +1,23 @@
#include "LocalStateDir.h"
#include <cpp-utils/system/homedir.h>
#include <boost/filesystem.hpp>
namespace bf = boost::filesystem;
namespace cryfs {
bf::path LocalStateDir::forFilesystemId(const CryConfig::FilesystemID &filesystemId) {
bf::path app_dir = cpputils::system::HomeDirectory::get() / ".cryfs";
_createDirIfNotExists(app_dir);
bf::path filesystems_dir = app_dir / "filesystems";
_createDirIfNotExists(filesystems_dir);
bf::path this_filesystem_dir = filesystems_dir / filesystemId.ToString();
_createDirIfNotExists(this_filesystem_dir);
return this_filesystem_dir;
}
void LocalStateDir::_createDirIfNotExists(const bf::path &path) {
if (!bf::exists(path)) {
bf::create_directory(path);
}
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#ifndef MESSMER_CRYFS_LOCALSTATE_LOCALSTATEDIR_H_
#define MESSMER_CRYFS_LOCALSTATE_LOCALSTATEDIR_H_
#include <cpp-utils/macros.h>
#include <boost/filesystem/path.hpp>
#include "../config/CryConfig.h"
namespace cryfs {
class LocalStateDir final {
public:
static boost::filesystem::path forFilesystemId(const CryConfig::FilesystemID &filesystemId);
private:
LocalStateDir(); // static functions only
static void _createDirIfNotExists(const boost::filesystem::path &path);
};
}
#endif

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#ifndef MESSMER_CRYFS_FILESYSTEM_MYCLIENTID_H_ #ifndef MESSMER_CRYFS_LOCALSTATE_MYCLIENTID_H_
#define MESSMER_CRYFS_FILESYSTEM_MYCLIENTID_H_ #define MESSMER_CRYFS_LOCALSTATE_MYCLIENTID_H_
#include <cpp-utils/macros.h> #include <cpp-utils/macros.h>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>

View File

@ -17,7 +17,7 @@ set(SOURCES
filesystem/CryFsTest.cpp filesystem/CryFsTest.cpp
filesystem/CryNodeTest.cpp filesystem/CryNodeTest.cpp
filesystem/FileSystemTest.cpp filesystem/FileSystemTest.cpp
filesystem/MyClientIdTest.cpp localstate/MyClientIdTest.cpp
) )
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})

View File

@ -63,91 +63,91 @@ public:
TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) { TEST_F(CryConfigCreatorTest, DoesAskForCipherIfNotSpecified) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher()); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseAnyCipher());
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) { TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfSpecified) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_DOES_NOT_ASK_FOR_CIPHER(); EXPECT_DOES_NOT_ASK_FOR_CIPHER();
CryConfig config = creator.create(string("aes-256-gcm"), none); CryConfig config = creator.create(string("aes-256-gcm"), none).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfUsingDefaultSettings) { TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfUsingDefaultSettings) {
AnswerYesToDefaultSettings(); AnswerYesToDefaultSettings();
EXPECT_DOES_NOT_ASK_FOR_CIPHER(); EXPECT_DOES_NOT_ASK_FOR_CIPHER();
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfNoninteractive) { TEST_F(CryConfigCreatorTest, DoesNotAskForCipherIfNoninteractive) {
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
EXPECT_DOES_NOT_ASK_FOR_CIPHER(); EXPECT_DOES_NOT_ASK_FOR_CIPHER();
CryConfig config = noninteractiveCreator.create(none, none); CryConfig config = noninteractiveCreator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, DoesAskForBlocksizeIfNotSpecified) { TEST_F(CryConfigCreatorTest, DoesAskForBlocksizeIfNotSpecified) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_ASK_FOR_BLOCKSIZE().WillOnce(Return(1)); EXPECT_ASK_FOR_BLOCKSIZE().WillOnce(Return(1));
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfSpecified) { TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfSpecified) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
CryConfig config = creator.create(none, 10*1024u); CryConfig config = creator.create(none, 10*1024u).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfNoninteractive) { TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfNoninteractive) {
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
CryConfig config = noninteractiveCreator.create(none, none); CryConfig config = noninteractiveCreator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfUsingDefaultSettings) { TEST_F(CryConfigCreatorTest, DoesNotAskForBlocksizeIfUsingDefaultSettings) {
AnswerYesToDefaultSettings(); AnswerYesToDefaultSettings();
EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE(); EXPECT_DOES_NOT_ASK_FOR_BLOCKSIZE();
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
} }
TEST_F(CryConfigCreatorTest, ChoosesEmptyRootBlobId) { TEST_F(CryConfigCreatorTest, ChoosesEmptyRootBlobId) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
EXPECT_EQ("", config.RootBlob()); // This tells CryFS to create a new root blob EXPECT_EQ("", config.RootBlob()); // This tells CryFS to create a new root blob
} }
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_448) { TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_448) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("mars-448-gcm")); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("mars-448-gcm"));
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
cpputils::Mars448_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid cpputils::Mars448_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
} }
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_256) { TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_256) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-256-gcm")); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-256-gcm"));
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
cpputils::AES256_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid cpputils::AES256_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
} }
TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) { TEST_F(CryConfigCreatorTest, ChoosesValidEncryptionKey_128) {
AnswerNoToDefaultSettings(); AnswerNoToDefaultSettings();
EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-128-gcm")); EXPECT_ASK_FOR_CIPHER().WillOnce(ChooseCipher("aes-128-gcm"));
CryConfig config = creator.create(none, none); CryConfig config = creator.create(none, none).config;
cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid cpputils::AES128_GCM::EncryptionKey::FromString(config.EncryptionKey()); // This crashes if invalid
} }
TEST_F(CryConfigCreatorTest, DoesNotAskForAnythingIfEverythingIsSpecified) { TEST_F(CryConfigCreatorTest, DoesNotAskForAnythingIfEverythingIsSpecified) {
EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS(); EXPECT_DOES_NOT_ASK_TO_USE_DEFAULT_SETTINGS();
EXPECT_DOES_NOT_ASK_FOR_CIPHER(); EXPECT_DOES_NOT_ASK_FOR_CIPHER();
CryConfig config = noninteractiveCreator.create(string("aes-256-gcm"), 10*1024u); CryConfig config = noninteractiveCreator.create(string("aes-256-gcm"), 10*1024u).config;
} }
TEST_F(CryConfigCreatorTest, SetsCorrectCreatedWithVersion) { TEST_F(CryConfigCreatorTest, SetsCorrectCreatedWithVersion) {
CryConfig config = noninteractiveCreator.create(none, none); CryConfig config = noninteractiveCreator.create(none, none).config;
EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion()); EXPECT_EQ(gitversion::VersionString(), config.CreatedWithVersion());
} }
TEST_F(CryConfigCreatorTest, SetsCorrectVersion) { TEST_F(CryConfigCreatorTest, SetsCorrectVersion) {
CryConfig config = noninteractiveCreator.create(none, none); CryConfig config = noninteractiveCreator.create(none, none).config;
EXPECT_EQ(gitversion::VersionString(), config.Version()); EXPECT_EQ(gitversion::VersionString(), config.Version());
} }

View File

@ -44,41 +44,45 @@ public:
CryConfigFile Create(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false) { CryConfigFile Create(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false) {
EXPECT_FALSE(file.exists()); EXPECT_FALSE(file.exists());
return loader(password, noninteractive, cipher).loadOrCreate(file.path()).value(); return loader(password, noninteractive, cipher).loadOrCreate(file.path()).value().configFile;
} }
optional<CryConfigFile> Load(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false) { optional<CryConfigFile> Load(const string &password = "mypassword", const optional<string> &cipher = none, bool noninteractive = false) {
EXPECT_TRUE(file.exists()); EXPECT_TRUE(file.exists());
return loader(password, noninteractive, cipher).loadOrCreate(file.path()); auto loadResult = loader(password, noninteractive, cipher).loadOrCreate(file.path());
if (loadResult == none) {
return none;
}
return std::move(loadResult->configFile);
} }
void CreateWithRootBlob(const string &rootBlob, const string &password = "mypassword") { void CreateWithRootBlob(const string &rootBlob, const string &password = "mypassword") {
auto cfg = loader(password, false).loadOrCreate(file.path()).value(); auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile;
cfg.config()->SetRootBlob(rootBlob); cfg.config()->SetRootBlob(rootBlob);
cfg.save(); cfg.save();
} }
void CreateWithCipher(const string &cipher, const string &password = "mypassword") { void CreateWithCipher(const string &cipher, const string &password = "mypassword") {
auto cfg = loader(password, false).loadOrCreate(file.path()).value(); auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile;
cfg.config()->SetCipher(cipher); cfg.config()->SetCipher(cipher);
cfg.save(); cfg.save();
} }
void CreateWithEncryptionKey(const string &encKey, const string &password = "mypassword") { void CreateWithEncryptionKey(const string &encKey, const string &password = "mypassword") {
auto cfg = loader(password, false).loadOrCreate(file.path()).value(); auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile;
cfg.config()->SetEncryptionKey(encKey); cfg.config()->SetEncryptionKey(encKey);
cfg.save(); cfg.save();
} }
void CreateWithVersion(const string &version, const string &password = "mypassword") { void CreateWithVersion(const string &version, const string &password = "mypassword") {
auto cfg = loader(password, false).loadOrCreate(file.path()).value(); auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile;
cfg.config()->SetVersion(version); cfg.config()->SetVersion(version);
cfg.config()->SetCreatedWithVersion(version); cfg.config()->SetCreatedWithVersion(version);
cfg.save(); cfg.save();
} }
void CreateWithFilesystemID(const CryConfig::FilesystemID &filesystemId, const string &password = "mypassword") { void CreateWithFilesystemID(const CryConfig::FilesystemID &filesystemId, const string &password = "mypassword") {
auto cfg = loader(password, false).loadOrCreate(file.path()).value(); auto cfg = loader(password, false).loadOrCreate(file.path()).value().configFile;
cfg.config()->SetFilesystemId(filesystemId); cfg.config()->SetFilesystemId(filesystemId);
cfg.save(); cfg.save();
} }

View File

@ -40,7 +40,7 @@ public:
CryConfigFile loadOrCreateConfig() { CryConfigFile loadOrCreateConfig() {
auto askPassword = [] {return "mypassword";}; auto askPassword = [] {return "mypassword";};
return CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true).loadOrCreate(config.path()).value(); return CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true).loadOrCreate(config.path()).value().configFile;
} }
unique_ref<OnDiskBlockStore> blockStore() { unique_ref<OnDiskBlockStore> blockStore() {
@ -53,20 +53,20 @@ public:
TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) { TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
{ {
CryDevice dev(loadOrCreateConfig(), blockStore()); CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678);
} }
CryDevice dev(loadOrCreateConfig(), blockStore()); CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678);
auto root = dev.Load(bf::path("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root.get()).get()->children(); dynamic_pointer_move<CryDir>(root.get()).get()->children();
} }
TEST_F(CryFsTest, LoadingFilesystemDoesntModifyConfigFile) { TEST_F(CryFsTest, LoadingFilesystemDoesntModifyConfigFile) {
{ {
CryDevice dev(loadOrCreateConfig(), blockStore()); CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678);
} }
Data configAfterCreating = Data::LoadFromFile(config.path()).value(); Data configAfterCreating = Data::LoadFromFile(config.path()).value();
{ {
CryDevice dev(loadOrCreateConfig(), blockStore()); CryDevice dev(loadOrCreateConfig(), blockStore(), 0x12345678);
} }
Data configAfterLoading = Data::LoadFromFile(config.path()).value(); Data configAfterLoading = Data::LoadFromFile(config.path()).value();
EXPECT_EQ(configAfterCreating, configAfterLoading); EXPECT_EQ(configAfterCreating, configAfterLoading);

View File

@ -31,7 +31,7 @@ public:
auto askPassword = [] {return "mypassword";}; auto askPassword = [] {return "mypassword";};
auto config = CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true) auto config = CryConfigLoader(mockConsole(), Random::PseudoRandom(), SCrypt::TestSettings, askPassword, askPassword, none, none, true)
.loadOrCreate(configFile.path()).value(); .loadOrCreate(configFile.path()).value();
return make_unique_ref<CryDevice>(std::move(config), std::move(blockStore)); return make_unique_ref<CryDevice>(std::move(config.configFile), std::move(blockStore), config.myClientId);
} }
cpputils::TempFile configFile; cpputils::TempFile configFile;

View File

@ -11,7 +11,7 @@ class CryTestBase : public TestWithFakeHomeDirectory {
public: public:
CryTestBase(): _configFile(false), _device(nullptr) { CryTestBase(): _configFile(false), _device(nullptr) {
auto fakeBlockStore = cpputils::make_unique_ref<blockstore::testfake::FakeBlockStore>(); auto fakeBlockStore = cpputils::make_unique_ref<blockstore::testfake::FakeBlockStore>();
_device = std::make_unique<cryfs::CryDevice>(configFile(), std::move(fakeBlockStore)); _device = std::make_unique<cryfs::CryDevice>(configFile(), std::move(fakeBlockStore), 0x12345678);
} }
cryfs::CryConfigFile configFile() { cryfs::CryConfigFile configFile() {

View File

@ -1,6 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "../../../src/cryfs/filesystem/MyClientId.h" #include "cryfs/localstate/MyClientId.h"
#include <cpp-utils/tempfile/TempDir.h> #include <cpp-utils/tempfile/TempDir.h>
using cpputils::TempDir; using cpputils::TempDir;