caching_sha2_password.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. // https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-plugin-caching_sha2_password/
  3. const PLUGIN_NAME = 'caching_sha2_password';
  4. const crypto = require('crypto');
  5. const { xor, xorRotating } = require('../auth_41');
  6. const REQUEST_SERVER_KEY_PACKET = Buffer.from([2]);
  7. const FAST_AUTH_SUCCESS_PACKET = Buffer.from([3]);
  8. const PERFORM_FULL_AUTHENTICATION_PACKET = Buffer.from([4]);
  9. const STATE_INITIAL = 0;
  10. const STATE_TOKEN_SENT = 1;
  11. const STATE_WAIT_SERVER_KEY = 2;
  12. const STATE_FINAL = -1;
  13. function sha256(msg) {
  14. const hash = crypto.createHash('sha256');
  15. hash.update(msg, 'binary');
  16. return hash.digest('binary');
  17. }
  18. function calculateToken(password, scramble) {
  19. if (!password) {
  20. return Buffer.alloc(0);
  21. }
  22. const stage1 = sha256(Buffer.from(password, 'utf8').toString('binary'));
  23. const stage2 = sha256(stage1);
  24. const stage3 = sha256(stage2 + scramble.toString('binary'));
  25. return xor(stage1, stage3);
  26. }
  27. function encrypt(password, scramble, key) {
  28. const stage1 = xorRotating(
  29. Buffer.from(`${password}\0`, 'utf8').toString('binary'),
  30. scramble.toString('binary')
  31. );
  32. return crypto.publicEncrypt(key, stage1);
  33. }
  34. module.exports = (pluginOptions = {}) => ({ connection }) => {
  35. let state = 0;
  36. let scramble = null;
  37. const password = connection.config.password;
  38. const authWithKey = serverKey => {
  39. const _password = encrypt(password, scramble, serverKey);
  40. state = STATE_FINAL;
  41. return _password;
  42. };
  43. return data => {
  44. switch (state) {
  45. case STATE_INITIAL:
  46. scramble = data.slice(0, 20);
  47. state = STATE_TOKEN_SENT;
  48. return calculateToken(password, scramble);
  49. case STATE_TOKEN_SENT:
  50. if (FAST_AUTH_SUCCESS_PACKET.equals(data)) {
  51. state = STATE_FINAL;
  52. return null;
  53. }
  54. if (PERFORM_FULL_AUTHENTICATION_PACKET.equals(data)) {
  55. const isSecureConnection =
  56. typeof pluginOptions.overrideIsSecure === 'undefined'
  57. ? connection.config.ssl || connection.config.socketPath
  58. : pluginOptions.overrideIsSecure;
  59. if (isSecureConnection) {
  60. state = STATE_FINAL;
  61. return Buffer.from(`${password}\0`, 'utf8');
  62. }
  63. // if client provides key we can save one extra roundrip on first connection
  64. if (pluginOptions.serverPublicKey) {
  65. return authWithKey(pluginOptions.serverPublicKey);
  66. }
  67. state = STATE_WAIT_SERVER_KEY;
  68. return REQUEST_SERVER_KEY_PACKET;
  69. }
  70. throw new Error(
  71. `Invalid AuthMoreData packet received by ${PLUGIN_NAME} plugin in STATE_TOKEN_SENT state.`
  72. );
  73. case STATE_WAIT_SERVER_KEY:
  74. if (pluginOptions.onServerPublicKey) {
  75. pluginOptions.onServerPublicKey(data);
  76. }
  77. return authWithKey(data);
  78. case STATE_FINAL:
  79. throw new Error(
  80. `Unexpected data in AuthMoreData packet received by ${PLUGIN_NAME} plugin in STATE_FINAL state.`
  81. );
  82. }
  83. throw new Error(
  84. `Unexpected data in AuthMoreData packet received by ${PLUGIN_NAME} plugin in state ${state}`
  85. );
  86. };
  87. };