aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/crypto
diff options
context:
space:
mode:
authorSam Whited <sam@samwhited.com>2014-11-15 08:48:40 -0500
committerSam Whited <sam@samwhited.com>2014-11-15 08:49:00 -0500
commit4b5d6f5b4fd29a4ee6d469f3b540dc5ba826f1a3 (patch)
treeb65f5d7b6e0861182be4774976d3601a46416bcc /src/main/java/eu/siacs/conversations/crypto
parent0e550789d372a1a83caa432e93a4f969a0607c9a (diff)
Improve auth error handling and state machine
Diffstat (limited to 'src/main/java/eu/siacs/conversations/crypto')
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/sasl/AuthenticationException.java11
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/sasl/DigestMd5.java17
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/sasl/SaslMechanism.java27
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/sasl/ScramSha1.java13
4 files changed, 36 insertions, 32 deletions
diff --git a/src/main/java/eu/siacs/conversations/crypto/sasl/AuthenticationException.java b/src/main/java/eu/siacs/conversations/crypto/sasl/AuthenticationException.java
deleted file mode 100644
index 62d3ddf1a..000000000
--- a/src/main/java/eu/siacs/conversations/crypto/sasl/AuthenticationException.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package eu.siacs.conversations.crypto.sasl;
-
-public class AuthenticationException extends Exception {
- public AuthenticationException(final String message) {
- super(message);
- }
-
- public AuthenticationException(final Exception inner) {
- super(inner);
- }
-}
diff --git a/src/main/java/eu/siacs/conversations/crypto/sasl/DigestMd5.java b/src/main/java/eu/siacs/conversations/crypto/sasl/DigestMd5.java
index bef76fef8..b56d2a466 100644
--- a/src/main/java/eu/siacs/conversations/crypto/sasl/DigestMd5.java
+++ b/src/main/java/eu/siacs/conversations/crypto/sasl/DigestMd5.java
@@ -21,11 +21,6 @@ public class DigestMd5 extends SaslMechanism {
return "DIGEST-MD5";
}
- private enum State {
- INITIAL,
- RESPONSE_SENT,
- }
-
private State state = State.INITIAL;
@Override
@@ -53,8 +48,7 @@ public class DigestMd5 extends SaslMechanism {
final byte[] y = md.digest(x.getBytes(Charset.defaultCharset()));
final String cNonce = new BigInteger(100, rng).toString(32);
final byte[] a1 = CryptoHelper.concatenateByteArrays(y,
- (":" + nonce + ":" + cNonce).getBytes(Charset
- .defaultCharset()));
+ (":" + nonce + ":" + cNonce).getBytes(Charset.defaultCharset()));
final String a2 = "AUTHENTICATE:" + digestUri;
final String ha1 = CryptoHelper.bytesToHex(md.digest(a1));
final String ha2 = CryptoHelper.bytesToHex(md.digest(a2.getBytes(Charset
@@ -72,13 +66,16 @@ public class DigestMd5 extends SaslMechanism {
saslString.getBytes(Charset.defaultCharset()),
Base64.NO_WRAP);
} catch (final NoSuchAlgorithmException e) {
- return "";
+ throw new AuthenticationException(e);
}
return encodedResponse;
case RESPONSE_SENT:
- return "";
+ state = State.VALID_SERVER_RESPONSE;
+ break;
+ default:
+ throw new InvalidStateException(state);
}
- return "";
+ return null;
}
}
diff --git a/src/main/java/eu/siacs/conversations/crypto/sasl/SaslMechanism.java b/src/main/java/eu/siacs/conversations/crypto/sasl/SaslMechanism.java
index 38a03c187..7dd5e99c3 100644
--- a/src/main/java/eu/siacs/conversations/crypto/sasl/SaslMechanism.java
+++ b/src/main/java/eu/siacs/conversations/crypto/sasl/SaslMechanism.java
@@ -11,6 +11,33 @@ public abstract class SaslMechanism {
final protected Account account;
final protected SecureRandom rng;
+ protected static enum State {
+ INITIAL,
+ AUTH_TEXT_SENT,
+ RESPONSE_SENT,
+ VALID_SERVER_RESPONSE,
+ }
+
+ public static class AuthenticationException extends Exception {
+ public AuthenticationException(final String message) {
+ super(message);
+ }
+
+ public AuthenticationException(final Exception inner) {
+ super(inner);
+ }
+ }
+
+ public static class InvalidStateException extends AuthenticationException {
+ public InvalidStateException(final String message) {
+ super(message);
+ }
+
+ public InvalidStateException(final State state) {
+ this("Invalid state: " + state.toString());
+ }
+ }
+
public SaslMechanism(final TagWriter tagWriter, final Account account, final SecureRandom rng) {
this.tagWriter = tagWriter;
this.account = account;
diff --git a/src/main/java/eu/siacs/conversations/crypto/sasl/ScramSha1.java b/src/main/java/eu/siacs/conversations/crypto/sasl/ScramSha1.java
index e7e31e73e..2073de2d8 100644
--- a/src/main/java/eu/siacs/conversations/crypto/sasl/ScramSha1.java
+++ b/src/main/java/eu/siacs/conversations/crypto/sasl/ScramSha1.java
@@ -33,13 +33,6 @@ public class ScramSha1 extends SaslMechanism {
HMAC = new HMac(new SHA1Digest());
}
- private enum State {
- INITIAL,
- AUTH_TEXT_SENT,
- RESPONSE_SENT,
- VALID_SERVER_RESPONSE,
- }
-
private State state = State.INITIAL;
public ScramSha1(final TagWriter tagWriter, final Account account, final SecureRandom rng) {
@@ -56,11 +49,9 @@ public class ScramSha1 extends SaslMechanism {
@Override
public String getClientFirstMessage() {
- if (clientFirstMessageBare.isEmpty()) {
+ if (clientFirstMessageBare.isEmpty() && state == State.INITIAL) {
clientFirstMessageBare = "n=" + CryptoHelper.saslPrep(account.getUsername()) +
",r=" + this.clientNonce;
- }
- if (state == State.INITIAL) {
state = State.AUTH_TEXT_SENT;
}
return Base64.encodeToString(
@@ -157,7 +148,7 @@ public class ScramSha1 extends SaslMechanism {
state = State.VALID_SERVER_RESPONSE;
return "";
default:
- throw new AuthenticationException("Invalid state: " + state);
+ throw new InvalidStateException(state);
}
}