diff --git a/smack-sasl-provided/src/main/java/org/jivesoftware/smack/sasl/provided/SASLDigestMD5Mechanism.java b/smack-sasl-provided/src/main/java/org/jivesoftware/smack/sasl/provided/SASLDigestMD5Mechanism.java index 1c0a8370a..4f5e43837 100644 --- a/smack-sasl-provided/src/main/java/org/jivesoftware/smack/sasl/provided/SASLDigestMD5Mechanism.java +++ b/smack-sasl-provided/src/main/java/org/jivesoftware/smack/sasl/provided/SASLDigestMD5Mechanism.java @@ -141,7 +141,7 @@ public class SASLDigestMD5Mechanism extends SASLMechanism { String responseValue = calcResponse(DigestType.ClientResponse); // @formatter:off // See RFC 2831 2.1.2 digest-response - String saslString = "username=\"" + authenticationId + '"' + String saslString = "username=\"" + quoteBackslash(authenticationId) + '"' + ",realm=\"" + serviceName + '"' + ",nonce=\"" + nonce + '"' + ",cnonce=\"" + cnonce + '"' @@ -216,4 +216,18 @@ public class SASLDigestMD5Mechanism extends SASLMechanism { return responseValue; } + /** + * Quote the backslash in the given String. Replaces all occurrences of "\" with "\\". + *

+ * According to RFC 2831 ยง 7.2 a quoted-string consists either of qdtext or quoted-pair. And since quoted-pair is a + * backslash followed by a char, every backslash in qdtext must be quoted, since it otherwise would be treated as + * qdtext. + *

+ * + * @param string the input string. + * @return the input string where the every backslash is quoted. + */ + public static String quoteBackslash(String string) { + return string.replace("\\", "\\\\"); + } }