Search in sources :

Example 1 with UserKeyPair

use of com.dracoon.sdk.crypto.model.UserKeyPair in project cyberduck by iterate-ch.

the class SDSMissingFileKeysSchedulerFeature method operate.

@Override
public List<UserFileKeySetRequest> operate(final Session<?> client, final PasswordCallback callback, final Path file) throws BackgroundException {
    final SDSSession session = (SDSSession) client;
    final SDSNodeIdProvider nodeid = (SDSNodeIdProvider) session._getFeature(VersionIdProvider.class);
    try {
        final UserAccountWrapper account = session.userAccount();
        if (!account.isEncryptionEnabled()) {
            log.warn(String.format("No key pair found in user account %s", account));
            return Collections.emptyList();
        }
        final List<UserFileKeySetRequest> processed = new ArrayList<>();
        final UserKeyPairContainer userKeyPairContainer = session.keyPair();
        final UserKeyPair keyPair = TripleCryptConverter.toCryptoUserKeyPair(userKeyPairContainer);
        final TripleCryptKeyPair triplecrypt = new TripleCryptKeyPair();
        final Credentials passphrase = triplecrypt.unlock(callback, session.getHost(), keyPair);
        final UserKeyPairContainer userKeyPairContainerDeprecated = session.keyPairDeprecated();
        Credentials passphraseDeprecated = passphrase;
        if (userKeyPairContainerDeprecated != null) {
            passphraseDeprecated = triplecrypt.unlock(callback, session.getHost(), TripleCryptConverter.toCryptoUserKeyPair(userKeyPairContainerDeprecated));
        }
        // Null when operating from scheduler. File reference is set for post upload.
        final Long fileId = file != null ? Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener())) : null;
        UserFileKeySetBatchRequest request;
        do {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Request a list of missing file keys for file %s", file));
            }
            request = new UserFileKeySetBatchRequest();
            final MissingKeysResponse missingKeys = new NodesApi(session.getClient()).requestMissingFileKeys(null, null, null, fileId, null, null, null);
            final Map<Long, List<UserUserPublicKey>> userPublicKeys = missingKeys.getUsers().stream().collect(groupingBy(UserUserPublicKey::getId));
            final Map<Long, List<FileFileKeys>> files = missingKeys.getFiles().stream().collect(groupingBy(FileFileKeys::getId));
            for (UserIdFileIdItem item : missingKeys.getItems()) {
                for (FileFileKeys fileKey : files.get(item.getFileId())) {
                    final EncryptedFileKey encryptedFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(fileKey.getFileKeyContainer());
                    final UserKeyPairContainer keyPairForDecryption = session.getKeyPairForFileKey(encryptedFileKey.getVersion());
                    for (UserUserPublicKey userPublicKey : userPublicKeys.get(item.getUserId())) {
                        final EncryptedFileKey fk = this.encryptFileKey(TripleCryptConverter.toCryptoUserPrivateKey(keyPairForDecryption.getPrivateKeyContainer()), encryptedFileKey.getVersion() == EncryptedFileKey.Version.RSA2048_AES256GCM ? passphraseDeprecated : passphrase, userPublicKey, fileKey);
                        final UserFileKeySetRequest keySetRequest = new UserFileKeySetRequest().fileId(item.getFileId()).userId(item.getUserId()).fileKey(TripleCryptConverter.toSwaggerFileKey(fk));
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Missing file key processed for file %d and user %d", item.getFileId(), item.getUserId()));
                        }
                        request.addItemsItem(keySetRequest);
                    }
                }
            }
            if (!request.getItems().isEmpty()) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Set file keys with %s", request));
                }
                new NodesApi(session.getClient()).setUserFileKeys(request, StringUtils.EMPTY);
                processed.addAll(request.getItems());
            }
        } while (!request.getItems().isEmpty());
        this.deleteDeprecatedKeyPair(session);
        return processed;
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map(e);
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map(e);
    }
}
Also used : MissingKeysResponse(ch.cyberduck.core.sds.io.swagger.client.model.MissingKeysResponse) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) ArrayList(java.util.ArrayList) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) FileFileKeys(ch.cyberduck.core.sds.io.swagger.client.model.FileFileKeys) ArrayList(java.util.ArrayList) List(java.util.List) UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) VersionIdProvider(ch.cyberduck.core.features.VersionIdProvider) UserUserPublicKey(ch.cyberduck.core.sds.io.swagger.client.model.UserUserPublicKey) UserFileKeySetRequest(ch.cyberduck.core.sds.io.swagger.client.model.UserFileKeySetRequest) UserIdFileIdItem(ch.cyberduck.core.sds.io.swagger.client.model.UserIdFileIdItem) UserFileKeySetBatchRequest(ch.cyberduck.core.sds.io.swagger.client.model.UserFileKeySetBatchRequest) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) Credentials(ch.cyberduck.core.Credentials) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 2 with UserKeyPair

