Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Simplified SCRAM SHA class layout
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed May 31, 2021
1 parent bc54d68 commit 2977159
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XMPP_API.Classes.Network.XML;
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA1;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA1;

namespace Component_Tests.Classes.XmppMessages
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public void Test_ScramSHA1SASLMechanism_3()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
Assert.IsTrue(msgs[0] is ScramSHA1ChallengeMessage);
Assert.IsTrue(msgs[0] is ScramSHAChallengeMessage);
}

[TestCategory("XMPP_Messages")]
Expand All @@ -52,9 +53,9 @@ public void Test_ScramSHA1SASLMechanism_4()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
ScramSHA1ChallengeMessage challenge = (ScramSHA1ChallengeMessage)msgs[0];
ScramSHAChallengeMessage challenge = (ScramSHAChallengeMessage)msgs[0];
AbstractMessage resp = mechanism.generateResponse(challenge);
Assert.IsTrue(resp is ScramSha1ChallengeSolutionMessage);
Assert.IsTrue(resp is ScramSHAChallengeSolutionMessage);
}

[TestCategory("XMPP_Messages")]
Expand All @@ -66,9 +67,9 @@ public void Test_ScramSHA1SASLMechanism_5()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
ScramSHA1ChallengeMessage challenge = (ScramSHA1ChallengeMessage)msgs[0];
ScramSHAChallengeMessage challenge = (ScramSHAChallengeMessage)msgs[0];
AbstractMessage resp = mechanism.generateResponse(challenge);
ScramSha1ChallengeSolutionMessage respMsg = (ScramSha1ChallengeSolutionMessage)resp;
ScramSHAChallengeSolutionMessage respMsg = (ScramSHAChallengeSolutionMessage)resp;
Assert.IsTrue(string.Equals("Yz1iaXdzLHI9ZnlrbytkMmxiYkZnT05Sdjlxa3hkYXdMM3JmY05IWUpZMVpWdldWczdqLHA9djBYOHYzQnoyVDBDSkdiSlF5RjBYK0hJNFRzPQ==".ToLower(), respMsg.SOLUTION.ToLower()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XMPP_API.Classes.Network.XML;
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA256;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA256;

namespace Component_Tests.Classes.XmppMessages
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public void Test_ScramSHA256SASLMechanism_3()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
Assert.IsTrue(msgs[0] is ScramSHA1ChallengeMessage);
Assert.IsTrue(msgs[0] is ScramSHAChallengeMessage);
}

[TestCategory("XMPP_Messages")]
Expand All @@ -52,9 +53,9 @@ public void Test_ScramSHA256SASLMechanism_4()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
ScramSHA1ChallengeMessage challenge = (ScramSHA1ChallengeMessage)msgs[0];
ScramSHAChallengeMessage challenge = (ScramSHAChallengeMessage)msgs[0];
AbstractMessage resp = mechanism.generateResponse(challenge);
Assert.IsTrue(resp is ScramSha1ChallengeSolutionMessage);
Assert.IsTrue(resp is ScramSHAChallengeSolutionMessage);
}

[TestCategory("XMPP_Messages")]
Expand All @@ -66,9 +67,9 @@ public void Test_ScramSHA256SASLMechanism_5()
MessageParser2 parser = new MessageParser2();
string s = "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=</challenge>";
List<AbstractMessage> msgs = parser.parseMessages(ref s);
ScramSHA1ChallengeMessage challenge = (ScramSHA1ChallengeMessage)msgs[0];
ScramSHAChallengeMessage challenge = (ScramSHAChallengeMessage)msgs[0];
AbstractMessage resp = mechanism.generateResponse(challenge);
ScramSha1ChallengeSolutionMessage respMsg = (ScramSha1ChallengeSolutionMessage)resp;
ScramSHAChallengeSolutionMessage respMsg = (ScramSHAChallengeSolutionMessage)resp;
Assert.IsTrue(string.Equals("Yz1iaXdzLHI9ck9wck5HZndFYmVSV2diTkVrcU8laHZZRHBXVWEyUmFUQ0FmdXhGSWxqKWhObEYkazAscD1kSHpiWmFwV0lrNGpVaE4rVXRlOXl0YWc5empmTUhnc3FtbWl6N0FuZFZRPQ==".ToLower(), respMsg.SOLUTION.ToLower()));
}
}
Expand Down
3 changes: 2 additions & 1 deletion XMPP_API/Classes/Network/XML/MessageParser2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Features;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM;
using XMPP_API.Classes.Network.XML.Messages.Features.TLS;
using XMPP_API.Classes.Network.XML.Messages.XEP_0030;
using XMPP_API.Classes.Network.XML.Messages.XEP_0045;
Expand Down Expand Up @@ -446,7 +447,7 @@ private List<AbstractMessage> parseMessageInternal(ref string msg)

