From b6d725306c5308d86f42f6e0ec76cf0c66b1fcc9 Mon Sep 17 00:00:00 2001 From: Yves Langisch Date: Fri, 29 Nov 2024 14:08:20 +0100 Subject: [PATCH] Wrap KeyPair and implement hashCode/equals. --- .../ch/cyberduck/core/sds/SDSSession.java | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/dracoon/src/main/java/ch/cyberduck/core/sds/SDSSession.java b/dracoon/src/main/java/ch/cyberduck/core/sds/SDSSession.java index c4b27d4ee96..94715fc6431 100644 --- a/dracoon/src/main/java/ch/cyberduck/core/sds/SDSSession.java +++ b/dracoon/src/main/java/ch/cyberduck/core/sds/SDSSession.java @@ -56,6 +56,8 @@ import ch.cyberduck.core.threading.CancelCallback; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.http.HttpException; import org.apache.http.HttpHeaders; import org.apache.http.HttpRequest; @@ -129,10 +131,10 @@ public class SDSSession extends HttpSession { private final ExpiringObjectHolder softwareVersion = new ExpiringObjectHolder<>(preferences.getLong("sds.useracount.ttl")); - private final LRUCache keyPairPassphrases - = LRUCache.build(new RemovalListener() { + private final LRUCache keyPairPassphrases + = LRUCache.build(new RemovalListener() { @Override - public void onRemoval(final RemovalNotification notification) { + public void onRemoval(final RemovalNotification notification) { // } }, 2, preferences.getLong("sds.encryption.keys.ttl"), false); @@ -317,27 +319,29 @@ private boolean isNewCryptoAvailable() throws BackgroundException { } public Credentials unlockTripleCryptKeyPair(final PasswordCallback callback, final UserKeyPair keypair) throws BackgroundException { - if(!keyPairPassphrases.contains(keypair)) { + final KeyPairCacheReference reference = new KeyPairCacheReference(keypair); + if(!keyPairPassphrases.contains(reference)) { try { - keyPairPassphrases.put(keypair, new TripleCryptKeyPair().unlock(callback, host, keypair)); + keyPairPassphrases.put(reference, new TripleCryptKeyPair().unlock(callback, host, keypair)); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } - return keyPairPassphrases.get(keypair); + return keyPairPassphrases.get(reference); } public Credentials unlockTripleCryptKeyPair(final PasswordCallback callback, final UserKeyPair keypair, final String passphrase) throws BackgroundException { - if(!keyPairPassphrases.contains(keypair)) { + final KeyPairCacheReference reference = new KeyPairCacheReference(keypair); + if(!keyPairPassphrases.contains(reference)) { try { - keyPairPassphrases.put(keypair, new TripleCryptKeyPair().unlock(callback, host, keypair, passphrase)); + keyPairPassphrases.put(reference, new TripleCryptKeyPair().unlock(callback, host, keypair, passphrase)); } catch(CryptoException e) { throw new TripleCryptExceptionMappingService().map(e); } } - return keyPairPassphrases.get(keypair); + return keyPairPassphrases.get(reference); } protected void unlockTripleCryptKeyPair(final LoginCallback prompt, final UserAccountWrapper user, @@ -545,6 +549,42 @@ public UserKeyPair.Version requiredKeyPairVersion() { return requiredKeyPairVersion; } + private static class KeyPairCacheReference { + + private final UserKeyPair keypair; + + public KeyPairCacheReference(final UserKeyPair keypair) { + this.keypair = keypair; + } + + @Override + public boolean equals(final Object o) { + if(this == o) { + return true; + } + + if(o == null || getClass() != o.getClass()) { + return false; + } + + final KeyPairCacheReference that = (KeyPairCacheReference) o; + + return new EqualsBuilder().append(keypair.getUserPrivateKey().getVersion().getValue(), that.keypair.getUserPrivateKey().getVersion().getValue()). + append(keypair.getUserPrivateKey().getPrivateKey(), that.keypair.getUserPrivateKey().getPrivateKey()). + append(keypair.getUserPublicKey().getVersion().getValue(), that.keypair.getUserPublicKey().getVersion().getValue()). + append(keypair.getUserPrivateKey().getPrivateKey(), that.keypair.getUserPrivateKey().getPrivateKey()).isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(). + append(keypair.getUserPrivateKey().getVersion().getValue()). + append(keypair.getUserPrivateKey().getPrivateKey()). + append(keypair.getUserPublicKey().getVersion().getValue()). + append(keypair.getUserPrivateKey().getPrivateKey()).toHashCode(); + } + } + @Override protected void logout() { scheduler.shutdown();