Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scram.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. 'use strict';
  2. var f = require('util').format,
  3. crypto = require('crypto'),
  4. retrieveBSON = require('../connection/utils').retrieveBSON,
  5. Query = require('../connection/commands').Query,
  6. MongoError = require('../error').MongoError,
  7. Buffer = require('safe-buffer').Buffer;
  8. let saslprep;
  9. try {
  10. saslprep = require('saslprep');
  11. } catch (e) {
  12. // don't do anything;
  13. }
  14. var BSON = retrieveBSON(),
  15. Binary = BSON.Binary;
  16. var AuthSession = function(db, username, password) {
  17. this.db = db;
  18. this.username = username;
  19. this.password = password;
  20. };
  21. AuthSession.prototype.equal = function(session) {
  22. return (
  23. session.db === this.db &&
  24. session.username === this.username &&
  25. session.password === this.password
  26. );
  27. };
  28. var id = 0;
  29. /**
  30. * Creates a new ScramSHA authentication mechanism
  31. * @class
  32. * @return {ScramSHA} A cursor instance
  33. */
  34. var ScramSHA = function(bson, cryptoMethod) {
  35. this.bson = bson;
  36. this.authStore = [];
  37. this.id = id++;
  38. this.cryptoMethod = cryptoMethod || 'sha1';
  39. };
  40. var parsePayload = function(payload) {
  41. var dict = {};
  42. var parts = payload.split(',');
  43. for (var i = 0; i < parts.length; i++) {
  44. var valueParts = parts[i].split('=');
  45. dict[valueParts[0]] = valueParts[1];
  46. }
  47. return dict;
  48. };
  49. var passwordDigest = function(username, password) {
  50. if (typeof username !== 'string') throw new MongoError('username must be a string');
  51. if (typeof password !== 'string') throw new MongoError('password must be a string');
  52. if (password.length === 0) throw new MongoError('password cannot be empty');
  53. // Use node md5 generator
  54. var md5 = crypto.createHash('md5');
  55. // Generate keys used for authentication
  56. md5.update(username + ':mongo:' + password, 'utf8');
  57. return md5.digest('hex');
  58. };
  59. // XOR two buffers
  60. function xor(a, b) {
  61. if (!Buffer.isBuffer(a)) a = Buffer.from(a);
  62. if (!Buffer.isBuffer(b)) b = Buffer.from(b);
  63. const length = Math.max(a.length, b.length);
  64. const res = [];
  65. for (let i = 0; i < length; i += 1) {
  66. res.push(a[i] ^ b[i]);
  67. }
  68. return Buffer.from(res).toString('base64');
  69. }
  70. function H(method, text) {
  71. return crypto
  72. .createHash(method)
  73. .update(text)
  74. .digest();
  75. }
  76. function HMAC(method, key, text) {
  77. return crypto
  78. .createHmac(method, key)
  79. .update(text)
  80. .digest();
  81. }
  82. var _hiCache = {};
  83. var _hiCacheCount = 0;
  84. var _hiCachePurge = function() {
  85. _hiCache = {};
  86. _hiCacheCount = 0;
  87. };
  88. const hiLengthMap = {
  89. sha256: 32,
  90. sha1: 20
  91. };
  92. function HI(data, salt, iterations, cryptoMethod) {
  93. // omit the work if already generated
  94. const key = [data, salt.toString('base64'), iterations].join('_');
  95. if (_hiCache[key] !== undefined) {
  96. return _hiCache[key];
  97. }
  98. // generate the salt
  99. const saltedData = crypto.pbkdf2Sync(
  100. data,
  101. salt,
  102. iterations,
  103. hiLengthMap[cryptoMethod],
  104. cryptoMethod
  105. );
  106. // cache a copy to speed up the next lookup, but prevent unbounded cache growth
  107. if (_hiCacheCount >= 200) {
  108. _hiCachePurge();
  109. }
  110. _hiCache[key] = saltedData;
  111. _hiCacheCount += 1;
  112. return saltedData;
  113. }
  114. /**
  115. * Authenticate
  116. * @method
  117. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  118. * @param {[]Connections} connections Connections to authenticate using this authenticator
  119. * @param {string} db Name of the database
  120. * @param {string} username Username
  121. * @param {string} password Password
  122. * @param {authResultCallback} callback The callback to return the result from the authentication
  123. * @return {object}
  124. */
  125. ScramSHA.prototype.auth = function(server, connections, db, username, password, callback) {
  126. var self = this;
  127. // Total connections
  128. var count = connections.length;
  129. if (count === 0) return callback(null, null);
  130. // Valid connections
  131. var numberOfValidConnections = 0;
  132. var errorObject = null;
  133. const cryptoMethod = this.cryptoMethod;
  134. let mechanism = 'SCRAM-SHA-1';
  135. let processedPassword;
  136. if (cryptoMethod === 'sha256') {
  137. mechanism = 'SCRAM-SHA-256';
  138. let saslprepFn = (server.s && server.s.saslprep) || saslprep;
  139. if (saslprepFn) {
  140. processedPassword = saslprepFn(password);
  141. } else {
  142. console.warn('Warning: no saslprep library specified. Passwords will not be sanitized');
  143. processedPassword = password;
  144. }
  145. } else {
  146. processedPassword = passwordDigest(username, password);
  147. }
  148. // Execute MongoCR
  149. var executeScram = function(connection) {
  150. // Clean up the user
  151. username = username.replace('=', '=3D').replace(',', '=2C');
  152. // Create a random nonce
  153. var nonce = crypto.randomBytes(24).toString('base64');
  154. // var nonce = 'MsQUY9iw0T9fx2MUEz6LZPwGuhVvWAhc'
  155. // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
  156. // Since the username is not sasl-prep-d, we need to do this here.
  157. const firstBare = Buffer.concat([
  158. Buffer.from('n=', 'utf8'),
  159. Buffer.from(username, 'utf8'),
  160. Buffer.from(',r=', 'utf8'),
  161. Buffer.from(nonce, 'utf8')
  162. ]);
  163. // Build command structure
  164. var cmd = {
  165. saslStart: 1,
  166. mechanism: mechanism,
  167. payload: new Binary(Buffer.concat([Buffer.from('n,,', 'utf8'), firstBare])),
  168. autoAuthorize: 1
  169. };
  170. // Handle the error
  171. var handleError = function(err, r) {
  172. if (err) {
  173. numberOfValidConnections = numberOfValidConnections - 1;
  174. errorObject = err;
  175. return false;
  176. } else if (r.result['$err']) {
  177. errorObject = r.result;
  178. return false;
  179. } else if (r.result['errmsg']) {
  180. errorObject = r.result;
  181. return false;
  182. } else {
  183. numberOfValidConnections = numberOfValidConnections + 1;
  184. }
  185. return true;
  186. };
  187. // Finish up
  188. var finish = function(_count, _numberOfValidConnections) {
  189. if (_count === 0 && _numberOfValidConnections > 0) {
  190. // Store the auth details
  191. addAuthSession(self.authStore, new AuthSession(db, username, password));
  192. // Return correct authentication
  193. return callback(null, true);
  194. } else if (_count === 0) {
  195. if (errorObject == null)
  196. errorObject = new MongoError(f('failed to authenticate using scram'));
  197. return callback(errorObject, false);
  198. }
  199. };
  200. var handleEnd = function(_err, _r) {
  201. // Handle any error
  202. handleError(_err, _r);
  203. // Adjust the number of connections
  204. count = count - 1;
  205. // Execute the finish
  206. finish(count, numberOfValidConnections);
  207. };
  208. // Write the commmand on the connection
  209. server(
  210. connection,
  211. new Query(self.bson, f('%s.$cmd', db), cmd, {
  212. numberToSkip: 0,
  213. numberToReturn: 1
  214. }),
  215. function(err, r) {
  216. // Do we have an error, handle it
  217. if (handleError(err, r) === false) {
  218. count = count - 1;
  219. if (count === 0 && numberOfValidConnections > 0) {
  220. // Store the auth details
  221. addAuthSession(self.authStore, new AuthSession(db, username, password));
  222. // Return correct authentication
  223. return callback(null, true);
  224. } else if (count === 0) {
  225. if (errorObject == null)
  226. errorObject = new MongoError(f('failed to authenticate using scram'));
  227. return callback(errorObject, false);
  228. }
  229. return;
  230. }
  231. // Get the dictionary
  232. var dict = parsePayload(r.result.payload.value());
  233. // Unpack dictionary
  234. var iterations = parseInt(dict.i, 10);
  235. var salt = dict.s;
  236. var rnonce = dict.r;
  237. // Set up start of proof
  238. var withoutProof = f('c=biws,r=%s', rnonce);
  239. var saltedPassword = HI(
  240. processedPassword,
  241. Buffer.from(salt, 'base64'),
  242. iterations,
  243. cryptoMethod
  244. );
  245. if (iterations && iterations < 4096) {
  246. const error = new MongoError(`Server returned an invalid iteration count ${iterations}`);
  247. return callback(error, false);
  248. }
  249. // Create the client key
  250. const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
  251. // Create the stored key
  252. const storedKey = H(cryptoMethod, clientKey);
  253. // Create the authentication message
  254. const authMessage = [
  255. firstBare,
  256. r.result.payload.value().toString('base64'),
  257. withoutProof
  258. ].join(',');
  259. // Create client signature
  260. const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
  261. // Create client proof
  262. const clientProof = f('p=%s', xor(clientKey, clientSignature));
  263. // Create client final
  264. const clientFinal = [withoutProof, clientProof].join(',');
  265. // Create continue message
  266. const cmd = {
  267. saslContinue: 1,
  268. conversationId: r.result.conversationId,
  269. payload: new Binary(Buffer.from(clientFinal))
  270. };
  271. //
  272. // Execute sasl continue
  273. // Write the commmand on the connection
  274. server(
  275. connection,
  276. new Query(self.bson, f('%s.$cmd', db), cmd, {
  277. numberToSkip: 0,
  278. numberToReturn: 1
  279. }),
  280. function(err, r) {
  281. if (r && r.result.done === false) {
  282. var cmd = {
  283. saslContinue: 1,
  284. conversationId: r.result.conversationId,
  285. payload: Buffer.alloc(0)
  286. };
  287. // Write the commmand on the connection
  288. server(
  289. connection,
  290. new Query(self.bson, f('%s.$cmd', db), cmd, {
  291. numberToSkip: 0,
  292. numberToReturn: 1
  293. }),
  294. function(err, r) {
  295. handleEnd(err, r);
  296. }
  297. );
  298. } else {
  299. handleEnd(err, r);
  300. }
  301. }
  302. );
  303. }
  304. );
  305. };
  306. var _execute = function(_connection) {
  307. process.nextTick(function() {
  308. executeScram(_connection);
  309. });
  310. };
  311. // For each connection we need to authenticate
  312. while (connections.length > 0) {
  313. _execute(connections.shift());
  314. }
  315. };
  316. // Add to store only if it does not exist
  317. var addAuthSession = function(authStore, session) {
  318. var found = false;
  319. for (var i = 0; i < authStore.length; i++) {
  320. if (authStore[i].equal(session)) {
  321. found = true;
  322. break;
  323. }
  324. }
  325. if (!found) authStore.push(session);
  326. };
  327. /**
  328. * Remove authStore credentials
  329. * @method
  330. * @param {string} db Name of database we are removing authStore details about
  331. * @return {object}
  332. */
  333. ScramSHA.prototype.logout = function(dbName) {
  334. this.authStore = this.authStore.filter(function(x) {
  335. return x.db !== dbName;
  336. });
  337. };
  338. /**
  339. * Re authenticate pool
  340. * @method
  341. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  342. * @param {[]Connections} connections Connections to authenticate using this authenticator
  343. * @param {authResultCallback} callback The callback to return the result from the authentication
  344. * @return {object}
  345. */
  346. ScramSHA.prototype.reauthenticate = function(server, connections, callback) {
  347. var authStore = this.authStore.slice(0);
  348. var count = authStore.length;
  349. // No connections
  350. if (count === 0) return callback(null, null);
  351. // Iterate over all the auth details stored
  352. for (var i = 0; i < authStore.length; i++) {
  353. this.auth(
  354. server,
  355. connections,
  356. authStore[i].db,
  357. authStore[i].username,
  358. authStore[i].password,
  359. function(err) {
  360. count = count - 1;
  361. // Done re-authenticating
  362. if (count === 0) {
  363. callback(err, null);
  364. }
  365. }
  366. );
  367. }
  368. };
  369. class ScramSHA1 extends ScramSHA {
  370. constructor(bson) {
  371. super(bson, 'sha1');
  372. }
  373. }
  374. class ScramSHA256 extends ScramSHA {
  375. constructor(bson) {
  376. super(bson, 'sha256');
  377. }
  378. }
  379. module.exports = { ScramSHA1, ScramSHA256 };