Search in sources :

Example 1 with RotatingNonceGenerator

use of ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator in project cyberduck by iterate-ch.

the class B2WriteFeatureTest method testWrite.

@Test
public void testWrite() throws Exception {
    final TransferStatus status = new TransferStatus();
    final int length = 1048576;
    final byte[] content = RandomUtils.nextBytes(length);
    status.setLength(content.length);
    final Path home = new Path("/test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final CryptoVault cryptomator = new CryptoVault(new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)));
    final Path vault = cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
    final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
    final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
    final CryptoWriteFeature<BaseB2Response> writer = new CryptoWriteFeature<BaseB2Response>(session, new B2WriteFeature(session, fileid), cryptomator);
    final FileHeader header = cryptomator.getFileHeaderCryptor().create();
    status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
    status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(content.length)));
    status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
    final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    out.close();
    assertTrue(new CryptoFindFeature(session, new B2FindFeature(session, fileid), cryptomator).find(test));
    final PathAttributes attributes = new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(test);
    assertEquals(content.length, attributes.getSize());
    assertEquals(content.length, writer.append(test, status.withRemote(attributes)).size, 0L);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
    final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
    new StreamCopier(status, status).transfer(in, buffer);
    assertArrayEquals(content, buffer.toByteArray());
    cryptomator.getFeature(session, Delete.class, new B2DeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Also used : Delete(ch.cyberduck.core.features.Delete) B2ReadFeature(ch.cyberduck.core.b2.B2ReadFeature) CryptoWriteFeature(ch.cyberduck.core.cryptomator.features.CryptoWriteFeature) B2VersionIdProvider(ch.cyberduck.core.b2.B2VersionIdProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) B2AttributesFinderFeature(ch.cyberduck.core.b2.B2AttributesFinderFeature) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) B2DeleteFeature(ch.cyberduck.core.b2.B2DeleteFeature) DefaultVaultRegistry(ch.cyberduck.core.vault.DefaultVaultRegistry) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) CryptoReadFeature(ch.cyberduck.core.cryptomator.features.CryptoReadFeature) FileHeader(org.cryptomator.cryptolib.api.FileHeader) Path(ch.cyberduck.core.Path) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PathAttributes(ch.cyberduck.core.PathAttributes) B2WriteFeature(ch.cyberduck.core.b2.B2WriteFeature) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CryptoFindFeature(ch.cyberduck.core.cryptomator.features.CryptoFindFeature) CryptoAttributesFeature(ch.cyberduck.core.cryptomator.features.CryptoAttributesFeature) ByteArrayInputStream(java.io.ByteArrayInputStream) B2FindFeature(ch.cyberduck.core.b2.B2FindFeature) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) BaseB2Response(synapticloop.b2.response.BaseB2Response) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) AbstractB2Test(ch.cyberduck.core.b2.AbstractB2Test) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 2 with RotatingNonceGenerator

use of ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator in project cyberduck by iterate-ch.

the class CryptoBulkFeature method pre.

@Override
public R pre(final Transfer.Type type, final Map<TransferItem, TransferStatus> files, final ConnectionCallback callback) throws BackgroundException {
    final Map<TransferItem, TransferStatus> encrypted = new HashMap<>(files.size());
    final ArrayList<Map.Entry<TransferItem, TransferStatus>> sorted = new ArrayList<>(files.entrySet());
    // Sort with folder first in list
    sorted.sort(new Comparator<Map.Entry<TransferItem, TransferStatus>>() {

        @Override
        public int compare(final Map.Entry<TransferItem, TransferStatus> o1, final Map.Entry<TransferItem, TransferStatus> o2) {
            return new PathPriorityComparator().compare(o1.getKey().remote, o2.getKey().remote);
        }
    });
    for (Map.Entry<TransferItem, TransferStatus> entry : sorted) {
        final Path file = entry.getKey().remote;
        final Local local = entry.getKey().local;
        final TransferStatus status = entry.getValue();
        if (null == status.getHeader()) {
            // Write header to be reused in writer
            final FileHeader header = cryptomator.getFileHeaderCryptor().create();
            status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
        }
        if (null == status.getNonces()) {
            status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(status.getLength())));
        }
        if (file.isDirectory()) {
            if (!status.isExists()) {
                switch(type) {
                    case upload:
                        // Preset directory ID for new folders to avert lookup with not found failure in directory ID provider
                        final String directoryId = random.random();
                        encrypted.put(new TransferItem(cryptomator.encrypt(session, file, directoryId, false), local), status);
                        break;
                    default:
                        encrypted.put(new TransferItem(cryptomator.encrypt(session, file), local), status);
                        break;
                }
            } else {
                encrypted.put(new TransferItem(cryptomator.encrypt(session, file), local), status);
            }
        } else {
            encrypted.put(new TransferItem(cryptomator.encrypt(session, file), local), status);
        }
    }
    return delegate.pre(type, encrypted, callback);
}
Also used : Path(ch.cyberduck.core.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Local(ch.cyberduck.core.Local) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) PathPriorityComparator(ch.cyberduck.core.transfer.download.PathPriorityComparator) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) TransferItem(ch.cyberduck.core.transfer.TransferItem) HashMap(java.util.HashMap) Map(java.util.Map) FileHeader(org.cryptomator.cryptolib.api.FileHeader)

