Skip to content

Commit

Permalink
lib,test: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed May 22, 2021
1 parent aac13eb commit 3f2c62d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 116 deletions.
73 changes: 22 additions & 51 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const { isParsedKey, parseKey } = require('./protocol/keyParser.js');

const {
makeBufferParser,
readString,
readUInt32BE,
writeUInt32BE,
writeUInt32LE,
Expand Down Expand Up @@ -40,7 +39,6 @@ const EMPTY_BUF = Buffer.alloc(0);
const binaryParser = makeBufferParser();

class BaseAgent {
constructor() {}
getIdentities(cb) {
cb(new Error('Missing getIdentities() implementation'));
}
Expand Down Expand Up @@ -71,7 +69,7 @@ class OpenSSHAgent extends BaseAgent {
function onFail() {
try {
sock.destroy();
} catch (ex) {}
} catch {}

cb(new Error('Failed to connect to agent'));
}
Expand All @@ -84,7 +82,7 @@ class OpenSSHAgent extends BaseAgent {
if (stream) {
try {
stream.destroy();
} catch (ex) {}
} catch {}
}
if (!err)
err = new Error('Failed to retrieve identities from agent');
Expand All @@ -107,7 +105,7 @@ class OpenSSHAgent extends BaseAgent {
return onFail(err);
try {
stream.destroy();
} catch (ex) {}
} catch {}
cb(null, keys);
});
});
Expand All @@ -127,7 +125,7 @@ class OpenSSHAgent extends BaseAgent {
if (stream) {
try {
stream.destroy();
} catch (ex) {}
} catch {}
}
if (!err)
err = new Error('Failed to sign data with agent');
Expand All @@ -151,7 +149,7 @@ class OpenSSHAgent extends BaseAgent {

try {
stream.destroy();
} catch (ex) {}
} catch {}

