mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
keybased auth cleanups
This commit is contained in:
@@ -20,7 +20,6 @@ import net.schmizz.sshj.common.KeyType;
|
|||||||
import net.schmizz.sshj.userauth.password.PasswordFinder;
|
import net.schmizz.sshj.userauth.password.PasswordFinder;
|
||||||
import net.schmizz.sshj.userauth.password.PasswordUtils;
|
import net.schmizz.sshj.userauth.password.PasswordUtils;
|
||||||
import net.schmizz.sshj.userauth.password.PrivateKeyFileResource;
|
import net.schmizz.sshj.userauth.password.PrivateKeyFileResource;
|
||||||
import net.schmizz.sshj.userauth.password.Resource;
|
|
||||||
import org.bouncycastle.openssl.EncryptionException;
|
import org.bouncycastle.openssl.EncryptionException;
|
||||||
import org.bouncycastle.openssl.PEMReader;
|
import org.bouncycastle.openssl.PEMReader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -53,8 +52,7 @@ public class PKCS8KeyFile
|
|||||||
|
|
||||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
protected final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
protected PasswordFinder pwdf;
|
protected PasswordFinder pwdf;
|
||||||
protected File location;
|
protected PrivateKeyFileResource resource;
|
||||||
protected Resource resource;
|
|
||||||
protected KeyPair kp;
|
protected KeyPair kp;
|
||||||
|
|
||||||
protected KeyType type;
|
protected KeyType type;
|
||||||
@@ -82,8 +80,7 @@ public class PKCS8KeyFile
|
|||||||
@Override
|
@Override
|
||||||
public void init(File location) {
|
public void init(File location) {
|
||||||
assert location != null;
|
assert location != null;
|
||||||
this.location = location;
|
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
||||||
resource = new PrivateKeyFileResource(location.getAbsolutePath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -114,7 +111,7 @@ public class PKCS8KeyFile
|
|||||||
for (; ;) {
|
for (; ;) {
|
||||||
// while the PasswordFinder tells us we should retry
|
// while the PasswordFinder tells us we should retry
|
||||||
try {
|
try {
|
||||||
r = new PEMReader(new InputStreamReader(new FileInputStream(location)), pFinder);
|
r = new PEMReader(new InputStreamReader(new FileInputStream(resource.getDetail())), pFinder);
|
||||||
o = r.readObject();
|
o = r.readObject();
|
||||||
} catch (EncryptionException e) {
|
} catch (EncryptionException e) {
|
||||||
if (pwdf.shouldRetry(resource))
|
if (pwdf.shouldRetry(resource))
|
||||||
@@ -131,7 +128,7 @@ public class PKCS8KeyFile
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (o == null)
|
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)
|
if (o instanceof KeyPair)
|
||||||
kp = (KeyPair) o;
|
kp = (KeyPair) o;
|
||||||
else
|
else
|
||||||
@@ -139,4 +136,8 @@ public class PKCS8KeyFile
|
|||||||
return kp;
|
return kp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PKCS8KeyFile{resource=" + resource + "}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ public abstract class AbstractAuthMethod
|
|||||||
.putString(params.getUsername()) // username goes first
|
.putString(params.getUsername()) // username goes first
|
||||||
.putString(params.getNextServiceName()) // the service that we'd like on success
|
.putString(params.getNextServiceName()) // the service that we'd like on success
|
||||||
.putString(name); // name of auth method
|
.putString(name); // name of auth method
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AccountResource makeAccountResource() {
|
protected AccountResource makeAccountResource() {
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ import net.schmizz.sshj.transport.TransportException;
|
|||||||
import net.schmizz.sshj.userauth.UserAuthException;
|
import net.schmizz.sshj.userauth.UserAuthException;
|
||||||
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
|
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the {@code "publickey"} SSH authentication method.
|
* Implements the {@code "publickey"} SSH authentication method.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -60,11 +58,7 @@ public class AuthPublickey
|
|||||||
*/
|
*/
|
||||||
private SSHPacket buildReq(boolean signed)
|
private SSHPacket buildReq(boolean signed)
|
||||||
throws UserAuthException {
|
throws UserAuthException {
|
||||||
try {
|
log.debug("Attempting authentication using {}", kProv);
|
||||||
kProv.getPublic();
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
throw new UserAuthException("Problem getting public key", ioe);
|
|
||||||
}
|
|
||||||
return putPubKey(super.buildReq().putBoolean(signed));
|
return putPubKey(super.buildReq().putBoolean(signed));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +70,7 @@ public class AuthPublickey
|
|||||||
*/
|
*/
|
||||||
private void sendSignedReq()
|
private void sendSignedReq()
|
||||||
throws UserAuthException, TransportException {
|
throws UserAuthException, TransportException {
|
||||||
log.debug("Sending signed request");
|
log.debug("Key acceptable, sending signed request");
|
||||||
params.getTransport().write(putSig(buildReq(true)));
|
params.getTransport().write(putSig(buildReq(true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public abstract class KeyedAuthMethod
|
|||||||
try {
|
try {
|
||||||
key = kProv.getPublic();
|
key = kProv.getPublic();
|
||||||
} catch (IOException ioe) {
|
} 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 ]
|
// public key as 2 strings: [ key type | key blob ]
|
||||||
@@ -59,7 +59,7 @@ public abstract class KeyedAuthMethod
|
|||||||
try {
|
try {
|
||||||
key = kProv.getPrivate();
|
key = kProv.getPrivate();
|
||||||
} catch (IOException ioe) {
|
} 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();
|
final String kt = KeyType.fromKey(key).toString();
|
||||||
|
|||||||
@@ -15,11 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.userauth.password;
|
package net.schmizz.sshj.userauth.password;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public class PrivateKeyFileResource
|
public class PrivateKeyFileResource
|
||||||
extends Resource<String> {
|
extends Resource<File> {
|
||||||
|
|
||||||
public PrivateKeyFileResource(String path) {
|
public PrivateKeyFileResource(File privateKeyFile) {
|
||||||
super(path);
|
super(privateKeyFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user