-
Notifications
You must be signed in to change notification settings - Fork 35
Feature: Public method for decrypting ciphertext name. #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9337642
first impl draft
infeo f03e42b
adjust to new methods
infeo ea70bb5
add TestCryptoException
infeo 4932e54
add unit tests for getCleartextNameInternal
infeo 0915cb8
move feature to its own class
infeo a01a2b2
Merge branch 'develop' into feature/cipher-to-clear
infeo eb78338
fix test
infeo 8e72527
resolve TODO
infeo ffede33
more unit tests
infeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/org/cryptomator/cryptofs/FileNameDecryptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package org.cryptomator.cryptofs; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.io.BaseEncoding; | ||
| import org.cryptomator.cryptofs.common.Constants; | ||
| import org.cryptomator.cryptofs.common.StringUtils; | ||
| import org.cryptomator.cryptolib.api.CryptoException; | ||
| import org.cryptomator.cryptolib.api.Cryptor; | ||
| import org.cryptomator.cryptolib.api.FileNameCryptor; | ||
|
|
||
| import javax.inject.Inject; | ||
| import java.io.IOException; | ||
| import java.nio.file.FileSystemException; | ||
| import java.nio.file.NoSuchFileException; | ||
| import java.nio.file.Path; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * @see CryptoFileSystem#getCleartextName(Path) | ||
| */ | ||
| @CryptoFileSystemScoped | ||
| class FileNameDecryptor { | ||
|
|
||
| private final DirectoryIdBackup dirIdBackup; | ||
| private final LongFileNameProvider longFileNameProvider; | ||
| private final Path vaultPath; | ||
| private final FileNameCryptor fileNameCryptor; | ||
|
|
||
| @Inject | ||
| public FileNameDecryptor(@PathToVault Path vaultPath, Cryptor cryptor, DirectoryIdBackup dirIdBackup, LongFileNameProvider longFileNameProvider) { | ||
| this.vaultPath = vaultPath; | ||
| this.fileNameCryptor = cryptor.fileNameCryptor(); | ||
| this.dirIdBackup = dirIdBackup; | ||
| this.longFileNameProvider = longFileNameProvider; | ||
| } | ||
|
|
||
| public String decryptFilename(Path ciphertextNode) throws IOException, UnsupportedOperationException { | ||
| validatePath(ciphertextNode.toAbsolutePath()); | ||
| return decryptFilenameInternal(ciphertextNode); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| String decryptFilenameInternal(Path ciphertextNode) throws IOException, UnsupportedOperationException { | ||
| byte[] dirId = null; | ||
| try { | ||
| dirId = dirIdBackup.read(ciphertextNode); | ||
| } catch (NoSuchFileException e) { | ||
| throw new UnsupportedOperationException("Directory does not have a " + Constants.DIR_ID_BACKUP_FILE_NAME + " file."); | ||
| } catch (CryptoException | IllegalStateException e) { | ||
| throw new FileSystemException(ciphertextNode.toString(), null, "Decryption of dirId backup file failed:" + e); | ||
| } | ||
| var fullCipherNodeName = ciphertextNode.getFileName().toString(); | ||
| var cipherNodeExtension = fullCipherNodeName.substring(fullCipherNodeName.length() - 4); | ||
|
|
||
| String actualEncryptedName = switch (cipherNodeExtension) { | ||
| case Constants.CRYPTOMATOR_FILE_SUFFIX -> StringUtils.removeEnd(fullCipherNodeName, Constants.CRYPTOMATOR_FILE_SUFFIX); | ||
| case Constants.DEFLATED_FILE_SUFFIX -> longFileNameProvider.inflate(ciphertextNode); | ||
| default -> throw new IllegalStateException("SHOULD NOT REACH HERE"); | ||
| }; | ||
| try { | ||
| return fileNameCryptor.decryptFilename(BaseEncoding.base64Url(), actualEncryptedName, dirId); | ||
| } catch (CryptoException e) { | ||
| throw new FileSystemException(ciphertextNode.toString(), null, "Filname decryption failed:" + e); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| void validatePath(Path absolutePath) { | ||
| if (!belongsToVault(absolutePath)) { | ||
| throw new IllegalArgumentException("Node %s is not a part of vault %s".formatted(absolutePath, vaultPath)); | ||
| } | ||
| if (!isAtCipherNodeLevel(absolutePath)) { | ||
| throw new IllegalArgumentException("Node %s is not located at depth 4 from vault storage root".formatted(absolutePath)); | ||
| } | ||
| if (!(hasCipherNodeExtension(absolutePath) && hasMinimumFileNameLength(absolutePath))) { | ||
| throw new IllegalArgumentException("Node %s does not end with %s or %s or filename is shorter than %d characters.".formatted(absolutePath, Constants.CRYPTOMATOR_FILE_SUFFIX, Constants.DEFLATED_FILE_SUFFIX, Constants.MIN_CIPHER_NAME_LENGTH)); | ||
| } | ||
| } | ||
|
|
||
| boolean hasCipherNodeExtension(Path p) { | ||
| var name = p.getFileName(); | ||
| return name != null && Stream.of(Constants.CRYPTOMATOR_FILE_SUFFIX, Constants.DEFLATED_FILE_SUFFIX).anyMatch(name.toString()::endsWith); | ||
| } | ||
|
|
||
| boolean isAtCipherNodeLevel(Path absolutPah) { | ||
| if (!absolutPah.isAbsolute()) { | ||
| throw new IllegalArgumentException("Path " + absolutPah + "must be absolute"); | ||
| } | ||
| return absolutPah.subpath(vaultPath.getNameCount(), absolutPah.getNameCount()).getNameCount() == 4; | ||
| } | ||
|
|
||
| boolean hasMinimumFileNameLength(Path p) { | ||
| return p.getFileName().toString().length() >= Constants.MIN_CIPHER_NAME_LENGTH; | ||
| } | ||
|
|
||
| boolean belongsToVault(Path p) { | ||
| return p.startsWith(vaultPath); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.