use of org.syncany.config.to.MasterTO in project syncany by syncany.
the class ConnectOperation method askPasswordAndCreateMasterKey.
private SaltedSecretKey askPasswordAndCreateMasterKey() throws CipherException, StorageException {
File tmpMasterFile = downloadFile(transferManager, new MasterRemoteFile());
MasterTO masterTO = readMasterFile(tmpMasterFile);
tmpMasterFile.delete();
String masterKeyPassword = getOrAskPassword();
byte[] masterKeySalt = masterTO.getSalt();
// This takes looong!
return createMasterKeyFromPassword(masterKeyPassword, masterKeySalt);
}
use of org.syncany.config.to.MasterTO in project syncany by syncany.
the class InitOperation method saveLocalConfig.
private void saveLocalConfig(File configFile, File repoFile, File masterFile, String masterKeyPassword) throws Exception {
if (options.isEncryptionEnabled()) {
// This takes looong!
SaltedSecretKey masterKey = createMasterKeyFromPassword(masterKeyPassword);
options.getConfigTO().setMasterKey(masterKey);
new MasterTO(masterKey.getSalt()).save(masterFile);
options.getRepoTO().save(repoFile, options.getCipherSpecs(), masterKey);
} else {
options.getRepoTO().save(repoFile);
}
options.getConfigTO().save(configFile);
}
use of org.syncany.config.to.MasterTO in project syncany by syncany.
the class ConnectOperation method execute.
@Override
public ConnectOperationResult execute() throws Exception {
logger.log(Level.INFO, "");
logger.log(Level.INFO, "Running 'Connect'");
logger.log(Level.INFO, "--------------------------------------------");
// Decrypt and init configTO
ConfigTO configTO = null;
try {
configTO = createConfigTO();
} catch (CipherException e) {
logger.log(Level.FINE, "Could not create config", e);
return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
}
// Init plugin and transfer manager
transferManager = createTransferManagerFromNullConfig(options.getConfigTO());
// Test the repo
if (!performRepoTest(transferManager)) {
logger.log(Level.INFO, "- Connecting to the repo failed, repo already exists or cannot be created: " + result.getResultCode());
return result;
}
logger.log(Level.INFO, "- Connecting to the repo was successful; now downloading repo file ...");
// Create local .syncany directory
File tmpRepoFile = downloadFile(transferManager, new SyncanyRemoteFile());
if (CipherUtil.isEncrypted(tmpRepoFile)) {
logger.log(Level.INFO, "- Repo is ENCRYPTED. Decryption necessary.");
if (configTO.getMasterKey() == null) {
logger.log(Level.INFO, "- No master key present; Asking for password ...");
boolean retryPassword = true;
while (retryPassword) {
SaltedSecretKey possibleMasterKey = askPasswordAndCreateMasterKey();
logger.log(Level.INFO, "- Master key created. Now verifying by decrypting repo file...");
if (decryptAndVerifyRepoFile(tmpRepoFile, possibleMasterKey)) {
logger.log(Level.INFO, "- SUCCESS: Repo file decrypted successfully.");
configTO.setMasterKey(possibleMasterKey);
retryPassword = false;
} else {
logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Asking for retry.");
retryPassword = askRetryPassword();
if (!retryPassword) {
logger.log(Level.INFO, "- No retry possible/desired. Returning NOK_DECRYPT_ERROR.");
return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
}
}
}
} else {
logger.log(Level.INFO, "- Master key present; Now verifying by decrypting repo file...");
if (!decryptAndVerifyRepoFile(tmpRepoFile, configTO.getMasterKey())) {
logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Returning NOK_DECRYPT_ERROR.");
return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
}
}
} else {
String repoFileStr = FileUtils.readFileToString(tmpRepoFile);
verifyRepoFile(repoFileStr);
}
// Success, now do the work!
File appDir = createAppDirs(options.getLocalDir());
// Write file 'config.xml'
File configFile = new File(appDir, Config.FILE_CONFIG);
configTO.save(configFile);
// Write file 'syncany'
File repoFile = new File(appDir, Config.FILE_REPO);
FileUtils.copyFile(tmpRepoFile, repoFile);
tmpRepoFile.delete();
// Write file 'master'
if (configTO.getMasterKey() != null) {
File masterFile = new File(appDir, Config.FILE_MASTER);
new MasterTO(configTO.getMasterKey().getSalt()).save(masterFile);
}
// Shutdown plugin
transferManager.disconnect();
// Add to daemon (if requested)
if (options.isDaemon()) {
try {
boolean addedToDaemonConfig = DaemonConfigHelper.addFolder(options.getLocalDir());
result.setAddedToDaemon(addedToDaemonConfig);
} catch (Exception e) {
logger.log(Level.WARNING, "Cannot add folder to daemon config.", e);
result.setAddedToDaemon(false);
}
}
result.setResultCode(ConnectResultCode.OK);
return result;
}
Aggregations