// SASL:
case "challenge":
messages.Add(new ScramSHA1ChallengeMessage(n));
messages.Add(new ScramSHAChallengeMessage(n));
break;

// SASL failure:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using XMPP_API.Classes.Crypto;
using XMPP_API.Classes.Network.XML.Messages.Processor;

namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA1
namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA1
{
public class ScramSHA1SASLMechanism: AbstractSASLMechanism
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public ScramSHA1SASLMechanism(string id, string password, string clientNonceBase
#region --Misc Methods (Public)--
public override AbstractMessage generateResponse(AbstractMessage msg)
{
if (msg is ScramSHA1ChallengeMessage challenge)
if (msg is ScramSHAChallengeMessage challenge)
{
serverFirstMsg = decodeStringBase64(challenge.CHALLENGE);

Expand Down Expand Up @@ -94,7 +94,7 @@ public override AbstractMessage generateResponse(AbstractMessage msg)
return null;
}

return new ScramSha1ChallengeSolutionMessage(computeAnswer(iters));
return new ScramSHAChallengeSolutionMessage(computeAnswer(iters));
}
onSaslError("SCRAM-SHA: Invalid challenge message received!");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using XMPP_API.Classes.Crypto;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA1;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA1;
using XMPP_API.Classes.Network.XML.Messages.Processor;

namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA256
namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA256
{
// https://tools.ietf.org/html/rfc7677
public class ScramSHA256SASLMechanism: ScramSHA1SASLMechanism
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Xml;
using System.Xml.Linq;

namespace XMPP_API.Classes.Network.XML.Messages
namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM
{
public class ScramSHA1ChallengeMessage: AbstractMessage
public class ScramSHAChallengeMessage: AbstractMessage
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
Expand All @@ -19,7 +19,7 @@ public class ScramSHA1ChallengeMessage: AbstractMessage
/// <history>
/// 22/08/2017 Created [Fabian Sauter]
/// </history>
public ScramSHA1ChallengeMessage(XmlNode node)
public ScramSHAChallengeMessage(XmlNode node)
{
CHALLENGE = node.InnerText;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Xml.Linq;

namespace XMPP_API.Classes.Network.XML.Messages
namespace XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM
{
public class ScramSha1ChallengeSolutionMessage: AbstractMessage
public class ScramSHAChallengeSolutionMessage: AbstractMessage
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
Expand All @@ -17,7 +17,7 @@ public class ScramSha1ChallengeSolutionMessage: AbstractMessage
/// <history>
/// 22/08/2017 Created [Fabian Sauter]
/// </history>
public ScramSha1ChallengeSolutionMessage(string solution)
public ScramSHAChallengeSolutionMessage(string solution)
{
SOLUTION = solution;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using XMPP_API.Classes.Network.XML.Messages.Features;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.Plain;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA1;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SHA256;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA1;
using XMPP_API.Classes.Network.XML.Messages.Features.SASL.SCRAM.SHA256;

namespace XMPP_API.Classes.Network.XML.Messages.Processor
{
Expand Down Expand Up @@ -163,7 +164,7 @@ protected async override Task processMessageAsync(NewValidMessageEventArgs args)

case SASLState.REQUESTED:
case SASLState.CHALLENGING:
if (msg is ScramSHA1ChallengeMessage)
if (msg is ScramSHAChallengeMessage)
{
state = SASLState.CHALLENGING;
setMessageProcessed(args);
Expand Down
8 changes: 4 additions & 4 deletions XMPP_API/XMPP_API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SASLFailureMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SASLStreamFeature.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SASLSuccessMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SHA1\ScramSHA1ChallengeMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SHA1\ScramSha1ChallengeSolutionMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SHA1\ScramSHA1SASLMechanism.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SHA256\ScramSHA256SASLMechanism.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SCRAM\ScramSHAChallengeMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SCRAM\ScramSHAChallengeSolutionMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SCRAM\SHA1\ScramSHA1SASLMechanism.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\SASL\SCRAM\SHA256\ScramSHA256SASLMechanism.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\StreamFeature.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\StreamFeaturesAnswerMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\Features\TLSStreamFeature.cs" />
Expand Down

0 comments on commit 2977159

Please sign in to comment.