Example 3 with RotatingNonceGenerator

use of ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator in project cyberduck by iterate-ch.

the class CryptoChecksumCompute method compute.

@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
    if (Checksum.NONE == delegate.compute(new NullInputStream(0L), new TransferStatus())) {
        return Checksum.NONE;
    }
    if (null == status.getHeader()) {
        // Write header to be reused in writer
        final FileHeader header = cryptomator.getFileHeaderCryptor().create();
        status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
    }
    if (null == status.getNonces()) {
        // Make nonces reusable in case we need to compute a checksum
        status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(status.getLength())));
    }
    return this.compute(this.normalize(in, status), status, status.getOffset(), status.getLength(), status.getHeader(), status.getNonces());
}
Also used : TransferStatus(ch.cyberduck.core.transfer.TransferStatus) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) FileHeader(org.cryptomator.cryptolib.api.FileHeader) NullInputStream(org.apache.commons.io.input.NullInputStream)

Example 4 with RotatingNonceGenerator

use of ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator in project cyberduck by iterate-ch.

the class EueWriteFeatureTest method testWrite.

@Test
public void testWrite() throws Exception {
    final TransferStatus status = new TransferStatus();
    final int length = 1048576;
    final byte[] content = RandomUtils.nextBytes(length);
    status.setLength(content.length);
    final Path home = new DefaultHomeFinderService(session).find();
    final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
    final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final CryptoVault cryptomator = new CryptoVault(vault, profile.getProperties().get("cryptomator.vault.masterkey.filename"), profile.getProperties().get("cryptomator.vault.config.filename"), profile.getProperties().get("cryptomator.vault.pepper").getBytes(StandardCharsets.UTF_8));
    cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
    session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
    final EueResourceIdProvider fileid = new EueResourceIdProvider(session);
    final CryptoWriteFeature<EueWriteFeature.Chunk> writer = new CryptoWriteFeature<EueWriteFeature.Chunk>(session, new EueWriteFeature(session, fileid), cryptomator);
    final FileHeader header = cryptomator.getFileHeaderCryptor().create();
    status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
    status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(content.length)));
    status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
    final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    out.close();
    assertTrue(new CryptoFindFeature(session, new EueFindFeature(session, fileid), cryptomator).find(test));
    assertEquals(content.length, new CryptoWriteFeature<>(session, new EueWriteFeature(session, fileid), cryptomator).append(test, status.withRemote(new CryptoAttributesFeature(session, new EueAttributesFinderFeature(session, fileid), cryptomator).find(test))).size, 0L);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
    final InputStream in = new CryptoReadFeature(session, new EueReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
    new StreamCopier(status, status).transfer(in, buffer);
    assertArrayEquals(content, buffer.toByteArray());
    cryptomator.getFeature(session, Delete.class, new EueDeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
    session.close();
}
Also used : Delete(ch.cyberduck.core.features.Delete) CryptoWriteFeature(ch.cyberduck.core.cryptomator.features.CryptoWriteFeature) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) EueAttributesFinderFeature(ch.cyberduck.core.eue.EueAttributesFinderFeature) EueFindFeature(ch.cyberduck.core.eue.EueFindFeature) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) EueReadFeature(ch.cyberduck.core.eue.EueReadFeature) EueDeleteFeature(ch.cyberduck.core.eue.EueDeleteFeature) DefaultVaultRegistry(ch.cyberduck.core.vault.DefaultVaultRegistry) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) CryptoReadFeature(ch.cyberduck.core.cryptomator.features.CryptoReadFeature) FileHeader(org.cryptomator.cryptolib.api.FileHeader) Path(ch.cyberduck.core.Path) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultHomeFinderService(ch.cyberduck.core.shared.DefaultHomeFinderService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EueResourceIdProvider(ch.cyberduck.core.eue.EueResourceIdProvider) EueWriteFeature(ch.cyberduck.core.eue.EueWriteFeature) CryptoFindFeature(ch.cyberduck.core.cryptomator.features.CryptoFindFeature) CryptoAttributesFeature(ch.cyberduck.core.cryptomator.features.CryptoAttributesFeature) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) AbstractEueSessionTest(ch.cyberduck.core.eue.AbstractEueSessionTest) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 5 with RotatingNonceGenerator

use of ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator in project cyberduck by iterate-ch.

the class S3VersioningFeatureTest method testRevert.