use of com.dracoon.sdk.crypto.model.UserKeyPair in project cyberduck by iterate-ch.

the class SDSSharesUrlProvider method toDownloadUrl.

@Override
public DescriptiveUrl toDownloadUrl(final Path file, CreateDownloadShareRequest options, final PasswordCallback callback) throws BackgroundException {
    try {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Create download share for %s", file));
        }
        if (null == options) {
            options = new CreateDownloadShareRequest();
            log.warn(String.format("Use default share options %s", options));
        }
        final Long fileid = Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener()));
        final Host bookmark = session.getHost();
        if (SDSNodeIdProvider.isEncrypted(file)) {
            // get existing file key associated with the sharing user
            final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(fileid, null, null);
            final EncryptedFileKey encFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(key);
            final UserKeyPairContainer keyPairContainer = session.getKeyPairForFileKey(encFileKey.getVersion());
            final UserKeyPair userKeyPair = TripleCryptConverter.toCryptoUserKeyPair(keyPairContainer);
            final Credentials passphrase = new TripleCryptKeyPair().unlock(callback, bookmark, userKeyPair);
            final PlainFileKey plainFileKey = Crypto.decryptFileKey(encFileKey, userKeyPair.getUserPrivateKey(), passphrase.getPassword());
            // encrypt file key with a new key pair
            final UserKeyPair pair;
            if (null == options.getPassword()) {
                pair = Crypto.generateUserKeyPair(session.requiredKeyPairVersion(), callback.prompt(bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), LocaleFactory.localizedString("Provide additional login credentials", "Credentials"), new LoginOptions().icon(session.getHost().getProtocol().disk())).getPassword());
            } else {
                pair = Crypto.generateUserKeyPair(session.requiredKeyPairVersion(), options.getPassword());
            }
            final EncryptedFileKey encryptedFileKey = Crypto.encryptFileKey(plainFileKey, pair.getUserPublicKey());
            options.setPassword(null);
            options.setKeyPair(TripleCryptConverter.toSwaggerUserKeyPairContainer(pair));
            options.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptedFileKey));
        }
        final DownloadShare share = new SharesApi(session.getClient()).createDownloadShare(options.nodeId(fileid), StringUtils.EMPTY, null);
        final String help;
        if (null == share.getExpireAt()) {
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3"));
        } else {
            final long expiry = share.getExpireAt().getMillis();
            help = MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Pre-Signed", "S3")) + " (" + MessageFormat.format(LocaleFactory.localizedString("Expires {0}", "S3") + ")", UserDateFormatterFactory.get().getShortFormat(expiry * 1000));
        }
        return new DescriptiveUrl(URI.create(String.format("%s://%s/#/public/shares-downloads/%s", bookmark.getProtocol().getScheme(), bookmark.getHostname(), share.getAccessKey())), DescriptiveUrl.Type.signed, help);
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map(e);
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map(e);
    }
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DownloadShare(ch.cyberduck.core.sds.io.swagger.client.model.DownloadShare) UserKeyPairContainer(ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) Host(ch.cyberduck.core.Host) CreateDownloadShareRequest(ch.cyberduck.core.sds.io.swagger.client.model.CreateDownloadShareRequest) TripleCryptKeyPair(ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair) LoginOptions(ch.cyberduck.core.LoginOptions) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) SharesApi(ch.cyberduck.core.sds.io.swagger.client.api.SharesApi) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) Credentials(ch.cyberduck.core.Credentials) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 3 with UserKeyPair

