scram.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ScramSHA256 = exports.ScramSHA1 = void 0;
  4. const saslprep_1 = require("@mongodb-js/saslprep");
  5. const crypto = require("crypto");
  6. const bson_1 = require("../../bson");
  7. const error_1 = require("../../error");
  8. const utils_1 = require("../../utils");
  9. const auth_provider_1 = require("./auth_provider");
  10. const providers_1 = require("./providers");
  11. class ScramSHA extends auth_provider_1.AuthProvider {
  12. constructor(cryptoMethod) {
  13. super();
  14. this.cryptoMethod = cryptoMethod || 'sha1';
  15. }
  16. async prepare(handshakeDoc, authContext) {
  17. const cryptoMethod = this.cryptoMethod;
  18. const credentials = authContext.credentials;
  19. if (!credentials) {
  20. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  21. }
  22. const nonce = await (0, utils_1.randomBytes)(24);
  23. // store the nonce for later use
  24. authContext.nonce = nonce;
  25. const request = {
  26. ...handshakeDoc,
  27. speculativeAuthenticate: {
  28. ...makeFirstMessage(cryptoMethod, credentials, nonce),
  29. db: credentials.source
  30. }
  31. };
  32. return request;
  33. }
  34. async auth(authContext) {
  35. const { reauthenticating, response } = authContext;
  36. if (response?.speculativeAuthenticate && !reauthenticating) {
  37. return await continueScramConversation(this.cryptoMethod, response.speculativeAuthenticate, authContext);
  38. }
  39. return await executeScram(this.cryptoMethod, authContext);
  40. }
  41. }
  42. function cleanUsername(username) {
  43. return username.replace('=', '=3D').replace(',', '=2C');
  44. }
  45. function clientFirstMessageBare(username, nonce) {
  46. // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
  47. // Since the username is not sasl-prep-d, we need to do this here.
  48. return Buffer.concat([
  49. Buffer.from('n=', 'utf8'),
  50. Buffer.from(username, 'utf8'),
  51. Buffer.from(',r=', 'utf8'),
  52. Buffer.from(nonce.toString('base64'), 'utf8')
  53. ]);
  54. }
  55. function makeFirstMessage(cryptoMethod, credentials, nonce) {
  56. const username = cleanUsername(credentials.username);
  57. const mechanism = cryptoMethod === 'sha1' ? providers_1.AuthMechanism.MONGODB_SCRAM_SHA1 : providers_1.AuthMechanism.MONGODB_SCRAM_SHA256;
  58. // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
  59. // Since the username is not sasl-prep-d, we need to do this here.
  60. return {
  61. saslStart: 1,
  62. mechanism,
  63. payload: new bson_1.Binary(Buffer.concat([Buffer.from('n,,', 'utf8'), clientFirstMessageBare(username, nonce)])),
  64. autoAuthorize: 1,
  65. options: { skipEmptyExchange: true }
  66. };
  67. }
  68. async function executeScram(cryptoMethod, authContext) {
  69. const { connection, credentials } = authContext;
  70. if (!credentials) {
  71. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  72. }
  73. if (!authContext.nonce) {
  74. throw new error_1.MongoInvalidArgumentError('AuthContext must contain a valid nonce property');
  75. }
  76. const nonce = authContext.nonce;
  77. const db = credentials.source;
  78. const saslStartCmd = makeFirstMessage(cryptoMethod, credentials, nonce);
  79. const response = await connection.command((0, utils_1.ns)(`${db}.$cmd`), saslStartCmd, undefined);
  80. await continueScramConversation(cryptoMethod, response, authContext);
  81. }
  82. async function continueScramConversation(cryptoMethod, response, authContext) {
  83. const connection = authContext.connection;
  84. const credentials = authContext.credentials;
  85. if (!credentials) {
  86. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  87. }
  88. if (!authContext.nonce) {
  89. throw new error_1.MongoInvalidArgumentError('Unable to continue SCRAM without valid nonce');
  90. }
  91. const nonce = authContext.nonce;
  92. const db = credentials.source;
  93. const username = cleanUsername(credentials.username);
  94. const password = credentials.password;
  95. const processedPassword = cryptoMethod === 'sha256' ? (0, saslprep_1.saslprep)(password) : passwordDigest(username, password);
  96. const payload = Buffer.isBuffer(response.payload)
  97. ? new bson_1.Binary(response.payload)
  98. : response.payload;
  99. const dict = parsePayload(payload);
  100. const iterations = parseInt(dict.i, 10);
  101. if (iterations && iterations < 4096) {
  102. // TODO(NODE-3483)
  103. throw new error_1.MongoRuntimeError(`Server returned an invalid iteration count ${iterations}`);
  104. }
  105. const salt = dict.s;
  106. const rnonce = dict.r;
  107. if (rnonce.startsWith('nonce')) {
  108. // TODO(NODE-3483)
  109. throw new error_1.MongoRuntimeError(`Server returned an invalid nonce: ${rnonce}`);
  110. }
  111. // Set up start of proof
  112. const withoutProof = `c=biws,r=${rnonce}`;
  113. const saltedPassword = HI(processedPassword, Buffer.from(salt, 'base64'), iterations, cryptoMethod);
  114. const clientKey = HMAC(cryptoMethod, saltedPassword, 'Client Key');
  115. const serverKey = HMAC(cryptoMethod, saltedPassword, 'Server Key');
  116. const storedKey = H(cryptoMethod, clientKey);
  117. const authMessage = [
  118. clientFirstMessageBare(username, nonce),
  119. payload.toString('utf8'),
  120. withoutProof
  121. ].join(',');
  122. const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
  123. const clientProof = `p=${xor(clientKey, clientSignature)}`;
  124. const clientFinal = [withoutProof, clientProof].join(',');
  125. const serverSignature = HMAC(cryptoMethod, serverKey, authMessage);
  126. const saslContinueCmd = {
  127. saslContinue: 1,
  128. conversationId: response.conversationId,
  129. payload: new bson_1.Binary(Buffer.from(clientFinal))
  130. };
  131. const r = await connection.command((0, utils_1.ns)(`${db}.$cmd`), saslContinueCmd, undefined);
  132. const parsedResponse = parsePayload(r.payload);
  133. if (!compareDigest(Buffer.from(parsedResponse.v, 'base64'), serverSignature)) {
  134. throw new error_1.MongoRuntimeError('Server returned an invalid signature');
  135. }
  136. if (r.done !== false) {
  137. // If the server sends r.done === true we can save one RTT
  138. return;
  139. }
  140. const retrySaslContinueCmd = {
  141. saslContinue: 1,
  142. conversationId: r.conversationId,
  143. payload: Buffer.alloc(0)
  144. };
  145. await connection.command((0, utils_1.ns)(`${db}.$cmd`), retrySaslContinueCmd, undefined);
  146. }
  147. function parsePayload(payload) {
  148. const payloadStr = payload.toString('utf8');
  149. const dict = {};
  150. const parts = payloadStr.split(',');
  151. for (let i = 0; i < parts.length; i++) {
  152. const valueParts = (parts[i].match(/^([^=]*)=(.*)$/) ?? []).slice(1);
  153. dict[valueParts[0]] = valueParts[1];
  154. }
  155. return dict;
  156. }
  157. function passwordDigest(username, password) {
  158. if (typeof username !== 'string') {
  159. throw new error_1.MongoInvalidArgumentError('Username must be a string');
  160. }
  161. if (typeof password !== 'string') {
  162. throw new error_1.MongoInvalidArgumentError('Password must be a string');
  163. }
  164. if (password.length === 0) {
  165. throw new error_1.MongoInvalidArgumentError('Password cannot be empty');
  166. }
  167. let md5;
  168. try {
  169. md5 = crypto.createHash('md5');
  170. }
  171. catch (err) {
  172. if (crypto.getFips()) {
  173. // This error is (slightly) more helpful than what comes from OpenSSL directly, e.g.
  174. // 'Error: error:060800C8:digital envelope routines:EVP_DigestInit_ex:disabled for FIPS'
  175. throw new Error('Auth mechanism SCRAM-SHA-1 is not supported in FIPS mode');
  176. }
  177. throw err;
  178. }
  179. md5.update(`${username}:mongo:${password}`, 'utf8');
  180. return md5.digest('hex');
  181. }
  182. // XOR two buffers
  183. function xor(a, b) {
  184. if (!Buffer.isBuffer(a)) {
  185. a = Buffer.from(a);
  186. }
  187. if (!Buffer.isBuffer(b)) {
  188. b = Buffer.from(b);
  189. }
  190. const length = Math.max(a.length, b.length);
  191. const res = [];
  192. for (let i = 0; i < length; i += 1) {
  193. res.push(a[i] ^ b[i]);
  194. }
  195. return Buffer.from(res).toString('base64');
  196. }
  197. function H(method, text) {
  198. return crypto.createHash(method).update(text).digest();
  199. }
  200. function HMAC(method, key, text) {
  201. return crypto.createHmac(method, key).update(text).digest();
  202. }
  203. let _hiCache = {};
  204. let _hiCacheCount = 0;
  205. function _hiCachePurge() {
  206. _hiCache = {};
  207. _hiCacheCount = 0;
  208. }
  209. const hiLengthMap = {
  210. sha256: 32,
  211. sha1: 20
  212. };
  213. function HI(data, salt, iterations, cryptoMethod) {
  214. // omit the work if already generated
  215. const key = [data, salt.toString('base64'), iterations].join('_');
  216. if (_hiCache[key] != null) {
  217. return _hiCache[key];
  218. }
  219. // generate the salt
  220. const saltedData = crypto.pbkdf2Sync(data, salt, iterations, hiLengthMap[cryptoMethod], cryptoMethod);
  221. // cache a copy to speed up the next lookup, but prevent unbounded cache growth
  222. if (_hiCacheCount >= 200) {
  223. _hiCachePurge();
  224. }
  225. _hiCache[key] = saltedData;
  226. _hiCacheCount += 1;
  227. return saltedData;
  228. }
  229. function compareDigest(lhs, rhs) {
  230. if (lhs.length !== rhs.length) {
  231. return false;
  232. }
  233. if (typeof crypto.timingSafeEqual === 'function') {
  234. return crypto.timingSafeEqual(lhs, rhs);
  235. }
  236. let result = 0;
  237. for (let i = 0; i < lhs.length; i++) {
  238. result |= lhs[i] ^ rhs[i];
  239. }
  240. return result === 0;
  241. }
  242. class ScramSHA1 extends ScramSHA {
  243. constructor() {
  244. super('sha1');
  245. }
  246. }
  247. exports.ScramSHA1 = ScramSHA1;
  248. class ScramSHA256 extends ScramSHA {
  249. constructor() {
  250. super('sha256');
  251. }
  252. }
  253. exports.ScramSHA256 = ScramSHA256;
  254. //# sourceMappingURL=scram.js.map