From f3c072fb06a4c4a677e207b62da8288d5bf8626b Mon Sep 17 00:00:00 2001 From: Shikhar Bhushan Date: Mon, 24 May 2010 23:51:24 +0100 Subject: [PATCH] keybased auth cleanups --- .../sshj/userauth/keyprovider/PKCS8KeyFile.java | 15 ++++++++------- .../sshj/userauth/method/AbstractAuthMethod.java | 1 - .../sshj/userauth/method/AuthPublickey.java | 10 ++-------- .../sshj/userauth/method/KeyedAuthMethod.java | 4 ++-- .../userauth/password/PrivateKeyFileResource.java | 10 ++++++---- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java index 62f8376f..0c5cfc3a 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java @@ -20,7 +20,6 @@ import net.schmizz.sshj.common.KeyType; import net.schmizz.sshj.userauth.password.PasswordFinder; import net.schmizz.sshj.userauth.password.PasswordUtils; import net.schmizz.sshj.userauth.password.PrivateKeyFileResource; -import net.schmizz.sshj.userauth.password.Resource; import org.bouncycastle.openssl.EncryptionException; import org.bouncycastle.openssl.PEMReader; import org.slf4j.Logger; @@ -53,8 +52,7 @@ public class PKCS8KeyFile protected final Logger log = LoggerFactory.getLogger(getClass()); protected PasswordFinder pwdf; - protected File location; - protected Resource resource; + protected PrivateKeyFileResource resource; protected KeyPair kp; protected KeyType type; @@ -82,8 +80,7 @@ public class PKCS8KeyFile @Override public void init(File location) { assert location != null; - this.location = location; - resource = new PrivateKeyFileResource(location.getAbsolutePath()); + resource = new PrivateKeyFileResource(location.getAbsoluteFile()); } @Override @@ -114,7 +111,7 @@ public class PKCS8KeyFile for (; ;) { // while the PasswordFinder tells us we should retry try { - r = new PEMReader(new InputStreamReader(new FileInputStream(location)), pFinder); + r = new PEMReader(new InputStreamReader(new FileInputStream(resource.getDetail())), pFinder); o = r.readObject(); } catch (EncryptionException e) { if (pwdf.shouldRetry(resource)) @@ -131,7 +128,7 @@ public class PKCS8KeyFile } if (o == null) - throw new IOException("Could not read key pair from: " + location); + throw new IOException("Could not read key pair from: " + resource); if (o instanceof KeyPair) kp = (KeyPair) o; else @@ -139,4 +136,8 @@ public class PKCS8KeyFile return kp; } + @Override + public String toString() { + return "PKCS8KeyFile{resource=" + resource + "}"; + } } diff --git a/src/main/java/net/schmizz/sshj/userauth/method/AbstractAuthMethod.java b/src/main/java/net/schmizz/sshj/userauth/method/AbstractAuthMethod.java index cd7130e6..b5b79198 100644 --- a/src/main/java/net/schmizz/sshj/userauth/method/AbstractAuthMethod.java +++ b/src/main/java/net/schmizz/sshj/userauth/method/AbstractAuthMethod.java @@ -78,7 +78,6 @@ public abstract class AbstractAuthMethod .putString(params.getUsername()) // username goes first .putString(params.getNextServiceName()) // the service that we'd like on success .putString(name); // name of auth method - } protected AccountResource makeAccountResource() { diff --git a/src/main/java/net/schmizz/sshj/userauth/method/AuthPublickey.java b/src/main/java/net/schmizz/sshj/userauth/method/AuthPublickey.java index 195ae95a..0ade52ec 100644 --- a/src/main/java/net/schmizz/sshj/userauth/method/AuthPublickey.java +++ b/src/main/java/net/schmizz/sshj/userauth/method/AuthPublickey.java @@ -21,8 +21,6 @@ import net.schmizz.sshj.transport.TransportException; import net.schmizz.sshj.userauth.UserAuthException; import net.schmizz.sshj.userauth.keyprovider.KeyProvider; -import java.io.IOException; - /** * Implements the {@code "publickey"} SSH authentication method. *

@@ -60,11 +58,7 @@ public class AuthPublickey */ private SSHPacket buildReq(boolean signed) throws UserAuthException { - try { - kProv.getPublic(); - } catch (IOException ioe) { - throw new UserAuthException("Problem getting public key", ioe); - } + log.debug("Attempting authentication using {}", kProv); return putPubKey(super.buildReq().putBoolean(signed)); } @@ -76,7 +70,7 @@ public class AuthPublickey */ private void sendSignedReq() throws UserAuthException, TransportException { - log.debug("Sending signed request"); + log.debug("Key acceptable, sending signed request"); params.getTransport().write(putSig(buildReq(true))); } diff --git a/src/main/java/net/schmizz/sshj/userauth/method/KeyedAuthMethod.java b/src/main/java/net/schmizz/sshj/userauth/method/KeyedAuthMethod.java index 49379989..920aa5b4 100644 --- a/src/main/java/net/schmizz/sshj/userauth/method/KeyedAuthMethod.java +++ b/src/main/java/net/schmizz/sshj/userauth/method/KeyedAuthMethod.java @@ -43,7 +43,7 @@ public abstract class KeyedAuthMethod try { key = kProv.getPublic(); } catch (IOException ioe) { - throw new UserAuthException("Problem getting public key", ioe); + throw new UserAuthException("Problem getting public key from " + kProv, ioe); } // public key as 2 strings: [ key type | key blob ] @@ -59,7 +59,7 @@ public abstract class KeyedAuthMethod try { key = kProv.getPrivate(); } catch (IOException ioe) { - throw new UserAuthException("Problem getting private key", ioe); + throw new UserAuthException("Problem getting private key from " + kProv, ioe); } final String kt = KeyType.fromKey(key).toString(); diff --git a/src/main/java/net/schmizz/sshj/userauth/password/PrivateKeyFileResource.java b/src/main/java/net/schmizz/sshj/userauth/password/PrivateKeyFileResource.java index fd5bb361..7b886e8d 100644 --- a/src/main/java/net/schmizz/sshj/userauth/password/PrivateKeyFileResource.java +++ b/src/main/java/net/schmizz/sshj/userauth/password/PrivateKeyFileResource.java @@ -15,11 +15,13 @@ */ package net.schmizz.sshj.userauth.password; -public class PrivateKeyFileResource - extends Resource { +import java.io.File; - public PrivateKeyFileResource(String path) { - super(path); +public class PrivateKeyFileResource + extends Resource { + + public PrivateKeyFileResource(File privateKeyFile) { + super(privateKeyFile); } }