@Test
public void testRevert() throws Exception {
    final Path bucket = new Path("versioning-test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path vault = new Path(bucket, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
    final CryptoVault cryptomator = new CryptoVault(vault);
    cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
    session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
    final AttributesFinder f = new CryptoAttributesFeature(session, new S3AttributesFinderFeature(session), cryptomator);
    final Path test = new CryptoTouchFeature<>(session, new S3TouchFeature(session), new S3WriteFeature(session), cryptomator).touch(new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus());
    final PathAttributes initialAttributes = new PathAttributes(test.attributes());
    final String initialVersion = test.attributes().getVersionId();
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus status = new TransferStatus();
    final Write<StorageObject> writer = new CryptoWriteFeature<>(session, new S3MultipartWriteFeature(session), cryptomator);
    final FileHeader header = cryptomator.getFileHeaderCryptor().create();
    status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
    status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(content.length)));
    status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
    final StatusOutputStream<StorageObject> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    final PathAttributes updated = new CryptoAttributesFeature(session, new S3AttributesFinderFeature(session, true), cryptomator).find(new Path(test).withAttributes(PathAttributes.EMPTY));
    assertNotEquals(initialVersion, updated.getVersionId());
    assertFalse(updated.getVersions().isEmpty());
    assertEquals(1, updated.getVersions().size());
    assertEquals(new Path(test).withAttributes(initialAttributes), updated.getVersions().get(0));
    assertTrue(new CryptoFindFeature(session, new S3FindFeature(session), cryptomator).find(updated.getVersions().get(0)));
    assertEquals(initialVersion, new CryptoAttributesFeature(session, new S3AttributesFinderFeature(session), cryptomator).find(updated.getVersions().get(0)).getVersionId());
    new CryptoVersioningFeature(session, new S3VersioningFeature(session, new S3AccessControlListFeature(session)), cryptomator).revert(new Path(test).withAttributes(initialAttributes));
    final PathAttributes reverted = new CryptoAttributesFeature(session, new S3AttributesFinderFeature(session, true), cryptomator).find(new Path(test).withAttributes(PathAttributes.EMPTY));
    assertNotEquals(initialVersion, reverted.getVersionId());
    assertEquals(2, reverted.getVersions().size());
    assertEquals(test.attributes().getSize(), reverted.getSize());
    assertEquals(content.length, reverted.getVersions().get(0).attributes().getSize());
    cryptomator.getFeature(session, Delete.class, new S3DefaultDeleteFeature(session)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Also used : Delete(ch.cyberduck.core.features.Delete) S3AttributesFinderFeature(ch.cyberduck.core.s3.S3AttributesFinderFeature) S3TouchFeature(ch.cyberduck.core.s3.S3TouchFeature) CryptoWriteFeature(ch.cyberduck.core.cryptomator.features.CryptoWriteFeature) AttributesFinder(ch.cyberduck.core.features.AttributesFinder) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) DefaultVaultRegistry(ch.cyberduck.core.vault.DefaultVaultRegistry) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) FileHeader(org.cryptomator.cryptolib.api.FileHeader) Path(ch.cyberduck.core.Path) S3FindFeature(ch.cyberduck.core.s3.S3FindFeature) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) StorageObject(org.jets3t.service.model.StorageObject) CryptoVersioningFeature(ch.cyberduck.core.cryptomator.features.CryptoVersioningFeature) S3DefaultDeleteFeature(ch.cyberduck.core.s3.S3DefaultDeleteFeature) PathAttributes(ch.cyberduck.core.PathAttributes) S3VersioningFeature(ch.cyberduck.core.s3.S3VersioningFeature) S3AccessControlListFeature(ch.cyberduck.core.s3.S3AccessControlListFeature) CryptoAttributesFeature(ch.cyberduck.core.cryptomator.features.CryptoAttributesFeature) CryptoFindFeature(ch.cyberduck.core.cryptomator.features.CryptoFindFeature) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) S3MultipartWriteFeature(ch.cyberduck.core.s3.S3MultipartWriteFeature) S3WriteFeature(ch.cyberduck.core.s3.S3WriteFeature) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) AbstractS3Test(ch.cyberduck.core.s3.AbstractS3Test) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Aggregations

RotatingNonceGenerator (ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator)15 FileHeader (org.cryptomator.cryptolib.api.FileHeader)15 Path (ch.cyberduck.core.Path)14 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)14 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)12 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)12 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)12 DisabledPasswordCallback (ch.cyberduck.core.DisabledPasswordCallback)12 DisabledPasswordStore (ch.cyberduck.core.DisabledPasswordStore)12 Delete (ch.cyberduck.core.features.Delete)12 DefaultVaultRegistry (ch.cyberduck.core.vault.DefaultVaultRegistry)12 VaultCredentials (ch.cyberduck.core.vault.VaultCredentials)12 IntegrationTest (ch.cyberduck.test.IntegrationTest)12 Test (org.junit.Test)12 CryptoFindFeature (ch.cyberduck.core.cryptomator.features.CryptoFindFeature)11 CryptoWriteFeature (ch.cyberduck.core.cryptomator.features.CryptoWriteFeature)11 StreamCopier (ch.cyberduck.core.io.StreamCopier)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 CryptoReadFeature (ch.cyberduck.core.cryptomator.features.CryptoReadFeature)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10