|
| 1 | +package org.cryptomator.linux.keychain; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.common.DisplayName; |
| 4 | +import org.cryptomator.integrations.common.OperatingSystem; |
| 5 | +import org.cryptomator.integrations.common.Priority; |
| 6 | +import org.cryptomator.integrations.keychain.KeychainAccessException; |
| 7 | +import org.cryptomator.integrations.keychain.KeychainAccessProvider; |
| 8 | +import org.cryptomator.linux.util.CheckUtil; |
| 9 | +import org.freedesktop.dbus.connections.impl.DBusConnection; |
| 10 | +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; |
| 11 | +import org.freedesktop.dbus.exceptions.DBusConnectionException; |
| 12 | +import org.freedesktop.dbus.exceptions.DBusException; |
| 13 | +import org.freedesktop.dbus.exceptions.DBusExecutionException; |
| 14 | +import org.purejava.kwallet.KWallet; |
| 15 | +import org.purejava.kwallet.KDEWallet; |
| 16 | +import org.purejava.kwallet.Static; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +@Priority(900) |
| 23 | +@OperatingSystem(OperatingSystem.Value.LINUX) |
| 24 | +@DisplayName("KDE Wallet") |
| 25 | +public class KDEWalletKeychainAccess implements KeychainAccessProvider { |
| 26 | + |
| 27 | + private static final Logger LOG = LoggerFactory.getLogger(KDEWalletKeychainAccess.class); |
| 28 | + private static final String FOLDER_NAME = "Cryptomator"; |
| 29 | + private static final String APP_NAME = "Cryptomator"; |
| 30 | + |
| 31 | + private final Optional<ConnectedWallet> wallet; |
| 32 | + |
| 33 | + public KDEWalletKeychainAccess() { |
| 34 | + this.wallet = ConnectedWallet.connect(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean isSupported() { |
| 39 | + return wallet.map(ConnectedWallet::isSupported).orElse(false); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean isLocked() { |
| 44 | + return wallet.map(ConnectedWallet::isLocked).orElse(false); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void storePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { |
| 49 | + CheckUtil.checkState(wallet.isPresent(), "Keychain not supported."); |
| 50 | + wallet.get().storePassphrase(key, passphrase); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public char[] loadPassphrase(String key) throws KeychainAccessException { |
| 55 | + CheckUtil.checkState(wallet.isPresent(), "Keychain not supported."); |
| 56 | + return wallet.get().loadPassphrase(key); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void deletePassphrase(String key) throws KeychainAccessException { |
| 61 | + CheckUtil.checkState(wallet.isPresent(), "Keychain not supported."); |
| 62 | + wallet.get().deletePassphrase(key); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void changePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { |
| 67 | + CheckUtil.checkState(wallet.isPresent(), "Keychain not supported."); |
| 68 | + wallet.get().changePassphrase(key, passphrase); |
| 69 | + } |
| 70 | + |
| 71 | + private static class ConnectedWallet { |
| 72 | + |
| 73 | + private final KDEWallet wallet; |
| 74 | + private int handle = -1; |
| 75 | + |
| 76 | + public ConnectedWallet(DBusConnection connection) { |
| 77 | + this.wallet = new KDEWallet(connection); |
| 78 | + } |
| 79 | + |
| 80 | + static Optional<ConnectedWallet> connect() { |
| 81 | + try { |
| 82 | + return Optional.of(new ConnectedWallet(getNewConnection())); |
| 83 | + } catch (DBusException e) { |
| 84 | + LOG.warn("Connecting to D-Bus failed.", e); |
| 85 | + return Optional.empty(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static DBusConnection getNewConnection() throws DBusException { |
| 90 | + try { |
| 91 | + return DBusConnectionBuilder.forSessionBus().withShared(false).build(); |
| 92 | + } catch (DBusConnectionException | DBusExecutionException de) { |
| 93 | + LOG.warn("Connecting to SESSION bus failed.", de); |
| 94 | + LOG.warn("Falling back to SYSTEM DBus"); |
| 95 | + return DBusConnectionBuilder.forSystemBus().build(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public boolean isSupported() { |
| 100 | + try { |
| 101 | + return wallet.isEnabled(); |
| 102 | + } catch (RuntimeException e) { |
| 103 | + LOG.warn("Failed to check if KDE Wallet is available.", e); |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public boolean isLocked() { |
| 109 | + try { |
| 110 | + return !wallet.isOpen(Static.DEFAULT_WALLET); |
| 111 | + } catch (RuntimeException e) { |
| 112 | + LOG.warn("Failed to check whether KDE Wallet is open, therefore considering it closed.", e); |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public void storePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 118 | + try { |
| 119 | + if (walletIsOpen() && |
| 120 | + !(wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1) |
| 121 | + && wallet.writePassword(handle, FOLDER_NAME, key, passphrase.toString(), APP_NAME) == 0) { |
| 122 | + LOG.debug("Passphrase successfully stored."); |
| 123 | + } else { |
| 124 | + LOG.debug("Passphrase was not stored."); |
| 125 | + } |
| 126 | + } catch (RuntimeException e) { |
| 127 | + throw new KeychainAccessException("Storing the passphrase failed.", e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public char[] loadPassphrase(String key) throws KeychainAccessException { |
| 132 | + String password = ""; |
| 133 | + try { |
| 134 | + if (walletIsOpen()) { |
| 135 | + password = wallet.readPassword(handle, FOLDER_NAME, key, APP_NAME); |
| 136 | + LOG.debug("loadPassphrase: wallet is open."); |
| 137 | + } else { |
| 138 | + LOG.debug("loadPassphrase: wallet is closed."); |
| 139 | + } |
| 140 | + return (password.isEmpty()) ? null : password.toCharArray(); |
| 141 | + } catch (RuntimeException e) { |
| 142 | + throw new KeychainAccessException("Loading the passphrase failed.", e); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public void deletePassphrase(String key) throws KeychainAccessException { |
| 147 | + try { |
| 148 | + if (walletIsOpen() |
| 149 | + && wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) |
| 150 | + && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1 |
| 151 | + && wallet.removeEntry(handle, FOLDER_NAME, key, APP_NAME) == 0) { |
| 152 | + LOG.debug("Passphrase successfully deleted."); |
| 153 | + } else { |
| 154 | + LOG.debug("Passphrase was not deleted."); |
| 155 | + } |
| 156 | + } catch (RuntimeException e) { |
| 157 | + throw new KeychainAccessException("Deleting the passphrase failed.", e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public void changePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 162 | + try { |
| 163 | + if (walletIsOpen() |
| 164 | + && wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) |
| 165 | + && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1 |
| 166 | + && wallet.writePassword(handle, FOLDER_NAME, key, passphrase.toString(), APP_NAME) == 0) { |
| 167 | + LOG.debug("Passphrase successfully changed."); |
| 168 | + } else { |
| 169 | + LOG.debug("Passphrase could not be changed."); |
| 170 | + } |
| 171 | + } catch (RuntimeException e) { |
| 172 | + throw new KeychainAccessException("Changing the passphrase failed.", e); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private boolean walletIsOpen() throws KeychainAccessException { |
| 177 | + try { |
| 178 | + if (wallet.isOpen(Static.DEFAULT_WALLET)) { |
| 179 | + // This is needed due to KeechainManager loading the passphase directly |
| 180 | + if (handle == -1) handle = wallet.open(Static.DEFAULT_WALLET, 0, APP_NAME); |
| 181 | + return true; |
| 182 | + } |
| 183 | + wallet.openAsync(Static.DEFAULT_WALLET, 0, APP_NAME, false); |
| 184 | + wallet.getSignalHandler().await(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5, () -> null); |
| 185 | + handle = wallet.getSignalHandler().getLastHandledSignal(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5).handle; |
| 186 | + LOG.debug("Wallet successfully initialized."); |
| 187 | + return handle != -1; |
| 188 | + } catch (RuntimeException e) { |
| 189 | + throw new KeychainAccessException("Asynchronous opening the wallet failed.", e); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + } |
| 195 | +} |
| 196 | + |
0 commit comments