You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2012/05/18 13:21:59 UTC
svn commit: r1340067 - in
/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client:
auth/UserAuthAgent.java auth/UserAuthPassword.java
auth/UserAuthPublicKey.java session/ClientSessionImpl.java
Author: gnodet
Date: Fri May 18 11:21:58 2012
New Revision: 1340067
URL: http://svn.apache.org/viewvc?rev=1340067&view=rev
Log:
Small refactoring of the user authentication to better cope with the agent authentication when no keys are available
Modified:
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java?rev=1340067&r1=1340066&r2=1340067&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java Fri May 18 11:21:58 2012
@@ -49,17 +49,12 @@ public class UserAuthAgent implements Us
this.username = username;
this.agent = session.getFactoryManager().getAgentFactory().createClient(session);
this.keys = agent.getIdentities().iterator();
- sendNextKey();
}
public String getUsername() {
return username;
}
- protected void sendNextKey() throws IOException {
- sendNextKey(keys.next().getFirst());
- }
-
protected void sendNextKey(PublicKey key) throws IOException {
try {
log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
@@ -98,21 +93,32 @@ public class UserAuthAgent implements Us
}
public Result next(Buffer buffer) throws IOException {
- SshConstants.Message cmd = buffer.getCommand();
- log.info("Received {}", cmd);
- if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
- agent.close();
- return Result.Success;
- } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+ if (buffer == null) {
if (keys.hasNext()) {
sendNextKey(keys.next().getFirst());
return Result.Continued;
+ } else {
+ agent.close();
+ return Result.Failure;
}
- agent.close();
- return Result.Failure;
} else {
- // TODO: check packets
- return Result.Continued;
+ SshConstants.Message cmd = buffer.getCommand();
+ log.info("Received {}", cmd);
+ if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+ agent.close();
+ return Result.Success;
+ } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+ if (keys.hasNext()) {
+ sendNextKey(keys.next().getFirst());
+ return Result.Continued;
+ } else {
+ agent.close();
+ return Result.Failure;
+ }
+ } else {
+ // TODO: check packets
+ return Result.Continued;
+ }
}
}
}
Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java?rev=1340067&r1=1340066&r2=1340067&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java Fri May 18 11:21:58 2012
@@ -36,18 +36,14 @@ public class UserAuthPassword implements
protected final Logger log = LoggerFactory.getLogger(getClass());
+ private final ClientSessionImpl session;
private final String username;
+ private final String password;
- public UserAuthPassword(ClientSessionImpl session, String username, String password) throws IOException {
+ public UserAuthPassword(ClientSessionImpl session, String username, String password) {
+ this.session = session;
this.username = username;
- log.info("Send SSH_MSG_USERAUTH_REQUEST for password");
- Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
- buffer.putString(username);
- buffer.putString("ssh-connection");
- buffer.putString("password");
- buffer.putByte((byte) 0);
- buffer.putString(password);
- session.writePacket(buffer);
+ this.password = password;
}
public String getUsername() {
@@ -55,15 +51,27 @@ public class UserAuthPassword implements
}
public Result next(Buffer buffer) throws IOException {
- SshConstants.Message cmd = buffer.getCommand();
- log.info("Received {}", cmd);
- if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
- return Result.Success;
- } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
- return Result.Failure;
- } else {
- // TODO: check packets
+ if (buffer == null) {
+ log.info("Send SSH_MSG_USERAUTH_REQUEST for password");
+ buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+ buffer.putString(username);
+ buffer.putString("ssh-connection");
+ buffer.putString("password");
+ buffer.putByte((byte) 0);
+ buffer.putString(password);
+ session.writePacket(buffer);
return Result.Continued;
+ } else {
+ SshConstants.Message cmd = buffer.getCommand();
+ log.info("Received {}", cmd);
+ if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+ return Result.Success;
+ } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+ return Result.Failure;
+ } else {
+ // TODO: check packets
+ return Result.Continued;
+ }
}
}
Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java?rev=1340067&r1=1340066&r2=1340067&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java Fri May 18 11:21:58 2012
@@ -18,6 +18,10 @@
*/
package org.apache.sshd.client.auth;
+import java.io.IOException;
+import java.security.KeyPair;
+import java.security.interfaces.RSAPublicKey;
+
import org.apache.sshd.client.UserAuth;
import org.apache.sshd.client.session.ClientSessionImpl;
import org.apache.sshd.common.KeyPairProvider;
@@ -25,16 +29,9 @@ import org.apache.sshd.common.NamedFacto
import org.apache.sshd.common.Signature;
import org.apache.sshd.common.SshConstants;
import org.apache.sshd.common.util.Buffer;
-import org.apache.sshd.common.util.BufferUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
-import java.security.KeyPair;
-import java.security.PublicKey;
-import java.security.interfaces.DSAPublicKey;
-import java.security.interfaces.RSAPublicKey;
-
/**
* TODO Add javadoc
*
@@ -44,47 +41,14 @@ public class UserAuthPublicKey implement
protected final Logger log = LoggerFactory.getLogger(getClass());
+ private final ClientSessionImpl session;
private final String username;
+ private final KeyPair key;
- public UserAuthPublicKey(ClientSessionImpl session, String username, KeyPair key) throws IOException {
- try {
- this.username = username;
- log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
- Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
- int pos1 = buffer.wpos() - 1;
- buffer.putString(username);
- buffer.putString("ssh-connection");
- buffer.putString("publickey");
- buffer.putByte((byte) 1);
- buffer.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
- int pos2 = buffer.wpos();
- buffer.putPublicKey(key.getPublic());
-
- Signature verif = NamedFactory.Utils.create(session.getFactoryManager().getSignatureFactories(), (key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
- verif.init(key.getPublic(), key.getPrivate());
-
- Buffer bs = new Buffer();
- bs.putString(session.getKex().getH());
- bs.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
- bs.putString(username);
- bs.putString("ssh-connection");
- bs.putString("publickey");
- bs.putByte((byte) 1);
- bs.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
- bs.putPublicKey(key.getPublic());
- verif.update(bs.array(), bs.rpos(), bs.available());
-
- bs = new Buffer();
- bs.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
- bs.putBytes(verif.sign());
- buffer.putBytes(bs.array(), bs.rpos(), bs.available());
-
- session.writePacket(buffer);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw (IOException) new IOException("Error performing public key authentication").initCause(e);
- }
+ public UserAuthPublicKey(ClientSessionImpl session, String username, KeyPair key) {
+ this.session = session;
+ this.username = username;
+ this.key = key;
}
public String getUsername() {
@@ -92,15 +56,56 @@ public class UserAuthPublicKey implement
}
public Result next(Buffer buffer) throws IOException {
- SshConstants.Message cmd = buffer.getCommand();
- log.info("Received {}", cmd);
- if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
- return Result.Success;
- } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
- return Result.Failure;
+ if (buffer == null) {
+ try {
+ log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
+ buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+ int pos1 = buffer.wpos() - 1;
+ buffer.putString(username);
+ buffer.putString("ssh-connection");
+ buffer.putString("publickey");
+ buffer.putByte((byte) 1);
+ buffer.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
+ int pos2 = buffer.wpos();
+ buffer.putPublicKey(key.getPublic());
+
+ Signature verif = NamedFactory.Utils.create(session.getFactoryManager().getSignatureFactories(), (key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
+ verif.init(key.getPublic(), key.getPrivate());
+
+ Buffer bs = new Buffer();
+ bs.putString(session.getKex().getH());
+ bs.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
+ bs.putString(username);
+ bs.putString("ssh-connection");
+ bs.putString("publickey");
+ bs.putByte((byte) 1);
+ bs.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
+ bs.putPublicKey(key.getPublic());
+ verif.update(bs.array(), bs.rpos(), bs.available());
+
+ bs = new Buffer();
+ bs.putString((key.getPublic() instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS);
+ bs.putBytes(verif.sign());
+ buffer.putBytes(bs.array(), bs.rpos(), bs.available());
+
+ session.writePacket(buffer);
+ return Result.Continued;
+ } catch (IOException e) {
+ throw e;
+ } catch (Exception e) {
+ throw (IOException) new IOException("Error performing public key authentication").initCause(e);
+ }
} else {
- // TODO: check packets
- return Result.Continued;
+ SshConstants.Message cmd = buffer.getCommand();
+ log.info("Received {}", cmd);
+ if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+ return Result.Success;
+ } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+ return Result.Failure;
+ } else {
+ // TODO: check packets
+ return Result.Continued;
+ }
}
}
Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java?rev=1340067&r1=1340066&r2=1340067&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java Fri May 18 11:21:58 2012
@@ -108,6 +108,22 @@ public class ClientSessionImpl extends A
authFuture = new DefaultAuthFuture(lock);
userAuth = new UserAuthAgent(this, username);
setState(ClientSessionImpl.State.UserAuth);
+
+ switch (userAuth.next(null)) {
+ case Success:
+ authFuture.setAuthed(true);
+ username = userAuth.getUsername();
+ authed = true;
+ setState(State.Running);
+ break;
+ case Failure:
+ authFuture.setAuthed(false);
+ userAuth = null;
+ setState(State.WaitForAuth);
+ break;
+ case Continued:
+ break;
+ }
return authFuture;
}
}
@@ -130,6 +146,22 @@ public class ClientSessionImpl extends A
authFuture = new DefaultAuthFuture(lock);
userAuth = new UserAuthPassword(this, username, password);
setState(ClientSessionImpl.State.UserAuth);
+
+ switch (userAuth.next(null)) {
+ case Success:
+ authFuture.setAuthed(true);
+ username = userAuth.getUsername();
+ authed = true;
+ setState(State.Running);
+ break;
+ case Failure:
+ authFuture.setAuthed(false);
+ userAuth = null;
+ setState(State.WaitForAuth);
+ break;
+ case Continued:
+ break;
+ }
return authFuture;
}
}
@@ -152,6 +184,22 @@ public class ClientSessionImpl extends A
authFuture = new DefaultAuthFuture(lock);
userAuth = new UserAuthPublicKey(this, username, key);
setState(ClientSessionImpl.State.UserAuth);
+
+ switch (userAuth.next(null)) {
+ case Success:
+ authFuture.setAuthed(true);
+ username = userAuth.getUsername();
+ authed = true;
+ setState(State.Running);
+ break;
+ case Failure:
+ authFuture.setAuthed(false);
+ userAuth = null;
+ setState(State.WaitForAuth);
+ break;
+ case Continued:
+ break;
+ }
return authFuture;
}
}