-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
scram.js
326 lines (281 loc) · 8.62 KB
/
scram.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
315
316
317
318
319
320
321
322
323
324
325
326
const crypto = require('crypto')
const scram = require('../../protocol/sasl/scram')
const { KafkaJSSASLAuthenticationError, KafkaJSNonRetriableError } = require('../../errors')
const GS2_HEADER = 'n,,'
const EQUAL_SIGN_REGEX = /=/g
const COMMA_SIGN_REGEX = /,/g
const URLSAFE_BASE64_PLUS_REGEX = /\+/g
const URLSAFE_BASE64_SLASH_REGEX = /\//g
const URLSAFE_BASE64_TRAILING_EQUAL_REGEX = /=+$/
const HMAC_CLIENT_KEY = 'Client Key'
const HMAC_SERVER_KEY = 'Server Key'
const DIGESTS = {
SHA256: {
length: 32,
type: 'sha256',
minIterations: 4096,
},
SHA512: {
length: 64,
type: 'sha512',
minIterations: 4096,
},
}
const encode64 = str => Buffer.from(str).toString('base64')
class SCRAM {
/**
* From https://tools.ietf.org/html/rfc5802#section-5.1
*
* The characters ',' or '=' in usernames are sent as '=2C' and
* '=3D' respectively. If the server receives a username that
* contains '=' not followed by either '2C' or '3D', then the
* server MUST fail the authentication.
*
* @returns {String}
*/
static sanitizeString(str) {
return str.replace(EQUAL_SIGN_REGEX, '=3D').replace(COMMA_SIGN_REGEX, '=2C')
}
/**
* In cryptography, a nonce is an arbitrary number that can be used just once.
* It is similar in spirit to a nonce * word, hence the name. It is often a random or pseudo-random
* number issued in an authentication protocol to * ensure that old communications cannot be reused
* in replay attacks.
*
* @returns {String}
*/
static nonce() {
return crypto
.randomBytes(16)
.toString('base64')
.replace(URLSAFE_BASE64_PLUS_REGEX, '-') // make it url safe
.replace(URLSAFE_BASE64_SLASH_REGEX, '_')
.replace(URLSAFE_BASE64_TRAILING_EQUAL_REGEX, '')
.toString('ascii')
}
/**
* Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the
* pseudorandom function (PRF) and with dkLen == output length of
* HMAC() == output length of H()
*
* @returns {Promise<Buffer>}
*/
static hi(password, salt, iterations, digestDefinition) {
return new Promise((resolve, reject) => {
crypto.pbkdf2(
password,
salt,
iterations,
digestDefinition.length,
digestDefinition.type,
(err, derivedKey) => (err ? reject(err) : resolve(derivedKey))
)
})
}
/**
* Apply the exclusive-or operation to combine the octet string
* on the left of this operator with the octet string on the right of
* this operator. The length of the output and each of the two
* inputs will be the same for this use
*
* @returns {Buffer}
*/
static xor(left, right) {
const bufferA = Buffer.from(left)
const bufferB = Buffer.from(right)
const length = Buffer.byteLength(bufferA)
if (length !== Buffer.byteLength(bufferB)) {
throw new KafkaJSNonRetriableError('Buffers must be of the same length')
}
const result = []
for (let i = 0; i < length; i++) {
result.push(bufferA[i] ^ bufferB[i])
}
return Buffer.from(result)
}
/**
* @param {Connection} connection
* @param {Logger} logger
* @param {Function} saslAuthenticate
* @param {DigestDefinition} digestDefinition
*/
constructor(connection, logger, saslAuthenticate, digestDefinition) {
this.connection = connection
this.logger = logger
this.saslAuthenticate = saslAuthenticate
this.digestDefinition = digestDefinition
const digestType = digestDefinition.type.toUpperCase()
this.PREFIX = `SASL SCRAM ${digestType} authentication`
this.currentNonce = SCRAM.nonce()
}
async authenticate() {
const { PREFIX } = this
const { host, port, sasl } = this.connection
const broker = `${host}:${port}`
if (sasl.username == null || sasl.password == null) {
throw new KafkaJSSASLAuthenticationError(`${this.PREFIX}: Invalid username or password`)
}
try {
this.logger.debug('Exchanging first client message', { broker })
const clientMessageResponse = await this.sendClientFirstMessage()
this.logger.debug('Sending final message', { broker })
const finalResponse = await this.sendClientFinalMessage(clientMessageResponse)
if (finalResponse.e) {
throw new Error(finalResponse.e)
}
const serverKey = await this.serverKey(clientMessageResponse)
const serverSignature = this.serverSignature(serverKey, clientMessageResponse)
if (finalResponse.v !== serverSignature) {
throw new Error('Invalid server signature in server final message')
}
this.logger.debug(`${PREFIX} successful`, { broker })
} catch (e) {
const error = new KafkaJSSASLAuthenticationError(`${PREFIX} failed: ${e.message}`)
this.logger.error(error.message, { broker })
throw error
}
}
/**
* @private
*/
async sendClientFirstMessage() {
const clientFirstMessage = `${GS2_HEADER}${this.firstMessageBare()}`
const request = scram.firstMessage.request({ clientFirstMessage })
const response = scram.firstMessage.response
return this.saslAuthenticate({
authExpectResponse: true,
request,
response,
})
}
/**
* @private
*/
async sendClientFinalMessage(clientMessageResponse) {
const { PREFIX } = this
const iterations = parseInt(clientMessageResponse.i, 10)
const { minIterations } = this.digestDefinition
if (!clientMessageResponse.r.startsWith(this.currentNonce)) {
throw new KafkaJSSASLAuthenticationError(
`${PREFIX} failed: Invalid server nonce, it does not start with the client nonce`
)
}
if (iterations < minIterations) {
throw new KafkaJSSASLAuthenticationError(
`${PREFIX} failed: Requested iterations ${iterations} is less than the minimum ${minIterations}`
)
}
const finalMessageWithoutProof = this.finalMessageWithoutProof(clientMessageResponse)
const clientProof = await this.clientProof(clientMessageResponse)
const finalMessage = `${finalMessageWithoutProof},p=${clientProof}`
const request = scram.finalMessage.request({ finalMessage })
const response = scram.finalMessage.response
return this.saslAuthenticate({
authExpectResponse: true,
request,
response,
})
}
/**
* @private
*/
async clientProof(clientMessageResponse) {
const clientKey = await this.clientKey(clientMessageResponse)
const storedKey = this.H(clientKey)
const clientSignature = this.clientSignature(storedKey, clientMessageResponse)
return encode64(SCRAM.xor(clientKey, clientSignature))
}
/**
* @private
*/
async clientKey(clientMessageResponse) {
const saltedPassword = await this.saltPassword(clientMessageResponse)
return this.HMAC(saltedPassword, HMAC_CLIENT_KEY)
}
/**
* @private
*/
async serverKey(clientMessageResponse) {
const saltedPassword = await this.saltPassword(clientMessageResponse)
return this.HMAC(saltedPassword, HMAC_SERVER_KEY)
}
/**
* @private
*/
clientSignature(storedKey, clientMessageResponse) {
return this.HMAC(storedKey, this.authMessage(clientMessageResponse))
}
/**
* @private
*/
serverSignature(serverKey, clientMessageResponse) {
return encode64(this.HMAC(serverKey, this.authMessage(clientMessageResponse)))
}
/**
* @private
*/
authMessage(clientMessageResponse) {
return [
this.firstMessageBare(),
clientMessageResponse.original,
this.finalMessageWithoutProof(clientMessageResponse),
].join(',')
}
/**
* @private
*/
async saltPassword(clientMessageResponse) {
const salt = Buffer.from(clientMessageResponse.s, 'base64')
const iterations = parseInt(clientMessageResponse.i, 10)
return SCRAM.hi(this.encodedPassword(), salt, iterations, this.digestDefinition)
}
/**
* @private
*/
firstMessageBare() {
return `n=${this.encodedUsername()},r=${this.currentNonce}`
}
/**
* @private
*/
finalMessageWithoutProof(clientMessageResponse) {
const rnonce = clientMessageResponse.r
return `c=${encode64(GS2_HEADER)},r=${rnonce}`
}
/**
* @private
*/
encodedUsername() {
const { username } = this.connection.sasl
return SCRAM.sanitizeString(username).toString('utf-8')
}
/**
* @private
*/
encodedPassword() {
const { password } = this.connection.sasl
return password.toString('utf-8')
}
/**
* @private
*/
H(data) {
return crypto
.createHash(this.digestDefinition.type)
.update(data)
.digest()
}
/**
* @private
*/
HMAC(key, data) {
return crypto
.createHmac(this.digestDefinition.type, key)
.update(data)
.digest()
}
}
module.exports = {
DIGESTS,
SCRAM,
}