@Test public void testPartialRead() throws Exception { final Path room = new Path("test", EnumSet.of(Path.Type.directory, Path.Type.volume, Path.Type.triplecrypt)); final byte[] content = RandomUtils.nextBytes(32769); final TransferStatus status = new TransferStatus(); status.setLength(content.length); final SDSNodeIdProvider nodeid = new SDSNodeIdProvider(session).withCache(cache); final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file, Path.Type.triplecrypt)); final SDSEncryptionBulkFeature bulk = new SDSEncryptionBulkFeature(session, nodeid); bulk.pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(test), status), new DisabledConnectionCallback()); final TripleCryptWriteFeature writer = new TripleCryptWriteFeature(session, nodeid, new SDSMultipartWriteFeature(session, nodeid)); final StatusOutputStream<VersionId> out = writer.write(test, status, new DisabledConnectionCallback()); assertNotNull(out); new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out); final VersionId version = out.getStatus(); assertNotNull(version); assertTrue(new DefaultFindFeature(session).find(test)); assertEquals(content.length, new SDSAttributesFinderFeature(session, nodeid).find(test).getSize()); final byte[] compare = new byte[content.length - 1000]; final InputStream stream = new TripleCryptReadFeature(session, nodeid, new SDSReadFeature(session, nodeid)).read(test, new TransferStatus(), new DisabledConnectionCallback() { @Override public void warn(final Host bookmark, final String title, final String message, final String defaultButton, final String cancelButton, final String preference) { } @Override public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) { return new VaultCredentials("eth[oh8uv4Eesij"); } }); stream.skip(1000); IOUtils.readFully(stream, compare); stream.close(); assertArrayEquals(Arrays.copyOfRange(content, 1000, content.length), compare); new SDSDeleteFeature(session, nodeid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@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.getFileid(file, new DisabledListProgressListener())), StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); if(log.isDebugEnabled()) { log.debug(String.format("Attempt to unlock private key %s", privateKey)); } final Credentials passphrase; try { passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); } catch(LoginCanceledException e) { throw new AccessDeniedException(LocaleFactory.localizedString("Decryption password required", "SDS"), e); } final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); return new TripleCryptInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag())); } catch(ApiException e) { throw new SDSExceptionMappingService().map("Download {0} failed", e, file); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file); } }
TripleCryptReadFeature implements 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.getFileid(file, new DisabledListProgressListener())), StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); if(log.isDebugEnabled()) { log.debug(String.format("Attempt to unlock private key %s", privateKey)); } final Credentials passphrase; try { passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); } catch(LoginCanceledException e) { throw new AccessDeniedException(LocaleFactory.localizedString("Decryption password required", "SDS"), e); } final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); return new TripleCryptInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag())); } catch(ApiException e) { throw new SDSExceptionMappingService().map("Download {0} failed", e, file); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file); } } }
TripleCryptReadFeature implements 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.getFileid(file, new DisabledListProgressListener())), StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); if(log.isDebugEnabled()) { log.debug(String.format("Attempt to unlock private key %s", privateKey)); } final Credentials passphrase; try { passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); } catch(LoginCanceledException e) { throw new AccessDeniedException(LocaleFactory.localizedString("Decryption password required", "SDS"), e); } final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); return new TripleCryptInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag())); } catch(ApiException e) { throw new SDSExceptionMappingService().map("Download {0} failed", e, file); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file); } } TripleCryptReadFeature(final SDSSession session, final SDSNodeIdProvider nodeid, final SDSReadFeature proxy); }
TripleCryptReadFeature implements 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.getFileid(file, new DisabledListProgressListener())), StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); if(log.isDebugEnabled()) { log.debug(String.format("Attempt to unlock private key %s", privateKey)); } final Credentials passphrase; try { passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); } catch(LoginCanceledException e) { throw new AccessDeniedException(LocaleFactory.localizedString("Decryption password required", "SDS"), e); } final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); return new TripleCryptInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag())); } catch(ApiException e) { throw new SDSExceptionMappingService().map("Download {0} failed", e, file); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file); } } TripleCryptReadFeature(final SDSSession session, final SDSNodeIdProvider nodeid, final SDSReadFeature proxy); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file); }
TripleCryptReadFeature implements 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.getFileid(file, new DisabledListProgressListener())), StringUtils.EMPTY); final UserPrivateKey privateKey = new UserPrivateKey(); final UserKeyPairContainer keyPairContainer = session.keyPair(); privateKey.setPrivateKey(keyPairContainer.getPrivateKeyContainer().getPrivateKey()); privateKey.setVersion(keyPairContainer.getPrivateKeyContainer().getVersion()); final UserKeyPair userKeyPair = new UserKeyPair(); userKeyPair.setUserPrivateKey(privateKey); if(log.isDebugEnabled()) { log.debug(String.format("Attempt to unlock private key %s", privateKey)); } final Credentials passphrase; try { passphrase = new TripleCryptKeyPair().unlock(callback, session.getHost(), userKeyPair); } catch(LoginCanceledException e) { throw new AccessDeniedException(LocaleFactory.localizedString("Decryption password required", "SDS"), e); } final PlainFileKey plainFileKey = Crypto.decryptFileKey(TripleCryptConverter.toCryptoEncryptedFileKey(key), privateKey, passphrase.getPassword()); return new TripleCryptInputStream(proxy.read(file, status, callback), Crypto.createFileDecryptionCipher(plainFileKey), CryptoUtils.stringToByteArray(plainFileKey.getTag())); } catch(ApiException e) { throw new SDSExceptionMappingService().map("Download {0} failed", e, file); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map("Download {0} failed", e, file); } } TripleCryptReadFeature(final SDSSession session, final SDSNodeIdProvider nodeid, final SDSReadFeature proxy); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file);  }