|
| 1 | +package ch.cyberduck.core.cryptomator; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2002-2025 iterate GmbH. All rights reserved. |
| 5 | + * https://cyberduck.io/ |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + */ |
| 17 | + |
| 18 | +import ch.cyberduck.core.ListService; |
| 19 | +import ch.cyberduck.core.Path; |
| 20 | +import ch.cyberduck.core.Session; |
| 21 | +import ch.cyberduck.core.UrlProvider; |
| 22 | +import ch.cyberduck.core.cryptomator.features.*; |
| 23 | +import ch.cyberduck.core.exception.BackgroundException; |
| 24 | +import ch.cyberduck.core.features.*; |
| 25 | +import ch.cyberduck.core.preferences.PreferencesFactory; |
| 26 | +import ch.cyberduck.core.shared.DefaultTouchFeature; |
| 27 | +import ch.cyberduck.core.transfer.TransferStatus; |
| 28 | + |
| 29 | +import org.cryptomator.cryptolib.api.Cryptor; |
| 30 | +import org.cryptomator.cryptolib.api.FileContentCryptor; |
| 31 | +import org.cryptomator.cryptolib.api.FileHeaderCryptor; |
| 32 | + |
| 33 | +public abstract class AbstractVault implements Vault { |
| 34 | + |
| 35 | + public static final int VAULT_VERSION_DEPRECATED = 6; |
| 36 | + public static final int VAULT_VERSION = PreferencesFactory.get().getInteger("cryptomator.vault.version"); |
| 37 | + |
| 38 | + public abstract Path getMasterkey(); |
| 39 | + |
| 40 | + public abstract Path getConfig(); |
| 41 | + |
| 42 | + public abstract int getVersion(); |
| 43 | + |
| 44 | + public abstract FileHeaderCryptor getFileHeaderCryptor(); |
| 45 | + |
| 46 | + public abstract FileContentCryptor getFileContentCryptor(); |
| 47 | + |
| 48 | + public abstract CryptorCache getFileNameCryptor(); |
| 49 | + |
| 50 | + public abstract CryptoFilename getFilenameProvider(); |
| 51 | + |
| 52 | + public abstract CryptoDirectory getDirectoryProvider(); |
| 53 | + |
| 54 | + public abstract Cryptor getCryptor(); |
| 55 | + |
| 56 | + public abstract int getNonceSize(); |
| 57 | + |
| 58 | + public int numberOfChunks(long cleartextFileSize) { |
| 59 | + return (int) (cleartextFileSize / this.getFileContentCryptor().cleartextChunkSize() + |
| 60 | + ((cleartextFileSize % this.getFileContentCryptor().cleartextChunkSize() > 0) ? 1 : 0)); |
| 61 | + } |
| 62 | + |
| 63 | + public long toCleartextSize(final long cleartextFileOffset, final long ciphertextFileSize) throws CryptoInvalidFilesizeException { |
| 64 | + if(TransferStatus.UNKNOWN_LENGTH == ciphertextFileSize) { |
| 65 | + return TransferStatus.UNKNOWN_LENGTH; |
| 66 | + } |
| 67 | + final int headerSize; |
| 68 | + if(0L == cleartextFileOffset) { |
| 69 | + headerSize = this.getFileHeaderCryptor().headerSize(); |
| 70 | + } |
| 71 | + else { |
| 72 | + headerSize = 0; |
| 73 | + } |
| 74 | + try { |
| 75 | + return this.getFileContentCryptor().cleartextSize(ciphertextFileSize - headerSize); |
| 76 | + } |
| 77 | + catch(AssertionError e) { |
| 78 | + throw new CryptoInvalidFilesizeException(String.format("Encrypted file size must be at least %d bytes", headerSize)); |
| 79 | + } |
| 80 | + catch(IllegalArgumentException e) { |
| 81 | + throw new CryptoInvalidFilesizeException(String.format("Invalid file size. %s", e.getMessage())); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Path encrypt(Session<?> session, Path file) throws BackgroundException { |
| 87 | + return this.encrypt(session, file, file.attributes().getDirectoryId(), false); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Path encrypt(Session<?> session, Path file, boolean metadata) throws BackgroundException { |
| 92 | + return this.encrypt(session, file, file.attributes().getDirectoryId(), metadata); |
| 93 | + } |
| 94 | + |
| 95 | + public abstract Path encrypt(Session<?> session, Path file, String directoryId, boolean metadata) throws BackgroundException; |
| 96 | + |
| 97 | + public synchronized boolean isUnlocked() { |
| 98 | + return this.getCryptor() != null; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @SuppressWarnings("unchecked") |
| 103 | + public <T> T getFeature(final Session<?> session, final Class<T> type, final T delegate) { |
| 104 | + if(this.isUnlocked()) { |
| 105 | + if(type == ListService.class) { |
| 106 | + return (T) new CryptoListService(session, (ListService) delegate, this); |
| 107 | + } |
| 108 | + if(type == Touch.class) { |
| 109 | + // Use default touch feature because touch with remote implementation will not add encrypted file header |
| 110 | + return (T) new CryptoTouchFeature(session, new DefaultTouchFeature(session._getFeature(Write.class)), session._getFeature(Write.class), this); |
| 111 | + } |
| 112 | + if(type == Directory.class) { |
| 113 | + return (T) (this.getVersion() == VAULT_VERSION_DEPRECATED ? |
| 114 | + new CryptoDirectoryV6Feature(session, (Directory) delegate, session._getFeature(Write.class), this) : |
| 115 | + new CryptoDirectoryV7Feature(session, (Directory) delegate, session._getFeature(Write.class), this) |
| 116 | + ); |
| 117 | + } |
| 118 | + if(type == Upload.class) { |
| 119 | + return (T) new CryptoUploadFeature(session, (Upload) delegate, session._getFeature(Write.class), this); |
| 120 | + } |
| 121 | + if(type == Download.class) { |
| 122 | + return (T) new CryptoDownloadFeature(session, (Download) delegate, session._getFeature(Read.class), this); |
| 123 | + } |
| 124 | + if(type == Read.class) { |
| 125 | + return (T) new CryptoReadFeature(session, (Read) delegate, this); |
| 126 | + } |
| 127 | + if(type == Write.class) { |
| 128 | + return (T) new CryptoWriteFeature(session, (Write) delegate, this); |
| 129 | + } |
| 130 | + if(type == MultipartWrite.class) { |
| 131 | + return (T) new CryptoMultipartWriteFeature(session, (Write) delegate, this); |
| 132 | + } |
| 133 | + if(type == Move.class) { |
| 134 | + return (T) (this.getVersion() == VAULT_VERSION_DEPRECATED ? |
| 135 | + new CryptoMoveV6Feature(session, (Move) delegate, this) : |
| 136 | + new CryptoMoveV7Feature(session, (Move) delegate, this)); |
| 137 | + |
| 138 | + } |
| 139 | + if(type == AttributesFinder.class) { |
| 140 | + return (T) new CryptoAttributesFeature(session, (AttributesFinder) delegate, this); |
| 141 | + } |
| 142 | + if(type == Find.class) { |
| 143 | + return (T) new CryptoFindFeature(session, (Find) delegate, this); |
| 144 | + } |
| 145 | + if(type == UrlProvider.class) { |
| 146 | + return (T) new CryptoUrlProvider(session, (UrlProvider) delegate, this); |
| 147 | + } |
| 148 | + if(type == FileIdProvider.class) { |
| 149 | + return (T) new CryptoFileIdProvider(session, (FileIdProvider) delegate, this); |
| 150 | + } |
| 151 | + if(type == VersionIdProvider.class) { |
| 152 | + return (T) new CryptoVersionIdProvider(session, (VersionIdProvider) delegate, this); |
| 153 | + } |
| 154 | + if(type == Delete.class) { |
| 155 | + return (T) (this.getVersion() == VAULT_VERSION_DEPRECATED ? |
| 156 | + new CryptoDeleteV6Feature(session, (Delete) delegate, this) : |
| 157 | + new CryptoDeleteV7Feature(session, (Delete) delegate, this)); |
| 158 | + } |
| 159 | + if(type == Trash.class) { |
| 160 | + return (T) (this.getVersion() == VAULT_VERSION_DEPRECATED ? |
| 161 | + new CryptoDeleteV6Feature(session, (Delete) delegate, this) : |
| 162 | + new CryptoDeleteV7Feature(session, (Delete) delegate, this)); |
| 163 | + } |
| 164 | + if(type == Symlink.class) { |
| 165 | + return (T) new CryptoSymlinkFeature(session, (Symlink) delegate, this); |
| 166 | + } |
| 167 | + if(type == Headers.class) { |
| 168 | + return (T) new CryptoHeadersFeature(session, (Headers) delegate, this); |
| 169 | + } |
| 170 | + if(type == Compress.class) { |
| 171 | + return (T) new CryptoCompressFeature(session, (Compress) delegate, this); |
| 172 | + } |
| 173 | + if(type == Bulk.class) { |
| 174 | + return (T) new CryptoBulkFeature(session, (Bulk) delegate, session._getFeature(Delete.class), this); |
| 175 | + } |
| 176 | + if(type == UnixPermission.class) { |
| 177 | + return (T) new CryptoUnixPermission(session, (UnixPermission) delegate, this); |
| 178 | + } |
| 179 | + if(type == AclPermission.class) { |
| 180 | + return (T) new CryptoAclPermission(session, (AclPermission) delegate, this); |
| 181 | + } |
| 182 | + if(type == Copy.class) { |
| 183 | + return (T) new CryptoCopyFeature(session, (Copy) delegate, this); |
| 184 | + } |
| 185 | + if(type == Timestamp.class) { |
| 186 | + return (T) new CryptoTimestampFeature(session, (Timestamp) delegate, this); |
| 187 | + } |
| 188 | + if(type == Encryption.class) { |
| 189 | + return (T) new CryptoEncryptionFeature(session, (Encryption) delegate, this); |
| 190 | + } |
| 191 | + if(type == Lifecycle.class) { |
| 192 | + return (T) new CryptoLifecycleFeature(session, (Lifecycle) delegate, this); |
| 193 | + } |
| 194 | + if(type == Location.class) { |
| 195 | + return (T) new CryptoLocationFeature(session, (Location) delegate, this); |
| 196 | + } |
| 197 | + if(type == Lock.class) { |
| 198 | + return (T) new CryptoLockFeature(session, (Lock) delegate, this); |
| 199 | + } |
| 200 | + if(type == Logging.class) { |
| 201 | + return (T) new CryptoLoggingFeature(session, (Logging) delegate, this); |
| 202 | + } |
| 203 | + if(type == Redundancy.class) { |
| 204 | + return (T) new CryptoRedundancyFeature(session, (Redundancy) delegate, this); |
| 205 | + } |
| 206 | + if(type == Search.class) { |
| 207 | + return (T) new CryptoSearchFeature(session, (Search) delegate, this); |
| 208 | + } |
| 209 | + if(type == TransferAcceleration.class) { |
| 210 | + return (T) new CryptoTransferAccelerationFeature<>(session, (TransferAcceleration) delegate, this); |
| 211 | + } |
| 212 | + if(type == Versioning.class) { |
| 213 | + return (T) new CryptoVersioningFeature(session, (Versioning) delegate, this); |
| 214 | + } |
| 215 | + } |
| 216 | + return delegate; |
| 217 | + } |
| 218 | +} |
0 commit comments