forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsasl-scram-tests.js
314 lines (277 loc) · 9.4 KB
/
sasl-scram-tests.js
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
'use strict'
const helper = require('./test-helper')
const assert = require('assert')
var sasl = require('../../../lib/crypto/sasl')
const suite = new helper.Suite()
suite.test('sasl/scram', function () {
suite.test('startSession', function () {
suite.test('fails when mechanisms does not include SCRAM-SHA-256', function () {
assert.throws(
function () {
sasl.startSession([])
},
{
message: 'SASL: Only mechanism(s) SCRAM-SHA-256 are supported',
}
)
})
suite.test('returns expected session data for SCRAM-SHA-256 (channel binding disabled, offered)', function () {
const session = sasl.startSession(['SCRAM-SHA-256', 'SCRAM-SHA-256-PLUS'])
assert.equal(session.mechanism, 'SCRAM-SHA-256')
assert.equal(String(session.clientNonce).length, 24)
assert.equal(session.message, 'SASLInitialResponse')
assert(session.response.match(/^n,,n=\*,r=.{24}$/))
})
suite.test('returns expected session data for SCRAM-SHA-256 (channel binding enabled, not offered)', function () {
const session = sasl.startSession(['SCRAM-SHA-256'], { getPeerCertificate() {} })
assert.equal(session.mechanism, 'SCRAM-SHA-256')
assert.equal(String(session.clientNonce).length, 24)
assert.equal(session.message, 'SASLInitialResponse')
assert(session.response.match(/^y,,n=\*,r=.{24}$/))
})
suite.test('returns expected session data for SCRAM-SHA-256 (channel binding enabled, offered)', function () {
const session = sasl.startSession(['SCRAM-SHA-256', 'SCRAM-SHA-256-PLUS'], { getPeerCertificate() {} })
assert.equal(session.mechanism, 'SCRAM-SHA-256-PLUS')
assert.equal(String(session.clientNonce).length, 24)
assert.equal(session.message, 'SASLInitialResponse')
assert(session.response.match(/^p=tls-server-end-point,,n=\*,r=.{24}$/))
})
suite.test('creates random nonces', function () {
const session1 = sasl.startSession(['SCRAM-SHA-256'])
const session2 = sasl.startSession(['SCRAM-SHA-256'])
assert(session1.clientNonce != session2.clientNonce)
})
})
suite.test('continueSession', function () {
suite.testAsync('fails when last session message was not SASLInitialResponse', async function () {
assert.rejects(
function () {
return sasl.continueSession({}, '', '')
},
{
message: 'SASL: Last message was not SASLInitialResponse',
}
)
})
suite.testAsync('fails when nonce is missing in server message', function () {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
},
'bad-password',
's=1,i=1'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing',
}
)
})
suite.testAsync('fails when salt is missing in server message', function () {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
},
'bad-password',
'r=1,i=1'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing',
}
)
})
suite.testAsync('fails when client password is not a string', function () {
for (const badPasswordValue of [null, undefined, 123, new Date(), {}]) {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
clientNonce: 'a',
},
badPasswordValue,
'r=1,i=1'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string',
}
)
}
})
suite.testAsync('fails when client password is an empty string', function () {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
clientNonce: 'a',
},
'',
'r=1,i=1'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a non-empty string',
}
)
})
suite.testAsync('fails when iteration is missing in server message', function () {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
},
'bad-password',
'r=1,s=abcd'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing',
}
)
})
suite.testAsync('fails when server nonce does not start with client nonce', function () {
assert.rejects(
function () {
return sasl.continueSession(
{
message: 'SASLInitialResponse',
clientNonce: '2',
},
'bad-password',
'r=1,s=abcd,i=1'
)
},
{
message: 'SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce does not start with client nonce',
}
)
})
suite.testAsync('sets expected session data (SCRAM-SHA-256)', async function () {
const session = {
message: 'SASLInitialResponse',
clientNonce: 'a',
}
await sasl.continueSession(session, 'password', 'r=ab,s=abcd,i=1')
assert.equal(session.message, 'SASLResponse')
assert.equal(session.serverSignature, 'jwt97IHWFn7FEqHykPTxsoQrKGOMXJl/PJyJ1JXTBKc=')
assert.equal(session.response, 'c=biws,r=ab,p=mU8grLfTjDrJer9ITsdHk0igMRDejG10EJPFbIBL3D0=')
})
suite.testAsync('sets expected session data (SCRAM-SHA-256, channel binding enabled)', async function () {
const session = {
message: 'SASLInitialResponse',
clientNonce: 'a',
}
await sasl.continueSession(session, 'password', 'r=ab,s=abcd,i=1', { getPeerCertificate() {} })
assert.equal(session.message, 'SASLResponse')
assert.equal(session.serverSignature, 'ETpURSc5OpddrPRSW3LaDPJzUzhh+rciM4uYwXSsohU=')
assert.equal(session.response, 'c=eSws,r=ab,p=YVTEOwOD7khu/NulscjFegHrZoTXJBFI/7L61AN9khc=')
})
suite.testAsync('sets expected session data (SCRAM-SHA-256-PLUS)', async function () {
const session = {
message: 'SASLInitialResponse',
mechanism: 'SCRAM-SHA-256-PLUS',
clientNonce: 'a',
}
await sasl.continueSession(session, 'password', 'r=ab,s=abcd,i=1', {
getPeerCertificate() {
return {
raw: Buffer.from([
// a minimal ASN.1 certificate structure which can be parsed for a hash type
0x30, // cert ASN.1 seq
0x16, // cert length (all bytes below)
0x30, // cert info ASN.1 seq
0x01, // cert info length
0x00, // cert info (skipped)
0x30, // signature algorithm ASN.1 seq
0x0d, // signature algorithm length
0x06, // ASN.1 OID
0x09, // OID length
0x2a, // OID: 1.2.840.113549.1.1.11 (RSASSA-PKCS1-v1_5 / SHA-256)
0x86,
0x48,
0x86,
0xf7,
0x0d,
0x01,
0x01,
0x0b,
0x05, // ASN.1 null (no algorithm parameters)
0x00, // null length
0x03, // ASN.1 bitstring (signature)
0x02, // bitstring length
0x00, // zero right-padding bits
0xff, // one-byte signature
]),
}
},
})
assert.equal(session.message, 'SASLResponse')
assert.equal(session.serverSignature, 'pU1hc6JkjvjO8Wd+o0/jyGjc1DpITtsx1UF+ZPa5u5M=')
assert.equal(
session.response,
'c=cD10bHMtc2VydmVyLWVuZC1wb2ludCwsmwepqKDDRcOvo3BN0rplYMfLUTpbaf38btkM5aAXBhQ=,r=ab,p=j0v2LsthoNaIBrKV4YipskF/lV8zWEt6acNRtt99MA4='
)
})
})
suite.test('finalizeSession', function () {
suite.test('fails when last session message was not SASLResponse', function () {
assert.throws(
function () {
sasl.finalizeSession({})
},
{
message: 'SASL: Last message was not SASLResponse',
}
)
})
suite.test('fails when server signature is not valid base64', function () {
assert.throws(
function () {
sasl.finalizeSession(
{
message: 'SASLResponse',
serverSignature: 'abcd',
},
'v=x1' // Purposefully invalid base64
)
},
{
message: 'SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature must be base64',
}
)
})
suite.test('fails when server signature does not match', function () {
assert.throws(
function () {
sasl.finalizeSession(
{
message: 'SASLResponse',
serverSignature: 'abcd',
},
'v=xyzq'
)
},
{
message: 'SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature does not match',
}
)
})
suite.test('does not fail when eveything is ok', function () {
sasl.finalizeSession(
{
message: 'SASLResponse',
serverSignature: 'abcd',
},
'v=abcd'
)
})
})
})