cb(null, sig);
});
Expand Down Expand Up @@ -246,7 +244,6 @@ const PageantAgent = (() => {
}

return class PageantAgent extends OpenSSHAgent {
constructor() {}
getStream(cb) {
cb(null, new PageantSocket());
}
Expand All @@ -257,9 +254,6 @@ const CygwinAgent = (() => {
const RE_CYGWIN_SOCK = /^!<socket >(\d+) s ([A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8})/;

return class CygwinAgent extends OpenSSHAgent {
constructor(socketPath) {
this.socketPath = socketPath;
}
getStream(cb) {
cb = once(cb);

Expand All @@ -278,7 +272,7 @@ const CygwinAgent = (() => {
// step 5 instead of zeroes.
// 9. Connection is ready to be used.

const socketPath = this.socketPath;
let socketPath = this.socketPath;
let triedCygpath = false;
readFile(socketPath, function readCygsocket(err, data) {
if (err) {
Expand Down Expand Up @@ -403,53 +397,30 @@ function createAgent(path) {
return new OpenSSHAgent(path);
}

/*
New Authentication/agent Design Plan:
* AgentProtocol
- Implements OpenSSH client/server protocol
* Agent
- For built-in implementations: communication layer + AgentProtocol
- Custom Agents need only implement `getIdentities(cb)` and
`sign(pubKey, data, cb)` (and optionally `getStream(cb)` for agent
forwarding ability)
- Keys must be in OpenSSH key binary format (initially)
- TODO: Also support parsed key from KeyParser
- TODO: Also support node's crypto.KeyObject, webcrypto.CryptoKey
* AgentContext
- Combines Agent instance with list of agent public keys and key
index/cursor
- Has `nextKey(cb)` and `sign(pubKey, data, cb)` methods
- This is what the ssh2 client code exclusively interacts with during the
authentication process
- `authhandler()` implementations use this internally to produce publickey
methods
*/
const AgentProtocol = (() => {
// Client->Server messages
const SSH_AGENTC_REQUEST_IDENTITIES = 11;
const SSH_AGENTC_SIGN_REQUEST = 13;
const SSH_AGENTC_ADD_IDENTITY = 17;
const SSH_AGENTC_REMOVE_IDENTITY = 18;
const SSH_AGENTC_REMOVE_ALL_IDENTITIES = 19;
const SSH_AGENTC_ADD_SMARTCARD_KEY = 20;
const SSH_AGENTC_REMOVE_SMARTCARD_KEY = 21;
const SSH_AGENTC_LOCK = 22;
const SSH_AGENTC_UNLOCK = 23;
const SSH_AGENTC_ADD_ID_CONSTRAINED = 25;
const SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED = 26;
const SSH_AGENTC_EXTENSION = 27;
// const SSH_AGENTC_ADD_IDENTITY = 17;
// const SSH_AGENTC_REMOVE_IDENTITY = 18;
// const SSH_AGENTC_REMOVE_ALL_IDENTITIES = 19;
// const SSH_AGENTC_ADD_SMARTCARD_KEY = 20;
// const SSH_AGENTC_REMOVE_SMARTCARD_KEY = 21;
// const SSH_AGENTC_LOCK = 22;
// const SSH_AGENTC_UNLOCK = 23;
// const SSH_AGENTC_ADD_ID_CONSTRAINED = 25;
// const SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED = 26;
// const SSH_AGENTC_EXTENSION = 27;
// Server->Client messages
const SSH_AGENT_FAILURE = 5;
const SSH_AGENT_SUCCESS = 6;
// const SSH_AGENT_SUCCESS = 6;
const SSH_AGENT_IDENTITIES_ANSWER = 12;
const SSH_AGENT_SIGN_RESPONSE = 14;
const SSH_AGENT_EXTENSION_FAILURE = 28;
// const SSH_AGENT_EXTENSION_FAILURE = 28;

const SSH_AGENT_CONSTRAIN_LIFETIME = 1;
const SSH_AGENT_CONSTRAIN_CONFIRM = 2;
const SSH_AGENT_CONSTRAIN_EXTENSION = 255;
// const SSH_AGENT_CONSTRAIN_LIFETIME = 1;
// const SSH_AGENT_CONSTRAIN_CONFIRM = 2;
// const SSH_AGENT_CONSTRAIN_EXTENSION = 255;

const SSH_AGENT_RSA_SHA2_256 = (1 << 1);
const SSH_AGENT_RSA_SHA2_512 = (1 << 2);
Expand Down Expand Up @@ -510,7 +481,7 @@ const AgentProtocol = (() => {
// Node streams hackery to make streams do the "right thing"
try {
protocol.end();
} catch (ex) {}
} catch {}
setImmediate(() => {
if (!protocol[SYM_ENDED])
protocol.emit('end');
Expand Down
5 changes: 4 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
SUPPORTED_SERVER_HOST_KEY,
} = require('./protocol/constants.js');
const Protocol = require('./protocol/Protocol.js');
const { isSupportedKeyType, parseKey } = require('./protocol/keyParser.js');
const { parseKey } = require('./protocol/keyParser.js');
const { SFTP } = require('./protocol/SFTP.js');
const {
makeError,
Expand Down Expand Up @@ -936,17 +936,20 @@ class Client extends EventEmitter {
hasSentAuth = false;
}
};

function skipAuth(msg) {
debug && debug(msg);
process.nextTick(tryNextAuth);
}

function tryNextAuth() {
hasSentAuth = false;
const auth = authHandler(curAuthsLeft, curPartial, doNextAuth);
if (hasSentAuth || auth === undefined)
return;
doNextAuth(auth);
}

const tryNextAgentKey = () => {
if (curAuth.type === 'agent') {
const key = curAuth.agentCtx.nextKey();
Expand Down
1 change: 0 additions & 1 deletion lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const {
bufferFill,
bufferSlice,
convertSignature,
readUInt32BE,
sendPacket,
writeUInt32BE,
} = require('./utils.js');
Expand Down
122 changes: 62 additions & 60 deletions lib/protocol/keyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,70 +1410,72 @@ function isParsedKey(val) {
return (typeof val[SYM_DECRYPTED] === 'boolean');
}

module.exports = {
isParsedKey,
isSupportedKeyType,
parseDERKey: (data, type) => parseDER(data, type, '', type),
parseKey: (data, passphrase) => {
if (isParsedKey(data))
return data;

let origBuffer;
if (Buffer.isBuffer(data)) {
origBuffer = data;
data = data.utf8Slice(0, data.length).trim();
} else if (typeof data === 'string') {
data = data.trim();
} else {
return new Error('Key data must be a Buffer or string');
}

// eslint-disable-next-line eqeqeq
if (passphrase != undefined) {
if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase);
else if (!Buffer.isBuffer(passphrase))
return new Error('Passphrase must be a string or Buffer when supplied');
}
function parseKey(data, passphrase) {
if (isParsedKey(data))
return data;

let origBuffer;
if (Buffer.isBuffer(data)) {
origBuffer = data;
data = data.utf8Slice(0, data.length).trim();
} else if (typeof data === 'string') {
data = data.trim();
} else {
return new Error('Key data must be a Buffer or string');
}

let ret;
// eslint-disable-next-line eqeqeq
if (passphrase != undefined) {
if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase);
else if (!Buffer.isBuffer(passphrase))
return new Error('Passphrase must be a string or Buffer when supplied');
}

// First try as printable string format (e.g. PEM)

// Private keys
if ((ret = OpenSSH_Private.parse(data, passphrase)) !== null)
return ret;
if ((ret = OpenSSH_Old_Private.parse(data, passphrase)) !== null)
return ret;
if ((ret = PPK_Private.parse(data, passphrase)) !== null)
return ret;

// Public keys
if ((ret = OpenSSH_Public.parse(data)) !== null)
return ret;
if ((ret = RFC4716_Public.parse(data)) !== null)
return ret;

// Finally try as a binary format if we were originally passed binary data
if (origBuffer) {
binaryKeyParser.init(origBuffer, 0);
const type = binaryKeyParser.readString(true);
if (type !== undefined) {
data = binaryKeyParser.readRaw();
if (data !== undefined) {
ret = parseDER(data, type, '', type);
// Ignore potentially useless errors in case the data was not actually
// in the binary format
if (ret instanceof Error)
ret = null;
}
let ret;

// First try as printable string format (e.g. PEM)

// Private keys
if ((ret = OpenSSH_Private.parse(data, passphrase)) !== null)
return ret;
if ((ret = OpenSSH_Old_Private.parse(data, passphrase)) !== null)
return ret;
if ((ret = PPK_Private.parse(data, passphrase)) !== null)
return ret;

// Public keys
if ((ret = OpenSSH_Public.parse(data)) !== null)
return ret;
if ((ret = RFC4716_Public.parse(data)) !== null)
return ret;

// Finally try as a binary format if we were originally passed binary data
if (origBuffer) {
binaryKeyParser.init(origBuffer, 0);
const type = binaryKeyParser.readString(true);
if (type !== undefined) {
data = binaryKeyParser.readRaw();
if (data !== undefined) {
ret = parseDER(data, type, '', type);
// Ignore potentially useless errors in case the data was not actually
// in the binary format
if (ret instanceof Error)
ret = null;
}
binaryKeyParser.clear();
}
binaryKeyParser.clear();
}

if (ret)
return ret;
if (ret)
return ret;

return new Error('Unsupported key format');
},
return new Error('Unsupported key format');
}

module.exports = {
isParsedKey,
isSupportedKeyType,
parseDERKey: (data, type) => parseDER(data, type, '', type),
parseKey,
};
3 changes: 0 additions & 3 deletions test/test-userauth-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const clientKey = fixtureKey('openssh_new_rsa');
let getIdentitiesCount = 0;
let signCount = 0;
class MyAgent extends BaseAgent {
constructor() {
super();
}
getIdentities(cb) {
assert.strictEqual(++getIdentitiesCount, 1);
// Ensure that no private portion of the key is used by re-parsing the
Expand Down

0 comments on commit 3f2c62d

Please sign in to comment.