123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- 'use strict';
- const assert = require('assert');
- const { inspect } = require('util');
- const {
- fixtureKey,
- mustCall,
- mustNotCall,
- setup,
- } = require('./common.js');
- const serverCfg = { hostKeys: [ fixtureKey('ssh_host_rsa_key').raw ] };
- const debug = false;
- // Keys ========================================================================
- [
- { desc: 'RSA (old OpenSSH)',
- clientKey: fixtureKey('id_rsa') },
- { desc: 'RSA (new OpenSSH)',
- clientKey: fixtureKey('openssh_new_rsa') },
- { desc: 'RSA (encrypted)',
- clientKey: fixtureKey('id_rsa_enc', 'foobarbaz'),
- passphrase: 'foobarbaz' },
- { desc: 'DSA',
- clientKey: fixtureKey('id_dsa') },
- { desc: 'ECDSA',
- clientKey: fixtureKey('id_ecdsa') },
- { desc: 'PPK',
- clientKey: fixtureKey('id_rsa.ppk') },
- ].forEach((test) => {
- const { desc, clientKey, passphrase } = test;
- const username = 'Key User';
- const { server } = setup(
- desc,
- {
- client: { username, privateKey: clientKey.raw, passphrase },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- switch (++authAttempt) {
- case 1:
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- case 3:
- assert(ctx.signature, 'Missing publickey signature');
- // FALLTHROUGH
- case 2:
- assert(ctx.method === 'publickey',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.key.algo === clientKey.key.type,
- `Wrong key algo: ${ctx.key.algo}`);
- assert.deepStrictEqual(clientKey.key.getPublicSSH(),
- ctx.key.data,
- 'Public key mismatch');
- break;
- }
- if (ctx.signature) {
- assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
- 'Could not verify publickey signature');
- }
- ctx.accept();
- }, 3)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- });
- // Password ====================================================================
- {
- const username = 'Password User';
- const password = 'hi mom';
- const { server } = setup(
- 'Password',
- {
- client: { username, password },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- if (++authAttempt === 1) {
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- }
- assert(ctx.method === 'password',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === password,
- `Wrong password: ${ctx.password}`);
- ctx.accept();
- }, 2)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- {
- const username = '';
- const password = 'hi mom';
- const { server } = setup(
- 'Password (empty username)',
- {
- client: { username, password },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- if (++authAttempt === 1) {
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- }
- assert(ctx.method === 'password',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === password,
- `Wrong password: ${ctx.password}`);
- ctx.accept();
- }, 2)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- {
- const username = 'foo';
- const oldPassword = 'bar';
- const newPassword = 'baz';
- const changePrompt = 'Prithee changeth thy password';
- const { client, server } = setup(
- 'Password (change requested)',
- {
- client: { username, password: oldPassword },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- if (++authAttempt === 1) {
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- }
- assert(ctx.method === 'password',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === oldPassword,
- `Wrong old password: ${ctx.password}`);
- ctx.requestChange(changePrompt, mustCall((newPassword_) => {
- assert(newPassword_ === newPassword,
- `Wrong new password: ${newPassword_}`);
- ctx.accept();
- }));
- }, 2)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- client.on('change password', mustCall((prompt, done) => {
- assert(prompt === changePrompt, `Wrong password change prompt: ${prompt}`);
- process.nextTick(done, newPassword);
- }));
- }
- // Hostbased ===================================================================
- {
- const localUsername = 'Local User Foo';
- const localHostname = 'Local Host Bar';
- const username = 'Hostbased User';
- const clientKey = fixtureKey('id_rsa');
- const { server } = setup(
- 'Hostbased',
- {
- client: {
- username,
- privateKey: clientKey.raw,
- localUsername,
- localHostname,
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- switch (++authAttempt) {
- case 1:
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- case 2:
- assert(ctx.method === 'publickey',
- `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- case 3:
- assert(ctx.method === 'hostbased',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.key.algo === clientKey.key.type,
- `Wrong key algo: ${ctx.key.algo}`);
- assert.deepStrictEqual(clientKey.key.getPublicSSH(),
- ctx.key.data,
- 'Public key mismatch');
- assert(ctx.signature, 'Expected signature');
- assert(ctx.localHostname === localHostname, 'Wrong local hostname');
- assert(ctx.localUsername === localUsername, 'Wrong local username');
- assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
- 'Could not verify hostbased signature');
- break;
- }
- ctx.accept();
- }, 3)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- // keyboard-interactive ========================================================
- {
- const username = 'Keyboard-Interactive User';
- const request = {
- name: 'SSH2 Authentication',
- instructions: 'These are instructions',
- prompts: [
- { prompt: 'Password: ', echo: false },
- { prompt: 'Is the cake a lie? ', echo: true },
- ],
- };
- const responses = [
- 'foobarbaz',
- 'yes',
- ];
- const { client, server } = setup(
- 'Password (empty username)',
- {
- client: {
- username,
- tryKeyboard: true,
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let authAttempt = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username,
- `Wrong username: ${ctx.username}`);
- if (++authAttempt === 1) {
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- return ctx.reject();
- }
- assert(ctx.method === 'keyboard-interactive',
- `Wrong auth method: ${ctx.method}`);
- ctx.prompt(request.prompts,
- request.name,
- request.instructions,
- mustCall((responses_) => {
- assert.deepStrictEqual(responses_, responses);
- ctx.accept();
- }));
- }, 2)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- client.on('keyboard-interactive',
- mustCall((name, instructions, lang, prompts, finish) => {
- assert(name === request.name, `Wrong prompt name: ${name}`);
- assert(instructions === request.instructions,
- `Wrong prompt instructions: ${instructions}`);
- assert.deepStrictEqual(
- prompts,
- request.prompts,
- `Wrong prompts: ${inspect(prompts)}`
- );
- process.nextTick(finish, responses);
- }));
- }
- // authHandler() tests =========================================================
- {
- const username = 'foo';
- const password = '1234';
- const clientKey = fixtureKey('id_rsa');
- const { server } = setup(
- 'authHandler() (sync)',
- {
- client: {
- username,
- password,
- privateKey: clientKey.raw,
- authHandler: mustCall((methodsLeft, partial, cb) => {
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- return 'none';
- }),
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username, `Wrong username: ${ctx.username}`);
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- ctx.accept();
- })).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const clientKey = fixtureKey('id_rsa');
- const { server } = setup(
- 'authHandler() (async)',
- {
- client: {
- username,
- password,
- privateKey: clientKey.raw,
- authHandler: mustCall((methodsLeft, partial, cb) => {
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- process.nextTick(mustCall(cb), 'none');
- }),
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username, `Wrong username: ${ctx.username}`);
- assert(ctx.method === 'none', `Wrong auth method: ${ctx.method}`);
- ctx.accept();
- })).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const clientKey = fixtureKey('id_rsa');
- const { client, server } = setup(
- 'authHandler() (no methods left -- sync)',
- {
- client: {
- username,
- password,
- privateKey: clientKey.raw,
- authHandler: mustCall((methodsLeft, partial, cb) => {
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- return false;
- }),
- },
- server: serverCfg,
- debug,
- noForceClientReady: true,
- noForceServerReady: true,
- }
- );
- // Remove default client error handler added by `setup()` since we are
- // expecting an error in this case
- client.removeAllListeners('error');
- client.on('error', mustCall((err) => {
- assert.strictEqual(err.level, 'client-authentication');
- assert(/configured authentication methods failed/i.test(err.message),
- 'Wrong error message');
- }));
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustNotCall())
- .on('ready', mustNotCall());
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const clientKey = fixtureKey('id_rsa');
- const { client, server } = setup(
- 'authHandler() (no methods left -- async)',
- {
- client: {
- username,
- password,
- privateKey: clientKey.raw,
- authHandler: mustCall((methodsLeft, partial, cb) => {
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- process.nextTick(mustCall(cb), false);
- }),
- },
- server: serverCfg,
- debug,
- noForceClientReady: true,
- noForceServerReady: true,
- }
- );
- // Remove default client error handler added by `setup()` since we are
- // expecting an error in this case
- client.removeAllListeners('error');
- client.on('error', mustCall((err) => {
- assert.strictEqual(err.level, 'client-authentication');
- assert(/configured authentication methods failed/i.test(err.message),
- 'Wrong error message');
- }));
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustNotCall())
- .on('ready', mustNotCall());
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const clientKey = fixtureKey('id_rsa');
- const events = [];
- const expectedEvents = [
- 'client', 'server', 'client', 'server'
- ];
- let clientCalls = 0;
- const { client, server } = setup(
- 'authHandler() (multi-step)',
- {
- client: {
- username,
- password,
- privateKey: clientKey.raw,
- authHandler: mustCall((methodsLeft, partial, cb) => {
- events.push('client');
- switch (++clientCalls) {
- case 1:
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- return 'publickey';
- case 2:
- assert.deepStrictEqual(
- methodsLeft,
- ['password'],
- `expected 'password' method left, saw: ${methodsLeft}`
- );
- assert(partial === true, 'expected partial success');
- return 'password';
- }
- }, 2),
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- let attempts = 0;
- conn.on('authentication', mustCall((ctx) => {
- assert(++attempts === clientCalls, 'server<->client state mismatch');
- assert(ctx.username === username,
- `Unexpected username: ${ctx.username}`);
- events.push('server');
- switch (attempts) {
- case 1:
- assert(ctx.method === 'publickey',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.key.algo === clientKey.key.type,
- `Unexpected key algo: ${ctx.key.algo}`);
- assert.deepEqual(clientKey.key.getPublicSSH(),
- ctx.key.data,
- 'Public key mismatch');
- ctx.reject(['password'], true);
- break;
- case 2:
- assert(ctx.method === 'password',
- `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === password,
- `Unexpected password: ${ctx.password}`);
- ctx.accept();
- break;
- }
- }, 2)).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- client.on('close', mustCall(() => {
- assert.deepStrictEqual(events, expectedEvents);
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const { server } = setup(
- 'authHandler() (custom auth configuration)',
- {
- client: {
- username: 'bar',
- password: '5678',
- authHandler: mustCall((methodsLeft, partial, cb) => {
- assert(methodsLeft === null, 'expected null methodsLeft');
- assert(partial === null, 'expected null partial');
- return {
- type: 'password',
- username,
- password,
- };
- }),
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username, `Wrong username: ${ctx.username}`);
- assert(ctx.method === 'password', `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === password, `Unexpected password: ${ctx.password}`);
- ctx.accept();
- })).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
- {
- const username = 'foo';
- const password = '1234';
- const { server } = setup(
- 'authHandler() (simple construction with custom auth configuration)',
- {
- client: {
- username: 'bar',
- password: '5678',
- authHandler: [{
- type: 'password',
- username,
- password,
- }],
- },
- server: serverCfg,
- debug,
- }
- );
- server.on('connection', mustCall((conn) => {
- conn.on('authentication', mustCall((ctx) => {
- assert(ctx.username === username, `Wrong username: ${ctx.username}`);
- assert(ctx.method === 'password', `Wrong auth method: ${ctx.method}`);
- assert(ctx.password === password, `Unexpected password: ${ctx.password}`);
- ctx.accept();
- })).on('ready', mustCall(() => {
- conn.end();
- }));
- }));
- }
|