test-integration-openssh.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // TODO: add more rekey tests that at least include switching from no
  2. // compression to compression and vice versa
  3. 'use strict';
  4. const assert = require('assert');
  5. const { spawn, spawnSync } = require('child_process');
  6. const { chmodSync, readdirSync } = require('fs');
  7. const { join } = require('path');
  8. const readline = require('readline');
  9. const Server = require('../lib/server.js');
  10. const {
  11. fixture,
  12. fixtureKey,
  13. FIXTURES_DIR,
  14. mustCall,
  15. mustCallAtLeast,
  16. } = require('./common.js');
  17. const SPAWN_OPTS = { windowsHide: true };
  18. const CLIENT_TIMEOUT = 5000;
  19. const debug = false;
  20. const opensshPath = 'ssh';
  21. let opensshVer;
  22. // TODO: figure out why this test is failing on Windows
  23. if (process.platform === 'win32') {
  24. console.log('Skipping OpenSSH integration tests on Windows');
  25. process.exit(0);
  26. }
  27. // Fix file modes to avoid OpenSSH client complaints about keys' permissions
  28. for (const file of readdirSync(FIXTURES_DIR, { withFileTypes: true })) {
  29. if (file.isFile())
  30. chmodSync(join(FIXTURES_DIR, file.name), 0o600);
  31. }
  32. {
  33. // Get OpenSSH client version first
  34. const {
  35. error, stderr, stdout
  36. } = spawnSync(opensshPath, ['-V'], SPAWN_OPTS);
  37. if (error) {
  38. console.error('OpenSSH client is required for these tests');
  39. process.exitCode = 5;
  40. return;
  41. }
  42. const re = /^OpenSSH_([\d.]+)/;
  43. let m = re.exec(stdout.toString());
  44. if (!m || !m[1]) {
  45. m = re.exec(stderr.toString());
  46. if (!m || !m[1]) {
  47. console.error('OpenSSH client is required for these tests');
  48. process.exitCode = 5;
  49. return;
  50. }
  51. }
  52. opensshVer = m[1];
  53. console.log(`Testing with OpenSSH version: ${opensshVer}`);
  54. }
  55. // Key-based authentication
  56. [
  57. { desc: 'RSA user key (old OpenSSH)',
  58. clientKey: fixtureKey('id_rsa') },
  59. { desc: 'RSA user key (new OpenSSH)',
  60. clientKey: fixtureKey('openssh_new_rsa') },
  61. { desc: 'DSA user key',
  62. clientKey: fixtureKey('id_dsa') },
  63. { desc: 'ECDSA user key',
  64. clientKey: fixtureKey('id_ecdsa') },
  65. ].forEach((test) => {
  66. const { desc, clientKey } = test;
  67. const username = 'KeyUser';
  68. const { server } = setup(
  69. desc,
  70. {
  71. client: {
  72. username,
  73. privateKeyPath: clientKey.fullPath,
  74. },
  75. server: { hostKeys: [ fixture('ssh_host_rsa_key') ] },
  76. debug,
  77. }
  78. );
  79. server.on('connection', mustCall((conn) => {
  80. let authAttempt = 0;
  81. conn.on('authentication', mustCallAtLeast((ctx) => {
  82. assert(ctx.username === username,
  83. `Wrong username: ${ctx.username}`);
  84. switch (++authAttempt) {
  85. case 1:
  86. assert(ctx.method === 'none',
  87. `Wrong auth method: ${ctx.method}`);
  88. return ctx.reject();
  89. case 2:
  90. case 3:
  91. if (authAttempt === 3)
  92. assert(ctx.signature, 'Missing publickey signature');
  93. assert(ctx.method === 'publickey',
  94. `Wrong auth method: ${ctx.method}`);
  95. assert(ctx.key.algo === clientKey.key.type,
  96. `Wrong key algo: ${ctx.key.algo}`);
  97. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  98. ctx.key.data,
  99. 'Public key mismatch');
  100. break;
  101. default:
  102. assert(false, 'Unexpected number of auth attempts');
  103. }
  104. if (ctx.signature) {
  105. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  106. 'Could not verify publickey signature');
  107. // We should not expect any further auth attempts after we verify a
  108. // signature
  109. authAttempt = Infinity;
  110. }
  111. ctx.accept();
  112. }, 2)).on('ready', mustCall(() => {
  113. conn.on('session', mustCall((accept, reject) => {
  114. accept().on('exec', mustCall((accept, reject) => {
  115. const stream = accept();
  116. stream.exit(0);
  117. stream.end();
  118. }));
  119. }));
  120. }));
  121. }));
  122. });
  123. // Different host key types
  124. [
  125. { desc: 'RSA host key (old OpenSSH)',
  126. hostKey: fixture('id_rsa') },
  127. { desc: 'RSA host key (new OpenSSH)',
  128. hostKey: fixture('openssh_new_rsa') },
  129. { desc: 'DSA host key',
  130. hostKey: fixture('ssh_host_dsa_key') },
  131. { desc: 'ECDSA host key',
  132. hostKey: fixture('ssh_host_ecdsa_key') },
  133. { desc: 'PPK',
  134. hostKey: fixture('id_rsa.ppk') },
  135. ].forEach((test) => {
  136. const { desc, hostKey } = test;
  137. const clientKey = fixtureKey('openssh_new_rsa');
  138. const username = 'KeyUser';
  139. const { server } = setup(
  140. desc,
  141. {
  142. client: {
  143. username,
  144. privateKeyPath: clientKey.fullPath,
  145. },
  146. server: { hostKeys: [ hostKey ] },
  147. debug,
  148. }
  149. );
  150. server.on('connection', mustCall((conn) => {
  151. let authAttempt = 0;
  152. conn.on('authentication', mustCallAtLeast((ctx) => {
  153. assert(ctx.username === username,
  154. `Wrong username: ${ctx.username}`);
  155. switch (++authAttempt) {
  156. case 1:
  157. assert(ctx.method === 'none',
  158. `Wrong auth method: ${ctx.method}`);
  159. return ctx.reject();
  160. case 2:
  161. case 3:
  162. if (authAttempt === 3)
  163. assert(ctx.signature, 'Missing publickey signature');
  164. assert(ctx.method === 'publickey',
  165. `Wrong auth method: ${ctx.method}`);
  166. assert(ctx.key.algo === clientKey.key.type,
  167. `Wrong key algo: ${ctx.key.algo}`);
  168. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  169. ctx.key.data,
  170. 'Public key mismatch');
  171. break;
  172. default:
  173. assert(false, 'Unexpected number of auth attempts');
  174. }
  175. if (ctx.signature) {
  176. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  177. 'Could not verify publickey signature');
  178. // We should not expect any further auth attempts after we verify a
  179. // signature
  180. authAttempt = Infinity;
  181. }
  182. ctx.accept();
  183. }, 2)).on('ready', mustCall(() => {
  184. conn.on('session', mustCall((accept, reject) => {
  185. accept().on('exec', mustCall((accept, reject) => {
  186. const stream = accept();
  187. stream.exit(0);
  188. stream.end();
  189. }));
  190. }));
  191. }));
  192. }));
  193. });
  194. // Various edge cases
  195. {
  196. const clientKey = fixtureKey('openssh_new_rsa');
  197. const username = 'KeyUser';
  198. const { server } = setup(
  199. 'Server closes stdin too early',
  200. {
  201. client: {
  202. username,
  203. privateKeyPath: clientKey.fullPath,
  204. },
  205. server: { hostKeys: [ fixture('ssh_host_rsa_key') ] },
  206. debug,
  207. }
  208. );
  209. server.on('_child', mustCall((childProc) => {
  210. childProc.stderr.once('data', mustCall((data) => {
  211. childProc.stdin.end();
  212. }));
  213. childProc.stdin.write('ping');
  214. })).on('connection', mustCall((conn) => {
  215. let authAttempt = 0;
  216. conn.on('authentication', mustCallAtLeast((ctx) => {
  217. assert(ctx.username === username,
  218. `Wrong username: ${ctx.username}`);
  219. switch (++authAttempt) {
  220. case 1:
  221. assert(ctx.method === 'none',
  222. `Wrong auth method: ${ctx.method}`);
  223. return ctx.reject();
  224. case 2:
  225. case 3:
  226. if (authAttempt === 3)
  227. assert(ctx.signature, 'Missing publickey signature');
  228. assert(ctx.method === 'publickey',
  229. `Wrong auth method: ${ctx.method}`);
  230. assert(ctx.key.algo === clientKey.key.type,
  231. `Wrong key algo: ${ctx.key.algo}`);
  232. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  233. ctx.key.data,
  234. 'Public key mismatch');
  235. break;
  236. default:
  237. assert(false, 'Unexpected number of auth attempts');
  238. }
  239. if (ctx.signature) {
  240. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  241. 'Could not verify publickey signature');
  242. // We should not expect any further auth attempts after we verify a
  243. // signature
  244. authAttempt = Infinity;
  245. }
  246. ctx.accept();
  247. }, 2)).on('ready', mustCall(() => {
  248. conn.on('session', mustCall((accept, reject) => {
  249. accept().on('exec', mustCall((accept, reject) => {
  250. const stream = accept();
  251. stream.stdin.on('data', mustCallAtLeast((data) => {
  252. stream.stdout.write('pong on stdout');
  253. stream.stderr.write('pong on stderr');
  254. })).on('end', mustCall(() => {
  255. stream.stdout.write('pong on stdout');
  256. stream.stderr.write('pong on stderr');
  257. stream.exit(0);
  258. stream.close();
  259. }));
  260. }));
  261. }));
  262. }));
  263. }));
  264. }
  265. {
  266. const clientKey = fixtureKey('openssh_new_rsa');
  267. const username = 'KeyUser';
  268. const { server } = setup(
  269. 'Rekey',
  270. {
  271. client: {
  272. username,
  273. privateKeyPath: clientKey.fullPath,
  274. },
  275. server: { hostKeys: [ fixture('ssh_host_rsa_key') ] },
  276. debug,
  277. }
  278. );
  279. server.on('connection', mustCall((conn) => {
  280. let authAttempt = 0;
  281. conn.on('authentication', mustCallAtLeast((ctx) => {
  282. assert(ctx.username === username,
  283. `Wrong username: ${ctx.username}`);
  284. switch (++authAttempt) {
  285. case 1:
  286. assert(ctx.method === 'none',
  287. `Wrong auth method: ${ctx.method}`);
  288. return ctx.reject();
  289. case 2:
  290. case 3:
  291. if (authAttempt === 3)
  292. assert(ctx.signature, 'Missing publickey signature');
  293. assert(ctx.method === 'publickey',
  294. `Wrong auth method: ${ctx.method}`);
  295. assert(ctx.key.algo === clientKey.key.type,
  296. `Wrong key algo: ${ctx.key.algo}`);
  297. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  298. ctx.key.data,
  299. 'Public key mismatch');
  300. break;
  301. default:
  302. assert(false, 'Unexpected number of auth attempts');
  303. }
  304. if (ctx.signature) {
  305. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  306. 'Could not verify publickey signature');
  307. // We should not expect any further auth attempts after we verify a
  308. // signature
  309. authAttempt = Infinity;
  310. }
  311. ctx.accept();
  312. }, 2)).on('ready', mustCall(() => {
  313. conn.on('session', mustCall((accept, reject) => {
  314. const session = accept();
  315. conn.rekey();
  316. session.on('exec', mustCall((accept, reject) => {
  317. const stream = accept();
  318. stream.exit(0);
  319. stream.end();
  320. }));
  321. }));
  322. }));
  323. }));
  324. }
  325. function setup(title, configs) {
  326. const {
  327. client: clientCfg,
  328. server: serverCfg,
  329. allReady: allReady_,
  330. timeout: timeout_,
  331. debug,
  332. noForceServerReady,
  333. } = configs;
  334. let clientClose = false;
  335. let serverClose = false;
  336. let serverReady = false;
  337. let client;
  338. const msg = (text) => {
  339. return `${title}: ${text}`;
  340. };
  341. const timeout = (typeof timeout_ === 'number'
  342. ? timeout_
  343. : CLIENT_TIMEOUT);
  344. const allReady = (typeof allReady_ === 'function' ? allReady_ : undefined);
  345. if (debug) {
  346. serverCfg.debug = (...args) => {
  347. console.log(`[${title}][SERVER]`, ...args);
  348. };
  349. }
  350. const serverReadyFn = (noForceServerReady ? onReady : mustCall(onReady));
  351. const server = new Server(serverCfg);
  352. server.on('error', onError)
  353. .on('connection', mustCall((conn) => {
  354. conn.on('error', onError)
  355. .on('ready', serverReadyFn);
  356. server.close();
  357. }))
  358. .on('close', mustCall(onClose));
  359. function onError(err) {
  360. const which = (arguments.length >= 3 ? 'client' : 'server');
  361. assert(false, msg(`Unexpected ${which} error: ${err}`));
  362. }
  363. function onReady() {
  364. assert(!serverReady, msg('Received multiple ready events for server'));
  365. serverReady = true;
  366. allReady && allReady();
  367. }
  368. function onClose() {
  369. if (arguments.length >= 3) {
  370. assert(!clientClose, msg('Received multiple close events for client'));
  371. clientClose = true;
  372. } else {
  373. assert(!serverClose, msg('Received multiple close events for server'));
  374. serverClose = true;
  375. }
  376. }
  377. process.nextTick(mustCall(() => {
  378. server.listen(0, 'localhost', mustCall(() => {
  379. const args = [
  380. '-o', 'UserKnownHostsFile=/dev/null',
  381. '-o', 'StrictHostKeyChecking=no',
  382. '-o', 'CheckHostIP=no',
  383. '-o', 'ConnectTimeout=3',
  384. '-o', 'GlobalKnownHostsFile=/dev/null',
  385. '-o', 'GSSAPIAuthentication=no',
  386. '-o', 'IdentitiesOnly=yes',
  387. '-o', 'BatchMode=yes',
  388. '-o', 'VerifyHostKeyDNS=no',
  389. '-vvvvvv',
  390. '-T',
  391. '-o', 'KbdInteractiveAuthentication=no',
  392. '-o', 'HostbasedAuthentication=no',
  393. '-o', 'PasswordAuthentication=no',
  394. '-o', 'PubkeyAuthentication=yes',
  395. '-o', 'PreferredAuthentications=publickey'
  396. ];
  397. if (clientCfg.privateKeyPath)
  398. args.push('-o', `IdentityFile=${clientCfg.privateKeyPath}`);
  399. if (!/^[0-6]\./.test(opensshVer)) {
  400. // OpenSSH 7.0+ disables DSS/DSA host (and user) key support by
  401. // default, so we explicitly enable it here
  402. args.push('-o', 'HostKeyAlgorithms=+ssh-dss');
  403. args.push('-o', 'PubkeyAcceptedKeyTypes=+ssh-dss');
  404. }
  405. args.push('-p', server.address().port.toString(),
  406. '-l', clientCfg.username,
  407. 'localhost',
  408. 'uptime');
  409. client = spawn(opensshPath, args, SPAWN_OPTS);
  410. server.emit('_child', client);
  411. if (debug) {
  412. readline.createInterface({
  413. input: client.stdout
  414. }).on('line', (line) => {
  415. console.log(`[${title}][CLIENT][STDOUT]`, line);
  416. });
  417. readline.createInterface({
  418. input: client.stderr
  419. }).on('line', (line) => {
  420. console.error(`[${title}][CLIENT][STDERR]`, line);
  421. });
  422. } else {
  423. client.stdout.resume();
  424. client.stderr.resume();
  425. }
  426. client.on('error', (err) => {
  427. onError(err, null, null);
  428. }).on('exit', (code) => {
  429. clearTimeout(client.timer);
  430. if (code !== 0)
  431. return onError(new Error(`Non-zero exit code ${code}`), null, null);
  432. onClose(null, null, null);
  433. });
  434. client.timer = setTimeout(() => {
  435. assert(false, msg('Client timeout'));
  436. }, timeout);
  437. }));
  438. }));
  439. return { server };
  440. }