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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. 'use strict';
  2. const crypto = require('crypto');
  3. const Buffer = require('safe-buffer').Buffer;
  4. const retrieveBSON = require('../connection/utils').retrieveBSON;
  5. const MongoError = require('../error').MongoError;
  6. const AuthProvider = require('./auth_provider').AuthProvider;
  7. const BSON = retrieveBSON();
  8. const Binary = BSON.Binary;
  9. let saslprep;
  10. try {
  11. saslprep = require('saslprep');
  12. } catch (e) {
  13. // don't do anything;
  14. }
  15. var parsePayload = function(payload) {
  16. var dict = {};
  17. var parts = payload.split(',');
  18. for (var i = 0; i < parts.length; i++) {
  19. var valueParts = parts[i].split('=');
  20. dict[valueParts[0]] = valueParts[1];
  21. }
  22. return dict;
  23. };
  24. var passwordDigest = function(username, password) {
  25. if (typeof username !== 'string') throw new MongoError('username must be a string');
  26. if (typeof password !== 'string') throw new MongoError('password must be a string');
  27. if (password.length === 0) throw new MongoError('password cannot be empty');
  28. // Use node md5 generator
  29. var md5 = crypto.createHash('md5');
  30. // Generate keys used for authentication
  31. md5.update(username + ':mongo:' + password, 'utf8');
  32. return md5.digest('hex');
  33. };
  34. // XOR two buffers
  35. function xor(a, b) {
  36. if (!Buffer.isBuffer(a)) a = Buffer.from(a);
  37. if (!Buffer.isBuffer(b)) b = Buffer.from(b);
  38. const length = Math.max(a.length, b.length);
  39. const res = [];
  40. for (let i = 0; i < length; i += 1) {
  41. res.push(a[i] ^ b[i]);
  42. }
  43. return Buffer.from(res).toString('base64');
  44. }
  45. function H(method, text) {
  46. return crypto
  47. .createHash(method)
  48. .update(text)
  49. .digest();
  50. }
  51. function HMAC(method, key, text) {
  52. return crypto
  53. .createHmac(method, key)
  54. .update(text)
  55. .digest();
  56. }
  57. var _hiCache = {};
  58. var _hiCacheCount = 0;
  59. var _hiCachePurge = function() {
  60. _hiCache = {};
  61. _hiCacheCount = 0;
  62. };
  63. const hiLengthMap = {
  64. sha256: 32,
  65. sha1: 20
  66. };
  67. function HI(data, salt, iterations, cryptoMethod) {
  68. // omit the work if already generated
  69. const key = [data, salt.toString('base64'), iterations].join('_');
  70. if (_hiCache[key] !== undefined) {
  71. return _hiCache[key];
  72. }
  73. // generate the salt
  74. const saltedData = crypto.pbkdf2Sync(
  75. data,
  76. salt,
  77. iterations,
  78. hiLengthMap[cryptoMethod],
  79. cryptoMethod
  80. );
  81. // cache a copy to speed up the next lookup, but prevent unbounded cache growth
  82. if (_hiCacheCount >= 200) {
  83. _hiCachePurge();
  84. }
  85. _hiCache[key] = saltedData;
  86. _hiCacheCount += 1;
  87. return saltedData;
  88. }
  89. /**
  90. * Creates a new ScramSHA authentication mechanism
  91. * @class
  92. * @extends AuthProvider
  93. */
  94. class ScramSHA extends AuthProvider {
  95. constructor(bson, cryptoMethod) {
  96. super(bson);
  97. this.cryptoMethod = cryptoMethod || 'sha1';
  98. }
  99. static _getError(err, r) {
  100. if (err) {
  101. return err;
  102. }
  103. if (r.$err || r.errmsg) {
  104. return new MongoError(r);
  105. }
  106. }
  107. /**
  108. * @ignore
  109. */
  110. _executeScram(sendAuthCommand, connection, credentials, nonce, callback) {
  111. let username = credentials.username;
  112. const password = credentials.password;
  113. const db = credentials.source;
  114. const cryptoMethod = this.cryptoMethod;
  115. let mechanism = 'SCRAM-SHA-1';
  116. let processedPassword;
  117. if (cryptoMethod === 'sha256') {
  118. mechanism = 'SCRAM-SHA-256';
  119. processedPassword = saslprep ? saslprep(password) : password;
  120. } else {
  121. try {
  122. processedPassword = passwordDigest(username, password);
  123. } catch (e) {
  124. return callback(e);
  125. }
  126. }
  127. // Clean up the user
  128. username = username.replace('=', '=3D').replace(',', '=2C');
  129. // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
  130. // Since the username is not sasl-prep-d, we need to do this here.
  131. const firstBare = Buffer.concat([
  132. Buffer.from('n=', 'utf8'),
  133. Buffer.from(username, 'utf8'),
  134. Buffer.from(',r=', 'utf8'),
  135. Buffer.from(nonce, 'utf8')
  136. ]);
  137. // Build command structure
  138. const saslStartCmd = {
  139. saslStart: 1,
  140. mechanism,
  141. payload: new Binary(Buffer.concat([Buffer.from('n,,', 'utf8'), firstBare])),
  142. autoAuthorize: 1
  143. };
  144. // Write the commmand on the connection
  145. sendAuthCommand(connection, `${db}.$cmd`, saslStartCmd, (err, r) => {
  146. let tmpError = ScramSHA._getError(err, r);
  147. if (tmpError) {
  148. return callback(tmpError, null);
  149. }
  150. const payload = Buffer.isBuffer(r.payload) ? new Binary(r.payload) : r.payload;
  151. const dict = parsePayload(payload.value());
  152. const iterations = parseInt(dict.i, 10);
  153. const salt = dict.s;
  154. const rnonce = dict.r;
  155. // Set up start of proof
  156. const withoutProof = `c=biws,r=${rnonce}`;
  157. const saltedPassword = HI(
  158. processedPassword,
  159. Buffer.from(salt, 'base64'),
  160. iterations,
  161. cryptoMethod
  162. );
  163. if (iterations && iterations < 4096) {
  164. const error = new MongoError(`Server returned an invalid iteration count ${iterations}`);
  165. return callback(error, false);
  166. }
  167. const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
  168. const storedKey = H(cryptoMethod, clientKey);
  169. const authMessage = [firstBare, payload.value().toString('base64'), withoutProof].join(',');
  170. const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
  171. const clientProof = `p=${xor(clientKey, clientSignature)}`;
  172. const clientFinal = [withoutProof, clientProof].join(',');
  173. const saslContinueCmd = {
  174. saslContinue: 1,
  175. conversationId: r.conversationId,
  176. payload: new Binary(Buffer.from(clientFinal))
  177. };
  178. sendAuthCommand(connection, `${db}.$cmd`, saslContinueCmd, (err, r) => {
  179. if (!r || r.done !== false) {
  180. return callback(err, r);
  181. }
  182. const retrySaslContinueCmd = {
  183. saslContinue: 1,
  184. conversationId: r.conversationId,
  185. payload: Buffer.alloc(0)
  186. };
  187. sendAuthCommand(connection, `${db}.$cmd`, retrySaslContinueCmd, callback);
  188. });
  189. });
  190. }
  191. /**
  192. * Implementation of authentication for a single connection
  193. * @override
  194. */
  195. _authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
  196. // Create a random nonce
  197. crypto.randomBytes(24, (err, buff) => {
  198. if (err) {
  199. return callback(err, null);
  200. }
  201. return this._executeScram(
  202. sendAuthCommand,
  203. connection,
  204. credentials,
  205. buff.toString('base64'),
  206. callback
  207. );
  208. });
  209. }
  210. /**
  211. * Authenticate
  212. * @override
  213. * @method
  214. */
  215. auth(sendAuthCommand, connections, credentials, callback) {
  216. this._checkSaslprep();
  217. super.auth(sendAuthCommand, connections, credentials, callback);
  218. }
  219. _checkSaslprep() {
  220. const cryptoMethod = this.cryptoMethod;
  221. if (cryptoMethod === 'sha256') {
  222. if (!saslprep) {
  223. console.warn('Warning: no saslprep library specified. Passwords will not be sanitized');
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * Creates a new ScramSHA1 authentication mechanism
  230. * @class
  231. * @extends ScramSHA
  232. */
  233. class ScramSHA1 extends ScramSHA {
  234. constructor(bson) {
  235. super(bson, 'sha1');
  236. }
  237. }
  238. /**
  239. * Creates a new ScramSHA256 authentication mechanism
  240. * @class
  241. * @extends ScramSHA
  242. */
  243. class ScramSHA256 extends ScramSHA {
  244. constructor(bson) {
  245. super(bson, 'sha256');
  246. }
  247. }
  248. module.exports = { ScramSHA1, ScramSHA256 };