|
| 1 | +package org.cryptomator.linux.keychain; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import org.cryptomator.integrations.keychain.KeychainAccessException; |
| 5 | +import org.cryptomator.integrations.keychain.KeychainAccessProvider; |
| 6 | +import org.freedesktop.dbus.connections.impl.DBusConnection; |
| 7 | +import org.freedesktop.dbus.exceptions.DBusException; |
| 8 | +import org.kde.KWallet; |
| 9 | +import org.kde.Static; |
| 10 | +import org.purejava.KDEWallet; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +public class KDEWalletKeychainAccess implements KeychainAccessProvider { |
| 17 | + |
| 18 | + private static final Logger LOG = LoggerFactory.getLogger(KDEWalletKeychainAccess.class); |
| 19 | + private static final String FOLDER_NAME = "Cryptomator"; |
| 20 | + private static final String APP_NAME = "Cryptomator"; |
| 21 | + |
| 22 | + private final Optional<ConnectedWallet> wallet; |
| 23 | + |
| 24 | + public KDEWalletKeychainAccess() { |
| 25 | + ConnectedWallet wallet = null; |
| 26 | + try { |
| 27 | + DBusConnection conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION); |
| 28 | + wallet = new ConnectedWallet(conn); |
| 29 | + } catch (DBusException e) { |
| 30 | + LOG.warn("Connecting to D-Bus failed.", e); |
| 31 | + } |
| 32 | + this.wallet = Optional.ofNullable(wallet); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean isSupported() { |
| 37 | + return wallet.map(ConnectedWallet::isSupported).orElse(false); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void storePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 42 | + Preconditions.checkState(wallet.isPresent(), "Keychain not supported."); |
| 43 | + wallet.get().storePassphrase(key, passphrase); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public char[] loadPassphrase(String key) throws KeychainAccessException { |
| 48 | + Preconditions.checkState(wallet.isPresent(), "Keychain not supported."); |
| 49 | + return wallet.get().loadPassphrase(key); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void deletePassphrase(String key) throws KeychainAccessException { |
| 54 | + Preconditions.checkState(wallet.isPresent(), "Keychain not supported."); |
| 55 | + wallet.get().deletePassphrase(key); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void changePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 60 | + Preconditions.checkState(wallet.isPresent(), "Keychain not supported."); |
| 61 | + wallet.get().changePassphrase(key, passphrase); |
| 62 | + } |
| 63 | + |
| 64 | + private static class ConnectedWallet { |
| 65 | + |
| 66 | + private final KDEWallet wallet; |
| 67 | + private int handle = -1; |
| 68 | + |
| 69 | + public ConnectedWallet(DBusConnection connection) { |
| 70 | + this.wallet = new KDEWallet(connection); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isSupported() { |
| 74 | + return wallet.isEnabled(); |
| 75 | + } |
| 76 | + |
| 77 | + public void storePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 78 | + try { |
| 79 | + if (walletIsOpen() && |
| 80 | + !(wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1) |
| 81 | + && wallet.writePassword(handle, FOLDER_NAME, key, passphrase.toString(), APP_NAME) == 0) { |
| 82 | + LOG.debug("Passphrase successfully stored."); |
| 83 | + } else { |
| 84 | + LOG.debug("Passphrase was not stored."); |
| 85 | + } |
| 86 | + } catch (RuntimeException e) { |
| 87 | + throw new KeychainAccessException("Storing the passphrase failed.", e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public char[] loadPassphrase(String key) throws KeychainAccessException { |
| 92 | + String password = ""; |
| 93 | + try { |
| 94 | + if (walletIsOpen()) { |
| 95 | + password = wallet.readPassword(handle, FOLDER_NAME, key, APP_NAME); |
| 96 | + LOG.debug("loadPassphrase: wallet is open."); |
| 97 | + } else { |
| 98 | + LOG.debug("loadPassphrase: wallet is closed."); |
| 99 | + } |
| 100 | + return (password.equals("")) ? null : password.toCharArray(); |
| 101 | + } catch (RuntimeException e) { |
| 102 | + throw new KeychainAccessException("Loading the passphrase failed.", e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void deletePassphrase(String key) throws KeychainAccessException { |
| 107 | + try { |
| 108 | + if (walletIsOpen() |
| 109 | + && wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) |
| 110 | + && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1 |
| 111 | + && wallet.removeEntry(handle, FOLDER_NAME, key, APP_NAME) == 0) { |
| 112 | + LOG.debug("Passphrase successfully deleted."); |
| 113 | + } else { |
| 114 | + LOG.debug("Passphrase was not deleted."); |
| 115 | + } |
| 116 | + } catch (RuntimeException e) { |
| 117 | + throw new KeychainAccessException("Deleting the passphrase failed.", e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public void changePassphrase(String key, CharSequence passphrase) throws KeychainAccessException { |
| 122 | + try { |
| 123 | + if (walletIsOpen() |
| 124 | + && wallet.hasEntry(handle, FOLDER_NAME, key, APP_NAME) |
| 125 | + && wallet.entryType(handle, FOLDER_NAME, key, APP_NAME) == 1 |
| 126 | + && wallet.writePassword(handle, FOLDER_NAME, key, passphrase.toString(), APP_NAME) == 0) { |
| 127 | + LOG.debug("Passphrase successfully changed."); |
| 128 | + } else { |
| 129 | + LOG.debug("Passphrase could not be changed."); |
| 130 | + } |
| 131 | + } catch (RuntimeException e) { |
| 132 | + throw new KeychainAccessException("Changing the passphrase failed.", e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private boolean walletIsOpen() throws KeychainAccessException { |
| 137 | + try { |
| 138 | + if (wallet.isOpen(Static.DEFAULT_WALLET)) { |
| 139 | + // This is needed due to KeechainManager loading the passphase directly |
| 140 | + if (handle == -1) handle = wallet.open(Static.DEFAULT_WALLET, 0, APP_NAME); |
| 141 | + return true; |
| 142 | + } |
| 143 | + wallet.openAsync(Static.DEFAULT_WALLET, 0, APP_NAME, false); |
| 144 | + wallet.getSignalHandler().await(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5, () -> null); |
| 145 | + handle = wallet.getSignalHandler().getLastHandledSignal(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5).handle; |
| 146 | + LOG.debug("Wallet successfully initialized."); |
| 147 | + return handle != -1; |
| 148 | + } catch (RuntimeException e) { |
| 149 | + throw new KeychainAccessException("Asynchronous opening the wallet failed.", e); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + } |
| 155 | +} |
0 commit comments