-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathScramAuthenticationMechanism.cs
More file actions
361 lines (302 loc) · 11.3 KB
/
ScramAuthenticationMechanism.cs
File metadata and controls
361 lines (302 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Waher.Runtime.Settings;
using Waher.Security.LoginMonitor;
namespace Waher.Networking.SASL
{
/// <summary>
/// Authentication done by SCRAM-* defined in RFC 5802 & 7677:
/// https://tools.ietf.org/html/rfc5802
/// https://tools.ietf.org/html/rfc7677
/// </summary>
public abstract class ScramAuthenticationMechanism : HashedAuthenticationMechanism
{
private static readonly byte[] clientKey = Encoding.UTF8.GetBytes("Client Key");
private static readonly byte[] serverKey = Encoding.UTF8.GetBytes("Server Key");
private static byte[] salt = null;
private static string saltBase64 = null;
/// <summary>
/// Authentication done by SCRAM-* defined in RFC 5802 & 7677:
/// https://tools.ietf.org/html/rfc5802
/// https://tools.ietf.org/html/rfc7677
/// </summary>
public ScramAuthenticationMechanism()
{
}
/// <summary>
/// Checks if a mechanism is allowed during the current conditions.
/// </summary>
/// <param name="SslStream">SSL stream, if available.</param>
/// <returns>If mechanism is allowed.</returns>
public override bool Allowed(SslStream SslStream)
{
return true;
}
/// <summary>
/// Authentication request has been made.
/// </summary>
/// <param name="Data">Data in authentication request.</param>
/// <param name="Connection">Connection performing the authentication.</param>
/// <param name="PersistenceLayer">Persistence layer.</param>
/// <returns>If authentication was successful (true). If null, mechanism must send the corresponding challenge. If false, connection must close.</returns>
public override Task<bool?> AuthenticationRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer)
{
byte[] Bin = Convert.FromBase64String(Data);
string Request = Encoding.UTF8.GetString(Bin);
string UserName = null;
string Nonce = null;
int NrIterations = 4096;
foreach (KeyValuePair<string, string> Pair in this.ParseCommaSeparatedParameterList(Request))
{
switch (Pair.Key.ToLower())
{
case "n":
UserName = Pair.Value;
break;
case "r":
Nonce = Pair.Value;
break;
}
}
if (UserName is null || Nonce is null)
{
Connection.SaslErrorMalformedRequest();
LoginAuditor.Fail("Login attempt using malformed request.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
return Task.FromResult<bool?>(null);
}
Connection.SetUserIdentity(UserName);
string ServerNonce = Nonce + Convert.ToBase64String(PersistenceLayer.GetRandomNumbers(32));
string Challenge = "r=" + ServerNonce + ",s=" + saltBase64 + ",i=" + NrIterations.ToString();
Bin = Encoding.UTF8.GetBytes(Challenge);
string ChallengeBase64 = Convert.ToBase64String(Bin);
Connection.Tag = new object[] { UserName, Nonce, NrIterations, ServerNonce, Request, Challenge };
Connection.SaslChallenge(ChallengeBase64);
return Task.FromResult<bool?>(null);
}
/// <summary>
/// Response request has been made.
/// </summary>
/// <param name="Data">Data in response request.</param>
/// <param name="Connection">Connection performing the authentication.</param>
/// <param name="PersistenceLayer">Persistence layer.</param>
/// <returns>If authentication was successful (true). If null, mechanism must send the corresponding error. If false, connection must close.</returns>
public override async Task<bool?> ResponseRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer)
{
byte[] Bin = Convert.FromBase64String(Data);
string Request = Encoding.UTF8.GetString(Bin);
object[] P = (object[])Connection.Tag;
string UserName = (string)P[0];
//string Nonce = (string)P[1];
int NrIterations = (int)P[2];
string ServerNonce = (string)P[3];
string ClientRequest = (string)P[4];
string ServerChallenge = (string)P[5];
string c;
string Proof = null;
bool ServerNonceChecked = false;
foreach (KeyValuePair<string, string> Pair in this.ParseCommaSeparatedParameterList(Request))
{
switch (Pair.Key.ToLower())
{
case "c":
c = Pair.Value;
break;
case "r":
ServerNonceChecked = true;
if (ServerNonce != Pair.Value)
{
await Connection.SaslErrorMalformedRequest();
LoginAuditor.Fail("Login attempt using malformed request.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
return null;
}
break;
case "p":
Proof = Pair.Value;
break;
}
}
if (!ServerNonceChecked)
{
await Connection.SaslErrorMalformedRequest();
LoginAuditor.Fail("Login attempt using malformed request.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
return null;
}
IAccount Account = await PersistenceLayer.GetAccount(UserName);
if (Account is null)
{
LoginAuditor.Fail("Login attempt using invalid user name.", UserName, Connection.RemoteEndPoint, Connection.Protocol,
new KeyValuePair<string, object>("UserName", UserName));
await Connection.SaslErrorNotAuthorized();
return null;
}
if (!Account.Enabled)
{
LoginAuditor.Fail("Login attempt using disabled account.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
await Connection.SaslErrorAccountDisabled();
return null;
}
byte[] SaltedPassword = this.Hi(Encoding.UTF8.GetBytes(Account.Password.Normalize()), salt, NrIterations);
byte[] ClientKey = this.HMAC(SaltedPassword, clientKey);
byte[] StoredKey = this.H(ClientKey);
StringBuilder sb;
sb = new StringBuilder();
int i;
i = ClientRequest.IndexOf("n=");
if (i < 0)
sb.Append(ClientRequest);
else
sb.Append(ClientRequest.Substring(i));
sb.Append(',');
sb.Append(ServerChallenge);
sb.Append(',');
i = Request.IndexOf(",p=");
if (i < 0)
sb.Append(Request);
else
sb.Append(Request.Substring(0, i));
byte[] AuthenticationMessage = Encoding.UTF8.GetBytes(sb.ToString());
byte[] ClientSignature = this.HMAC(StoredKey, AuthenticationMessage);
byte[] ClientProof = XOR(ClientKey, ClientSignature);
byte[] ServerKey = this.HMAC(SaltedPassword, serverKey);
byte[] ServerSignature = this.HMAC(ServerKey, AuthenticationMessage);
string ClientProofStr = Convert.ToBase64String(ClientProof);
if (Proof == ClientProofStr)
{
await Connection.SetAccount(Account);
string Response = "v=" + Convert.ToBase64String(ServerSignature);
Response = Convert.ToBase64String(Encoding.UTF8.GetBytes(Response));
Connection.ResetState(true);
await Connection.SaslSuccess(Response);
LoginAuditor.Success("Login successful.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
}
else
{
await Connection.SaslErrorNotAuthorized();
LoginAuditor.Fail("Login attempt failed.", UserName, Connection.RemoteEndPoint, Connection.Protocol);
}
return null;
}
private byte[] Hi(byte[] String, byte[] Salt, int NrIterations)
{
byte[] U1 = this.HMAC(String, CONCAT(Salt, One));
byte[] U2 = this.HMAC(String, U1);
byte[] Response = XOR(U1, U2);
while (NrIterations > 2)
{
U1 = U2;
U2 = this.HMAC(String, U1);
Response = XOR(Response, U2);
NrIterations--;
}
return Response;
}
private static readonly byte[] One = new byte[] { 0, 0, 0, 1 };
/// <summary>
/// Performs intitialization of the mechanism. Can be used to set
/// static properties that will be used through-out the runtime of the
/// server.
/// </summary>
public override async Task Initialize()
{
saltBase64 = await GetSaltBase64("XMPP.SCRAM.Server.Salt");
salt = Convert.FromBase64String(saltBase64);
}
/// <summary>
/// Gets a salt value, given a key.
/// </summary>
/// <param name="Key">Salt key.</param>
/// <returns>Base64-encoded salt value.§</returns>
public static async Task<string> GetSaltBase64(string Key)
{
string Result = await RuntimeSettings.GetAsync(Key, string.Empty);
if (string.IsNullOrEmpty(Result))
{
byte[] Bin = new byte[32];
using (RandomNumberGenerator Rnd = RandomNumberGenerator.Create())
{
Rnd.GetBytes(Bin);
}
Result = Convert.ToBase64String(Bin);
await RuntimeSettings.SetAsync(Key, Result);
}
return Result;
}
/// <summary>
/// Authenticates the user using the provided credentials.
/// </summary>
/// <param name="UserName">User Name</param>
/// <param name="Password">Password</param>
/// <param name="Connection">Connection</param>
/// <returns>If authentication was successful or not. If null is returned, the mechanism did not perform authentication.</returns>
public override async Task<bool?> Authenticate(string UserName, string Password, ISaslClientSide Connection)
{
string Nonce = Convert.ToBase64String(GetRandomBytes(16));
string s = "n,,n=" + UserName + ",r=" + Nonce;
byte[] Data = Encoding.UTF8.GetBytes(s);
string Challenge = await Connection.Initiate(this, Convert.ToBase64String(Data));
byte[] ChallengeBinary = Convert.FromBase64String(Challenge);
string ChallengeString = Encoding.UTF8.GetString(ChallengeBinary);
string ServerNonce = null;
string SaltString;
int NrIterations = 0;
byte[] Salt = null;
foreach (KeyValuePair<string, string> Pair in this.ParseCommaSeparatedParameterList(ChallengeString))
{
switch (Pair.Key.ToLower())
{
case "r":
ServerNonce = Pair.Value;
break;
case "s":
SaltString = Pair.Value;
Salt = Convert.FromBase64String(SaltString);
break;
case "i":
NrIterations = int.Parse(Pair.Value);
break;
}
}
if (string.IsNullOrEmpty(ServerNonce) || !ServerNonce.StartsWith(Nonce) || Salt is null || NrIterations <= 0)
return null;
byte[] SaltedPassword = this.Hi(Encoding.UTF8.GetBytes(Password.Normalize()), Salt, NrIterations);
byte[] ClientKey = this.HMAC(SaltedPassword, Encoding.UTF8.GetBytes("Client Key"));
byte[] StoredKey = this.H(ClientKey);
StringBuilder sb;
sb = new StringBuilder();
sb.Append("n=");
sb.Append(UserName);
sb.Append(",r=");
sb.Append(Nonce);
sb.Append(',');
sb.Append(ChallengeString);
sb.Append(",c=biws,r=");
sb.Append(ServerNonce);
byte[] AuthenticationMessage = Encoding.UTF8.GetBytes(sb.ToString());
byte[] ClientSignature = this.HMAC(StoredKey, AuthenticationMessage);
byte[] ClientProof = XOR(ClientKey, ClientSignature);
byte[] ServerKey = this.HMAC(SaltedPassword, Encoding.UTF8.GetBytes("Server Key"));
byte[] ServerSignatureBinary = this.HMAC(ServerKey, AuthenticationMessage);
string ServerSignature = Convert.ToBase64String(ServerSignatureBinary);
sb = new StringBuilder();
sb.Append("c=biws,r="); // biws="n,,"
sb.Append(ServerNonce);
sb.Append(",p=");
sb.Append(Convert.ToBase64String(ClientProof));
string Success = await Connection.FinalResponse(this, Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString())));
if (string.IsNullOrEmpty(Success))
return true;
byte[] ResponseBinary = Convert.FromBase64String(Success);
string ResponseString = Encoding.UTF8.GetString(ResponseBinary);
foreach (KeyValuePair<string, string> Pair in this.ParseCommaSeparatedParameterList(ResponseString))
{
if (string.Compare(Pair.Key, "v", true) == 0)
return (Pair.Value == ServerSignature);
}
return false;
}
}
}