use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.
the class EncryptionUtils method encryptFolderMetadata.
/*
METADATA
*/
/**
* Encrypt folder metaData
*
* @param decryptedFolderMetadata folder metaData to encrypt
* @return EncryptedFolderMetadata encrypted folder metadata
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static EncryptedFolderMetadata encryptFolderMetadata(DecryptedFolderMetadata decryptedFolderMetadata, String privateKey) throws IOException, NoSuchAlgorithmException, ShortBufferException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, BadPaddingException, NoSuchProviderException, IllegalBlockSizeException, InvalidKeySpecException, CertificateException {
HashMap<String, EncryptedFolderMetadata.EncryptedFile> files = new HashMap<>();
EncryptedFolderMetadata encryptedFolderMetadata = new EncryptedFolderMetadata(decryptedFolderMetadata.getMetadata(), files);
// Encrypt each file in "files"
for (Map.Entry<String, DecryptedFolderMetadata.DecryptedFile> entry : decryptedFolderMetadata.getFiles().entrySet()) {
String key = entry.getKey();
DecryptedFolderMetadata.DecryptedFile decryptedFile = entry.getValue();
EncryptedFolderMetadata.EncryptedFile encryptedFile = new EncryptedFolderMetadata.EncryptedFile();
encryptedFile.setInitializationVector(decryptedFile.getInitializationVector());
encryptedFile.setMetadataKey(decryptedFile.getMetadataKey());
encryptedFile.setAuthenticationTag(decryptedFile.getAuthenticationTag());
byte[] decryptedMetadataKey = EncryptionUtils.decodeStringToBase64Bytes(EncryptionUtils.decryptStringAsymmetric(decryptedFolderMetadata.getMetadata().getMetadataKeys().get(encryptedFile.getMetadataKey()), privateKey));
// encrypt
String dataJson = EncryptionUtils.serializeJSON(decryptedFile.getEncrypted());
encryptedFile.setEncrypted(EncryptionUtils.encryptStringSymmetric(dataJson, decryptedMetadataKey));
files.put(key, encryptedFile);
}
return encryptedFolderMetadata;
}
use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.
the class RefreshFolderOperation method synchronizeData.
/**
* Synchronizes the data retrieved from the server about the contents of the target folder
* with the current data in the local database.
*
* Grants that mChildren is updated with fresh data after execution.
*
* @param folderAndFiles Remote folder and children files in Folder
*/
private void synchronizeData(ArrayList<Object> folderAndFiles) {
// get 'fresh data' from the database
mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
// parse data from remote folder
OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(0));
remoteFolder.setParentId(mLocalFolder.getParentId());
remoteFolder.setFileId(mLocalFolder.getFileId());
Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
mFilesToSyncContents.clear();
// if local folder is encrypted, download fresh metadata
DecryptedFolderMetadata metadata;
boolean encryptedAncestor = FileStorageUtils.checkEncryptionStatus(mLocalFolder, mStorageManager);
mLocalFolder.setEncrypted(encryptedAncestor);
if (encryptedAncestor && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
metadata = EncryptionUtils.downloadFolderMetadata(mLocalFolder, getClient(), mContext, mAccount);
} else {
metadata = null;
}
// get current data about local contents of the folder to synchronize
List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder, false);
Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
for (OCFile file : localFiles) {
String remotePath = file.getRemotePath();
if (metadata != null && !file.isFolder()) {
remotePath = file.getParentRemotePath() + file.getEncryptedFileName();
}
localFilesMap.put(remotePath, file);
}
// loop to update every child
OCFile remoteFile;
OCFile localFile;
OCFile updatedFile;
RemoteFile r;
for (int i = 1; i < folderAndFiles.size(); i++) {
// / new OCFile instance with the data from the server
r = (RemoteFile) folderAndFiles.get(i);
remoteFile = FileStorageUtils.fillOCFile(r);
// new OCFile instance to merge fresh data from server with local state
updatedFile = FileStorageUtils.fillOCFile(r);
updatedFile.setParentId(mLocalFolder.getFileId());
// retrieve local data for the read file
localFile = localFilesMap.remove(remoteFile.getRemotePath());
// add to updatedFile data about LOCAL STATE (not existing in server)
updatedFile.setLastSyncDateForProperties(mCurrentSyncTime);
if (localFile != null) {
updatedFile.setFileId(localFile.getFileId());
updatedFile.setAvailableOffline(localFile.isAvailableOffline());
updatedFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
updatedFile.setModificationTimestampAtLastSyncForData(localFile.getModificationTimestampAtLastSyncForData());
updatedFile.setStoragePath(localFile.getStoragePath());
// eTag will not be updated unless file CONTENTS are synchronized
if (!updatedFile.isFolder() && localFile.isDown() && !updatedFile.getEtag().equals(localFile.getEtag())) {
updatedFile.setEtagInConflict(updatedFile.getEtag());
}
updatedFile.setEtag(localFile.getEtag());
if (updatedFile.isFolder()) {
updatedFile.setFileLength(remoteFile.getFileLength());
updatedFile.setMountType(remoteFile.getMountType());
} else if (mRemoteFolderChanged && MimeTypeUtil.isImage(remoteFile) && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
updatedFile.setNeedsUpdateThumbnail(true);
Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
}
updatedFile.setPublicLink(localFile.getPublicLink());
updatedFile.setShareViaLink(localFile.isSharedViaLink());
updatedFile.setShareWithSharee(localFile.isSharedWithSharee());
} else {
// remote eTag will not be updated unless file CONTENTS are synchronized
updatedFile.setEtag("");
}
// check and fix, if needed, local storage path
FileStorageUtils.searchForLocalFileInDefaultPath(updatedFile, mAccount);
// prepare content synchronization for kept-in-sync files
if (updatedFile.isAvailableOffline()) {
SynchronizeFileOperation operation = new SynchronizeFileOperation(localFile, remoteFile, mAccount, true, mContext);
mFilesToSyncContents.add(operation);
}
// update file name for encrypted files
if (metadata != null) {
updatedFile.setEncryptedFileName(updatedFile.getFileName());
try {
String decryptedFileName = metadata.getFiles().get(updatedFile.getFileName()).getEncrypted().getFilename();
String mimetype = metadata.getFiles().get(updatedFile.getFileName()).getEncrypted().getMimetype();
updatedFile.setFileName(decryptedFileName);
if (mimetype == null || mimetype.isEmpty()) {
updatedFile.setMimetype("application/octet-stream");
} else {
updatedFile.setMimetype(mimetype);
}
} catch (NullPointerException e) {
Log_OC.e(TAG, "Metadata for file " + updatedFile.getFileId() + " not found!");
}
}
// we parse content, so either the folder itself or its direct parent (which we check) must be encrypted
boolean encrypted = updatedFile.isEncrypted() || mLocalFolder.isEncrypted();
updatedFile.setEncrypted(encrypted);
updatedFiles.add(updatedFile);
}
// save updated contents in local database
mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
mChildren = updatedFiles;
}
use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.
the class UploadFileOperation method encryptedUpload.
// gson cannot handle sparse arrays easily, therefore use hashmap
@SuppressLint("AndroidLintUseSparseArrays")
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile parentFile) {
RemoteOperationResult result = null;
File temporalFile = null;
File originalFile = new File(mOriginalStoragePath);
File expectedFile = null;
FileLock fileLock = null;
long size;
boolean metadataExists = false;
String token = null;
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver());
String privateKey = arbitraryDataProvider.getValue(getAccount().name, EncryptionUtils.PRIVATE_KEY);
String publicKey = arbitraryDataProvider.getValue(getAccount().name, EncryptionUtils.PUBLIC_KEY);
try {
// check conditions
result = checkConditions(originalFile);
if (result != null) {
return result;
}
/**
*** E2E ****
*/
// Lock folder
LockFileOperation lockFileOperation = new LockFileOperation(parentFile.getLocalId());
RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client, true);
if (lockFileOperationResult.isSuccess()) {
token = (String) lockFileOperationResult.getData().get(0);
// immediately store it
mUpload.setFolderUnlockToken(token);
uploadsStorageManager.updateUpload(mUpload);
} else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
throw new Exception("Forbidden! Please try again later.)");
} else {
throw new Exception("Unknown error!");
}
// Update metadata
GetMetadataOperation getMetadataOperation = new GetMetadataOperation(parentFile.getLocalId());
RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client, true);
DecryptedFolderMetadata metadata;
if (getMetadataOperationResult.isSuccess()) {
metadataExists = true;
// decrypt metadata
String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
});
metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
} else if (getMetadataOperationResult.getHttpCode() == HttpStatus.SC_NOT_FOUND) {
// new metadata
metadata = new DecryptedFolderMetadata();
metadata.setMetadata(new DecryptedFolderMetadata.Metadata());
metadata.getMetadata().setMetadataKeys(new HashMap<>());
String metadataKey = EncryptionUtils.encodeBytesToBase64String(EncryptionUtils.generateKey());
String encryptedMetadataKey = EncryptionUtils.encryptStringAsymmetric(metadataKey, publicKey);
metadata.getMetadata().getMetadataKeys().put(0, encryptedMetadataKey);
} else {
// TODO error
throw new Exception("something wrong");
}
/**
*** E2E ****
*/
// check name collision
checkNameCollision(client, metadata, parentFile.isEncrypted());
String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
expectedFile = new File(expectedPath);
result = copyFile(originalFile, expectedPath);
if (result != null) {
return result;
}
// Get the last modification date of the file from the file system
Long timeStampLong = originalFile.lastModified() / 1000;
String timeStamp = timeStampLong.toString();
/**
*** E2E ****
*/
// Key, always generate new one
byte[] key = EncryptionUtils.generateKey();
// IV, always generate new one
byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength);
EncryptionUtils.EncryptedFile encryptedFile = EncryptionUtils.encryptFile(mFile, key, iv);
// new random file name, check if it exists in metadata
String encryptedFileName = UUID.randomUUID().toString().replaceAll("-", "");
while (metadata.getFiles().get(encryptedFileName) != null) {
encryptedFileName = UUID.randomUUID().toString().replaceAll("-", "");
}
mFile.setEncryptedFileName(encryptedFileName);
File encryptedTempFile = File.createTempFile("encFile", encryptedFileName);
FileOutputStream fileOutputStream = new FileOutputStream(encryptedTempFile);
fileOutputStream.write(encryptedFile.encryptedBytes);
fileOutputStream.close();
/**
*** E2E ****
*/
FileChannel channel = null;
try {
channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
fileLock = channel.tryLock();
} catch (FileNotFoundException e) {
// this basically means that the file is on SD card
// try to copy file to temporary dir if it doesn't exist
String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
mFile.setStoragePath(temporalPath);
temporalFile = new File(temporalPath);
Files.deleteIfExists(Paths.get(temporalPath));
result = copy(originalFile, temporalFile);
if (result == null) {
if (temporalFile.length() == originalFile.length()) {
channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
fileLock = channel.tryLock();
} else {
result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
}
}
}
try {
size = channel.size();
} catch (IOException e1) {
size = new File(mFile.getStoragePath()).length();
}
for (OCUpload ocUpload : uploadsStorageManager.getAllStoredUploads()) {
if (ocUpload.getUploadId() == getOCUploadId()) {
ocUpload.setFileSize(size);
uploadsStorageManager.updateUpload(ocUpload);
break;
}
}
// / perform the upload
if (mChunked && (size > ChunkedUploadRemoteFileOperation.CHUNK_SIZE)) {
mUploadOperation = new ChunkedUploadRemoteFileOperation(mContext, encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
} else {
mUploadOperation = new UploadRemoteFileOperation(encryptedTempFile.getAbsolutePath(), mFile.getParentRemotePath() + encryptedFileName, mFile.getMimetype(), mFile.getEtagInConflict(), timeStamp);
}
Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
while (listener.hasNext()) {
mUploadOperation.addDatatransferProgressListener(listener.next());
}
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
}
result = mUploadOperation.execute(client, true);
// location in the Nextcloud local folder
if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
}
if (result.isSuccess()) {
// upload metadata
DecryptedFolderMetadata.DecryptedFile decryptedFile = new DecryptedFolderMetadata.DecryptedFile();
DecryptedFolderMetadata.Data data = new DecryptedFolderMetadata.Data();
data.setFilename(mFile.getFileName());
data.setMimetype(mFile.getMimetype());
data.setKey(EncryptionUtils.encodeBytesToBase64String(key));
decryptedFile.setEncrypted(data);
decryptedFile.setInitializationVector(EncryptionUtils.encodeBytesToBase64String(iv));
decryptedFile.setAuthenticationTag(encryptedFile.authenticationTag);
metadata.getFiles().put(encryptedFileName, decryptedFile);
EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
// upload metadata
RemoteOperationResult uploadMetadataOperationResult;
if (metadataExists) {
// update metadata
UpdateMetadataOperation storeMetadataOperation = new UpdateMetadataOperation(parentFile.getLocalId(), serializedFolderMetadata, token);
uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
} else {
// store metadata
StoreMetadataOperation storeMetadataOperation = new StoreMetadataOperation(parentFile.getLocalId(), serializedFolderMetadata);
uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
}
if (!uploadMetadataOperationResult.isSuccess()) {
throw new Exception();
}
}
} catch (FileNotFoundException e) {
Log_OC.d(TAG, mFile.getStoragePath() + " not exists anymore");
result = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
} catch (OverlappingFileLockException e) {
Log_OC.d(TAG, "Overlapping file lock exception");
result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
} catch (Exception e) {
result = new RemoteOperationResult(e);
} finally {
mUploadStarted.set(false);
if (fileLock != null) {
try {
fileLock.release();
} catch (IOException e) {
Log_OC.e(TAG, "Failed to unlock file with path " + mFile.getStoragePath());
}
}
if (temporalFile != null && !originalFile.equals(temporalFile)) {
temporalFile.delete();
}
if (result == null) {
result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
}
if (result.isSuccess()) {
Log_OC.i(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
} else {
if (result.getException() != null) {
if (result.isCancelled()) {
Log_OC.w(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
} else {
Log_OC.e(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage(), result.getException());
}
} else {
Log_OC.e(TAG, "Upload of " + mFile.getStoragePath() + " to " + mFile.getRemotePath() + ": " + result.getLogMessage());
}
}
}
if (result.isSuccess()) {
handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
RemoteOperationResult unlockFolderResult = unlockFolder(parentFile, client, token);
if (!unlockFolderResult.isSuccess()) {
return unlockFolderResult;
}
} else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
}
return result;
}
use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.
the class EncryptionUtils method decryptFolderMetaData.
/*
* decrypt folder metaData with private key
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static DecryptedFolderMetadata decryptFolderMetaData(EncryptedFolderMetadata encryptedFolderMetadata, String privateKey) throws IOException, NoSuchAlgorithmException, ShortBufferException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, BadPaddingException, NoSuchProviderException, IllegalBlockSizeException, CertificateException, InvalidKeySpecException {
HashMap<String, DecryptedFolderMetadata.DecryptedFile> files = new HashMap<>();
DecryptedFolderMetadata decryptedFolderMetadata = new DecryptedFolderMetadata(encryptedFolderMetadata.getMetadata(), files);
for (Map.Entry<String, EncryptedFolderMetadata.EncryptedFile> entry : encryptedFolderMetadata.getFiles().entrySet()) {
String key = entry.getKey();
EncryptedFolderMetadata.EncryptedFile encryptedFile = entry.getValue();
DecryptedFolderMetadata.DecryptedFile decryptedFile = new DecryptedFolderMetadata.DecryptedFile();
decryptedFile.setInitializationVector(encryptedFile.getInitializationVector());
decryptedFile.setMetadataKey(encryptedFile.getMetadataKey());
decryptedFile.setAuthenticationTag(encryptedFile.getAuthenticationTag());
byte[] decryptedMetadataKey = EncryptionUtils.decodeStringToBase64Bytes(EncryptionUtils.decryptStringAsymmetric(decryptedFolderMetadata.getMetadata().getMetadataKeys().get(encryptedFile.getMetadataKey()), privateKey));
// decrypt
String dataJson = EncryptionUtils.decryptStringSymmetric(encryptedFile.getEncrypted(), decryptedMetadataKey);
decryptedFile.setEncrypted(EncryptionUtils.deserializeJSON(dataJson, new TypeToken<DecryptedFolderMetadata.Data>() {
}));
files.put(key, decryptedFile);
}
return decryptedFolderMetadata;
}
use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.
the class DownloadFileOperation method run.
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
File newFile;
boolean moved;
// / download will be performed to a temporal file, then moved to the final location
File tmpFile = new File(getTmpPath());
String tmpFolder = getTmpFolder();
// / perform the download
synchronized (mCancellationRequested) {
if (mCancellationRequested.get()) {
return new RemoteOperationResult(new OperationCancelledException());
}
}
mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
while (listener.hasNext()) {
mDownloadOperation.addDatatransferProgressListener(listener.next());
}
result = mDownloadOperation.execute(client, client.useNextcloudUserAgent());
if (result.isSuccess()) {
mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
mEtag = mDownloadOperation.getEtag();
newFile = new File(getSavePath());
newFile.getParentFile().mkdirs();
// decrypt file
if (mFile.isEncrypted() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(mAccount, mContext.getContentResolver());
OCFile parent = fileDataStorageManager.getFileByPath(mFile.getParentRemotePath());
DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, mContext, mAccount);
if (metadata == null) {
return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
}
byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(mFile.getEncryptedFileName()).getEncrypted().getKey());
byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(mFile.getEncryptedFileName()).getInitializationVector());
byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(mFile.getEncryptedFileName()).getAuthenticationTag());
try {
byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
fileOutputStream.write(decryptedBytes);
fileOutputStream.close();
} catch (Exception e) {
return new RemoteOperationResult(e);
}
}
moved = tmpFile.renameTo(newFile);
newFile.setLastModified(mFile.getModificationTimestamp());
if (!moved) {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
}
}
Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
return result;
}
Aggregations