-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
SASL 2 #3113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
SASL 2 #3113
Changes from all commits
611fbd2
86e4a24
8edbaf5
92407f2
c75543e
2eecb2d
d6bbce3
b216ada
fef20b6
00902b3
2168470
bbdf000
81a0f06
db78de0
41a45bd
b68fa0a
213a90f
778b6ac
c3498b0
06d7220
336d78c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,19 @@ private void readStream() throws Exception { | |
| } | ||
| else if ("auth".equals(tag)) { | ||
| // User is trying to authenticate using SASL | ||
| if (authenticateClient(doc)) { | ||
| if (authenticateClient(doc, false)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the exception of the second boolean argument to |
||
| // SASL authentication was successful so open a new stream and offer | ||
| // resource binding and session establishment (to client sessions only) | ||
| saslSuccessful(); | ||
| } | ||
| else if (socketReader.connection.isClosed()) { | ||
| socketReader.open = false; | ||
| socketReader.session = null; | ||
| } | ||
| } | ||
| else if ("authenticate".equals(tag)) { | ||
| // User is trying to authenticate using SASL | ||
| if (authenticateClient(doc, true)) { | ||
| // SASL authentication was successful so open a new stream and offer | ||
| // resource binding and session establishment (to client sessions only) | ||
| saslSuccessful(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this explicit SASL2 branch, Also, the code comment is wrong here. It describes the SASL(1) functionality, not the SASL2 functionality, which will cause future-us to scratch behind the ears. |
||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,10 +106,7 @@ protected void tlsNegotiated() throws XmlPullParserException, IOException | |
|
|
||
| // Offer stream features including SASL Mechanisms | ||
| final Element features = DocumentHelper.createElement(QName.get("features", "stream", "http://etherx.jabber.org/streams")); | ||
| final Element mechanisms = SASLAuthentication.getSASLMechanisms(socketReader.session); | ||
| if (mechanisms != null) { | ||
| features.add(mechanisms); | ||
| } | ||
| SASLAuthentication.addSASLMechanisms(features, socketReader.session); | ||
| final List<Element> specificFeatures = socketReader.session.getAvailableStreamFeatures(); | ||
| if (specificFeatures != null) { | ||
| for (final Element feature : specificFeatures) { | ||
|
|
@@ -121,7 +118,7 @@ protected void tlsNegotiated() throws XmlPullParserException, IOException | |
| socketReader.connection.deliverRawText(StringUtils.asUnclosedStream(document)); | ||
| } | ||
|
|
||
| protected boolean authenticateClient(Element doc) throws DocumentException, IOException, | ||
| protected boolean authenticateClient(Element doc, boolean usingSASL2) throws DocumentException, IOException, | ||
| XmlPullParserException { | ||
| // Ensure that connection was encrypted if TLS was required | ||
| if (socketReader.connection.getConfiguration().getTlsPolicy() == Connection.TLSPolicy.required && | ||
|
|
@@ -133,7 +130,7 @@ protected boolean authenticateClient(Element doc) throws DocumentException, IOEx | |
| boolean isComplete = false; | ||
| boolean success = false; | ||
| while (!isComplete) { | ||
| SASLAuthentication.Status status = SASLAuthentication.handle(socketReader.session, doc); | ||
| SASLAuthentication.Status status = SASLAuthentication.handle(socketReader.session, doc, usingSASL2); | ||
| if (status == SASLAuthentication.Status.needResponse) { | ||
| // Get the next answer since we are not done yet | ||
| doc = socketReader.reader.parseDocument().getRootElement(); | ||
|
|
@@ -145,6 +142,10 @@ protected boolean authenticateClient(Element doc) throws DocumentException, IOEx | |
| else { | ||
| isComplete = true; | ||
| success = status == SASLAuthentication.Status.authenticated; | ||
| if (success && usingSASL2) { | ||
| Element features = generateFeatures(); | ||
| socketReader.session.deliverRawText(features.asXML()); | ||
| } | ||
| } | ||
| } | ||
| return success; | ||
|
|
@@ -159,6 +160,13 @@ protected boolean authenticateClient(Element doc) throws DocumentException, IOEx | |
| protected void saslSuccessful() throws XmlPullParserException, IOException | ||
| { | ||
| final Document document = getStreamHeader(); | ||
| final Element features = generateFeatures(); | ||
| document.getRootElement().add(features); | ||
|
|
||
| socketReader.connection.deliverRawText(StringUtils.asUnclosedStream(document)); | ||
| } | ||
|
|
||
| protected Element generateFeatures() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use some Javadoc. |
||
| final Element features = DocumentHelper.createElement(QName.get("features", "stream", "http://etherx.jabber.org/streams")); | ||
|
|
||
| // Include specific features such as resource binding and session establishment for client sessions | ||
|
|
@@ -168,9 +176,8 @@ protected void saslSuccessful() throws XmlPullParserException, IOException | |
| features.add(feature); | ||
| } | ||
| } | ||
| document.getRootElement().add(features); | ||
|
|
||
| socketReader.connection.deliverRawText(StringUtils.asUnclosedStream(document)); | ||
| return features; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -248,14 +255,8 @@ protected void compressionSuccessful() throws XmlPullParserException, IOExceptio | |
| final Element features = DocumentHelper.createElement(QName.get("features", "stream", "http://etherx.jabber.org/streams")); | ||
| document.getRootElement().add(features); | ||
|
|
||
| // Include SASL mechanisms only if client has not been authenticated | ||
| if (!socketReader.session.isAuthenticated()) { | ||
| // Include available SASL Mechanisms | ||
| final Element saslMechanisms = SASLAuthentication.getSASLMechanisms(socketReader.session); | ||
| if (saslMechanisms != null) { | ||
| features.add(saslMechanisms); | ||
| } | ||
| } | ||
| // Include available SASL Mechanisms | ||
| SASLAuthentication.addSASLMechanisms(features, socketReader.session); | ||
| // Include specific features such as resource binding and session establishment for client sessions. | ||
| final List<Element> specificFeatures = socketReader.session.getAvailableStreamFeatures(); | ||
| if (specificFeatures != null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ public abstract class StanzaHandler { | |
| // Flag that indicates that the client requested to be authenticated. Once the | ||
| // authentication process is over the value will return to false. | ||
| protected boolean startedSASL = false; | ||
| protected boolean usingSASL2 = false; | ||
| /** | ||
| * SASL status based on the last SASL interaction | ||
| */ | ||
|
|
@@ -202,10 +203,23 @@ else if ("auth".equals(tag)) { | |
| // User is trying to authenticate using SASL | ||
| startedSASL = true; | ||
| // Process authentication stanza | ||
| saslStatus = SASLAuthentication.handle(session, doc); | ||
| saslStatus = SASLAuthentication.handle(session, doc, usingSASL2); | ||
| } else if ("authenticate".equals(tag)) { | ||
| // User is trying to authenticate using SASL2. | ||
| startedSASL = true; | ||
| usingSASL2 = true; | ||
| saslStatus = SASLAuthentication.handle(session, doc, usingSASL2); | ||
| } else if (startedSASL && "response".equals(tag) || "abort".equals(tag)) { | ||
|
dwd marked this conversation as resolved.
|
||
| // User is responding to SASL challenge. Process response | ||
| saslStatus = SASLAuthentication.handle(session, doc); | ||
| saslStatus = SASLAuthentication.handle(session, doc, usingSASL2); | ||
| if (saslStatus == SASLAuthentication.Status.failed) { | ||
| startedSASL = false; | ||
| usingSASL2 = false; | ||
| } | ||
| if (saslStatus == SASLAuthentication.Status.authenticated && usingSASL2) { | ||
| Element features = generateFeatures(); | ||
| session.deliverRawText(features.asXML()); | ||
|
dwd marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sending the (post SASL) features here happens after receiving "response" or "abort" from the client. Shouldn't it also happen after receiving "success"? |
||
| } | ||
| } | ||
| else if ("compress".equals(tag)) { | ||
| // Client is trying to initiate compression | ||
|
|
@@ -313,7 +327,7 @@ else if ("iq".equals(tag)) { | |
| // The original packet contains a malformed JID so answer an error | ||
| IQ reply = new IQ(); | ||
| if (!doc.elements().isEmpty()) { | ||
| reply.setChildElement(((Element)doc.elements().get(0)).createCopy()); | ||
| reply.setChildElement((doc.elements().get(0)).createCopy()); | ||
| } | ||
| reply.setID(doc.attributeValue("id")); | ||
| reply.setTo(session.getAddress()); | ||
|
|
@@ -353,7 +367,7 @@ private IQ getIQ(Element doc) { | |
| for (Element element : elements){ | ||
| session.setSoftwareVersionData(element.getName(), element.getStringValue()); | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| Log.error("Unexpected exception while processing IQ Version stanza from '{}'", session.getAddress(), e); | ||
| } | ||
|
|
@@ -492,10 +506,7 @@ protected void tlsNegotiated(XmlPullParser xpp) throws XmlPullParserException, I | |
| document.getRootElement().add(features); | ||
|
|
||
| // Include available SASL Mechanisms | ||
| final Element mechanismsElement=SASLAuthentication.getSASLMechanisms(session); | ||
| if (mechanismsElement!=null) { | ||
| features.add(mechanismsElement); | ||
| } | ||
| SASLAuthentication.addSASLMechanisms(features, session); | ||
|
|
||
| // Include specific features such as auth and register for client sessions | ||
| final List<Element> specificFeatures = session.getAvailableStreamFeatures(); | ||
|
|
@@ -516,8 +527,12 @@ protected void tlsNegotiated(XmlPullParser xpp) throws XmlPullParserException, I | |
| */ | ||
| protected void saslSuccessful() { | ||
| final Document document = getStreamHeader(); | ||
| final Element features = DocumentHelper.createElement(QName.get("features", "stream", "http://etherx.jabber.org/streams")); | ||
| final Element features = generateFeatures(); | ||
| document.getRootElement().add(features); | ||
| connection.deliverRawText(StringUtils.asUnclosedStream(document)); | ||
| } | ||
| protected Element generateFeatures() { | ||
| final Element features = DocumentHelper.createElement(QName.get("features", "stream", "http://etherx.jabber.org/streams")); | ||
|
|
||
| // Include specific features such as resource binding and session establishment for client sessions | ||
| final List<Element> specificFeatures = session.getAvailableStreamFeatures(); | ||
|
|
@@ -527,7 +542,7 @@ protected void saslSuccessful() { | |
| } | ||
| } | ||
|
|
||
| connection.deliverRawText(StringUtils.asUnclosedStream(document)); | ||
| return features; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -594,12 +609,8 @@ protected void compressionSuccessful() { | |
| document.getRootElement().add(features); | ||
|
|
||
| // Include SASL mechanisms only if client has not been authenticated | ||
| if (!session.isAuthenticated()) { | ||
| final Element saslMechanisms = SASLAuthentication.getSASLMechanisms(session); | ||
| if (saslMechanisms != null) { | ||
| features.add(saslMechanisms); | ||
| } | ||
| } | ||
| SASLAuthentication.addSASLMechanisms(features, session); | ||
|
|
||
| // Include specific features such as resource binding and session establishment for client sessions | ||
| final List<Element> specificFeatures = session.getAvailableStreamFeatures(); | ||
| if (specificFeatures != null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be an immutable class. UserAgentInfo is effectively a parsed data carrier with no behavior that requires later mutation. That will make it a easier/safer to use, and to pass data without risking it being modified. If instances are likely to be shared between Openfire cluster nodes, consider making it implement |
||
| * Copyright (C) 2025 Ignite Realtime Foundation. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.jivesoftware.openfire.net; | ||
|
|
||
| import org.dom4j.Element; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Represents user agent information provided by XMPP clients during authentication. | ||
| * This information includes an optional UUID v4 identifier, software description, | ||
| * and device description. This information is not exposed to other entities. | ||
| */ | ||
| public class UserAgentInfo { | ||
| private static final Logger Log = LoggerFactory.getLogger(UserAgentInfo.class); | ||
|
|
||
| private String id; // UUID v4 | ||
| private String software; // Software description | ||
| private String device; // Device description | ||
|
|
||
| /** | ||
| * Extracts and validates user agent information from the authentication element. | ||
| * | ||
| * @param userAgentElement the authentication element containing potential user agent data | ||
| * @return UserAgentInfo containing the parsed data, or null if no user agent data present | ||
| */ | ||
| public static UserAgentInfo extract(Element userAgentElement) { | ||
| if (userAgentElement == null) { | ||
| return null; | ||
| } | ||
|
|
||
| UserAgentInfo userAgentInfo = new UserAgentInfo(); | ||
|
|
||
| // Extract and validate UUID v4 id if present | ||
| String id = userAgentElement.attributeValue("id"); | ||
| if (id != null) { | ||
| try { | ||
| UUID uuid = UUID.fromString(id); | ||
| // Validate it's a v4 UUID | ||
| if (uuid.version() == 4) { | ||
| userAgentInfo.setId(id); | ||
| } else { | ||
| Log.warn("Invalid UUID version in user-agent id (must be v4): " + id); | ||
| } | ||
| } catch (IllegalArgumentException e) { | ||
| Log.warn("Invalid UUID format in user-agent id: " + id); | ||
| } | ||
| } | ||
|
|
||
| // Extract software info if present | ||
| Element softwareElement = userAgentElement.element("software"); | ||
| if (softwareElement != null) { | ||
| userAgentInfo.setSoftware(softwareElement.getTextTrim()); | ||
| } | ||
|
|
||
| // Extract device info if present | ||
| Element deviceElement = userAgentElement.element("device"); | ||
| if (deviceElement != null) { | ||
| userAgentInfo.setDevice(deviceElement.getTextTrim()); | ||
| } | ||
|
|
||
| return userAgentInfo; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getSoftware() { | ||
| return software; | ||
| } | ||
|
|
||
| void setSoftware(String software) { | ||
| this.software = software; | ||
| } | ||
|
|
||
| public String getDevice() { | ||
| return device; | ||
| } | ||
|
|
||
| void setDevice(String device) { | ||
| this.device = device; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think BOSH/HTTP-Bind now advertises SASL2 but cannot process SASL2 stanzas.
HttpSessionuses itssendPendingPacketsmethod to have inbound client data be processed by the server (the term 'send' here is a bit confusing). It uses aorg.jivesoftware.openfire.SessionPacketRouterinstance to do this.org.jivesoftware.openfire.SessionPacketRouter#route(org.dom4j.Element)processes SASL1 ("auth"and"response"elements) but not the SASL2 elements.This may be indicative of a larger issue. Why is the inbound stanza handling for BOSH/Http-Binding different from other client transports? All those transports use a StanzaHandler to process inbound data, but BOSH/HTTP-Bind does not.