Search in sources :

Example 1 with CipherException

use of org.syncany.crypto.CipherException in project syncany by syncany.

the class UserConfig method writeExampleUserConfigFile.

private static void writeExampleUserConfigFile(File userConfigFile) {
    UserConfigTO userConfigTO = new UserConfigTO();
    try {
        System.out.println("First launch, creating a secret key (could take a sec)...");
        SaltedSecretKey configEncryptionKey = CipherUtil.createMasterKey(CipherUtil.createRandomAlphabeticString(USER_CONFIG_ENCRYPTION_KEY_LENGTH));
        userConfigTO.setConfigEncryptionKey(configEncryptionKey);
        userConfigTO.save(userConfigFile);
    } catch (CipherException e) {
        System.err.println("ERROR: " + e.getMessage());
        System.err.println("       Failed to create masterkey.");
        System.err.println();
    } catch (ConfigException e) {
        System.err.println("ERROR: " + e.getMessage());
        System.err.println("       Failed to save to file.");
        System.err.println();
    }
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) CipherException(org.syncany.crypto.CipherException) UserConfigTO(org.syncany.config.to.UserConfigTO)

Example 2 with CipherException

use of org.syncany.crypto.CipherException in project syncany by syncany.

the class ConnectOperation method decryptRepoFile.

private String decryptRepoFile(File file, SaltedSecretKey masterKey) throws CipherException {
    try {
        logger.log(Level.INFO, "Decrypting repo file ...");
        FileInputStream encryptedRepoConfig = new FileInputStream(file);
        String repoFileStr = new String(CipherUtil.decrypt(encryptedRepoConfig, masterKey));
        logger.log(Level.INFO, "Repo file decrypted:");
        logger.log(Level.INFO, repoFileStr);
        return repoFileStr;
    } catch (Exception e) {
        logger.log(Level.INFO, "Invalid password given, or repo file corrupt.", e);
        throw new CipherException("Invalid password given, or repo file corrupt.", e);
    }
}
Also used : CipherException(org.syncany.crypto.CipherException) FileInputStream(java.io.FileInputStream) StorageException(org.syncany.plugins.transfer.StorageException) CipherException(org.syncany.crypto.CipherException)

Example 3 with CipherException

use of org.syncany.crypto.CipherException in project syncany by syncany.

the class ConnectOperation method createConfigTOFromLink.

private ConfigTO createConfigTOFromLink(ConfigTO configTO, String link, String masterPassword) throws StorageException, CipherException {
    logger.log(Level.INFO, "Creating config TO from link: " + link + " ...");
    ApplicationLink applicationLink = new ApplicationLink(link);
    try {
        if (applicationLink.isEncrypted()) {
            // Non-interactive mode
            if (masterPassword != null) {
                logger.log(Level.INFO, " - Link is encrypted. Password available.");
                SaltedSecretKey masterKey = createMasterKeyFromPassword(masterPassword, applicationLink.getMasterKeySalt());
                TransferSettings transferSettings = applicationLink.createTransferSettings(masterKey);
                configTO.setMasterKey(masterKey);
                configTO.setTransferSettings(transferSettings);
            } else {
                logger.log(Level.INFO, " - Link is encrypted. Asking for password.");
                boolean retryPassword = true;
                while (retryPassword) {
                    // Ask password
                    masterPassword = getOrAskPassword();
                    // Generate master key
                    SaltedSecretKey masterKey = createMasterKeyFromPassword(masterPassword, applicationLink.getMasterKeySalt());
                    // Decrypt config
                    try {
                        TransferSettings transferSettings = applicationLink.createTransferSettings(masterKey);
                        configTO.setMasterKey(masterKey);
                        configTO.setTransferSettings(transferSettings);
                        retryPassword = false;
                    } catch (CipherException e) {
                        retryPassword = askRetryPassword();
                    }
                }
            }
            if (configTO.getTransferSettings() == null) {
                throw new CipherException("Unable to decrypt link.");
            }
        } else {
            logger.log(Level.INFO, " - Link is NOT encrypted. No password needed.");
            TransferSettings transferSettings = applicationLink.createTransferSettings();
            configTO.setTransferSettings(transferSettings);
        }
    } catch (Exception e) {
        throw new StorageException("Unable to extract connection settings: " + e.getMessage(), e);
    }
    return configTO;
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) CipherException(org.syncany.crypto.CipherException) TransferSettings(org.syncany.plugins.transfer.TransferSettings) StorageException(org.syncany.plugins.transfer.StorageException) StorageException(org.syncany.plugins.transfer.StorageException) CipherException(org.syncany.crypto.CipherException)

Example 4 with CipherException

use of org.syncany.crypto.CipherException 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;
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) CipherException(org.syncany.crypto.CipherException) ConfigTO(org.syncany.config.to.ConfigTO) MasterTO(org.syncany.config.to.MasterTO) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) MasterRemoteFile(org.syncany.plugins.transfer.files.MasterRemoteFile) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) File(java.io.File) StorageException(org.syncany.plugins.transfer.StorageException) CipherException(org.syncany.crypto.CipherException)

Aggregations

CipherException (org.syncany.crypto.CipherException)4 SaltedSecretKey (org.syncany.crypto.SaltedSecretKey)3 StorageException (org.syncany.plugins.transfer.StorageException)3 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ConfigTO (org.syncany.config.to.ConfigTO)1 MasterTO (org.syncany.config.to.MasterTO)1 UserConfigTO (org.syncany.config.to.UserConfigTO)1 TransferSettings (org.syncany.plugins.transfer.TransferSettings)1 MasterRemoteFile (org.syncany.plugins.transfer.files.MasterRemoteFile)1 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)1 SyncanyRemoteFile (org.syncany.plugins.transfer.files.SyncanyRemoteFile)1