Search in sources :

Example 1 with PlainFileKey

use of com.dracoon.sdk.crypto.model.PlainFileKey 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 2 with PlainFileKey

use of com.dracoon.sdk.crypto.model.PlainFileKey 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 3 with PlainFileKey

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

the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptZeroBytes.

@Test
public void testEncryptDecryptZeroBytes() throws Exception {
    final byte[] content = RandomUtils.nextBytes(0);
    final ByteArrayInputStream plain = new ByteArrayInputStream(content);
    final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
    final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {

        @Override
        public SDSApiClient getClient() {
            return new SDSApiClient(new MockHttpClient());
        }
    };
    final TransferStatus status = new TransferStatus();
    final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
    status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
    final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOUtils.copy(encryptInputStream, os, 42);
    encryptInputStream.close();
    out.close();
    final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
    final FileKey fileKey = reader.readValue(status.getFilekey().array());
    final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
    final byte[] compare = new byte[content.length];
    IOUtils.read(cryptInputStream, compare);
    assertArrayEquals(content, compare);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) Test(org.junit.Test)

Example 4 with PlainFileKey

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

the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize.

@Test
public void testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize() throws Exception {
    final byte[] content = RandomUtils.nextBytes(1024 * 1024);
    final ByteArrayInputStream plain = new ByteArrayInputStream(content);
    final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
    final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {

        @Override
        public SDSApiClient getClient() {
            return new SDSApiClient(new MockHttpClient());
        }
    };
    final TransferStatus status = new TransferStatus();
    final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
    status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
    final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    new StreamCopier(StreamCancelation.noop, StreamProgress.noop).withLimit((long) content.length).withChunksize(32768).transfer(encryptInputStream, os);
    encryptInputStream.close();
    out.close();
    final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
    final FileKey fileKey = reader.readValue(status.getFilekey().array());
    assertNotNull(fileKey.getTag());
    final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
    final byte[] compare = new byte[content.length];
    IOUtils.read(cryptInputStream, compare);
    assertArrayEquals(content, compare);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test)

Example 5 with PlainFileKey

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

the class TripleCryptConverter method toCryptoPlainFileKey.

public static PlainFileKey toCryptoPlainFileKey(final FileKey key) throws UnknownVersionException {
    final PlainFileKey fileKey = new PlainFileKey(PlainFileKey.Version.getByValue(key.getVersion()), key.getKey(), key.getIv());
    fileKey.setTag(key.getTag());
    return fileKey;
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey)

Aggregations

PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)6 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)5 Host (ch.cyberduck.core.Host)4 TestProtocol (ch.cyberduck.core.TestProtocol)3 SDSApiClient (ch.cyberduck.core.sds.SDSApiClient)3 SDSSession (ch.cyberduck.core.sds.SDSSession)3 DefaultX509KeyManager (ch.cyberduck.core.ssl.DefaultX509KeyManager)3 DisabledX509TrustManager (ch.cyberduck.core.ssl.DisabledX509TrustManager)3 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)3 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 MockHttpClient (com.google.api.client.testing.http.apache.MockHttpClient)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Test (org.junit.Test)3 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)2 StreamCopier (ch.cyberduck.core.io.StreamCopier)2 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)2 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)2 CryptoException (com.dracoon.sdk.crypto.error.CryptoException)2