forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_scram_test.go
368 lines (324 loc) · 9.81 KB
/
auth_scram_test.go
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
362
363
364
365
366
367
368
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package server
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"hash"
"strconv"
"strings"
"testing"
"github.com/ortuman/jackal/server/transport"
"github.com/ortuman/jackal/storage/model"
"github.com/ortuman/jackal/util"
"github.com/ortuman/jackal/xml"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/pbkdf2"
)
type scramAuthTestCase struct {
id int
scramType scramType
usesCb bool
cbBytes []byte
gs2BindFlag string
authID string
n string
r string
password string
expectedErr error
}
type scramAuthResult struct {
clientFinalMessage string
v string
}
var tt = []scramAuthTestCase{
// Success cases
{
// SCRAM-SHA-1
id: 1,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "n",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
},
{
id: 2,
scramType: sha256ScramType, // SCRAM-SHA-256
usesCb: false,
gs2BindFlag: "n",
n: "ortuman",
r: "6d805d99-6dc3-4e5a-9a68-653856fc5129",
password: "1234",
},
{
// SCRAM-SHA-1-PLUS
id: 3,
scramType: sha1ScramType,
usesCb: true,
cbBytes: util.RandomBytes(23),
gs2BindFlag: "p=tls-unique",
authID: "a=jackal.im",
n: "ortuman",
r: "7e51aff7-6875-4dce-820a-6d4970635006",
password: "1234",
},
{
// SCRAM-SHA-256-PLUS
id: 4,
scramType: sha256ScramType,
usesCb: true,
cbBytes: util.RandomBytes(32),
gs2BindFlag: "p=tls-unique",
authID: "a=jackal.im",
n: "ortuman",
r: "d712875c-bd3b-4b41-801d-eb9c541d9884",
password: "1234",
},
// Fail cases
{
// invalid user
id: 5,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "n",
n: "mariana",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLNotAuthorized,
},
{
// invalid password
id: 6,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "n",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "12345678",
expectedErr: errSASLNotAuthorized,
},
{
// not authorized gs2BindFlag
id: 7,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "y",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLNotAuthorized,
},
{
// invalid authID
id: 8,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "n",
authID: "b=jackal.im",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLMalformedRequest,
},
{
// not matching gs2BindFlag
id: 9,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "p=tls-unique",
authID: "a=jackal.im",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLNotAuthorized,
},
{
// not matching gs2BindFlag
id: 10,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "q=tls-unique",
authID: "a=jackal.im",
n: "ortuman",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLMalformedRequest,
},
{
// empty username
id: 10,
scramType: sha1ScramType,
usesCb: false,
gs2BindFlag: "n",
authID: "a=jackal.im",
n: "",
r: "bb769406-eaa4-4f38-a279-2b90e596f6dd",
password: "1234",
expectedErr: errSASLMalformedRequest,
},
}
func TestScramMechanisms(t *testing.T) {
testTr := transport.NewMockTransport()
testStrm := authTestSetup(&model.User{Username: "ortuman", Password: "1234"})
defer authTestTeardown()
authr := newScram(testStrm, testTr, sha1ScramType, false)
require.Equal(t, authr.Mechanism(), "SCRAM-SHA-1")
require.False(t, authr.UsesChannelBinding())
authr2 := newScram(testStrm, testTr, sha1ScramType, true)
require.Equal(t, authr2.Mechanism(), "SCRAM-SHA-1-PLUS")
require.True(t, authr2.UsesChannelBinding())
authr3 := newScram(testStrm, testTr, sha256ScramType, false)
require.Equal(t, authr3.Mechanism(), "SCRAM-SHA-256")
require.False(t, authr3.UsesChannelBinding())
authr4 := newScram(testStrm, testTr, sha256ScramType, true)
require.Equal(t, authr4.Mechanism(), "SCRAM-SHA-256-PLUS")
require.True(t, authr4.UsesChannelBinding())
authr5 := newScram(testStrm, testTr, scramType(99), true)
require.Equal(t, authr5.Mechanism(), "")
}
func TestScramBadPayload(t *testing.T) {
testTr := transport.NewMockTransport()
testStrm := authTestSetup(&model.User{Username: "ortuman", Password: "1234"})
defer authTestTeardown()
authr := newScram(testStrm, testTr, sha1ScramType, false)
auth := xml.NewElementNamespace("auth", "urn:ietf:params:xml:ns:xmpp-sasl")
auth.SetAttribute("mechanism", authr.Mechanism())
// empty auth payload
require.Equal(t, errSASLIncorrectEncoding, authr.ProcessElement(auth))
// incorrect auth payload encoding
authr.Reset()
auth.SetText(".")
require.Equal(t, errSASLIncorrectEncoding, authr.ProcessElement(auth))
}
func TestScramSuccessTestCases(t *testing.T) {
for _, tc := range tt {
err := processScramTestCase(t, &tc)
if err != nil {
require.Equal(t, tc.expectedErr, err, fmt.Sprintf("TC identifier: %d", tc.id))
continue
}
}
}
func processScramTestCase(t *testing.T, tc *scramAuthTestCase) error {
tr := transport.NewMockTransport()
if tc.usesCb {
tr.SetChannelBindingBytes(tc.cbBytes)
}
testStrm := authTestSetup(&model.User{Username: "ortuman", Password: "1234"})
defer authTestTeardown()
authr := newScram(testStrm, tr, tc.scramType, tc.usesCb)
auth := xml.NewElementNamespace("auth", saslNamespace)
auth.SetAttribute("mechanism", authr.Mechanism())
clientInitialMessage := fmt.Sprintf(`n=%s,r=%s`, tc.n, tc.r)
gs2Header := fmt.Sprintf(`%s,%s,`, tc.gs2BindFlag, tc.authID)
authPayload := gs2Header + clientInitialMessage
auth.SetText(base64.StdEncoding.EncodeToString([]byte(authPayload)))
err := authr.ProcessElement(auth)
if err != nil {
return err
}
challenge := testStrm.FetchElement()
require.NotNil(t, challenge)
require.Equal(t, "challenge", challenge.Name())
srvInitialMessage, err := base64.StdEncoding.DecodeString(challenge.Text())
require.Nil(t, err)
resp, err := parseScramResponse(challenge.Text())
require.Nil(t, err)
srvNonce := resp["r"]
salt, err := base64.StdEncoding.DecodeString(resp["s"])
require.Nil(t, err)
iterations, _ := strconv.Atoi(resp["i"])
buf := new(bytes.Buffer)
buf.Write([]byte(gs2Header))
if tc.usesCb {
buf.Write(tc.cbBytes)
}
cBytes := base64.StdEncoding.EncodeToString(buf.Bytes())
res := computeScramAuthResult(tc.scramType, clientInitialMessage, string(srvInitialMessage), srvNonce, cBytes, tc.password, salt, iterations)
response := xml.NewElementNamespace("response", saslNamespace)
response.SetText(base64.StdEncoding.EncodeToString([]byte(res.clientFinalMessage)))
err = authr.ProcessElement(response)
if err != nil {
return err
}
success := testStrm.FetchElement()
require.Equal(t, "success", success.Name())
vb64, err := base64.StdEncoding.DecodeString(success.Text())
require.Nil(t, err)
require.Equal(t, res.v, string(vb64))
require.True(t, authr.Authenticated())
require.Equal(t, tc.n, authr.Username())
require.Nil(t, authr.ProcessElement(auth)) // test already authenticated...
return nil
}
func computeScramAuthResult(scramType scramType, clientInitialMessage, serverInitialMessage, srvNonce, cBytes, password string, salt []byte, iterations int) *scramAuthResult {
clientFinalMessageBare := fmt.Sprintf("c=%s,r=%s", cBytes, srvNonce)
saltedPassword := testScramAuthPbkdf2([]byte(password), salt, scramType, iterations)
clientKey := testScramAuthHmac([]byte("Client Key"), saltedPassword, scramType)
storedKey := testScramAuthHash(clientKey, scramType)
authMessage := clientInitialMessage + "," + serverInitialMessage + "," + clientFinalMessageBare
clientSignature := testScramAuthHmac([]byte(authMessage), storedKey, scramType)
clientProof := make([]byte, len(clientKey))
for i := 0; i < len(clientKey); i++ {
clientProof[i] = clientKey[i] ^ clientSignature[i]
}
serverKey := testScramAuthHmac([]byte("Server Key"), saltedPassword, scramType)
serverSignature := testScramAuthHmac([]byte(authMessage), serverKey, scramType)
res := &scramAuthResult{}
res.clientFinalMessage = clientFinalMessageBare + ",p=" + base64.StdEncoding.EncodeToString(clientProof)
res.v = "v=" + base64.StdEncoding.EncodeToString(serverSignature)
return res
}
func parseScramResponse(b64 string) (map[string]string, error) {
s, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return nil, err
}
ret := map[string]string{}
s1 := strings.Split(string(s), ",")
for _, s0 := range s1 {
k, v := util.SplitKeyAndValue(s0, '=')
ret[k] = v
}
return ret, nil
}
func testScramAuthPbkdf2(b []byte, salt []byte, scramType scramType, iterationCount int) []byte {
switch scramType {
case sha1ScramType:
return pbkdf2.Key(b, salt, iterationCount, sha1.Size, sha1.New)
case sha256ScramType:
return pbkdf2.Key(b, salt, iterationCount, sha256.Size, sha256.New)
}
return nil
}
func testScramAuthHmac(b []byte, key []byte, scramType scramType) []byte {
var h func() hash.Hash
switch scramType {
case sha1ScramType:
h = sha1.New
case sha256ScramType:
h = sha256.New
}
m := hmac.New(h, key)
m.Write(b)
return m.Sum(nil)
}
func testScramAuthHash(b []byte, scramType scramType) []byte {
var h hash.Hash
switch scramType {
case sha1ScramType:
h = sha1.New()
case sha256ScramType:
h = sha256.New()
}
h.Write(b)
return h.Sum(nil)
}