Skip to content

Client: Allow explicitely specifying a publicKey #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ You can find more examples in the `examples` directory of this repository.

* **privateKey** - _mixed_ - _Buffer_ or _string_ that contains a private key for either key-based or hostbased user authentication (OpenSSH format). **Default:** (none)

* **publicKey** - _mixed_ - _Buffer_ or _string_ that contains a public key or SSH certificate for either key-based or hostbased user authentication (OpenSSH format). **Default:** (derived from private key)

* **passphrase** - _string_ - For an encrypted private key, this is the passphrase used to decrypt it. **Default:** (none)

* **localHostname** - _string_ - Along with **localUsername** and **privateKey**, set this to a non-empty string for hostbased user authentication. **Default:** (none)
Expand Down
31 changes: 26 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Client() {
username: undefined,
password: undefined,
privateKey: undefined,
publicKey: undefined,
tryKeyboard: undefined,
agent: undefined,
allowAgentFwd: undefined,
Expand Down Expand Up @@ -198,6 +199,10 @@ Client.prototype.connect = function(cfg) {
|| Buffer.isBuffer(cfg.privateKey)
? cfg.privateKey
: undefined);
this.config.publicKey = (typeof cfg.publicKey === 'string'
|| Buffer.isBuffer(cfg.publicKey)
? cfg.publicKey
: undefined);
this.config.localHostname = (typeof cfg.localHostname === 'string'
&& cfg.localHostname.length
? cfg.localHostname
Expand Down Expand Up @@ -235,7 +240,7 @@ Client.prototype.connect = function(cfg) {
this._agentFwdEnabled = false;
this._curChan = -1;
this._remoteVer = undefined;
var privateKey;
var privateKey, publicKey;

if (this.config.privateKey) {
privateKey = parseKey(this.config.privateKey, cfg.passphrase);
Expand All @@ -245,6 +250,22 @@ Client.prototype.connect = function(cfg) {
privateKey = privateKey[0]; // OpenSSH's newer format only stores 1 key for now
if (privateKey.getPrivatePEM() === null)
throw new Error('privateKey value does not contain a (valid) private key');

if (this.config.publicKey) {
publicKey = parseKey(this.config.publicKey);
if (publicKey instanceof Error)
throw new Error('Cannot parse publicKey: ' + publicKey.message);
if (Array.isArray(publicKey))
publicKey = publicKey[0]; // OpenSSH's newer format only stores 1 key for now
if (publicKey.getPublicSSH() === null)
throw new Error('publicKey value does not contain a (valid) public key');
if (publicKey.getPublicPEM() !== privateKey.getPublicPEM()) {
throw new Error('publicKey does not belong to the private key');
}
}
else {
publicKey = privateKey;
}
}

var stream = this._sshstream = new SSH2Stream({
Expand Down Expand Up @@ -383,7 +404,7 @@ Client.prototype.connect = function(cfg) {
var authsAllowed = ['none'];
if (this.config.password !== undefined)
authsAllowed.push('password');
if (privateKey !== undefined)
if (privateKey !== undefined && publicKey !== undefined)
authsAllowed.push('publickey');
if (this.config.agent !== undefined)
authsAllowed.push('agent');
Expand Down Expand Up @@ -425,7 +446,7 @@ Client.prototype.connect = function(cfg) {
stream.authPassword(self.config.username, self.config.password);
break;
case 'publickey':
stream.authPK(self.config.username, privateKey);
stream.authPK(self.config.username, publicKey);
stream.once('USERAUTH_PK_OK', onUSERAUTH_PK_OK);
break;
case 'hostbased':
Expand All @@ -442,7 +463,7 @@ Client.prototype.connect = function(cfg) {
cb(signature);
}
stream.authHostbased(self.config.username,
privateKey,
publicKey,
self.config.localHostname,
self.config.localUsername,
hostbasedCb);
Expand Down Expand Up @@ -569,7 +590,7 @@ Client.prototype.connect = function(cfg) {
});
});
} else if (curAuth === 'publickey') {
stream.authPK(self.config.username, privateKey, function(buf, cb) {
stream.authPK(self.config.username, publicKey, function(buf, cb) {
var signature = privateKey.sign(buf);
if (signature instanceof Error) {
signature.message = 'Error while signing data with privateKey: '
Expand Down