use of com.dracoon.sdk.crypto.model.UserKeyPair in project cyberduck by iterate-ch.

the class TripleCryptReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final FileKey key = new NodesApi(session.getClient()).requestUserFileKey(Long.parseLong(nodeid.getVersionId(file, new DisabledListProgressListener())), null, null);
        final EncryptedFileKey encFileKey = TripleCryptConverter.toCryptoEncryptedFileKey(key);
        try {
            final UserKeyPair userKeyPair = this.getUserKeyPair(encFileKey);
            final PlainFileKey plainFileKey = Crypto.decryptFileKey(encFileKey, userKeyPair.getUserPrivateKey(), this.unlock(callback, userKeyPair).getPassword());
            return new TripleCryptDecryptingInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag()));
        } catch (InvalidFileKeyException e) {
            log.warn(String.format("Failure %s  decrypting file key for %s. Invalidate cache", e, file));
            session.resetUserKeyPairs();
            final UserKeyPair userKeyPair = this.getUserKeyPair(encFileKey);
            final PlainFileKey plainFileKey = Crypto.decryptFileKey(encFileKey, userKeyPair.getUserPrivateKey(), this.unlock(callback, userKeyPair).getPassword());
            return new TripleCryptDecryptingInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag()));
        }
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Download {0} failed", e, file);
    } catch (CryptoException e) {
        throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) InvalidFileKeyException(com.dracoon.sdk.crypto.error.InvalidFileKeyException) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) SDSExceptionMappingService(ch.cyberduck.core.sds.SDSExceptionMappingService) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) CryptoException(com.dracoon.sdk.crypto.error.CryptoException) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 4 with UserKeyPair

use of com.dracoon.sdk.crypto.model.UserKeyPair in project cyberduck by iterate-ch.

the class TripleCryptConverter method toCryptoUserKeyPair.

