|
| 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.freedesktop.dbus.DBusPath; |
| 9 | +import org.purejava.secret.api.Collection; |
| 10 | +import org.purejava.secret.api.EncryptedSession; |
| 11 | +import org.purejava.secret.api.Item; |
| 12 | +import org.purejava.secret.api.Static; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +@Priority(1100) |
| 22 | +@OperatingSystem(OperatingSystem.Value.LINUX) |
| 23 | +@DisplayName("Secret Service") |
| 24 | +public class SecretServiceKeychainAccess implements KeychainAccessProvider { |
| 25 | + |
| 26 | + private static final Logger LOG = LoggerFactory.getLogger(SecretServiceKeychainAccess.class); |
| 27 | + private static final String LABEL_FOR_SECRET_IN_KEYRING = "Cryptomator"; |
| 28 | + private static final String ID_KEY = "Vault"; |
| 29 | + private static final String NAME_KEY = "Name"; |
| 30 | + private final EncryptedSession session = new EncryptedSession(); |
| 31 | + private final Collection collection = new Collection(new DBusPath(Static.DBusPath.DEFAULT_COLLECTION)); |
| 32 | + |
| 33 | + public SecretServiceKeychainAccess() { |
| 34 | + session.getService().addCollectionChangedHandler(collection -> LOG.debug("Collection {} changed", collection.getPath())); |
| 35 | + session.getService().addCollectionCreatedHandler(collection -> LOG.debug("Collection {} created", collection.getPath())); |
| 36 | + session.getService().addCollectionDeletedHandler(collection -> LOG.debug("Collection {} deleted", collection.getPath())); |
| 37 | + var getAlias = session.getService().readAlias("default"); |
| 38 | + if (getAlias.isSuccess() && "/".equals(getAlias.value().getPath())) { |
| 39 | + // default alias is not set; set it to the login keyring |
| 40 | + session.getService().setAlias("default", new DBusPath(Static.DBusPath.LOGIN_COLLECTION)); |
| 41 | + } |
| 42 | + collection.addItemChangedHandler(item -> LOG.debug("Item {} changed", item.getPath())); |
| 43 | + collection.addItemCreatedHandler(item -> LOG.debug("Item {} created", item.getPath())); |
| 44 | + collection.addItemDeletedHandler(item -> LOG.debug("Item {} deleted", item.getPath())); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void storePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { |
| 50 | + try { |
| 51 | + var call = collection.searchItems(withKey(key)); |
| 52 | + if (call.isSuccess()) { |
| 53 | + if (call.value().isEmpty()) { |
| 54 | + List<DBusPath> lockable = new ArrayList<>(); |
| 55 | + lockable.add(new DBusPath(collection.getDBusPath())); |
| 56 | + session.getService().unlock(lockable); |
| 57 | + var itemProps = Item.createProperties(LABEL_FOR_SECRET_IN_KEYRING, withKeyAndName(key, displayName)); |
| 58 | + var secret = session.encrypt(passphrase); |
| 59 | + var created = collection.createItem(itemProps, secret, false); |
| 60 | + if (!created.isSuccess()) { |
| 61 | + throw new KeychainAccessException("Storing password failed", created.error()); |
| 62 | + } |
| 63 | + } else { |
| 64 | + changePassphrase(key, displayName, passphrase); |
| 65 | + } |
| 66 | + } else { |
| 67 | + throw new KeychainAccessException("Storing password failed", call.error()); |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + throw new KeychainAccessException("Storing password failed.", e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public char[] loadPassphrase(String key) throws KeychainAccessException { |
| 76 | + try { |
| 77 | + var call = collection.searchItems(withKey(key)); |
| 78 | + if (call.isSuccess()) { |
| 79 | + if (!call.value().isEmpty()) { |
| 80 | + var path = call.value().getFirst(); |
| 81 | + session.getService().ensureUnlocked(path); |
| 82 | + var secret = new Item(path).getSecret(session.getSession()); |
| 83 | + return session.decrypt(secret); |
| 84 | + } else { |
| 85 | + return null; |
| 86 | + } |
| 87 | + } else { |
| 88 | + throw new KeychainAccessException("Loading password failed", call.error()); |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + throw new KeychainAccessException("Loading password failed.", e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void deletePassphrase(String key) throws KeychainAccessException { |
| 97 | + try { |
| 98 | + var call = collection.searchItems(withKey(key)); |
| 99 | + if (call.isSuccess()) { |
| 100 | + if (!call.value().isEmpty()) { |
| 101 | + var path = call.value().getFirst(); |
| 102 | + session.getService().ensureUnlocked(path); |
| 103 | + var item = new Item(path); |
| 104 | + var deleted = item.delete(); |
| 105 | + if (!deleted.isSuccess()) { |
| 106 | + throw new KeychainAccessException("Deleting password failed", deleted.error()); |
| 107 | + } |
| 108 | + } else { |
| 109 | + LOG.debug("Deleting entry with {}={} failed: No such item found", ID_KEY, key); |
| 110 | + } |
| 111 | + } else { |
| 112 | + throw new KeychainAccessException("Deleting password failed", call.error()); |
| 113 | + } |
| 114 | + } catch (Exception e) { |
| 115 | + throw new KeychainAccessException("Deleting password failed", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void changePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { |
| 121 | + try { |
| 122 | + var call = collection.searchItems(withKey(key)); |
| 123 | + if (call.isSuccess()) { |
| 124 | + if (!call.value().isEmpty()) { |
| 125 | + session.getService().ensureUnlocked(call.value().getFirst()); |
| 126 | + var secret = session.encrypt(passphrase); |
| 127 | + var itemProps = Item.createProperties(LABEL_FOR_SECRET_IN_KEYRING, withKeyAndName(key, displayName)); |
| 128 | + var updated = collection.createItem(itemProps, secret, true); |
| 129 | + if (!updated.isSuccess()) { |
| 130 | + throw new KeychainAccessException("Updating password failed", updated.error()); |
| 131 | + } |
| 132 | + } else { |
| 133 | + var msg = "Vault " + key + " not found, updating failed"; |
| 134 | + throw new KeychainAccessException(msg); |
| 135 | + } |
| 136 | + } else { |
| 137 | + throw new KeychainAccessException("Updating password failed", call.error()); |
| 138 | + } |
| 139 | + } catch (Exception e) { |
| 140 | + throw new KeychainAccessException("Updating password failed", e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean isSupported() { |
| 146 | + return session.setupEncryptedSession() && |
| 147 | + session.getService().hasDefaultCollection(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean isLocked() { |
| 152 | + var call = collection.isLocked(); |
| 153 | + return !call.isSuccess() || call.value(); |
| 154 | + } |
| 155 | + |
| 156 | + private Map<String, String> withKey(String key) { |
| 157 | + if (key == null) { |
| 158 | + throw new IllegalArgumentException("Arguments must not be null"); |
| 159 | + } |
| 160 | + return Map.of(ID_KEY, key); |
| 161 | + } |
| 162 | + |
| 163 | + private Map<String, String> withKeyAndName(String key, String name) { |
| 164 | + if (key == null) { |
| 165 | + throw new IllegalArgumentException("Arguments must not be null"); |
| 166 | + } |
| 167 | + return Map.of(ID_KEY, key, NAME_KEY, Objects.requireNonNullElse(name, "")); |
| 168 | + } |
| 169 | +} |
0 commit comments