Search in sources :

Example 6 with CryptoException

use of com.dracoon.sdk.crypto.error.CryptoException in project cyberduck by iterate-ch.

the class SDSSession method unlockTripleCryptKeyPair.

protected void unlockTripleCryptKeyPair(final LoginCallback prompt, final UserAccountWrapper user, final UserKeyPair.Version requiredKeyPairVersion) throws BackgroundException {
    try {
        Credentials deprecatedCredentials = null;
        if (this.isNewCryptoAvailable()) {
            final List<UserKeyPairContainer> pairs = new UserApi(client).requestUserKeyPairs(StringUtils.EMPTY, null);
            if (pairs.size() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("No keypair found for user %s", user));
                }
                return;
            }
            boolean migrated = false;
            for (UserKeyPairContainer pair : pairs) {
                if (requiredKeyPairVersion == TripleCryptConverter.toCryptoUserKeyPair(pair).getUserPublicKey().getVersion()) {
                    migrated = true;
                    break;
                }
            }
            if (migrated && pairs.size() == 2) {
                final UserKeyPairContainer deprecated = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, UserKeyPair.Version.RSA2048.getValue(), null);
                final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(deprecated);
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Attempt to unlock deprecated private key %s", keypair.getUserPrivateKey()));
                }
                deprecatedCredentials = new TripleCryptKeyPair().unlock(prompt, host, keypair);
                keyPairDeprecated.set(deprecated);
            }
            if (!migrated) {
                final UserKeyPairContainer deprecated = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, UserKeyPair.Version.RSA2048.getValue(), null);
                final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(deprecated);
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Attempt to unlock and migrate deprecated private key %s", keypair.getUserPrivateKey()));
                }
                deprecatedCredentials = new TripleCryptKeyPair().unlock(prompt, host, keypair);
                final UserKeyPair newPair = Crypto.generateUserKeyPair(requiredKeyPairVersion, deprecatedCredentials.getPassword());
                final CreateKeyPairRequest request = new CreateKeyPairRequest();
                request.setPreviousPrivateKey(deprecated.getPrivateKeyContainer());
                final UserKeyPairContainer userKeyPairContainer = TripleCryptConverter.toSwaggerUserKeyPairContainer(newPair);
                request.setPrivateKeyContainer(userKeyPairContainer.getPrivateKeyContainer());
                request.setPublicKeyContainer(userKeyPairContainer.getPublicKeyContainer());
                if (log.isDebugEnabled()) {
                    log.debug("Create new key pair");
                }
                new UserApi(client).createAndPreserveUserKeyPair(request, null);
                keyPairDeprecated.set(deprecated);
            }
        }
        final UserKeyPairContainer container = new UserApi(client).requestUserKeyPair(StringUtils.EMPTY, requiredKeyPairVersion.getValue(), null);
        keyPair.set(container);
        final UserKeyPair keypair = TripleCryptConverter.toCryptoUserKeyPair(keyPair.get());
        if (deprecatedCredentials != null) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Attempt to unlock private key with passphrase from deprecated private key %s", keypair.getUserPrivateKey()));
            }
            if (Crypto.checkUserKeyPair(keypair, deprecatedCredentials.getPassword())) {
                new TripleCryptKeyPair().unlock(prompt, host, keypair, deprecatedCredentials.getPassword());
            } else {
                new TripleCryptKeyPair().unlock(prompt, host, keypair);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Attempt to unlock private key %s", keypair.getUserPrivateKey()));
            }
            new TripleCryptKeyPair().unlock(prompt, host, keypair);
        }
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map(e);
    } catch (ApiException e) {
        log.warn(String.format("Ignore failure unlocking user key pair. %s", new SDSExceptionMappingService(nodeid).map(e)));
    } catch (LoginCanceledException e) {
        log.warn("Ignore cancel unlocking triple crypt private key pair");
    }
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) CreateKeyPairRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateKeyPairRequest) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) UserApi(ch.cyberduck.core.sds.io.swagger.client.api.UserApi) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Aggregations

CryptoException (com.dracoon.sdk.crypto.error.CryptoException)6 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)4 UserKeyPair (com.dracoon.sdk.crypto.model.UserKeyPair)4 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)3 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)3 UserKeyPairContainer (ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer)3 TripleCryptExceptionMappingService (ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService)3 TripleCryptKeyPair (ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair)3 EncryptedFileKey (com.dracoon.sdk.crypto.model.EncryptedFileKey)3 Credentials (ch.cyberduck.core.Credentials)2 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)2 PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)2 IOException (java.io.IOException)2 DescriptiveUrl (ch.cyberduck.core.DescriptiveUrl)1 Host (ch.cyberduck.core.Host)1 LoginOptions (ch.cyberduck.core.LoginOptions)1 LoginCanceledException (ch.cyberduck.core.exception.LoginCanceledException)1 VersionIdProvider (ch.cyberduck.core.features.VersionIdProvider)1 SDSExceptionMappingService (ch.cyberduck.core.sds.SDSExceptionMappingService)1 SharesApi (ch.cyberduck.core.sds.io.swagger.client.api.SharesApi)1