public static UserKeyPair toCryptoUserKeyPair(final UserKeyPairContainer c) throws UnknownVersionException {
    final UserPrivateKey privateKey = new UserPrivateKey(UserKeyPair.Version.getByValue(c.getPrivateKeyContainer().getVersion()), c.getPrivateKeyContainer().getPrivateKey());
    final UserPublicKey publicKey = new UserPublicKey(UserKeyPair.Version.getByValue(c.getPublicKeyContainer().getVersion()), c.getPublicKeyContainer().getPublicKey());
    return new UserKeyPair(privateKey, publicKey);
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserPublicKey(com.dracoon.sdk.crypto.model.UserPublicKey) UserPrivateKey(com.dracoon.sdk.crypto.model.UserPrivateKey)

Example 5 with UserKeyPair

use of com.dracoon.sdk.crypto.model.UserKeyPair in project cyberduck by iterate-ch.

the class CryptoDecryptPrivateKeyTest method testDecryptPrivateKey.

@Test
public void testDecryptPrivateKey() throws Exception {
    final String pk = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" + "MIIFnDCBxQYJKoZIhvcNAQUNMIG3MIGVBgkqhkiG9w0BBQwwgYcEgYAzxG7mDxu4\n" + "TYtue+YQykOFgXlbFSJ9wbvVhVlbJP/16jewb4DhMZ/EWGzufLougL1WCwMc0BGM\n" + "wiozMk3Xl7kYCILBe78dsfS8bDZG9bMyS3PBuUn5Zuo4hhNMMtrJ4qYIOXYKsjml\n" + "rq5aipZXy3d/nfaHpGYTLGJTvSyZKDaGaQICJxAwHQYJYIZIAWUDBAEqBBDBxJJq\n" + "F0v+NbVvALtyDqOyBIIE0Kx8ce2gVTbC4FAowW/cV2texJWMZDe/i/NcTYvXide7\n" + "d8abj23zxWMT6+h89d27mXOD5BQMyXZSXMNAzHoMq2T0/uJwHNbm9VSXIZWrJ1wT\n" + "AHaipyUY5oQ3L0eu5CWqXISspRL7xlLAuIptpHunJIcvMvrK1o9xpJoEiZWUmq67\n" + "1tkDBH3hokcWU9u2YSwQsuZGHY1S0Z9QTiCry1nL8iz1ceRSee6J+ysjnbpxPsO1\n" + "UnNoKLIYYSm8Fep6O635Tc/XkytMF5GpJvLJNYZXuxTIRGUGcnk82hEwmTPp9ANc\n" + "4vycg7c4M7VMqPZsGjQtoYZLE0EQ243BZQdKG7pob9HIt+zTLmBtbMPrTsxCMEeS\n" + "eX5BrK7RDmHz4bM+Jv9KsO7xjWX4lZRXgRAkiM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nKLBMejBfrK0+uFqGuGDP4jOl8H26V+dycY/5ecBYhF9Bts5aKEFhkqibG\n" + "2Qt3tuP/WvAzJAAFw+YYTKUyW8YvDh1hf74cD8RxiNYZ0UuVoqADxbycNZ3+26rC\n" + "lnXEetmYD961oHZclzis4CUcDbLRbgcmv/fvpoj9OIgBOumB/1ku0ZmicXvlJ6wD\n" + "S+2l6QSAHZF3rmD0D7lSGMu0rdF648h8HkLsoABONdoHJrCE5ehnBvSLd34Hdwt7\n" + "-----END ENCRYPTED PRIVATE KEY-----\n";
    final UserPrivateKey privateKey = new UserPrivateKey(UserKeyPair.Version.RSA2048, pk);
    final UserPublicKey publicKey = new UserPublicKey(UserKeyPair.Version.RSA2048, "pubkey");
    final UserKeyPair pair = new UserKeyPair(privateKey, publicKey);
    Assert.assertTrue(Crypto.checkUserKeyPair(pair, "abcdabc1"));
}
Also used : UserKeyPair(com.dracoon.sdk.crypto.model.UserKeyPair) UserPublicKey(com.dracoon.sdk.crypto.model.UserPublicKey) UserPrivateKey(com.dracoon.sdk.crypto.model.UserPrivateKey) Test(org.junit.Test)

Aggregations

UserKeyPair (com.dracoon.sdk.crypto.model.UserKeyPair)9 UserKeyPairContainer (ch.cyberduck.core.sds.io.swagger.client.model.UserKeyPairContainer)6 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)5 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)4 CryptoException (com.dracoon.sdk.crypto.error.CryptoException)4 EncryptedFileKey (com.dracoon.sdk.crypto.model.EncryptedFileKey)4 Credentials (ch.cyberduck.core.Credentials)3 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)3 LoginCanceledException (ch.cyberduck.core.exception.LoginCanceledException)3 UserApi (ch.cyberduck.core.sds.io.swagger.client.api.UserApi)3 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)3 TripleCryptExceptionMappingService (ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService)3 TripleCryptKeyPair (ch.cyberduck.core.sds.triplecrypt.TripleCryptKeyPair)3 Test (org.junit.Test)3 Host (ch.cyberduck.core.Host)2 LoginOptions (ch.cyberduck.core.LoginOptions)2 UserFileKeySetRequest (ch.cyberduck.core.sds.io.swagger.client.model.UserFileKeySetRequest)2 VaultCredentials (ch.cyberduck.core.vault.VaultCredentials)2 IntegrationTest (ch.cyberduck.test.IntegrationTest)2 PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)2