Files
Netcatty/patches/ssh2+1.17.0.patch
T
陈大猫 8d826594d3 fix(ssh): support ChaCha20 in Electron crypto runtimes (#3992)
* fix(ssh): support ChaCha20 in Electron crypto runtimes

* test(ssh): verify packaged cipher transport across platforms

* test(ssh): isolate unused Electron UI APIs in packaged transport harness

* test(ssh): isolate packaged dependency resolution from the checkout
2026-10-27 01:59:11 +08:00

1745 lines
63 KiB
Diff

diff --git a/node_modules/ssh2/lib/agent.js b/node_modules/ssh2/lib/agent.js
index bb495d1..32c48a0 100644
--- a/node_modules/ssh2/lib/agent.js
+++ b/node_modules/ssh2/lib/agent.js
@@ -6,7 +6,7 @@ const { resolve } = require('path');
const { readFile } = require('fs');
const { execFile, spawn } = require('child_process');
-const { isParsedKey, parseKey } = require('./protocol/keyParser.js');
+const { isParsedKey, parseKey, getOpenSSHCertSignatureAlgorithm } = require('./protocol/keyParser.js');
const {
makeBufferParser,
@@ -655,8 +655,12 @@ const AgentProtocol = (() => {
// TODO: verify signature type based on key and options used
// during initial sign request
binaryParser.init(signature, 0);
- binaryParser.readString(true);
- signature = binaryParser.readString();
+ const signatureType = binaryParser.readString(true);
+ // SK signatures append flags/counter outside the inner string.
+ // Preserve the complete algorithm-specific payload for authPK.
+ signature = signatureType && signatureType.startsWith('sk-')
+ ? signature.subarray(binaryParser.pos())
+ : binaryParser.readString();
binaryParser.clear();
if (signature === undefined)
@@ -711,7 +715,10 @@ const AgentProtocol = (() => {
hash: undefined,
};
let ctx;
- if (pubKey.type === 'ssh-rsa') {
+ if (pubKey.type === 'ssh-rsa'
+ || pubKey.type === 'ssh-rsa-cert-v01@openssh.com'
+ || pubKey.type === 'rsa-sha2-256-cert-v01@openssh.com'
+ || pubKey.type === 'rsa-sha2-512-cert-v01@openssh.com') {
if (flagsVal & SSH_AGENT_RSA_SHA2_256) {
ctx = 'rsa-sha2-256';
flags.hash = 'sha256';
@@ -721,7 +728,7 @@ const AgentProtocol = (() => {
}
}
if (ctx === undefined)
- ctx = pubKey.type;
+ ctx = getOpenSSHCertSignatureAlgorithm(pubKey.type);
const req = new AgentInboundRequest(msgType, ctx);
this[SYM_REQS].push(req);
@@ -732,6 +739,9 @@ const AgentProtocol = (() => {
default: {
const req = new AgentInboundRequest(msgType);
this[SYM_REQS].push(req);
+ // Unknown extensions can carry data. Consume their entire frame
+ // before reading the next request on this connection.
+ p = 4 + this[SYM_MSGLEN];
this.failureReply(req);
}
}
@@ -781,7 +791,11 @@ const AgentProtocol = (() => {
if (pubKey instanceof Error)
throw new Error('Invalid public key argument');
- if (pubKey.type === 'ssh-rsa' && options) {
+ if ((pubKey.type === 'ssh-rsa'
+ || pubKey.type === 'ssh-rsa-cert-v01@openssh.com'
+ || pubKey.type === 'rsa-sha2-256-cert-v01@openssh.com'
+ || pubKey.type === 'rsa-sha2-512-cert-v01@openssh.com')
+ && options) {
switch (options.hash) {
case 'sha256':
flags = SSH_AGENT_RSA_SHA2_256;
@@ -992,19 +1006,24 @@ const AgentProtocol = (() => {
let p = 0;
const sigFormat = req.getContext();
const sigFormatLen = Buffer.byteLength(sigFormat);
+ const sigPrefixLen = sigFormat.startsWith('sk-') ? 0 : 4;
const buf = Buffer.allocUnsafe(
- 4 + 1 + 4 + 4 + sigFormatLen + 4 + signature.length
+ 4 + 1 + 4 + 4 + sigFormatLen + sigPrefixLen + signature.length
);
writeUInt32BE(buf, buf.length - 4, p);
buf[p += 4] = SSH_AGENT_SIGN_RESPONSE;
- writeUInt32BE(buf, 4 + sigFormatLen + 4 + signature.length, ++p);
+ writeUInt32BE(buf, 4 + sigFormatLen + sigPrefixLen + signature.length, ++p);
writeUInt32BE(buf, sigFormatLen, p += 4);
buf.utf8Write(sigFormat, p += 4, sigFormatLen);
- writeUInt32BE(buf, signature.length, p += sigFormatLen);
- signature.copy(buf, p += 4);
+ p += sigFormatLen;
+ if (sigPrefixLen) {
+ writeUInt32BE(buf, signature.length, p);
+ p += 4;
+ }
+ signature.copy(buf, p);
return respond(this, req, buf);
}
diff --git a/node_modules/ssh2/lib/client.js b/node_modules/ssh2/lib/client.js
index 7291c2c..8943c9a 100644
--- a/node_modules/ssh2/lib/client.js
+++ b/node_modules/ssh2/lib/client.js
@@ -2170,6 +2170,29 @@ function getKeyAlgos(client, key, serverSigAlgs) {
return [];
}
return [['ssh-rsa', 'sha1']];
+
+ // OpenSSH RSA user certificates
+ // - publickey algorithm name must be the *cert* variant
+ // - signature algorithm uses the non-cert variant (handled in Protocol.js)
+ case 'ssh-rsa-cert-v01@openssh.com':
+ case 'rsa-sha2-256-cert-v01@openssh.com':
+ case 'rsa-sha2-512-cert-v01@openssh.com':
+ if (client._protocol._compatFlags & COMPAT.IMPLY_RSA_SHA2_SIGALGS) {
+ if (!Array.isArray(serverSigAlgs))
+ serverSigAlgs = ['rsa-sha2-256', 'rsa-sha2-512'];
+ else
+ serverSigAlgs = ['rsa-sha2-256', 'rsa-sha2-512', ...serverSigAlgs];
+ }
+ if (Array.isArray(serverSigAlgs)) {
+ // Prefer SHA-512 when available (matches modern OpenSSH default)
+ if (serverSigAlgs.indexOf('rsa-sha2-512') !== -1)
+ return [['rsa-sha2-512-cert-v01@openssh.com', 'sha512']];
+ if (serverSigAlgs.indexOf('rsa-sha2-256') !== -1)
+ return [['rsa-sha2-256-cert-v01@openssh.com', 'sha256']];
+ if (serverSigAlgs.indexOf('ssh-rsa') === -1)
+ return [];
+ }
+ return [['ssh-rsa-cert-v01@openssh.com', 'sha1']];
}
}
diff --git a/node_modules/ssh2/lib/protocol/Protocol.js b/node_modules/ssh2/lib/protocol/Protocol.js
index 7302488..df0c9b3 100644
--- a/node_modules/ssh2/lib/protocol/Protocol.js
+++ b/node_modules/ssh2/lib/protocol/Protocol.js
@@ -58,6 +58,7 @@ const {
} = require('./kex.js');
const {
parseKey,
+ getOpenSSHCertSignatureAlgorithm,
} = require('./keyParser.js');
const MESSAGE_HANDLERS = require('./handlers.js');
const {
@@ -701,11 +702,19 @@ class Protocol {
if (signature === false)
throw new Error('Error while converting handshake signature');
+ let sigAlgo = keyAlgo;
+ if (signature && typeof signature === 'object' && signature._signatureAlgorithm)
+ sigAlgo = signature._signatureAlgorithm;
+ else
+ sigAlgo = getOpenSSHCertSignatureAlgorithm(keyAlgo);
+
const sigLen = signature.length;
+ const sigPrefixLen = sigAlgo.startsWith('sk-') ? 0 : 4;
+ const sigAlgoLen = Buffer.byteLength(sigAlgo);
p = this._packetRW.write.allocStart;
packet = this._packetRW.write.alloc(
1 + 4 + userLen + 4 + 14 + 4 + 9 + 1 + 4 + algoLen + 4 + pubKeyLen + 4
- + 4 + algoLen + 4 + sigLen
+ + 4 + sigAlgoLen + sigPrefixLen + sigLen
);
// TODO: simply copy from original "packet" to new `packet` to avoid
@@ -729,13 +738,17 @@ class Protocol {
writeUInt32BE(packet, pubKeyLen, p += algoLen);
packet.set(pubKey, p += 4);
- writeUInt32BE(packet, 4 + algoLen + 4 + sigLen, p += pubKeyLen);
+ writeUInt32BE(packet, 4 + sigAlgoLen + sigPrefixLen + sigLen, p += pubKeyLen);
- writeUInt32BE(packet, algoLen, p += 4);
- packet.utf8Write(keyAlgo, p += 4, algoLen);
+ writeUInt32BE(packet, sigAlgoLen, p += 4);
+ packet.utf8Write(sigAlgo, p += 4, sigAlgoLen);
- writeUInt32BE(packet, sigLen, p += algoLen);
- packet.set(signature, p += 4);
+ p += sigAlgoLen;
+ if (sigPrefixLen) {
+ writeUInt32BE(packet, sigLen, p);
+ p += 4;
+ }
+ packet.set(signature, p);
// Servers shouldn't send packet type 60 in response to signed publickey
// attempts, but if they do, interpret as type 60.
@@ -810,20 +823,32 @@ class Protocol {
if (!signature)
throw new Error('Error while converting handshake signature');
+ let sigAlgo = keyAlgo;
+ if (signature && typeof signature === 'object' && signature._signatureAlgorithm)
+ sigAlgo = signature._signatureAlgorithm;
+ else
+ sigAlgo = getOpenSSHCertSignatureAlgorithm(keyAlgo);
+
const sigLen = signature.length;
+ const sigPrefixLen = sigAlgo.startsWith('sk-') ? 0 : 4;
+ const sigAlgoLen = Buffer.byteLength(sigAlgo);
const reqDataLen = (data.length - sesLen - 4);
p = this._packetRW.write.allocStart;
const packet = this._packetRW.write.alloc(
- reqDataLen + 4 + 4 + algoLen + 4 + sigLen
+ reqDataLen + 4 + 4 + sigAlgoLen + sigPrefixLen + sigLen
);
bufferCopy(data, packet, 4 + sesLen, data.length, p);
- writeUInt32BE(packet, 4 + algoLen + 4 + sigLen, p += reqDataLen);
- writeUInt32BE(packet, algoLen, p += 4);
- packet.utf8Write(keyAlgo, p += 4, algoLen);
- writeUInt32BE(packet, sigLen, p += algoLen);
- packet.set(signature, p += 4);
+ writeUInt32BE(packet, 4 + sigAlgoLen + sigPrefixLen + sigLen, p += reqDataLen);
+ writeUInt32BE(packet, sigAlgoLen, p += 4);
+ packet.utf8Write(sigAlgo, p += 4, sigAlgoLen);
+ p += sigAlgoLen;
+ if (sigPrefixLen) {
+ writeUInt32BE(packet, sigLen, p);
+ p += 4;
+ }
+ packet.set(signature, p);
this._authsQueue.push('hostbased');
@@ -1916,7 +1941,10 @@ class Protocol {
}
// SSH-protoversion-softwareversion (SP comments) CR LF
-const RE_IDENT = /^SSH-(2\.0|1\.99)-([^ ]+)(?: (.*))?$/;
+// RFC 4253 requires a non-empty softwareversion, but some embedded SSH
+// daemons send "SSH-2.0-" with an empty token. Accept that specific
+// compatibility case while still rejecting whitespace in the token itself.
+const RE_IDENT = /^SSH-(2\.0|1\.99)-([^ ]*)(?: (.*))?$/;
// TODO: optimize this by starting n bytes from the end of this._buffer instead
// of the beginning
diff --git a/node_modules/ssh2/lib/protocol/SFTP.js b/node_modules/ssh2/lib/protocol/SFTP.js
index 9f33c02..9751164 100644
--- a/node_modules/ssh2/lib/protocol/SFTP.js
+++ b/node_modules/ssh2/lib/protocol/SFTP.js
@@ -117,6 +117,20 @@ const OPENSSH_MAX_PKT_LEN = 256 * 1024;
const bufferParser = makeBufferParser();
+function getStringLen(value, encoding) {
+ if (Buffer.isBuffer(value)) return value.length;
+ // Honor encoding parameter if provided, otherwise default to UTF-8
+ return encoding ? Buffer.byteLength(value, encoding) : Buffer.byteLength(value);
+}
+
+function writeString(buf, offset, value, len) {
+ if (Buffer.isBuffer(value)) {
+ buf.set(value, offset);
+ return len ?? value.length;
+ }
+ return buf.utf8Write(value, offset, len ?? Buffer.byteLength(value));
+}
+
const fakeStderr = {
readable: false,
writable: false,
@@ -155,6 +169,8 @@ class SFTP extends EventEmitter {
this._writeReqid = -1;
this._requests = {};
this._maxInPktLen = OPENSSH_MAX_PKT_LEN;
+ this._preambleSkipped = false; // Track if we've found the start of SFTP binary data
+ this._preambleBuf = null; // Buffer for partial preamble data across frames
this._maxOutPktLen = 34000;
this._maxReadLen =
(this._isOpenSSH ? OPENSSH_MAX_PKT_LEN : 34000) - PKT_RW_OVERHEAD;
@@ -196,6 +212,53 @@ class SFTP extends EventEmitter {
this.emit('end');
return;
}
+
+ // Skip non-SFTP preamble data (e.g. MOTD/banner text from misconfigured servers)
+ // Only applies to client mode; server mode expects SSH_FXP_INIT directly.
+ if (!this._preambleSkipped) {
+ if (this.server) {
+ // Server mode: no preamble skipping, proceed to normal parsing
+ this._preambleSkipped = true;
+ } else {
+ // Concatenate with any previously buffered partial data
+ if (this._preambleBuf) {
+ data = Buffer.concat([this._preambleBuf, data]);
+ this._preambleBuf = null;
+ }
+
+ // Look for the start of a valid SFTP packet in the data.
+ // The first SFTP packet from the server is SSH_FXP_VERSION (type=2).
+ // Format: uint32 length, byte type=0x02, uint32 version, ...
+ // The length should be >= 5 (1 byte type + 4 bytes version).
+ let found = -1;
+ for (let i = 0; i <= data.length - 5; i++) {
+ const len = (data[i] << 24) | (data[i+1] << 16) | (data[i+2] << 8) | data[i+3];
+ if (len >= 5 && len <= this._maxInPktLen && data[i+4] === 0x02) {
+ found = i;
+ break;
+ }
+ }
+ if (found === -1) {
+ // No valid SFTP packet header found yet.
+ // Keep up to the last 4 bytes in case a valid header spans this and the
+ // next chunk (the uint32 length could be split across frames).
+ const keep = Math.min(data.length, 4);
+ this._preambleBuf = Buffer.from(data.slice(data.length - keep));
+ this._debug && this._debug(
+ 'SFTP: Skipping non-SFTP preamble data (' + data.length + ' bytes, buffered last ' + keep + ')'
+ );
+ return;
+ }
+ if (found > 0) {
+ this._debug && this._debug(
+ 'SFTP: Skipped ' + found + ' bytes of non-SFTP preamble data'
+ );
+ data = data.slice(found);
+ }
+ this._preambleSkipped = true;
+ }
+ }
+
/*
uint32 length
byte type
@@ -339,7 +402,7 @@ class SFTP extends EventEmitter {
uint32 pflags
ATTRS attrs
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen + 4 + 4 + attrsLen);
@@ -349,7 +412,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
writeUInt32BE(buf, flags, p += pathLen);
writeUInt32BE(buf, attrsFlags, p += 4);
if (attrsLen) {
@@ -734,7 +797,7 @@ class SFTP extends EventEmitter {
uint32 id
string filename
*/
- const fnameLen = Buffer.byteLength(filename);
+ const fnameLen = getStringLen(filename);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + fnameLen);
@@ -744,7 +807,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, fnameLen, p);
- buf.utf8Write(filename, p += 4, fnameLen);
+ writeString(buf, p += 4, filename, fnameLen);
this._requests[reqid] = { cb };
@@ -762,8 +825,8 @@ class SFTP extends EventEmitter {
string oldpath
string newpath
*/
- const oldLen = Buffer.byteLength(oldPath);
- const newLen = Buffer.byteLength(newPath);
+ const oldLen = getStringLen(oldPath);
+ const newLen = getStringLen(newPath);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + oldLen + 4 + newLen);
@@ -773,9 +836,9 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, oldLen, p);
- buf.utf8Write(oldPath, p += 4, oldLen);
+ writeString(buf, p += 4, oldPath, oldLen);
writeUInt32BE(buf, newLen, p += oldLen);
- buf.utf8Write(newPath, p += 4, newLen);
+ writeString(buf, p += 4, newPath, newLen);
this._requests[reqid] = { cb };
@@ -806,7 +869,7 @@ class SFTP extends EventEmitter {
string path
ATTRS attrs
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen + 4 + attrsLen);
@@ -816,7 +879,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
writeUInt32BE(buf, flags, p += pathLen);
if (attrsLen) {
p += 4;
@@ -844,7 +907,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -854,7 +917,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = { cb };
@@ -987,7 +1050,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -997,7 +1060,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = { cb };
@@ -1014,7 +1077,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -1024,7 +1087,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = { cb };
@@ -1041,7 +1104,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -1051,7 +1114,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = { cb };
@@ -1080,7 +1143,7 @@ class SFTP extends EventEmitter {
string path
ATTRS attrs
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen + 4 + attrsLen);
@@ -1090,7 +1153,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
writeUInt32BE(buf, flags, p += pathLen);
if (attrsLen) {
p += 4;
@@ -1205,7 +1268,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -1215,7 +1278,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = {
cb: (err, names) => {
@@ -1243,8 +1306,8 @@ class SFTP extends EventEmitter {
string linkpath
string targetpath
*/
- const linkLen = Buffer.byteLength(linkPath);
- const targetLen = Buffer.byteLength(targetPath);
+ const linkLen = getStringLen(linkPath);
+ const targetLen = getStringLen(targetPath);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + linkLen + 4 + targetLen);
@@ -1256,14 +1319,14 @@ class SFTP extends EventEmitter {
if (this._isOpenSSH) {
// OpenSSH has linkpath and targetpath positions switched
writeUInt32BE(buf, targetLen, p);
- buf.utf8Write(targetPath, p += 4, targetLen);
+ writeString(buf, p += 4, targetPath, targetLen);
writeUInt32BE(buf, linkLen, p += targetLen);
- buf.utf8Write(linkPath, p += 4, linkLen);
+ writeString(buf, p += 4, linkPath, linkLen);
} else {
writeUInt32BE(buf, linkLen, p);
- buf.utf8Write(linkPath, p += 4, linkLen);
+ writeString(buf, p += 4, linkPath, linkLen);
writeUInt32BE(buf, targetLen, p += linkLen);
- buf.utf8Write(targetPath, p += 4, targetLen);
+ writeString(buf, p += 4, targetPath, targetLen);
}
this._requests[reqid] = { cb };
@@ -1281,7 +1344,7 @@ class SFTP extends EventEmitter {
uint32 id
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + pathLen);
@@ -1291,7 +1354,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, pathLen, p);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = {
cb: (err, names) => {
@@ -1325,8 +1388,8 @@ class SFTP extends EventEmitter {
string oldpath
string newpath
*/
- const oldLen = Buffer.byteLength(oldPath);
- const newLen = Buffer.byteLength(newPath);
+ const oldLen = getStringLen(oldPath);
+ const newLen = getStringLen(newPath);
let p = 9;
const buf =
Buffer.allocUnsafe(4 + 1 + 4 + 4 + 24 + 4 + oldLen + 4 + newLen);
@@ -1337,11 +1400,11 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 24, p);
- buf.utf8Write('posix-rename@openssh.com', p += 4, 24);
+ writeString(buf, p += 4, 'posix-rename@openssh.com', 24);
writeUInt32BE(buf, oldLen, p += 24);
- buf.utf8Write(oldPath, p += 4, oldLen);
+ writeString(buf, p += 4, oldPath, oldLen);
writeUInt32BE(buf, newLen, p += oldLen);
- buf.utf8Write(newPath, p += 4, newLen);
+ writeString(buf, p += 4, newPath, newLen);
this._requests[reqid] = { cb };
@@ -1364,7 +1427,7 @@ class SFTP extends EventEmitter {
string "statvfs@openssh.com"
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 19 + 4 + pathLen);
@@ -1374,9 +1437,9 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 19, p);
- buf.utf8Write('statvfs@openssh.com', p += 4, 19);
+ writeString(buf, p += 4, 'statvfs@openssh.com', 19);
writeUInt32BE(buf, pathLen, p += 19);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = { extended: 'statvfs@openssh.com', cb };
@@ -1411,7 +1474,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 20, p);
- buf.utf8Write('fstatvfs@openssh.com', p += 4, 20);
+ writeString(buf, p += 4, 'fstatvfs@openssh.com', 20);
writeUInt32BE(buf, handleLen, p += 20);
buf.set(handle, p += 4);
@@ -1437,8 +1500,8 @@ class SFTP extends EventEmitter {
string oldpath
string newpath
*/
- const oldLen = Buffer.byteLength(oldPath);
- const newLen = Buffer.byteLength(newPath);
+ const oldLen = getStringLen(oldPath);
+ const newLen = getStringLen(newPath);
let p = 9;
const buf =
Buffer.allocUnsafe(4 + 1 + 4 + 4 + 20 + 4 + oldLen + 4 + newLen);
@@ -1449,11 +1512,11 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 20, p);
- buf.utf8Write('hardlink@openssh.com', p += 4, 20);
+ writeString(buf, p += 4, 'hardlink@openssh.com', 20);
writeUInt32BE(buf, oldLen, p += 20);
- buf.utf8Write(oldPath, p += 4, oldLen);
+ writeString(buf, p += 4, oldPath, oldLen);
writeUInt32BE(buf, newLen, p += oldLen);
- buf.utf8Write(newPath, p += 4, newLen);
+ writeString(buf, p += 4, newPath, newLen);
this._requests[reqid] = { cb };
@@ -1488,7 +1551,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 17, p);
- buf.utf8Write('fsync@openssh.com', p += 4, 17);
+ writeString(buf, p += 4, 'fsync@openssh.com', 17);
writeUInt32BE(buf, handleLen, p += 17);
buf.set(handle, p += 4);
@@ -1524,7 +1587,7 @@ class SFTP extends EventEmitter {
string path
ATTRS attrs
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf =
Buffer.allocUnsafe(4 + 1 + 4 + 4 + 20 + 4 + pathLen + 4 + attrsLen);
@@ -1535,10 +1598,10 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 20, p);
- buf.utf8Write('lsetstat@openssh.com', p += 4, 20);
+ writeString(buf, p += 4, 'lsetstat@openssh.com', 20);
writeUInt32BE(buf, pathLen, p += 20);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
writeUInt32BE(buf, flags, p += pathLen);
if (attrsLen) {
@@ -1573,7 +1636,7 @@ class SFTP extends EventEmitter {
string "expand-path@openssh.com"
string path
*/
- const pathLen = Buffer.byteLength(path);
+ const pathLen = getStringLen(path);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 23 + 4 + pathLen);
@@ -1583,10 +1646,10 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 23, p);
- buf.utf8Write('expand-path@openssh.com', p += 4, 23);
+ writeString(buf, p += 4, 'expand-path@openssh.com', 23);
writeUInt32BE(buf, pathLen, p += 20);
- buf.utf8Write(path, p += 4, pathLen);
+ writeString(buf, p += 4, path, pathLen);
this._requests[reqid] = {
cb: (err, names) => {
@@ -1653,7 +1716,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, 9, p);
p += 4;
- buf.utf8Write('copy-data', p, 9);
+ writeString(buf, p, 'copy-data', 9);
p += 9;
writeUInt32BE(buf, srcHandle.length, p);
@@ -1708,7 +1771,7 @@ class SFTP extends EventEmitter {
string username
*/
let p = 0;
- const usernameLen = Buffer.byteLength(username);
+ const usernameLen = getStringLen(username);
const buf = Buffer.allocUnsafe(
4 + 1
+ 4
@@ -1728,12 +1791,12 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, 14, p);
p += 4;
- buf.utf8Write('home-directory', p, 14);
+ writeString(buf, p, 'home-directory', 14);
p += 14;
writeUInt32BE(buf, usernameLen, p);
p += 4;
- buf.utf8Write(username, p, usernameLen);
+ writeString(buf, p, username, usernameLen);
p += usernameLen;
this._requests[reqid] = {
@@ -1806,7 +1869,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, 30, p);
p += 4;
- buf.utf8Write('users-groups-by-id@openssh.com', p, 30);
+ writeString(buf, p, 'users-groups-by-id@openssh.com', 30);
p += 30;
writeUInt32BE(buf, 4 * uids.length, p);
@@ -1871,7 +1934,7 @@ class SFTP extends EventEmitter {
message || (message = '');
- const msgLen = Buffer.byteLength(message);
+ const msgLen = getStringLen(message);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + 4 + msgLen + 4);
@@ -1884,7 +1947,7 @@ class SFTP extends EventEmitter {
writeUInt32BE(buf, msgLen, p += 4);
p += 4;
if (msgLen) {
- buf.utf8Write(message, p, msgLen);
+ writeString(buf, p, message, msgLen);
p += msgLen;
}
@@ -1913,7 +1976,7 @@ class SFTP extends EventEmitter {
const dataLen = (
isBuffer
? data.length
- : Buffer.byteLength(data, encoding)
+ : getStringLen(data, encoding)
);
let p = 9;
const buf = Buffer.allocUnsafe(4 + 1 + 4 + 4 + dataLen);
@@ -1927,7 +1990,7 @@ class SFTP extends EventEmitter {
if (isBuffer)
buf.set(data, p += 4);
else if (isUTF8)
- buf.utf8Write(data, p += 4, dataLen);
+ writeString(buf, p += 4, data, dataLen);
else
buf.write(data, p += 4, dataLen, encoding);
}
@@ -1959,13 +2022,13 @@ class SFTP extends EventEmitter {
? ''
: name.filename
);
- namesLen += 4 + Buffer.byteLength(filename);
+ namesLen += 4 + getStringLen(filename);
const longname = (
!name || !name.longname || typeof name.longname !== 'string'
? ''
: name.longname
);
- namesLen += 4 + Buffer.byteLength(longname);
+ namesLen += 4 + getStringLen(longname);
if (typeof name.attrs === 'object' && name.attrs !== null) {
nameAttrs = attrsToBytes(name.attrs);
@@ -2011,11 +2074,11 @@ class SFTP extends EventEmitter {
? ''
: name.filename
);
- const len = Buffer.byteLength(filename);
+ const len = getStringLen(filename);
writeUInt32BE(buf, len, p);
p += 4;
if (len) {
- buf.utf8Write(filename, p, len);
+ writeString(buf, p, filename, len);
p += len;
}
}
@@ -2026,11 +2089,11 @@ class SFTP extends EventEmitter {
? ''
: name.longname
);
- const len = Buffer.byteLength(longname);
+ const len = getStringLen(longname);
writeUInt32BE(buf, len, p);
p += 4;
if (len) {
- buf.utf8Write(longname, p, len);
+ writeString(buf, p, longname, len);
p += len;
}
}
@@ -2749,7 +2812,7 @@ function requestLimits(sftp, cb) {
writeUInt32BE(buf, reqid, 5);
writeUInt32BE(buf, 18, p);
- buf.utf8Write('limits@openssh.com', p += 4, 18);
+ writeString(buf, p += 4, 'limits@openssh.com', 18);
sftp._requests[reqid] = { extended: 'limits@openssh.com', cb };
@@ -2953,18 +3016,28 @@ const CLIENT_HANDLERS = {
// spec not specifying an encoding because the specs for newer
// versions of the protocol all explicitly specify UTF-8 for
// filenames
- const filename = bufferParser.readString(true);
+ const filenameRaw = bufferParser.readString();
+ if (filenameRaw === undefined) {
+ names = undefined;
+ break;
+ }
+ const filename = filenameRaw.toString('utf8');
// `longname` only exists in SFTPv3 and since it typically will
// contain the filename, we assume it is also UTF-8
- const longname = bufferParser.readString(true);
+ const longnameRaw = bufferParser.readString();
+ if (longnameRaw === undefined) {
+ names = undefined;
+ break;
+ }
+ const longname = longnameRaw.toString('utf8');
const attrs = readAttrs(sftp._biOpt);
if (attrs === undefined) {
names = undefined;
break;
}
- names.push({ filename, longname, attrs });
+ names.push({ filename, longname, filenameRaw, longnameRaw, attrs });
}
if (names !== undefined) {
sftp._debug && sftp._debug(
diff --git a/node_modules/ssh2/lib/protocol/constants.js b/node_modules/ssh2/lib/protocol/constants.js
index ad77592..03ab33b 100644
--- a/node_modules/ssh2/lib/protocol/constants.js
+++ b/node_modules/ssh2/lib/protocol/constants.js
@@ -71,14 +71,37 @@ const DEFAULT_SERVER_HOST_KEY = [
];
if (eddsaSupported)
DEFAULT_SERVER_HOST_KEY.unshift('ssh-ed25519');
+// OpenSSH host certificates, offered before the matching plain key so a
+// cert-only server (for example ssh-ed25519-cert-v01@openssh.com) can
+// negotiate. Plain keys stay as fallbacks.
+const OPENSSH_HOST_CERT_ALGS = [
+ 'ecdsa-sha2-nistp256-cert-v01@openssh.com',
+ 'ecdsa-sha2-nistp384-cert-v01@openssh.com',
+ 'ecdsa-sha2-nistp521-cert-v01@openssh.com',
+ 'rsa-sha2-512-cert-v01@openssh.com',
+ 'rsa-sha2-256-cert-v01@openssh.com',
+ 'ssh-rsa-cert-v01@openssh.com',
+];
+if (eddsaSupported)
+ OPENSSH_HOST_CERT_ALGS.unshift('ssh-ed25519-cert-v01@openssh.com');
+DEFAULT_SERVER_HOST_KEY.unshift(...OPENSSH_HOST_CERT_ALGS);
const SUPPORTED_SERVER_HOST_KEY = DEFAULT_SERVER_HOST_KEY.concat([
'ssh-dss',
+ // FIDO/U2F security keys (client publickey auth / rare host keys).
+ 'sk-ecdsa-sha2-nistp256@openssh.com',
+ 'sk-ecdsa-sha2-nistp256-cert-v01@openssh.com',
]);
+if (eddsaSupported) {
+ SUPPORTED_SERVER_HOST_KEY.push('sk-ssh-ed25519@openssh.com');
+ SUPPORTED_SERVER_HOST_KEY.push('sk-ssh-ed25519-cert-v01@openssh.com');
+}
const canUseCipher = (() => {
const ciphers = crypto.getCiphers();
- return (name) => ciphers.includes(CIPHER_INFO[name].sslName);
+ // crypto.js supplies the original ChaCha20 primitive on BoringSSL too.
+ return (name) => (name === 'chacha20-poly1305@openssh.com'
+ || ciphers.includes(CIPHER_INFO[name].sslName));
})();
let DEFAULT_CIPHER = [
// http://tools.ietf.org/html/rfc5647
@@ -160,6 +183,7 @@ const COMPAT = {
DYN_RPORT_BUG: 1 << 2,
BUG_DHGEX_LARGE: 1 << 3,
IMPLY_RSA_SHA2_SIGALGS: 1 << 4,
+ COMWARE_DHGEX_1024: 1 << 5,
};
module.exports = {
@@ -330,6 +354,7 @@ module.exports = {
COMPAT_CHECKS: [
[ 'Cisco-1.25', COMPAT.BAD_DHGEX ],
[ /^Cisco-1[.]/, COMPAT.BUG_DHGEX_LARGE ],
+ [ /^Comware-/, COMPAT.COMWARE_DHGEX_1024 ],
[ /^[0-9.]+$/, COMPAT.OLD_EXIT ], // old SSH.com implementations
[ /^OpenSSH_5[.][0-9]+/, COMPAT.DYN_RPORT_BUG ],
[ /^OpenSSH_7[.]4/, COMPAT.IMPLY_RSA_SHA2_SIGALGS ],
diff --git a/node_modules/ssh2/lib/protocol/crypto.js b/node_modules/ssh2/lib/protocol/crypto.js
index 851cc73..088de2a 100644
--- a/node_modules/ssh2/lib/protocol/crypto.js
+++ b/node_modules/ssh2/lib/protocol/crypto.js
@@ -11,6 +11,7 @@ const {
} = require('crypto');
const { readUInt32BE, writeUInt32BE } = require('./utils.js');
+const { chacha20, nativeChacha20 } = require('./crypto/chacha20.js');
const FastBuffer = Buffer[Symbol.species];
const MAX_SEQNO = 2 ** 32 - 1;
@@ -198,20 +199,17 @@ class ChaChaPolyCipherNative {
POLY1305_OUT_COMPUTE[0] = 0; // Set counter to 0 (little endian)
writeUInt32BE(POLY1305_OUT_COMPUTE, this.outSeqno, 12);
const polyKey =
- createCipheriv('chacha20', this._encKeyMain, POLY1305_OUT_COMPUTE)
- .update(POLY1305_ZEROS);
+ chacha20(this._encKeyMain, POLY1305_OUT_COMPUTE, POLY1305_ZEROS);
// Encrypt packet length
const pktLenEnc =
- createCipheriv('chacha20', this._encKeyPktLen, POLY1305_OUT_COMPUTE)
- .update(packet.slice(0, 4));
+ chacha20(this._encKeyPktLen, POLY1305_OUT_COMPUTE, packet.slice(0, 4));
this._onWrite(pktLenEnc);
// Encrypt rest of packet
POLY1305_OUT_COMPUTE[0] = 1; // Set counter to 1 (little endian)
const payloadEnc =
- createCipheriv('chacha20', this._encKeyMain, POLY1305_OUT_COMPUTE)
- .update(packet.slice(4));
+ chacha20(this._encKeyMain, POLY1305_OUT_COMPUTE, packet.slice(4));
this._onWrite(payloadEnc);
// Calculate Poly1305 MAC
@@ -650,8 +648,7 @@ class ChaChaPolyDecipherNative {
writeUInt32BE(POLY1305_OUT_COMPUTE, this.inSeqno, 12);
const decLenBytes =
- createDecipheriv('chacha20', this._decKeyPktLen, POLY1305_OUT_COMPUTE)
- .update(this._lenBuf);
+ chacha20(this._decKeyPktLen, POLY1305_OUT_COMPUTE, this._lenBuf);
this._len = readUInt32BE(decLenBytes, 0);
if (this._len > MAX_PACKET_SIZE
@@ -706,8 +703,7 @@ class ChaChaPolyDecipherNative {
POLY1305_OUT_COMPUTE[0] = 0; // Set counter to 0 (little endian)
writeUInt32BE(POLY1305_OUT_COMPUTE, this.inSeqno, 12);
const polyKey =
- createCipheriv('chacha20', this._decKeyMain, POLY1305_OUT_COMPUTE)
- .update(POLY1305_ZEROS);
+ chacha20(this._decKeyMain, POLY1305_OUT_COMPUTE, POLY1305_ZEROS);
// Calculate and compare Poly1305 MACs
poly1305_auth(POLY1305_RESULT_MALLOC,
@@ -729,8 +725,7 @@ class ChaChaPolyDecipherNative {
// Decrypt packet
POLY1305_OUT_COMPUTE[0] = 1; // Set counter to 1 (little endian)
const packet =
- createDecipheriv('chacha20', this._decKeyMain, POLY1305_OUT_COMPUTE)
- .update(this._packet);
+ chacha20(this._decKeyMain, POLY1305_OUT_COMPUTE, this._packet);
const payload = new FastBuffer(packet.buffer,
packet.byteOffset + 1,
@@ -1496,7 +1491,7 @@ function createCipher(config) {
? new AESGCMCipherBinding(config)
: new AESGCMCipherNative(config));
case 'chacha20':
- return (ChaChaPolyCipher && !forceNative
+ return (ChaChaPolyCipher && nativeChacha20 && !forceNative
? new ChaChaPolyCipherBinding(config)
: new ChaChaPolyCipherNative(config));
default: {
@@ -1556,7 +1551,7 @@ function createDecipher(config) {
? new AESGCMDecipherBinding(config)
: new AESGCMDecipherNative(config));
case 'chacha20':
- return (ChaChaPolyDecipher && !forceNative
+ return (ChaChaPolyDecipher && nativeChacha20 && !forceNative
? new ChaChaPolyDecipherBinding(config)
: new ChaChaPolyDecipherNative(config));
default: {
diff --git a/node_modules/ssh2/lib/protocol/crypto/chacha20.js b/node_modules/ssh2/lib/protocol/crypto/chacha20.js
new file mode 100644
index 0000000..86dd731
--- /dev/null
+++ b/node_modules/ssh2/lib/protocol/crypto/chacha20.js
@@ -0,0 +1,17 @@
+// OpenSSH uses original ChaCha20 (64-bit nonce), not the IETF/TLS AEAD.
+// Electron's BoringSSL lacks the standalone cipher, including in sshcrypto.
+'use strict';
+const { createCipheriv, getCiphers } = require('crypto');
+const nativeChacha20 = getCiphers().includes('chacha20');
+const { chacha20orig } = nativeChacha20 ? {} : require('@noble/ciphers/chacha.js');
+
+function chacha20(key, iv, data) {
+ if (nativeChacha20)
+ return createCipheriv('chacha20', key, iv).update(data);
+ // ssh2 only uses block counters 0 (length/Poly1305 key) and 1 (payload).
+ // The last eight IV bytes are its big-endian SSH packet sequence nonce.
+ return Buffer.from(chacha20orig(key, iv.subarray(8), data, undefined,
+ iv.readUInt32LE(0)));
+}
+
+module.exports = { chacha20, nativeChacha20 };
diff --git a/node_modules/ssh2/lib/protocol/kex.js b/node_modules/ssh2/lib/protocol/kex.js
index 811e631..422a198 100644
--- a/node_modules/ssh2/lib/protocol/kex.js
+++ b/node_modules/ssh2/lib/protocol/kex.js
@@ -30,7 +30,10 @@ const {
createDecipher,
MAC_INFO,
} = require('./crypto.js');
-const { parseDERKey } = require('./keyParser.js');
+const {
+ parseDERKey,
+ getOpenSSHCertSignatureAlgorithm,
+} = require('./keyParser.js');
const {
bufferFill,
bufferParser,
@@ -49,6 +52,68 @@ const {
let MESSAGE_HANDLERS;
+const LEGACY_RSA_HOST_CERT = /^ssh-rsa-cert-v0[01]@openssh\.com$/;
+const RSA_SHA2_256_HOST_CERT = /^rsa-sha2-256-cert-v0[01]@openssh\.com$/;
+const RSA_SHA2_512_HOST_CERT = /^rsa-sha2-512-cert-v0[01]@openssh\.com$/;
+const LEGACY_RSA_HOST_SIGNATURES = new Set([
+ 'rsa-sha2-256',
+ 'rsa-sha2-512',
+ 'ssh-rsa',
+]);
+
+// Client KEX accepts the negotiated host-key name or, for OpenSSH host
+// certificates, the underlying key algorithm. Real servers sign with the
+// embedded key (ssh-ed25519, ecdsa-sha2-*, rsa-sha2-*) rather than the
+// certificate algorithm name. OpenSSH allows any RSA signature only for the
+// legacy ssh-rsa certificate. rsa-sha2-256/512 certificates must use the
+// hash that was negotiated.
+function hostKeySignatureTypeAccepted(negotiatedServerHostKey, sigType) {
+ if (RSA_SHA2_256_HOST_CERT.test(negotiatedServerHostKey))
+ return sigType === 'rsa-sha2-256';
+ if (RSA_SHA2_512_HOST_CERT.test(negotiatedServerHostKey))
+ return sigType === 'rsa-sha2-512';
+ if (sigType === negotiatedServerHostKey)
+ return true;
+ if (sigType === getOpenSSHCertSignatureAlgorithm(negotiatedServerHostKey))
+ return true;
+ return LEGACY_RSA_HOST_CERT.test(negotiatedServerHostKey)
+ && LEGACY_RSA_HOST_SIGNATURES.has(sigType);
+}
+
+// Plain rsa-sha2-* keeps an ssh-rsa key blob. An RSA certificate negotiation
+// is KEY_RSA_CERT: the blob must be ssh-rsa-cert-v01@openssh.com, never a
+// plain ssh-rsa key.
+function hostKeyBlobTypeAccepted(negotiatedServerHostKey, hostPubKeyType) {
+ if (hostPubKeyType === negotiatedServerHostKey)
+ return true;
+ switch (negotiatedServerHostKey) {
+ case 'rsa-sha2-256':
+ case 'rsa-sha2-512':
+ return hostPubKeyType === 'ssh-rsa';
+ case 'rsa-sha2-256-cert-v01@openssh.com':
+ case 'rsa-sha2-512-cert-v01@openssh.com':
+ return hostPubKeyType === 'ssh-rsa-cert-v01@openssh.com';
+ default:
+ return false;
+ }
+}
+
+function hashAlgoForHostKey(negotiatedServerHostKey, sigType) {
+ // Negotiated RSA-SHA2 wins over the signature label so a mismatched
+ // signature cannot select a weaker hash.
+ if (negotiatedServerHostKey === 'rsa-sha2-256'
+ || RSA_SHA2_256_HOST_CERT.test(negotiatedServerHostKey))
+ return 'sha256';
+ if (negotiatedServerHostKey === 'rsa-sha2-512'
+ || RSA_SHA2_512_HOST_CERT.test(negotiatedServerHostKey))
+ return 'sha512';
+ if (sigType === 'rsa-sha2-256')
+ return 'sha256';
+ if (sigType === 'rsa-sha2-512')
+ return 'sha512';
+ return undefined;
+}
+
const GEX_MIN_BITS = 2048; // RFC 8270
const GEX_MAX_BITS = 8192; // RFC 8270
@@ -635,7 +700,7 @@ const createKeyExchange = (() => {
);
}
- if (sigType !== negotiated.serverHostKey) {
+ if (!hostKeySignatureTypeAccepted(negotiated.serverHostKey, sigType)) {
return doFatalError(
this._protocol,
`Wrong signature type: ${sigType}, `
@@ -659,7 +724,10 @@ const createKeyExchange = (() => {
);
}
- if (!(sigValue = sigSSHToASN1(sigValue, sigType))) {
+ if (!(sigValue = sigSSHToASN1(
+ sigValue,
+ getOpenSSHCertSignatureAlgorithm(sigType)
+ ))) {
return doFatalError(
this._protocol,
'Malformed signature',
@@ -685,12 +753,10 @@ const createKeyExchange = (() => {
}
}
- let hashAlgo;
- // Check if we need to override the default hash algorithm
- switch (this.negotiated.serverHostKey) {
- case 'rsa-sha2-256': hashAlgo = 'sha256'; break;
- case 'rsa-sha2-512': hashAlgo = 'sha512'; break;
- }
+ const hashAlgo = hashAlgoForHostKey(
+ this.negotiated.serverHostKey,
+ sigType
+ );
this._protocol._debug
&& this._protocol._debug('Verifying signature ...');
@@ -717,12 +783,7 @@ const createKeyExchange = (() => {
} else {
// Server
- let hashAlgo;
- // Check if we need to override the default hash algorithm
- switch (this.negotiated.serverHostKey) {
- case 'rsa-sha2-256': hashAlgo = 'sha256'; break;
- case 'rsa-sha2-512': hashAlgo = 'sha512'; break;
- }
+ const hashAlgo = hashAlgoForHostKey(this.negotiated.serverHostKey);
this._protocol._debug && this._protocol._debug(
'Generating signature ...'
@@ -739,7 +800,12 @@ const createKeyExchange = (() => {
);
}
- signature = convertSignature(signature, this._hostKey.type);
+ // OpenSSH signs a host certificate with the embedded key algorithm,
+ // not the certificate algorithm name.
+ const sigType = getOpenSSHCertSignatureAlgorithm(
+ this.negotiated.serverHostKey
+ );
+ signature = convertSignature(signature, sigType);
if (signature === false) {
return doFatalError(
this._protocol,
@@ -759,7 +825,6 @@ const createKeyExchange = (() => {
string <method-specific data>
string signature of H
*/
- const sigType = this.negotiated.serverHostKey;
const sigTypeLen = Buffer.byteLength(sigType);
const sigLen = 4 + sigTypeLen + 4 + signature.length;
let p = this._protocol._packetRW.write.allocStartKEX;
@@ -1167,22 +1232,16 @@ const createKeyExchange = (() => {
DISCONNECT_REASON.KEY_EXCHANGE_FAILED
);
}
- if (hostPubKeyType !== this.negotiated.serverHostKey) {
- // Check if we need to make an exception
- switch (this.negotiated.serverHostKey) {
- case 'rsa-sha2-256':
- case 'rsa-sha2-512':
- if (hostPubKeyType === 'ssh-rsa')
- break;
- // FALLTHROUGH
- default:
- return doFatalError(
- this._protocol,
- 'Host key does not match negotiated type',
- 'handshake',
- DISCONNECT_REASON.KEY_EXCHANGE_FAILED
- );
- }
+ if (!hostKeyBlobTypeAccepted(
+ this.negotiated.serverHostKey,
+ hostPubKeyType
+ )) {
+ return doFatalError(
+ this._protocol,
+ 'Host key does not match negotiated type',
+ 'handshake',
+ DISCONNECT_REASON.KEY_EXCHANGE_FAILED
+ );
}
this._hostKey = hostPubKey;
@@ -1377,8 +1436,13 @@ const createKeyExchange = (() => {
this._generator = null;
this._minBits = GEX_MIN_BITS;
this._prefBits = dhEstimate(this.negotiated);
- if (this._protocol._compatFlags & COMPAT.BUG_DHGEX_LARGE)
+ if (hashName === 'sha1'
+ && (this._protocol._compatFlags & COMPAT.COMWARE_DHGEX_1024)) {
+ this._minBits = 1024;
+ this._prefBits = 1024;
+ } else if (this._protocol._compatFlags & COMPAT.BUG_DHGEX_LARGE) {
this._prefBits = Math.min(this._prefBits, 4096);
+ }
this._maxBits = GEX_MAX_BITS;
}
start() {
@@ -1868,6 +1932,8 @@ function trySendNEWKEYS(kex) {
module.exports = {
KexInit,
+ hostKeyBlobTypeAccepted,
+ hostKeySignatureTypeAccepted,
kexinit,
onKEXPayload,
DEFAULT_KEXINIT_CLIENT: new KexInit({
diff --git a/node_modules/ssh2/lib/protocol/keyParser.js b/node_modules/ssh2/lib/protocol/keyParser.js
index a276c1a..57a05d6 100644
--- a/node_modules/ssh2/lib/protocol/keyParser.js
+++ b/node_modules/ssh2/lib/protocol/keyParser.js
@@ -35,6 +35,9 @@ const SYM_PRIV_PEM = Symbol('Private key PEM');
const SYM_PUB_PEM = Symbol('Public key PEM');
const SYM_PUB_SSH = Symbol('Public key SSH');
const SYM_DECRYPTED = Symbol('Decrypted Key');
+const SYM_SK_APP = Symbol('SK Application');
+const SYM_SK_HANDLE = Symbol('SK Key Handle');
+const SYM_SK_FLAGS = Symbol('SK Flags');
// Create OpenSSL cipher name -> SSH cipher name conversion table
const CIPHER_INFO_OPENSSL = Object.create(null);
@@ -258,6 +261,55 @@ function genOpenSSHEdPub(pub) {
return publicKey;
}
+// FIDO/U2F (security key) SSH wire-format public keys.
+// See OpenSSH's PROTOCOL.u2f.
+function genOpenSSHSKEdPub(pub, application) {
+ const typeName = 'sk-ssh-ed25519@openssh.com';
+ const total =
+ 4 + typeName.length + 4 + pub.length + 4 + application.length;
+ const publicKey = Buffer.allocUnsafe(total);
+
+ let i = 0;
+ writeUInt32BE(publicKey, typeName.length, i);
+ publicKey.utf8Write(typeName, i += 4, typeName.length);
+ i += typeName.length;
+
+ writeUInt32BE(publicKey, pub.length, i);
+ publicKey.set(pub, i += 4);
+ i += pub.length;
+
+ writeUInt32BE(publicKey, application.length, i);
+ publicKey.utf8Write(application, i += 4, application.length);
+
+ return publicKey;
+}
+
+function genOpenSSHSKECDSAPub(Q, application) {
+ const typeName = 'sk-ecdsa-sha2-nistp256@openssh.com';
+ const curveName = 'nistp256';
+ const total = 4 + typeName.length + 4 + curveName.length + 4 + Q.length
+ + 4 + application.length;
+ const publicKey = Buffer.allocUnsafe(total);
+
+ let i = 0;
+ writeUInt32BE(publicKey, typeName.length, i);
+ publicKey.utf8Write(typeName, i += 4, typeName.length);
+ i += typeName.length;
+
+ writeUInt32BE(publicKey, curveName.length, i);
+ publicKey.utf8Write(curveName, i += 4, curveName.length);
+ i += curveName.length;
+
+ writeUInt32BE(publicKey, Q.length, i);
+ publicKey.set(Q, i += 4);
+ i += Q.length;
+
+ writeUInt32BE(publicKey, application.length, i);
+ publicKey.utf8Write(application, i += 4, application.length);
+
+ return publicKey;
+}
+
function genOpenSSLEdPriv(priv) {
const asnWriter = new Ber.Writer();
asnWriter.startSequence();
@@ -451,6 +503,82 @@ const BaseKey = {
},
};
+// Base for FIDO/U2F security key public keys (sk-ssh-ed25519@openssh.com,
+// sk-ecdsa-sha2-nistp256@openssh.com).
+//
+// Signing requires the actual hardware token (typically performed by
+// ssh-agent talking to libfido2), so sign() is not implemented locally.
+//
+// Verifying takes the original message ("data"); the SK signature blob
+// includes the device flags and counter that must be folded into the
+// signed payload as defined by OpenSSH PROTOCOL.u2f:
+//
+// sha256(application) || flags || counter || sha256(message)
+const SKBaseKey = {
+ ...BaseKey,
+ sign: function sign() {
+ return new Error(
+ 'Cannot sign locally with a security-key (SK) public key; '
+ + 'signing must be performed by the FIDO/U2F hardware token '
+ + '(e.g. via ssh-agent)'
+ );
+ },
+ verify: function verify(data, signature, algo) {
+ /*
+ The SK signature blob is:
+ string raw_signature
+ - sk-ssh-ed25519: 64 raw bytes
+ - sk-ecdsa-sha2-nistp256: SSH ECDSA blob (string r, string s)
+ byte flags
+ uint32 counter
+ */
+ const sigParser = makeBufferParser();
+ sigParser.init(signature, 0);
+ const rawSig = sigParser.readString();
+ if (rawSig === undefined) {
+ sigParser.clear();
+ return new Error('Malformed SK signature');
+ }
+ const pos = sigParser.pos();
+ sigParser.clear();
+ if (pos + 5 > signature.length)
+ return new Error('Malformed SK signature');
+ const flags = signature[pos];
+ const counter = readUInt32BE(signature, pos + 1);
+
+ const appHash = createHash('sha256').update(this[SYM_SK_APP]).digest();
+ const dataHash = createHash('sha256').update(data).digest();
+
+ const signedData = Buffer.allocUnsafe(
+ appHash.length + 1 + 4 + dataHash.length
+ );
+ let p = 0;
+ signedData.set(appHash, p);
+ p += appHash.length;
+ signedData[p++] = flags;
+ writeUInt32BE(signedData, counter, p);
+ p += 4;
+ signedData.set(dataHash, p);
+
+ let toVerify = rawSig;
+ if (getOpenSSHCertBaseType(this.type) === 'sk-ecdsa-sha2-nistp256@openssh.com') {
+ // Convert SSH ECDSA r,s blob to ASN.1 for OpenSSL
+ const { sigSSHToASN1 } = require('./utils.js');
+ toVerify = sigSSHToASN1(rawSig, 'ecdsa-sha2-nistp256');
+ if (!toVerify)
+ return new Error('Malformed SK ECDSA signature');
+ }
+
+ return BaseKey.verify.call(this, signedData, toVerify, algo);
+ },
+ getApplication: function getApplication() {
+ return this[SYM_SK_APP];
+ },
+ getKeyHandle: function getKeyHandle() {
+ return this[SYM_SK_HANDLE];
+ },
+};
+
function OpenSSH_Private(type, comment, privPEM, pubPEM, pubSSH, algo,
decrypted) {
@@ -463,6 +591,26 @@ function OpenSSH_Private(type, comment, privPEM, pubPEM, pubSSH, algo,
this[SYM_DECRYPTED] = decrypted;
}
OpenSSH_Private.prototype = BaseKey;
+
+// FIDO/U2F security key OpenSSH private-key entry. The "private" portion is
+// just the device key handle (used by the host to talk to the FIDO token);
+// the actual private key never leaves the hardware. Local sign() therefore
+// returns an error -- signing has to be performed by ssh-agent (or another
+// component talking to the device).
+function OpenSSH_SK_Private(type, comment, pubPEM, pubSSH, algo, decrypted,
+ application, keyHandle, flags) {
+ this.type = type;
+ this.comment = comment;
+ this[SYM_PRIV_PEM] = null;
+ this[SYM_PUB_PEM] = pubPEM;
+ this[SYM_PUB_SSH] = pubSSH;
+ this[SYM_HASH_ALGO] = algo;
+ this[SYM_DECRYPTED] = decrypted;
+ this[SYM_SK_APP] = application;
+ this[SYM_SK_HANDLE] = keyHandle;
+ this[SYM_SK_FLAGS] = flags;
+}
+OpenSSH_SK_Private.prototype = SKBaseKey;
{
const regexp = /^-----BEGIN OPENSSH PRIVATE KEY-----(?:\r\n|\n)([\s\S]+)(?:\r\n|\n)-----END OPENSSH PRIVATE KEY-----$/;
OpenSSH_Private.parse = (str, passphrase) => {
@@ -771,6 +919,87 @@ OpenSSH_Private.prototype = BaseKey;
privPEM = genOpenSSLECDSAPriv(oid, ecpub, ecpriv);
break;
}
+ case 'sk-ssh-ed25519@openssh.com': {
+ if (!eddsaSupported)
+ return new Error(`Unsupported OpenSSH private key type: ${type}`);
+ /*
+ string public key
+ string application
+ uint8 flags
+ string key_handle
+ string reserved
+ */
+ const edpub = readString(data, data._pos);
+ if (edpub === undefined || edpub.length !== 32)
+ return new Error('Malformed OpenSSH SK private key');
+ const application = readString(data, data._pos, true);
+ if (application === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ if (data._pos >= data.length)
+ return new Error('Malformed OpenSSH SK private key');
+ const flagsByte = data[data._pos];
+ data._pos += 1;
+ const keyHandle = readString(data, data._pos);
+ if (keyHandle === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ const reserved = readString(data, data._pos);
+ if (reserved === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+
+ pubPEM = genOpenSSLEdPub(edpub);
+ pubSSH = genOpenSSHSKEdPub(edpub, application);
+ algo = null;
+ const privComment = readString(data, data._pos, true);
+ if (privComment === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ const skKey = new OpenSSH_SK_Private(
+ type, privComment, pubPEM, pubSSH, algo, decrypted,
+ application, keyHandle, flagsByte
+ );
+ keys.push(skKey);
+ continue;
+ }
+ case 'sk-ecdsa-sha2-nistp256@openssh.com': {
+ /*
+ string curve name ("nistp256")
+ string Q -- public
+ string application
+ uint8 flags
+ string key_handle
+ string reserved
+ */
+ if (!skipFields(data, 1)) // Skip curve name
+ return new Error('Malformed OpenSSH SK private key');
+ const ecpub = readString(data, data._pos);
+ if (ecpub === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ const application = readString(data, data._pos, true);
+ if (application === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ if (data._pos >= data.length)
+ return new Error('Malformed OpenSSH SK private key');
+ const flagsByte = data[data._pos];
+ data._pos += 1;
+ const keyHandle = readString(data, data._pos);
+ if (keyHandle === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ const reserved = readString(data, data._pos);
+ if (reserved === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+
+ pubPEM = genOpenSSLECDSAPub('1.2.840.10045.3.1.7', ecpub);
+ pubSSH = genOpenSSHSKECDSAPub(ecpub, application);
+ algo = 'sha256';
+ const privComment = readString(data, data._pos, true);
+ if (privComment === undefined)
+ return new Error('Malformed OpenSSH SK private key');
+ const skKey = new OpenSSH_SK_Private(
+ type, privComment, pubPEM, pubSSH, algo, decrypted,
+ application, keyHandle, flagsByte
+ );
+ keys.push(skKey);
+ continue;
+ }
default:
return new Error(`Unsupported OpenSSH private key type: ${type}`);
}
@@ -1186,31 +1415,97 @@ function OpenSSH_Public(type, comment, pubPEM, pubSSH, algo) {
this[SYM_DECRYPTED] = false;
}
OpenSSH_Public.prototype = BaseKey;
+
+// FIDO/U2F security key public key wrapper. See SKBaseKey.
+function OpenSSH_SK_Public(type, comment, pubPEM, pubSSH, algo, application) {
+ this.type = type;
+ this.comment = comment;
+ this[SYM_PRIV_PEM] = null;
+ this[SYM_PUB_PEM] = pubPEM;
+ this[SYM_PUB_SSH] = pubSSH;
+ this[SYM_HASH_ALGO] = algo;
+ this[SYM_DECRYPTED] = false;
+ this[SYM_SK_APP] = application;
+ this[SYM_SK_HANDLE] = null;
+ this[SYM_SK_FLAGS] = 0;
+}
+OpenSSH_SK_Public.prototype = SKBaseKey;
+
+function isOpenSSHCertType(type) {
+ return (typeof type === 'string'
+ && /-cert-v0[01]@openssh\.com$/.test(type));
+}
+
+function getOpenSSHCertBaseType(type) {
+ if (!isOpenSSHCertType(type))
+ return type;
+
+ const baseType = type.replace(/-cert-v0[01]@openssh\.com$/, '');
+ switch (baseType) {
+ case 'rsa-sha2-256':
+ case 'rsa-sha2-512':
+ return 'ssh-rsa';
+ // OpenSSH places `-cert-v01` before `@openssh.com` for SK certs
+ // (sk-ssh-ed25519-cert-v01@openssh.com), so strip leaves the bare algorithm.
+ case 'sk-ssh-ed25519':
+ return 'sk-ssh-ed25519@openssh.com';
+ case 'sk-ecdsa-sha2-nistp256':
+ return 'sk-ecdsa-sha2-nistp256@openssh.com';
+ default:
+ return baseType;
+ }
+}
+
+// Wire signature algorithm for a publickey/hostbased cert identity.
+// Unlike getOpenSSHCertBaseType, RSA SHA2 certs keep rsa-sha2-* (not ssh-rsa).
+// SK certs must restore the trailing @openssh.com required on the wire.
+function getOpenSSHCertSignatureAlgorithm(type) {
+ if (typeof type !== 'string' || !/-cert-v0[01]@openssh\.com$/.test(type))
+ return type;
+
+ const baseType = type.replace(/-cert-v0[01]@openssh\.com$/, '');
+ switch (baseType) {
+ case 'sk-ssh-ed25519':
+ return 'sk-ssh-ed25519@openssh.com';
+ case 'sk-ecdsa-sha2-nistp256':
+ return 'sk-ecdsa-sha2-nistp256@openssh.com';
+ default:
+ return baseType;
+ }
+}
+
{
let regexp;
- if (eddsaSupported)
- regexp = /^(((?:ssh-(?:rsa|dss|ed25519))|ecdsa-sha2-nistp(?:256|384|521))(?:-cert-v0[01]@openssh.com)?) ([A-Z0-9a-z/+=]+)(?:$|\s+([\S].*)?)$/;
- else
- regexp = /^(((?:ssh-(?:rsa|dss))|ecdsa-sha2-nistp(?:256|384|521))(?:-cert-v0[01]@openssh.com)?) ([A-Z0-9a-z/+=]+)(?:$|\s+([\S].*)?)$/;
+ if (eddsaSupported) {
+ // SK cert names use `-cert-v01` before `@openssh.com`, unlike classic keys.
+ regexp = /^((((?:ssh-(?:rsa|dss|ed25519))|ecdsa-sha2-nistp(?:256|384|521))(?:-cert-v0[01]@openssh\.com)?)|(sk-(?:ssh-ed25519|ecdsa-sha2-nistp256)(?:-cert-v0[01])?@openssh\.com)) ([A-Z0-9a-z/+=]+)(?:$|\s+([\S].*)?)$/;
+ } else {
+ regexp = /^((((?:ssh-(?:rsa|dss))|ecdsa-sha2-nistp(?:256|384|521))(?:-cert-v0[01]@openssh\.com)?)|(sk-ecdsa-sha2-nistp256(?:-cert-v0[01])?@openssh\.com)) ([A-Z0-9a-z/+=]+)(?:$|\s+([\S].*)?)$/;
+ }
OpenSSH_Public.parse = (str) => {
const m = regexp.exec(str);
if (m === null)
return null;
// m[1] = full type
- // m[2] = base type
- // m[3] = base64-encoded public key
- // m[4] = comment
+ // m[5] = base64-encoded public key
+ // m[6] = comment
const fullType = m[1];
- const baseType = m[2];
- const data = Buffer.from(m[3], 'base64');
- const comment = (m[4] || '');
+ const baseType = getOpenSSHCertBaseType(fullType);
+ const data = Buffer.from(m[5], 'base64');
+ const comment = (m[6] || '');
const type = readString(data, data._pos, true);
- if (type === undefined || type.indexOf(baseType) !== 0)
+ if (type === undefined || getOpenSSHCertBaseType(type) !== baseType)
return new Error('Malformed OpenSSH public key');
- return parseDER(data, baseType, comment, fullType);
+ return parseDER(
+ data,
+ baseType,
+ comment,
+ fullType,
+ isOpenSSHCertType(fullType) ? data : undefined
+ );
};
}
@@ -1309,7 +1604,7 @@ RFC4716_Public.prototype = BaseKey;
}
-function parseDER(data, baseType, comment, fullType) {
+function parseDER(data, baseType, comment, fullType, originalPubSSH) {
if (!isSupportedKeyType(baseType))
return new Error(`Unsupported OpenSSH public key type: ${baseType}`);
@@ -1317,6 +1612,10 @@ function parseDER(data, baseType, comment, fullType) {
let oid;
let pubPEM = null;
let pubSSH = null;
+ const isCertificate = isOpenSSHCertType(fullType);
+
+ if (isCertificate && !skipFields(data, 1))
+ return new Error('Malformed OpenSSH public key');
switch (baseType) {
case 'ssh-rsa': {
@@ -1383,11 +1682,66 @@ function parseDER(data, baseType, comment, fullType) {
pubSSH = genOpenSSHECDSAPub(oid, ecpub);
break;
}
+ case 'sk-ssh-ed25519@openssh.com': {
+ /*
+ string public key (32 raw bytes)
+ string application
+ */
+ const edpub = readString(data, data._pos || 0);
+ if (edpub === undefined || edpub.length !== 32)
+ return new Error('Malformed OpenSSH SK public key');
+ const application = readString(data, data._pos, true);
+ if (application === undefined)
+ return new Error('Malformed OpenSSH SK public key');
+ pubPEM = genOpenSSLEdPub(edpub);
+ pubSSH = genOpenSSHSKEdPub(edpub, application);
+ algo = null;
+ return new OpenSSH_SK_Public(
+ fullType,
+ comment,
+ pubPEM,
+ isCertificate ? Buffer.from(originalPubSSH || data) : pubSSH,
+ algo,
+ application
+ );
+ }
+ case 'sk-ecdsa-sha2-nistp256@openssh.com': {
+ /*
+ string curve name ("nistp256")
+ string Q -- public
+ string application
+ */
+ if (!skipFields(data, 1)) // Skip curve name
+ return new Error('Malformed OpenSSH SK public key');
+ const ecpub = readString(data, data._pos || 0);
+ if (ecpub === undefined)
+ return new Error('Malformed OpenSSH SK public key');
+ const application = readString(data, data._pos, true);
+ if (application === undefined)
+ return new Error('Malformed OpenSSH SK public key');
+ pubPEM = genOpenSSLECDSAPub('1.2.840.10045.3.1.7', ecpub);
+ pubSSH = genOpenSSHSKECDSAPub(ecpub, application);
+ algo = 'sha256';
+ return new OpenSSH_SK_Public(
+ fullType,
+ comment,
+ pubPEM,
+ isCertificate ? Buffer.from(originalPubSSH || data) : pubSSH,
+ algo,
+ application
+ );
+ }
default:
return new Error(`Unsupported OpenSSH public key type: ${baseType}`);
}
- return new OpenSSH_Public(fullType, comment, pubPEM, pubSSH, algo);
+ return new OpenSSH_Public(
+ fullType,
+ comment,
+ pubPEM,
+ isCertificate ? Buffer.from(originalPubSSH || data) : pubSSH,
+ algo
+ );
}
function isSupportedKeyType(type) {
@@ -1397,8 +1751,10 @@ function isSupportedKeyType(type) {
case 'ecdsa-sha2-nistp256':
case 'ecdsa-sha2-nistp384':
case 'ecdsa-sha2-nistp521':
+ case 'sk-ecdsa-sha2-nistp256@openssh.com':
return true;
case 'ssh-ed25519':
+ case 'sk-ssh-ed25519@openssh.com':
if (eddsaSupported)
return true;
// FALLTHROUGH
@@ -1460,7 +1816,13 @@ function parseKey(data, passphrase) {
if (type !== undefined) {
data = binaryKeyParser.readRaw();
if (data !== undefined) {
- ret = parseDER(data, type, '', type);
+ ret = parseDER(
+ data,
+ getOpenSSHCertBaseType(type),
+ '',
+ type,
+ isOpenSSHCertType(type) ? origBuffer : undefined
+ );
// Ignore potentially useless errors in case the data was not actually
// in the binary format
if (ret instanceof Error)
@@ -1479,6 +1841,7 @@ function parseKey(data, passphrase) {
module.exports = {
isParsedKey,
isSupportedKeyType,
- parseDERKey: (data, type) => parseDER(data, type, '', type),
+ getOpenSSHCertSignatureAlgorithm,
+ parseDERKey: (data, type) => parseDER(data, getOpenSSHCertBaseType(type), '', type),
parseKey,
};