2017-09-30 23:42:34 +02:00
|
|
|
#include "testutils/CliTest.h"
|
|
|
|
#include <cryfs/config/CryConfigFile.h>
|
2018-02-02 03:09:28 +01:00
|
|
|
#include <cryfs/ErrorCodes.h>
|
2018-10-22 04:31:08 +02:00
|
|
|
#include <cpp-utils/crypto/kdf/Scrypt.h>
|
|
|
|
#include <cpp-utils/data/DataFixture.h>
|
2017-09-30 23:42:34 +02:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using cryfs::CryConfig;
|
|
|
|
using cryfs::CryConfigFile;
|
2018-02-02 03:09:28 +01:00
|
|
|
using cryfs::ErrorCode;
|
2018-10-22 04:31:08 +02:00
|
|
|
using cryfs::CryKeyProvider;
|
|
|
|
using cpputils::Data;
|
|
|
|
using cpputils::EncryptionKey;
|
|
|
|
using cpputils::SCrypt;
|
|
|
|
|
|
|
|
class FakeCryKeyProvider final : public CryKeyProvider {
|
|
|
|
EncryptionKey requestKeyForExistingFilesystem(size_t keySize, const Data& kdfParameters) override {
|
|
|
|
return SCrypt(SCrypt::TestSettings).deriveExistingKey(keySize, "pass", kdfParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyResult requestKeyForNewFilesystem(size_t keySize) override {
|
|
|
|
auto derived = SCrypt(SCrypt::TestSettings).deriveNewKey(keySize, "pass");
|
|
|
|
return {
|
|
|
|
std::move(derived.key),
|
|
|
|
std::move(derived.kdfParameters)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2017-09-30 23:42:34 +02:00
|
|
|
|
|
|
|
class CliTest_IntegrityCheck: public CliTest {
|
|
|
|
public:
|
|
|
|
void modifyFilesystemId() {
|
2018-10-22 04:31:08 +02:00
|
|
|
FakeCryKeyProvider keyProvider;
|
|
|
|
auto configFile = CryConfigFile::load(basedir / "cryfs.config", &keyProvider).value();
|
2017-09-30 23:42:34 +02:00
|
|
|
configFile.config()->SetFilesystemId(CryConfig::FilesystemID::FromString("0123456789ABCDEF0123456789ABCDEF"));
|
|
|
|
configFile.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void modifyFilesystemKey() {
|
2018-10-22 04:31:08 +02:00
|
|
|
FakeCryKeyProvider keyProvider;
|
|
|
|
auto configFile = CryConfigFile::load(basedir / "cryfs.config", &keyProvider).value();
|
2017-09-30 23:42:34 +02:00
|
|
|
configFile.config()->SetEncryptionKey("0123456789ABCDEF0123456789ABCDEF");
|
|
|
|
configFile.save();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemId_thenFails) {
|
2018-09-07 07:44:23 +02:00
|
|
|
vector<string> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"};
|
2017-09-30 23:42:34 +02:00
|
|
|
//TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that
|
|
|
|
EXPECT_RUN_SUCCESS(args, mountdir);
|
|
|
|
modifyFilesystemId();
|
|
|
|
EXPECT_RUN_ERROR(
|
|
|
|
args,
|
2018-02-02 03:09:28 +01:00
|
|
|
"Error: The filesystem id in the config file is different to the last time we loaded a filesystem from this basedir.",
|
|
|
|
ErrorCode::FilesystemIdChanged
|
2017-09-30 23:42:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemKey_thenFails) {
|
2018-09-07 07:44:23 +02:00
|
|
|
vector<string> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"};
|
2017-09-30 23:42:34 +02:00
|
|
|
//TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that
|
|
|
|
EXPECT_RUN_SUCCESS(args, mountdir);
|
|
|
|
modifyFilesystemKey();
|
|
|
|
EXPECT_RUN_ERROR(
|
|
|
|
args,
|
2018-02-02 03:09:28 +01:00
|
|
|
"Error: The filesystem encryption key differs from the last time we loaded this filesystem. Did an attacker replace the file system?",
|
|
|
|
ErrorCode::EncryptionKeyChanged
|
2017-09-30 23:42:34 +02:00
|
|
|
);
|
|
|
|
}
|