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>
2016-02-11 16:39:42 +01:00
# include <cpp-utils/random/Random.h>
# include <cpp-utils/logging/logging.h>
2016-02-09 10:55:28 +01:00
# include <boost/algorithm/string/predicate.hpp>
2016-03-02 13:53:37 +01:00
# include <gitversion/gitversion.h>
2016-03-26 16:53:08 +01:00
# include <gitversion/VersionCompare.h>
2016-06-27 01:53:10 +02:00
# include "../localstate/LocalStateDir.h"
2017-09-23 21:17:05 +02:00
# include "../localstate/LocalStateMetadata.h"
2018-02-02 01:08:01 +01:00
# include "../CryfsException.h"
2015-06-16 18:20:31 +02:00
namespace bf = boost : : filesystem ;
2015-09-12 20:16:13 +02:00
using cpputils : : Console ;
2015-10-24 19:35:37 +02:00
using cpputils : : RandomGenerator ;
2018-10-22 04:31:08 +02:00
using cpputils : : unique_ref ;
2015-06-18 13:45:08 +02:00
using boost : : optional ;
using boost : : none ;
2016-01-17 14:57:40 +01:00
using std : : shared_ptr ;
2015-07-01 14:33:18 +02:00
using std : : string ;
2015-11-04 05:27:00 +01:00
using std : : shared_ptr ;
2016-03-26 16:53:08 +01:00
using gitversion : : VersionCompare ;
2015-10-26 16:36:57 +01:00
using namespace cpputils : : logging ;
2015-06-16 18:20:31 +02:00
namespace cryfs {
2018-10-22 04:31:08 +02:00
CryConfigLoader : : CryConfigLoader ( shared_ptr < Console > console , RandomGenerator & keyGenerator , unique_ref < CryKeyProvider > keyProvider , LocalStateDir localStateDir , const optional < string > & cipherFromCommandLine , const boost : : optional < uint32_t > & blocksizeBytesFromCommandLine , const boost : : optional < bool > & missingBlockIsIntegrityViolationFromCommandLine )
: _console ( console ) , _creator ( std : : move ( console ) , keyGenerator , localStateDir ) , _keyProvider ( std : : move ( keyProvider ) ) ,
2016-06-27 08:24:32 +02:00
_cipherFromCommandLine ( cipherFromCommandLine ) , _blocksizeBytesFromCommandLine ( blocksizeBytesFromCommandLine ) ,
2018-04-22 07:04:21 +02:00
_missingBlockIsIntegrityViolationFromCommandLine ( missingBlockIsIntegrityViolationFromCommandLine ) ,
_localStateDir ( std : : move ( localStateDir ) ) {
2015-10-24 19:35:37 +02:00
}
2015-07-26 13:09:55 +02:00
2018-02-03 17:33:59 +01:00
optional < CryConfigLoader : : ConfigLoadResult > CryConfigLoader : : _loadConfig ( bf : : path filename , bool allowFilesystemUpgrade , bool allowReplacedFilesystem ) {
2018-10-22 04:31:08 +02:00
auto config = CryConfigFile : : load ( std : : move ( filename ) , _keyProvider . get ( ) ) ;
2015-10-24 19:35:37 +02:00
if ( config = = none ) {
2015-10-26 16:36:57 +01:00
return none ;
2015-06-16 18:20:31 +02:00
}
2018-02-03 18:08:03 +01:00
# ifndef CRYFS_NO_COMPATIBILITY
//Since 0.9.7 and 0.9.8 set their own version to cryfs.version instead of the filesystem format version (which is 0.9.6), overwrite it
if ( config - > config ( ) - > Version ( ) = = " 0.9.7 " | | config - > config ( ) - > Version ( ) = = " 0.9.8 " ) {
config - > config ( ) - > SetVersion ( " 0.9.6 " ) ;
}
# endif
2018-02-01 11:04:59 +01:00
_checkVersion ( * config - > config ( ) , allowFilesystemUpgrade ) ;
2016-03-26 16:53:08 +01:00
# ifndef CRYFS_NO_COMPATIBILITY
//Since 0.9.3-alpha set the config value cryfs.blocksizeBytes wrongly to 32768 (but didn't use the value), we have to fix this here.
if ( config - > config ( ) - > Version ( ) ! = " 0+unknown " & & VersionCompare : : isOlderThan ( config - > config ( ) - > Version ( ) , " 0.9.3-rc1 " ) ) {
config - > config ( ) - > SetBlocksizeBytes ( 32832 ) ;
}
# endif
2018-02-03 18:08:03 +01:00
if ( config - > config ( ) - > Version ( ) ! = CryConfig : : FilesystemFormatVersion ) {
config - > config ( ) - > SetVersion ( CryConfig : : FilesystemFormatVersion ) ;
config - > save ( ) ;
}
if ( config - > config ( ) - > LastOpenedWithVersion ( ) ! = gitversion : : VersionString ( ) ) {
config - > config ( ) - > SetLastOpenedWithVersion ( gitversion : : VersionString ( ) ) ;
2016-03-26 17:09:07 +01:00
config - > save ( ) ;
}
2016-02-09 10:55:28 +01:00
_checkCipher ( * config - > config ( ) ) ;
2018-04-22 07:04:21 +02:00
auto localState = LocalStateMetadata : : loadOrGenerate ( _localStateDir . forFilesystemId ( config - > config ( ) - > FilesystemId ( ) ) , cpputils : : Data : : FromString ( config - > config ( ) - > EncryptionKey ( ) ) , allowReplacedFilesystem ) ;
2017-09-23 21:17:05 +02:00
uint32_t myClientId = localState . myClientId ( ) ;
2016-06-27 08:24:32 +02:00
_checkMissingBlocksAreIntegrityViolations ( & * config , myClientId ) ;
2016-06-27 01:53:10 +02:00
return ConfigLoadResult { std : : move ( * config ) , myClientId } ;
2015-10-24 19:35:37 +02:00
}
2018-02-01 11:04:59 +01:00
void CryConfigLoader : : _checkVersion ( const CryConfig & config , bool allowFilesystemUpgrade ) {
2018-02-03 18:08:03 +01:00
if ( gitversion : : VersionCompare : : isOlderThan ( CryConfig : : FilesystemFormatVersion , config . Version ( ) ) ) {
if ( ! _console - > askYesNo ( " This filesystem is for CryFS " + config . Version ( ) + " or later and should not be opened with older versions. It is strongly recommended to update your CryFS version. However, if you have backed up your base directory and know what you're doing, you can continue trying to load it. Do you want to continue? " , false ) ) {
throw CryfsException ( " This filesystem is for CryFS " + config . Version ( ) + " or later. Please update your CryFS version. " , ErrorCode : : TooNewFilesystemFormat ) ;
2016-06-07 00:31:09 +02:00
}
2016-05-04 05:34:30 +02:00
}
2018-02-03 18:08:03 +01:00
if ( ! allowFilesystemUpgrade & & gitversion : : VersionCompare : : isOlderThan ( config . Version ( ) , CryConfig : : FilesystemFormatVersion ) ) {
2019-01-21 09:12:07 +01:00
if ( ! _console - > askYesNo ( " This filesystem is for CryFS " + config . Version ( ) + " (or a later version with the same storage format). You're running a CryFS version using storage format " + CryConfig : : FilesystemFormatVersion + " . It is recommended to create a new filesystem with CryFS 0.10 and copy your files into it. If you don't want to do that, we can also attempt to migrate the existing filesystem, but that can take a long time, you won't be getting some of the performance advantages of the 0.10 release series, and if the migration fails, your data may be lost. If you decide to continue, please make sure you have a backup of your data. Do you want to attempt a migration now? " , false ) ) {
2018-02-03 18:08:03 +01:00
throw CryfsException ( " This filesystem is for CryFS " + config . Version ( ) + " (or a later version with the same storage format). It has to be migrated. " , ErrorCode : : TooOldFilesystemFormat ) ;
2016-05-04 05:34:30 +02:00
}
2016-04-29 21:46:14 +02:00
}
2016-02-09 10:55:28 +01:00
}
void CryConfigLoader : : _checkCipher ( const CryConfig & config ) const {
if ( _cipherFromCommandLine ! = none & & config . Cipher ( ) ! = * _cipherFromCommandLine ) {
2018-02-02 01:08:01 +01:00
throw CryfsException ( string ( ) + " Filesystem uses " + config . Cipher ( ) + " cipher and not " + * _cipherFromCommandLine + " as specified. " , ErrorCode : : WrongCipher ) ;
2016-02-09 10:55:28 +01:00
}
}
2016-06-27 08:24:32 +02:00
void CryConfigLoader : : _checkMissingBlocksAreIntegrityViolations ( CryConfigFile * configFile , uint32_t myClientId ) {
if ( _missingBlockIsIntegrityViolationFromCommandLine = = optional < bool > ( true ) & & configFile - > config ( ) - > ExclusiveClientId ( ) = = none ) {
2018-02-02 03:09:28 +01:00
throw CryfsException ( " You specified on the command line to treat missing blocks as integrity violations, but the file system is not setup to do that. " , ErrorCode : : FilesystemHasDifferentIntegritySetup ) ;
2016-06-27 08:24:32 +02:00
}
if ( _missingBlockIsIntegrityViolationFromCommandLine = = optional < bool > ( false ) & & configFile - > config ( ) - > ExclusiveClientId ( ) ! = none ) {
2018-02-02 03:09:28 +01:00
throw CryfsException ( " You specified on the command line to not treat missing blocks as integrity violations, but the file system is setup to do that. " , ErrorCode : : FilesystemHasDifferentIntegritySetup ) ;
2016-06-27 08:24:32 +02:00
}
// If the file system is set up to treat missing blocks as integrity violations, but we're accessing from a different client, ask whether they want to disable the feature.
auto exclusiveClientId = configFile - > config ( ) - > ExclusiveClientId ( ) ;
if ( exclusiveClientId ! = none & & * exclusiveClientId ! = myClientId ) {
2016-09-24 20:43:54 +02:00
if ( ! _console - > askYesNo ( " \n This filesystem is setup to treat missing blocks as integrity violations and therefore only works in single-client mode. You are trying to access it from a different client. \n Do you want to disable this integrity feature and stop treating missing blocks as integrity violations? \n Choosing yes will not affect the confidentiality of your data, but in future you might not notice if an attacker deletes one of your files. " , false ) ) {
2018-02-02 03:09:28 +01:00
throw CryfsException ( " File system is in single-client mode and can only be used from the client that created it. " , ErrorCode : : SingleClientFileSystem ) ;
2016-06-27 08:24:32 +02:00
}
configFile - > config ( ) - > SetExclusiveClientId ( none ) ;
configFile - > save ( ) ;
}
}
2018-11-10 21:38:57 +01:00
optional < CryConfigLoader : : ConfigLoadResult > CryConfigLoader : : load ( bf : : path filename , bool allowFilesystemUpgrade , bool allowReplacedFilesystem ) {
return _loadConfig ( std : : move ( filename ) , allowFilesystemUpgrade , allowReplacedFilesystem ) ;
}
2018-02-03 17:33:59 +01:00
optional < CryConfigLoader : : ConfigLoadResult > CryConfigLoader : : loadOrCreate ( bf : : path filename , bool allowFilesystemUpgrade , bool allowReplacedFilesystem ) {
2015-11-04 05:27:00 +01:00
if ( bf : : exists ( filename ) ) {
2018-02-03 17:33:59 +01:00
return _loadConfig ( std : : move ( filename ) , allowFilesystemUpgrade , allowReplacedFilesystem ) ;
2015-11-04 05:27:00 +01:00
} else {
2018-02-03 17:33:59 +01:00
return _createConfig ( std : : move ( filename ) , allowReplacedFilesystem ) ;
2015-11-04 05:27:00 +01:00
}
}
2018-02-03 17:33:59 +01:00
CryConfigLoader : : ConfigLoadResult CryConfigLoader : : _createConfig ( bf : : path filename , bool allowReplacedFilesystem ) {
auto config = _creator . create ( _cipherFromCommandLine , _blocksizeBytesFromCommandLine , _missingBlockIsIntegrityViolationFromCommandLine , allowReplacedFilesystem ) ;
2018-10-22 04:31:08 +02:00
auto result = CryConfigFile : : create ( std : : move ( filename ) , std : : move ( config . config ) , _keyProvider . get ( ) ) ;
2016-06-27 01:53:10 +02:00
return ConfigLoadResult { std : : move ( result ) , config . myClientId } ;
2015-11-04 05:27:00 +01:00
}
2015-06-16 18:20:31 +02:00
}