2015-10-23 12:16:23 +02:00
# pragma once
# ifndef MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
# define MESSMER_CRYFS_SRC_CONFIG_CRYCONFIGFILE_H
2015-10-19 02:46:47 +02:00
# include <boost/optional.hpp>
2015-10-27 13:28:42 +01:00
# include <boost/filesystem.hpp>
2015-10-19 02:46:47 +02:00
# include "CryConfig.h"
2015-10-24 19:35:37 +02:00
# include <messmer/blockstore/implementations/encrypted/ciphers/ciphers.h>
2015-10-27 13:28:42 +01:00
# include "crypto/CryConfigEncryptorFactory.h"
2015-10-19 02:46:47 +02:00
namespace cryfs {
class CryConfigFile final {
public :
2015-10-24 19:35:37 +02:00
CryConfigFile ( CryConfigFile & & rhs ) = default ;
2015-10-23 00:04:03 +02:00
~ CryConfigFile ( ) ;
2015-10-19 02:46:47 +02:00
2015-10-27 13:28:42 +01:00
template < class SCryptConfig >
2015-10-24 19:35:37 +02:00
static CryConfigFile create ( const boost : : filesystem : : path & path , CryConfig config , const std : : string & password ) ;
static boost : : optional < CryConfigFile > load ( const boost : : filesystem : : path & path , const std : : string & password ) ;
2015-10-19 02:46:47 +02:00
void save ( ) const ;
CryConfig * config ( ) ;
private :
2015-10-26 16:36:57 +01:00
CryConfigFile ( const boost : : filesystem : : path & path , CryConfig config , cpputils : : unique_ref < CryConfigEncryptor > encryptor ) ;
2015-10-19 02:46:47 +02:00
boost : : filesystem : : path _path ;
CryConfig _config ;
2015-10-26 16:36:57 +01:00
cpputils : : unique_ref < CryConfigEncryptor > _encryptor ;
2015-10-19 02:46:47 +02:00
DISALLOW_COPY_AND_ASSIGN ( CryConfigFile ) ;
} ;
2015-10-27 13:28:42 +01:00
template < class SCryptSettings >
CryConfigFile CryConfigFile : : create ( const boost : : filesystem : : path & path , CryConfig config , const std : : string & password ) {
using ConfigCipher = blockstore : : encrypted : : AES256_GCM ; // TODO Take cipher from config instead
if ( boost : : filesystem : : exists ( path ) ) {
throw std : : runtime_error ( " Config file exists already. " ) ;
}
2015-10-27 18:50:58 +01:00
auto result = CryConfigFile ( path , std : : move ( config ) , CryConfigEncryptorFactory : : deriveKey < ConfigCipher , SCryptSettings > ( password , " aes-256-gcm " ) ) ; // TODO Take cipher from config instead
2015-10-27 13:28:42 +01:00
result . save ( ) ;
return result ;
}
2015-10-19 02:46:47 +02:00
}
# endif