test-kexdh.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var SSH2Stream = require('../lib/ssh');
  2. var parseKey = require('../lib/utils').parseKey;
  3. var MESSAGE = require('../lib/constants').MESSAGE;
  4. var assert = require('assert');
  5. var fs = require('fs');
  6. var SERVER_KEY = fs.readFileSync(__dirname + '/fixtures/openssh_new_rsa');
  7. var SERVER_KEY_PRV = parseKey(SERVER_KEY);
  8. var server = new SSH2Stream({
  9. server: true,
  10. hostKeys: {
  11. 'ssh-rsa': SERVER_KEY_PRV
  12. },
  13. algorithms: {
  14. serverHostKey: ['ssh-rsa']
  15. }
  16. });
  17. var client = new SSH2Stream();
  18. var cliError;
  19. var srvError;
  20. server.on('error', function(err) {
  21. assert(err);
  22. assert(/unexpected/.test(err.message));
  23. assert(!srvError);
  24. srvError = err;
  25. });
  26. // Removed 'KEXDH_REPLY' listeners as it causes client to send 'NEWKEYS' which
  27. // changes server's state.
  28. client.removeAllListeners('KEXDH_REPLY');
  29. // Removed 'NEWKEYS' listeners as server sends 'NEWKEYS' after receiving
  30. // 'KEXDH_INIT' which causes errors on client if 'NEWKEYS' is processed
  31. // without processing 'KEXDH_REPLY'
  32. client.removeAllListeners('NEWKEYS');
  33. // Added 'KEXDH_REPLY' which violates protocol and re-sends 'KEXDH_INIT'
  34. // packet
  35. client.on('KEXDH_REPLY', function(info) {
  36. var state = client._state;
  37. var outstate = state.outgoing;
  38. var buf = Buffer.allocUnsafe(1 + 4 + outstate.pubkey.length);
  39. buf[0] = MESSAGE.KEXDH_INIT;
  40. buf.writeUInt32BE(outstate.pubkey.length, 1, true);
  41. outstate.pubkey.copy(buf, 5);
  42. SSH2Stream._send(client, buf, undefined, true);
  43. });
  44. client.on('error', function(err) {
  45. assert(!cliError);
  46. assert(err);
  47. assert.equal(
  48. err.message,
  49. 'PROTOCOL_ERROR',
  50. 'Expected Error: PROTOCOL_ERROR Got Error: ' + err.message
  51. );
  52. cliError = err;
  53. });
  54. client.pipe(server).pipe(client);
  55. process.on('exit', function() {
  56. assert(cliError, 'Expected client error');
  57. });