scram.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 dict = parsePayload(r.payload.value());
  151. const iterations = parseInt(dict.i, 10);
  152. const salt = dict.s;
  153. const rnonce = dict.r;
  154. // Set up start of proof
  155. const withoutProof = `c=biws,r=${rnonce}`;
  156. const saltedPassword = HI(
  157. processedPassword,
  158. Buffer.from(salt, 'base64'),
  159. iterations,
  160. cryptoMethod
  161. );
  162. if (iterations && iterations < 4096) {
  163. const error = new MongoError(`Server returned an invalid iteration count ${iterations}`);
  164. return callback(error, false);
  165. }
  166. const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
  167. const storedKey = H(cryptoMethod, clientKey);
  168. const authMessage = [firstBare, r.payload.value().toString('base64'), withoutProof].join(',');
  169. const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
  170. const clientProof = `p=${xor(clientKey, clientSignature)}`;
  171. const clientFinal = [withoutProof, clientProof].join(',');
  172. const saslContinueCmd = {
  173. saslContinue: 1,
  174. conversationId: r.conversationId,
  175. payload: new Binary(Buffer.from(clientFinal))
  176. };
  177. sendAuthCommand(connection, `${db}.$cmd`, saslContinueCmd, (err, r) => {
  178. if (!r || r.done !== false) {
  179. return callback(err, r);
  180. }
  181. const retrySaslContinueCmd = {
  182. saslContinue: 1,
  183. conversationId: r.conversationId,
  184. payload: Buffer.alloc(0)
  185. };
  186. sendAuthCommand(connection, `${db}.$cmd`, retrySaslContinueCmd, callback);
  187. });
  188. });
  189. }
  190. /**
  191. * Implementation of authentication for a single connection
  192. * @override
  193. */
  194. _authenticateSingleConnection(sendAuthCommand, connection, credentials, callback) {
  195. // Create a random nonce
  196. crypto.randomBytes(24, (err, buff) => {
  197. if (err) {
  198. return callback(err, null);
  199. }
  200. return this._executeScram(
  201. sendAuthCommand,
  202. connection,
  203. credentials,
  204. buff.toString('base64'),
  205. callback
  206. );
  207. });
  208. }
  209. /**
  210. * Authenticate
  211. * @override
  212. * @method
  213. */
  214. auth(sendAuthCommand, connections, credentials, callback) {
  215. this._checkSaslprep();
  216. super.auth(sendAuthCommand, connections, credentials, callback);
  217. }
  218. _checkSaslprep() {
  219. const cryptoMethod = this.cryptoMethod;
  220. if (cryptoMethod === 'sha256') {
  221. if (!saslprep) {
  222. console.warn('Warning: no saslprep library specified. Passwords will not be sanitized');
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * Creates a new ScramSHA1 authentication mechanism
  229. * @class
  230. * @extends ScramSHA
  231. */
  232. class ScramSHA1 extends ScramSHA {
  233. constructor(bson) {
  234. super(bson, 'sha1');
  235. }
  236. }
  237. /**
  238. * Creates a new ScramSHA256 authentication mechanism
  239. * @class
  240. * @extends ScramSHA
  241. */
  242. class ScramSHA256 extends ScramSHA {
  243. constructor(bson) {
  244. super(bson, 'sha256');
  245. }
  246. }
  247. module.exports = { ScramSHA1, ScramSHA256 };