/
mongodb_scram.js
250 lines (211 loc) · 7.48 KB
/
mongodb_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
var DbCommand = require('../commands/db_command').DbCommand
, utils = require('../utils')
, crypto = require('crypto')
, Binary = require('bson').Binary
, f = require('util').format;
var authenticate = function(db, username, password, authdb, options, callback) {
var numberOfConnections = 0;
var errorObject = null;
var numberOfValidConnections = 0;
var credentialsValid = false;
options = options || {};
// Grab all the connections
var connections = options['connection'] != null ? [options['connection']] : db.serverConfig.allRawConnections().slice(0);
if(connections.length > 1) {
options['onAll'] = true;
}
// Total connections
var count = connections.length;
if(count == 0) return callback(null, null);
// Valid connections
var numberOfValidConnections = 0;
var credentialsValid = false;
var errorObject = null;
// For each connection we need to authenticate
while(connections.length > 0) {
// Execute MongoCR
var executeScram = function(connection) {
// Clean up the user
username = username.replace('=', "=3D").replace(',', '=2C');
// Create a random nonce
var nonce = crypto.randomBytes(24).toString('base64');
// var nonce = 'MsQUY9iw0T9fx2MUEz6LZPwGuhVvWAhc'
var firstBare = f("n=%s,r=%s", username, nonce);
// Build command structure
var cmd = {
saslStart: 1
, mechanism: 'SCRAM-SHA-1'
, payload: new Binary(f("n,,%s", firstBare))
, autoAuthorize: 1
}
// Handle the error
var handleError = function(err, r) {
if(err) {
errorObject = err; return false;
} else if(r['$err']) {
errorObject = r; return false;
} else if(r['errmsg']) {
errorObject = r; return false;
} else {
credentialsValid = true;
numberOfValidConnections = numberOfValidConnections + 1;
}
return true
}
// Finish up
var finish = function(_count, _numberOfValidConnections) {
if(_count == 0 && _numberOfValidConnections > 0) {
db.serverConfig.auth.add('SCRAM-SHA-1', db.databaseName, username, password, authdb);
// Return correct authentication
return callback(null, true);
} else if(_count == 0) {
if(errorObject == null) errorObject = utils.toError(f("failed to authenticate using scram"));
return callback(errorObject, false);
}
}
var handleEnd = function(_err, _r) {
// Handle any error
handleError(_err, _r)
// Adjust the number of connections
count = count - 1;
// Execute the finish
finish(count, numberOfValidConnections);
}
// Execute start sasl command
db.db(authdb).command(cmd, { connection: connection }, function(err, r) {
// Do we have an error, handle it
if(handleError(err, r) == false) {
count = count - 1;
if(count == 0 && numberOfValidConnections > 0) {
// Store the auth details
addAuthSession(new AuthSession(db, username, password));
// Return correct authentication
return callback(null, true);
} else if(count == 0) {
if(errorObject == null) errorObject = utils.toError(f("failed to authenticate using scram"));
return callback(errorObject, false);
}
return;
}
// Get the dictionary
var dict = parsePayload(r.payload.value())
// Unpack dictionary
var iterations = parseInt(dict.i, 10);
var salt = dict.s;
var rnonce = dict.r;
// Set up start of proof
var withoutProof = f("c=biws,r=%s", rnonce);
var passwordDig = passwordDigest(username, password);
var saltedPassword = hi(passwordDig
, new Buffer(salt, 'base64')
, iterations);
// Create the client key
var hmac = crypto.createHmac('sha1', saltedPassword);
hmac.update(new Buffer("Client Key"));
var clientKey = hmac.digest();
// Create the stored key
var hash = crypto.createHash('sha1');
hash.update(clientKey);
var storedKey = hash.digest();
// Create the authentication message
var authMsg = [firstBare, r.payload.value().toString('base64'), withoutProof].join(',');
// Create client signature
var hmac = crypto.createHmac('sha1', storedKey);
hmac.update(new Buffer(authMsg));
var clientSig = hmac.digest();
// Create client proof
var clientProof = f("p=%s", new Buffer(xor(clientKey, clientSig)).toString('base64'));
// Create client final
var clientFinal = [withoutProof, clientProof].join(',');
// Generate server key
var hmac = crypto.createHmac('sha1', saltedPassword);
hmac.update(new Buffer('Server Key'))
var serverKey = hmac.digest();
// Generate server signature
var hmac = crypto.createHmac('sha1', serverKey);
hmac.update(new Buffer(authMsg))
var serverSig = hmac.digest();
//
// Create continue message
var cmd = {
saslContinue: 1
, conversationId: r.conversationId
, payload: new Binary(new Buffer(clientFinal))
}
//
// Execute sasl continue
db.db(authdb).command(cmd, { connection: connection }, function(err, r) {
if(r.done == false) {
var cmd = {
saslContinue: 1
, conversationId: r.conversationId
, payload: new Buffer(0)
}
db.db(authdb).command(cmd, { connection: connection }, function(err, r) {
handleEnd(err, r);
});
} else {
handleEnd(err, r);
}
});
});
}
// Get the connection
executeScram(connections.shift());
}
}
var parsePayload = function(payload) {
var dict = {};
var parts = payload.split(',');
for(var i = 0; i < parts.length; i++) {
var valueParts = parts[i].split('=');
dict[valueParts[0]] = valueParts[1];
}
return dict;
}
var passwordDigest = function(username, password) {
if(typeof username != 'string') throw utils.toError("username must be a string");
if(typeof password != 'string') throw utils.toError("password must be a string");
if(password.length == 0) throw utils.toError("password cannot be empty");
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ":mongo:" + password);
return md5.digest('hex');
}
// XOR two buffers
var xor = function(a, b) {
if (!Buffer.isBuffer(a)) a = new Buffer(a)
if (!Buffer.isBuffer(b)) b = new Buffer(b)
var res = []
if (a.length > b.length) {
for (var i = 0; i < b.length; i++) {
res.push(a[i] ^ b[i])
}
} else {
for (var i = 0; i < a.length; i++) {
res.push(a[i] ^ b[i])
}
}
return new Buffer(res);
}
// Create a final digest
var hi = function(data, salt, iterations) {
// Create digest
var digest = function(msg) {
var hmac = crypto.createHmac('sha1', data);
hmac.update(msg);
var result = hmac.digest()
return result;
}
// Create variables
salt = Buffer.concat([salt, new Buffer('\x00\x00\x00\x01')])
var ui = digest(salt);
var u1 = ui;
for(var i = 0; i < iterations - 1; i++) {
u1 = digest(u1);
ui = xor(ui, u1);
}
return ui;
}
exports.authenticate = authenticate;