|
| 1 | +package org.cryptomator.cryptofs; |
| 2 | + |
| 3 | +import com.google.common.annotations.VisibleForTesting; |
| 4 | +import com.google.common.io.BaseEncoding; |
| 5 | +import org.cryptomator.cryptofs.common.Constants; |
| 6 | +import org.cryptomator.cryptofs.common.StringUtils; |
| 7 | +import org.cryptomator.cryptolib.api.CryptoException; |
| 8 | +import org.cryptomator.cryptolib.api.Cryptor; |
| 9 | +import org.cryptomator.cryptolib.api.FileNameCryptor; |
| 10 | + |
| 11 | +import javax.inject.Inject; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.FileSystemException; |
| 14 | +import java.nio.file.NoSuchFileException; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see CryptoFileSystem#getCleartextName(Path) |
| 20 | + */ |
| 21 | +@CryptoFileSystemScoped |
| 22 | +class FileNameDecryptor { |
| 23 | + |
| 24 | + private final DirectoryIdBackup dirIdBackup; |
| 25 | + private final LongFileNameProvider longFileNameProvider; |
| 26 | + private final Path vaultPath; |
| 27 | + private final FileNameCryptor fileNameCryptor; |
| 28 | + |
| 29 | + @Inject |
| 30 | + public FileNameDecryptor(@PathToVault Path vaultPath, Cryptor cryptor, DirectoryIdBackup dirIdBackup, LongFileNameProvider longFileNameProvider) { |
| 31 | + this.vaultPath = vaultPath; |
| 32 | + this.fileNameCryptor = cryptor.fileNameCryptor(); |
| 33 | + this.dirIdBackup = dirIdBackup; |
| 34 | + this.longFileNameProvider = longFileNameProvider; |
| 35 | + } |
| 36 | + |
| 37 | + public String decryptFilename(Path ciphertextNode) throws IOException, UnsupportedOperationException { |
| 38 | + validatePath(ciphertextNode.toAbsolutePath()); |
| 39 | + return decryptFilenameInternal(ciphertextNode); |
| 40 | + } |
| 41 | + |
| 42 | + @VisibleForTesting |
| 43 | + String decryptFilenameInternal(Path ciphertextNode) throws IOException, UnsupportedOperationException { |
| 44 | + byte[] dirId = null; |
| 45 | + try { |
| 46 | + dirId = dirIdBackup.read(ciphertextNode); |
| 47 | + } catch (NoSuchFileException e) { |
| 48 | + throw new UnsupportedOperationException("Directory does not have a " + Constants.DIR_ID_BACKUP_FILE_NAME + " file."); |
| 49 | + } catch (CryptoException | IllegalStateException e) { |
| 50 | + throw new FileSystemException(ciphertextNode.toString(), null, "Decryption of dirId backup file failed:" + e); |
| 51 | + } |
| 52 | + var fullCipherNodeName = ciphertextNode.getFileName().toString(); |
| 53 | + var cipherNodeExtension = fullCipherNodeName.substring(fullCipherNodeName.length() - 4); |
| 54 | + |
| 55 | + String actualEncryptedName = switch (cipherNodeExtension) { |
| 56 | + case Constants.CRYPTOMATOR_FILE_SUFFIX -> StringUtils.removeEnd(fullCipherNodeName, Constants.CRYPTOMATOR_FILE_SUFFIX); |
| 57 | + case Constants.DEFLATED_FILE_SUFFIX -> longFileNameProvider.inflate(ciphertextNode); |
| 58 | + default -> throw new IllegalStateException("SHOULD NOT REACH HERE"); |
| 59 | + }; |
| 60 | + try { |
| 61 | + return fileNameCryptor.decryptFilename(BaseEncoding.base64Url(), actualEncryptedName, dirId); |
| 62 | + } catch (CryptoException e) { |
| 63 | + throw new FileSystemException(ciphertextNode.toString(), null, "Filname decryption failed:" + e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @VisibleForTesting |
| 68 | + void validatePath(Path absolutePath) { |
| 69 | + if (!belongsToVault(absolutePath)) { |
| 70 | + throw new IllegalArgumentException("Node %s is not a part of vault %s".formatted(absolutePath, vaultPath)); |
| 71 | + } |
| 72 | + if (!isAtCipherNodeLevel(absolutePath)) { |
| 73 | + throw new IllegalArgumentException("Node %s is not located at depth 4 from vault storage root".formatted(absolutePath)); |
| 74 | + } |
| 75 | + if (!(hasCipherNodeExtension(absolutePath) && hasMinimumFileNameLength(absolutePath))) { |
| 76 | + 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)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + boolean hasCipherNodeExtension(Path p) { |
| 81 | + var name = p.getFileName(); |
| 82 | + return name != null && Stream.of(Constants.CRYPTOMATOR_FILE_SUFFIX, Constants.DEFLATED_FILE_SUFFIX).anyMatch(name.toString()::endsWith); |
| 83 | + } |
| 84 | + |
| 85 | + boolean isAtCipherNodeLevel(Path absolutPah) { |
| 86 | + if (!absolutPah.isAbsolute()) { |
| 87 | + throw new IllegalArgumentException("Path " + absolutPah + "must be absolute"); |
| 88 | + } |
| 89 | + return absolutPah.subpath(vaultPath.getNameCount(), absolutPah.getNameCount()).getNameCount() == 4; |
| 90 | + } |
| 91 | + |
| 92 | + boolean hasMinimumFileNameLength(Path p) { |
| 93 | + return p.getFileName().toString().length() >= Constants.MIN_CIPHER_NAME_LENGTH; |
| 94 | + } |
| 95 | + |
| 96 | + boolean belongsToVault(Path p) { |
| 97 | + return p.startsWith(vaultPath); |
| 98 | + } |
| 99 | +} |
0 commit comments