bdy 1.26.10-stage → 1.26.11-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/distTs/package.json +2 -2
  2. package/distTs/src/command/agent/selftest.js +2 -3
  3. package/distTs/src/index.js +0 -8
  4. package/distTs/src/sshChannelWindow.js +45 -124
  5. package/distTs/src/sshKex.js +32 -0
  6. package/distTs/src/sshKeygen.js +1 -10
  7. package/distTs/src/tunnel/server/sftp.js +0 -24
  8. package/distTs/src/tunnel/server/ssh.js +4 -2
  9. package/distTs/src/tunnel/ssh/client.js +11 -12
  10. package/node_modules/ssh2/.github/workflows/ci.yml +12 -18
  11. package/node_modules/ssh2/.github/workflows/lint.yml +2 -2
  12. package/node_modules/ssh2/README.md +30 -8
  13. package/node_modules/ssh2/lib/Channel.js +38 -3
  14. package/node_modules/ssh2/lib/agent.js +3 -101
  15. package/node_modules/ssh2/lib/client.js +30 -10
  16. package/node_modules/ssh2/lib/keygen.js +6 -7
  17. package/node_modules/ssh2/lib/protocol/Protocol.js +3 -1
  18. package/node_modules/ssh2/lib/protocol/SFTP.js +8 -0
  19. package/node_modules/ssh2/lib/protocol/constants.js +21 -0
  20. package/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +33 -32
  21. package/node_modules/ssh2/lib/protocol/kex.js +122 -0
  22. package/node_modules/ssh2/lib/protocol/zlib.js +11 -1
  23. package/node_modules/ssh2/lib/server.js +57 -12
  24. package/node_modules/ssh2/package.json +9 -6
  25. package/node_modules/ssh2/test/test-channel-window.js +142 -0
  26. package/node_modules/ssh2/test/test-destroy.js +97 -0
  27. package/node_modules/ssh2/test/test-integration-openssh.js +70 -7
  28. package/node_modules/ssh2/test/test-kex-mlkem.js +211 -0
  29. package/node_modules/ssh2/test/test-keygen.js +16 -6
  30. package/node_modules/ssh2/test/test-late-write.js +61 -0
  31. package/node_modules/ssh2/test/test-misc-client-server.js +29 -0
  32. package/node_modules/ssh2/test/test-nodelay.js +54 -0
  33. package/node_modules/ssh2/test/test-protocol-keyparser.js +1 -1
  34. package/node_modules/ssh2/test/test-sftp.js +33 -0
  35. package/node_modules/ssh2/test/test-userauth-signature.js +157 -0
  36. package/package.json +2 -2
  37. package/node_modules/ssh2/util/build_pagent.bat +0 -2
  38. package/node_modules/ssh2/util/pagent.c +0 -88
  39. package/node_modules/ssh2/util/pagent.exe +0 -0
@@ -0,0 +1,142 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+
5
+ const Client = require('../lib/client.js');
6
+ const Server = require('../lib/server.js');
7
+ const { MAX_WINDOW } = require('../lib/Channel.js');
8
+
9
+ const {
10
+ fixture,
11
+ mustCall,
12
+ setup,
13
+ } = require('./common.js');
14
+
15
+ const debug = false;
16
+ const hostKeys = [ fixture('ssh_host_rsa_key') ];
17
+
18
+ // Bad values are refused where the option is given
19
+ [ 0, MAX_WINDOW - 1, 2 ** 32, 1.5 * MAX_WINDOW + 0.5, '8388608', NaN ]
20
+ .forEach((channelWindow) => {
21
+ assert.throws(() => new Server({ hostKeys, channelWindow }),
22
+ /Invalid channelWindow/,
23
+ `Server accepted ${channelWindow}`);
24
+ assert.throws(() => {
25
+ new Client().connect({ username: 'foo', sock: {}, channelWindow });
26
+ }, /Invalid channelWindow/, `Client accepted ${channelWindow}`);
27
+ });
28
+
29
+ // The window each side announces, and what it is topped up to afterwards.
30
+ // The refill is the half that matters: with only the initial window raised a
31
+ // transfer would run on the default from the first refill on.
32
+ [
33
+ { desc: 'default on both sides', client: undefined, server: undefined },
34
+ { desc: 'larger on the server', client: undefined, server: 8 * MAX_WINDOW },
35
+ { desc: 'larger on the client', client: 4 * MAX_WINDOW, server: undefined },
36
+ { desc: 'larger on both sides',
37
+ client: 4 * MAX_WINDOW,
38
+ server: 8 * MAX_WINDOW },
39
+ ].forEach((test) => {
40
+ const clientWindow = (test.client || MAX_WINDOW);
41
+ const serverWindow = (test.server || MAX_WINDOW);
42
+ // More than the larger window, so both directions have to be refilled
43
+ const total = 3 * Math.max(clientWindow, serverWindow);
44
+ const chunk = Buffer.alloc(64 * 1024, 'a');
45
+
46
+ const { client, server } = setup(
47
+ `Channel window, ${test.desc}`,
48
+ {
49
+ client: { username: 'foo', password: 'bar', channelWindow: test.client },
50
+ server: { hostKeys, channelWindow: test.server },
51
+ debug,
52
+ timeout: 60 * 1000,
53
+ }
54
+ );
55
+
56
+ // Every window adjustment a side sends, as the window it results in
57
+ const watchRefills = (conn, window, into) => {
58
+ const proto = conn._protocol;
59
+ const channelWindowAdjust = proto.channelWindowAdjust;
60
+ proto.channelWindowAdjust = function(id, amount) {
61
+ into.push(amount);
62
+ assert(amount <= window, `Refill of ${amount} is over the window`);
63
+ return channelWindowAdjust.call(this, id, amount);
64
+ };
65
+ };
66
+
67
+ const pump = (stream, done) => {
68
+ let sent = 0;
69
+ (function write() {
70
+ while (sent < total) {
71
+ sent += chunk.length;
72
+ if (!stream.write(chunk))
73
+ return stream.once('drain', write);
74
+ }
75
+ done();
76
+ })();
77
+ };
78
+
79
+ const serverRefills = [];
80
+ const clientRefills = [];
81
+
82
+ server.on('connection', mustCall((conn) => {
83
+ conn.on('authentication', mustCall((ctx) => {
84
+ ctx.accept();
85
+ })).on('ready', mustCall(() => {
86
+ watchRefills(conn, serverWindow, serverRefills);
87
+ conn.on('session', mustCall((accept, reject) => {
88
+ accept().on('exec', mustCall((accept, reject) => {
89
+ const stream = accept();
90
+ assert.strictEqual(stream.incoming.window, serverWindow);
91
+ let received = 0;
92
+ stream.on('data', (data) => {
93
+ received += data.length;
94
+ if (received === total) {
95
+ // And back, to exercise the client's window the same way
96
+ pump(stream, () => {
97
+ stream.exit(0);
98
+ stream.end();
99
+ });
100
+ }
101
+ });
102
+ }));
103
+ }));
104
+ }));
105
+ }));
106
+
107
+ client.on('ready', mustCall(() => {
108
+ watchRefills(client, clientWindow, clientRefills);
109
+ client.exec('cat', mustCall((err, stream) => {
110
+ assert(!err, `Unexpected exec error: ${err}`);
111
+ assert.strictEqual(stream.incoming.window, clientWindow);
112
+ // What the other side announced is what we may send before waiting
113
+ assert.strictEqual(stream.outgoing.window, serverWindow);
114
+
115
+ let received = 0;
116
+ stream.on('data', (data) => {
117
+ received += data.length;
118
+ }).on('close', mustCall(() => {
119
+ assert.strictEqual(received, total);
120
+ // A window that is only ever topped up to the default needs a refill
121
+ // per megabyte or so. Topped up to its own size it needs a handful
122
+ const fewerThan = (window) => Math.ceil(total / (window / 2)) + 2;
123
+ const sum = (list) => list.reduce((a, b) => a + b, 0);
124
+ for (const [ which, refills, window ] of [
125
+ [ 'server', serverRefills, serverWindow ],
126
+ [ 'client', clientRefills, clientWindow ],
127
+ ]) {
128
+ assert(refills.length > 0, `No ${which} refills`);
129
+ assert(refills.length <= fewerThan(window),
130
+ `Too many ${which} refills: ${refills.length}`);
131
+ // All of it was let through, in pieces of at least half a window
132
+ assert(sum(refills) >= total - window,
133
+ `Too little refilled by the ${which}: ${sum(refills)}`);
134
+ assert(Math.max(...refills) >= window / 2,
135
+ `The ${which} never refilled half of its window`);
136
+ }
137
+ client.end();
138
+ }));
139
+ pump(stream, () => {});
140
+ }));
141
+ }));
142
+ });
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+ const net = require('net');
5
+
6
+ const Client = require('../lib/client.js');
7
+ const Server = require('../lib/server.js');
8
+
9
+ const {
10
+ fixture,
11
+ mustCall,
12
+ } = require('./common.js');
13
+
14
+ const hostKeys = [ fixture('ssh_host_rsa_key') ];
15
+
16
+ // A destroy() that does nothing leaves a socket open and the process running
17
+ setTimeout(() => {
18
+ assert(false, 'Test timed out');
19
+ }, 10 * 1000).unref();
20
+
21
+ // A peer that is gone without closing: its socket is paused, so it reads
22
+ // neither our disconnect nor our FIN and never closes its side. After end() our
23
+ // socket stays half-closed until something destroys it
24
+
25
+ // Server side
26
+ {
27
+ const server = new Server({ hostKeys }, mustCall((conn) => {
28
+ conn.on('authentication', mustCall((ctx) => {
29
+ ctx.accept();
30
+ })).on('ready', mustCall(() => {
31
+ const sock = conn._sock;
32
+ peer.pause();
33
+ conn.end();
34
+ setTimeout(mustCall(() => {
35
+ assert(!sock.destroyed, 'end() alone must not have freed the socket');
36
+ assert(!sock.writable, 'Socket should be half-closed');
37
+ conn.on('close', mustCall(() => {
38
+ assert(sock.destroyed, 'Socket not destroyed');
39
+ peer.destroy();
40
+ server.close();
41
+ }));
42
+ assert.strictEqual(conn.destroy(), conn);
43
+ // Again is harmless
44
+ conn.destroy();
45
+ }), 200);
46
+ }));
47
+ }));
48
+
49
+ const peer = new net.Socket({ allowHalfOpen: true });
50
+ const client = new Client();
51
+ client.on('error', () => {});
52
+ server.listen(0, 'localhost', mustCall(() => {
53
+ peer.connect(server.address().port, 'localhost');
54
+ client.connect({ username: 'foo', password: 'bar', sock: peer });
55
+ }));
56
+ }
57
+
58
+ // Client side
59
+ {
60
+ let serverSock;
61
+ const server = net.createServer({ allowHalfOpen: true }, mustCall((sock) => {
62
+ serverSock = sock;
63
+ sshServer.injectSocket(sock);
64
+ sock.on('error', () => {});
65
+ client.on('close', () => sock.destroy());
66
+ }));
67
+ const sshServer = new Server({ hostKeys }, mustCall((conn) => {
68
+ conn.on('authentication', mustCall((ctx) => {
69
+ ctx.accept();
70
+ })).on('error', () => {});
71
+ }));
72
+
73
+ const client = new Client();
74
+ client.on('error', () => {}).on('ready', mustCall(() => {
75
+ const sock = client._sock;
76
+ serverSock.pause();
77
+ client.end();
78
+ setTimeout(mustCall(() => {
79
+ assert(!sock.destroyed, 'end() alone must not have freed the socket');
80
+ assert(!sock.writable, 'Socket should be half-closed');
81
+ client.on('close', mustCall(() => {
82
+ assert(sock.destroyed, 'Socket not destroyed');
83
+ server.close();
84
+ }));
85
+ assert.strictEqual(client.destroy(), client);
86
+ client.destroy();
87
+ }), 200);
88
+ }));
89
+ server.listen(0, 'localhost', mustCall(() => {
90
+ client.connect({
91
+ host: 'localhost',
92
+ port: server.address().port,
93
+ username: 'foo',
94
+ password: 'bar',
95
+ });
96
+ }));
97
+ }
@@ -9,6 +9,7 @@ const { join } = require('path');
9
9
  const readline = require('readline');
10
10
 
11
11
  const Server = require('../lib/server.js');
12
+ const { mlkem768x25519Supported } = require('../lib/protocol/constants.js');
12
13
 
13
14
  const {
14
15
  fixture,
@@ -64,6 +65,10 @@ for (const file of readdirSync(FIXTURES_DIR, { withFileTypes: true })) {
64
65
  console.log(`Testing with OpenSSH version: ${opensshVer}`);
65
66
  }
66
67
 
68
+ // OpenSSH 9.8 disabled DSA support at compile time, so let's not assume that
69
+ // the available binary was compiled with it explicitly re-added at compile
70
+ // time.
71
+ const supportsDSA = /^[0-8]\.[0-9]+|9\.[0-7]/.test(opensshVer);
67
72
 
68
73
  // Key-based authentication
69
74
  [
@@ -71,8 +76,11 @@ for (const file of readdirSync(FIXTURES_DIR, { withFileTypes: true })) {
71
76
  clientKey: fixtureKey('id_rsa') },
72
77
  { desc: 'RSA user key (new OpenSSH)',
73
78
  clientKey: fixtureKey('openssh_new_rsa') },
74
- { desc: 'DSA user key',
75
- clientKey: fixtureKey('id_dsa') },
79
+ ...(
80
+ supportsDSA
81
+ ? [ { desc: 'DSA user key', clientKey: fixtureKey('id_dsa') } ]
82
+ : []
83
+ ),
76
84
  { desc: 'ECDSA user key',
77
85
  clientKey: fixtureKey('id_ecdsa') },
78
86
  ].forEach((test) => {
@@ -143,8 +151,11 @@ for (const file of readdirSync(FIXTURES_DIR, { withFileTypes: true })) {
143
151
  hostKey: fixture('id_rsa') },
144
152
  { desc: 'RSA host key (new OpenSSH)',
145
153
  hostKey: fixture('openssh_new_rsa') },
146
- { desc: 'DSA host key',
147
- hostKey: fixture('ssh_host_dsa_key') },
154
+ ...(
155
+ supportsDSA
156
+ ? [ { desc: 'DSA host key', hostKey: fixture('ssh_host_dsa_key') } ]
157
+ : []
158
+ ),
148
159
  { desc: 'ECDSA host key',
149
160
  hostKey: fixture('ssh_host_ecdsa_key') },
150
161
  { desc: 'PPK',
@@ -212,6 +223,56 @@ for (const file of readdirSync(FIXTURES_DIR, { withFileTypes: true })) {
212
223
  });
213
224
 
214
225
 
226
+ // Post-quantum hybrid key exchange, in OpenSSH since 9.9. The server offers
227
+ // nothing else, so the connection is only made if both ends agree on it
228
+ {
229
+ const m = /^(\d+)\.(\d+)/.exec(opensshVer);
230
+ const major = +m[1];
231
+ const minor = +m[2];
232
+ if (mlkem768x25519Supported && (major > 9 || (major === 9 && minor >= 9))) {
233
+ const kex = 'mlkem768x25519-sha256';
234
+ const clientKey = fixtureKey('openssh_new_rsa');
235
+ const username = 'KeyUser';
236
+ const { server } = setup(
237
+ `${kex} key exchange`,
238
+ {
239
+ client: {
240
+ username,
241
+ privateKeyPath: clientKey.fullPath,
242
+ },
243
+ server: {
244
+ hostKeys: [ fixture('ssh_host_rsa_key') ],
245
+ algorithms: { kex: [ kex ] },
246
+ },
247
+ debug,
248
+ }
249
+ );
250
+
251
+ server.on('connection', mustCall((conn) => {
252
+ conn.on('handshake', mustCall((negotiated) => {
253
+ assert.strictEqual(negotiated.kex, kex, 'Wrong kex');
254
+ })).on('authentication', mustCallAtLeast((ctx) => {
255
+ if (ctx.method === 'none')
256
+ return ctx.reject();
257
+ assert(ctx.method === 'publickey',
258
+ `Wrong auth method: ${ctx.method}`);
259
+ ctx.accept();
260
+ }, 2)).on('ready', mustCall(() => {
261
+ conn.on('session', mustCall((accept, reject) => {
262
+ accept().on('exec', mustCall((accept, reject) => {
263
+ const stream = accept();
264
+ stream.exit(0);
265
+ stream.end();
266
+ }));
267
+ }));
268
+ }));
269
+ }));
270
+ } else {
271
+ console.log('Skipping mlkem768x25519-sha256 test');
272
+ }
273
+ }
274
+
275
+
215
276
  // Various edge cases
216
277
  {
217
278
  const clientKey = fixtureKey('openssh_new_rsa');
@@ -435,14 +496,16 @@ function setup(title, configs) {
435
496
  if (clientCfg.privateKeyPath)
436
497
  args.push('-o', `IdentityFile=${clientCfg.privateKeyPath}`);
437
498
 
438
- if (!/^[0-6]\./.test(opensshVer)) {
439
- // OpenSSH 7.0+ disables DSS/DSA host (and user) key support by
440
- // default, so we explicitly enable it here
499
+ if (supportsDSA) {
441
500
  args.push('-o', 'HostKeyAlgorithms=+ssh-dss');
442
501
  args.push('-o', 'PubkeyAcceptedKeyTypes=+ssh-dss');
443
502
  args.push('-o', 'PubkeyAcceptedAlgorithms=+ssh-dss');
444
503
  }
445
504
 
505
+ args.push('-o', 'HostKeyAlgorithms=+ssh-rsa');
506
+ args.push('-o', 'PubkeyAcceptedKeyTypes=+ssh-rsa');
507
+ args.push('-o', 'PubkeyAcceptedAlgorithms=+ssh-rsa');
508
+
446
509
  args.push('-p', server.address().port.toString(),
447
510
  '-l', clientCfg.username,
448
511
  'localhost',
@@ -0,0 +1,211 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+
5
+ const {
6
+ DEFAULT_KEX,
7
+ mlkem768x25519Supported,
8
+ } = require('../lib/protocol/constants.js');
9
+
10
+ const {
11
+ fixture,
12
+ mustCall,
13
+ setup,
14
+ } = require('./common.js');
15
+
16
+ const KEX = 'mlkem768x25519-sha256';
17
+ const debug = false;
18
+
19
+ if (!mlkem768x25519Supported) {
20
+ assert(!DEFAULT_KEX.includes(KEX),
21
+ 'Unsupported kex must not be offered');
22
+ console.log(`Skipping ${KEX} tests: no ML-KEM in this node`);
23
+ process.exit(0);
24
+ }
25
+
26
+ assert.strictEqual(DEFAULT_KEX[0], KEX, 'Wrong most preferred kex');
27
+
28
+ function serve(server, onHandshake, sessions = 1) {
29
+ server.on('connection', mustCall((conn) => {
30
+ conn.on('handshake', onHandshake);
31
+ conn.on('authentication', mustCall((ctx) => {
32
+ ctx.accept();
33
+ })).on('ready', mustCall(() => {
34
+ conn.on('session', mustCall((accept, reject) => {
35
+ accept().on('exec', mustCall((accept, reject, info) => {
36
+ const stream = accept();
37
+ stream.end(`ran: ${info.command}`);
38
+ stream.exit(0);
39
+ }));
40
+ }, sessions));
41
+ }));
42
+ }));
43
+ }
44
+
45
+ function exec(client, done) {
46
+ client.exec('uptime', mustCall((err, stream) => {
47
+ assert(!err, `Unexpected exec error: ${err}`);
48
+ let out = '';
49
+ stream.on('data', (chunk) => {
50
+ out += chunk;
51
+ }).on('close', mustCall(() => {
52
+ assert.strictEqual(out, 'ran: uptime');
53
+ done();
54
+ }));
55
+ stream.resume();
56
+ }));
57
+ }
58
+
59
+ // What both sides negotiate, with it requested explicitly and with nothing
60
+ // requested at all
61
+ [
62
+ { desc: 'explicitly requested', algorithms: { kex: [ KEX ] } },
63
+ { desc: 'by default', algorithms: undefined },
64
+ ].forEach(({ desc, algorithms }) => {
65
+ const { client, server } = setup(
66
+ `${KEX} ${desc}`,
67
+ {
68
+ client: { username: 'foo', password: 'bar', algorithms },
69
+ server: { hostKeys: [ fixture('ssh_host_rsa_key') ], algorithms },
70
+ debug,
71
+ }
72
+ );
73
+
74
+ serve(server, mustCall((negotiated) => {
75
+ assert.strictEqual(negotiated.kex, KEX, 'Wrong server kex');
76
+ }));
77
+
78
+ client.on('handshake', mustCall((negotiated) => {
79
+ assert.strictEqual(negotiated.kex, KEX, 'Wrong client kex');
80
+ })).on('ready', mustCall(() => {
81
+ exec(client, () => client.end());
82
+ }));
83
+
84
+ if (algorithms) {
85
+ // The exchange class is not exported, so it is taken from a connection
86
+ // that has used it
87
+ let Exchange;
88
+ client.on('handshake', () => {
89
+ Exchange = client._protocol._kex.constructor;
90
+ }).on('close', mustCall(() => testMalformed(Exchange)));
91
+ }
92
+ });
93
+
94
+ // A peer without it still gets a classical exchange
95
+ [
96
+ { desc: 'client without it', which: 'client' },
97
+ { desc: 'server without it', which: 'server' },
98
+ ].forEach(({ desc, which }) => {
99
+ const classical = { kex: [ 'curve25519-sha256' ] };
100
+ const { client, server } = setup(
101
+ `${KEX} fallback, ${desc}`,
102
+ {
103
+ client: {
104
+ username: 'foo',
105
+ password: 'bar',
106
+ algorithms: (which === 'client' ? classical : undefined),
107
+ },
108
+ server: {
109
+ hostKeys: [ fixture('ssh_host_rsa_key') ],
110
+ algorithms: (which === 'server' ? classical : undefined),
111
+ },
112
+ debug,
113
+ }
114
+ );
115
+
116
+ serve(server, mustCall((negotiated) => {
117
+ assert.strictEqual(negotiated.kex, 'curve25519-sha256');
118
+ }));
119
+
120
+ client.on('ready', mustCall(() => {
121
+ exec(client, () => client.end());
122
+ }));
123
+ });
124
+
125
+ // A rekey makes new keys with it too, and the connection goes on with them
126
+ {
127
+ const algorithms = { kex: [ KEX ] };
128
+ const { client, server } = setup(
129
+ `${KEX} rekey`,
130
+ {
131
+ client: { username: 'foo', password: 'bar', algorithms },
132
+ server: { hostKeys: [ fixture('ssh_host_rsa_key') ], algorithms },
133
+ debug,
134
+ }
135
+ );
136
+
137
+ let rekeyed;
138
+ server.on('connection', mustCall((conn) => {
139
+ rekeyed = (done) => conn.rekey(mustCall((err) => {
140
+ assert(!err, `Unexpected rekey error: ${err}`);
141
+ done();
142
+ }));
143
+ }));
144
+ serve(server, mustCall((negotiated) => {
145
+ assert.strictEqual(negotiated.kex, KEX, 'Wrong server kex');
146
+ }, 2), 2);
147
+
148
+ client.on('handshake', mustCall((negotiated) => {
149
+ assert.strictEqual(negotiated.kex, KEX, 'Wrong client kex');
150
+ }, 2)).on('ready', mustCall(() => {
151
+ exec(client, () => {
152
+ rekeyed(() => exec(client, () => client.end()));
153
+ });
154
+ }));
155
+ }
156
+
157
+ // A value of the wrong size ends the handshake instead of being used. Both
158
+ // directions, one after the other as they patch the same prototype: the
159
+ // client's public keys cut short, then the server's reply.
160
+ function testMalformed(Exchange, which = 'client') {
161
+ const getPublicKey = Exchange.prototype.getPublicKey;
162
+ const algorithms = { kex: [ KEX ] };
163
+ const { client, server } = setup(
164
+ `${KEX} malformed ${which} value`,
165
+ {
166
+ client: { username: 'foo', password: 'bar', algorithms },
167
+ server: { hostKeys: [ fixture('ssh_host_rsa_key') ], algorithms },
168
+ debug,
169
+ noForceClientReady: true,
170
+ noForceServerReady: true,
171
+ noClientError: true,
172
+ noServerError: true,
173
+ }
174
+ );
175
+
176
+ // The side whose value is cut short
177
+ let bad;
178
+ Exchange.prototype.getPublicKey = function() {
179
+ const value = getPublicKey.call(this);
180
+ return (this._protocol === bad ? value.slice(1) : value);
181
+ };
182
+
183
+ // Reported by the side that received it
184
+ const onError = mustCall((err) => {
185
+ assert(/Invalid length of the remote key exchange value/.test(err),
186
+ `Wrong error: ${err}`);
187
+ });
188
+ const notReady = () => assert(false, 'Handshake must not complete');
189
+
190
+ server.on('connection', mustCall((conn) => {
191
+ if (which === 'server')
192
+ bad = conn._protocol;
193
+ conn.on('error', (which === 'client' ? onError : () => {}))
194
+ .on('ready', notReady);
195
+ }));
196
+
197
+ const connect = client.connect;
198
+ client.connect = function(...args) {
199
+ const ret = connect.apply(this, args);
200
+ if (which === 'client')
201
+ bad = this._protocol;
202
+ return ret;
203
+ };
204
+ client.on('error', (which === 'server' ? onError : () => {}))
205
+ .on('ready', notReady)
206
+ .on('close', mustCall(() => {
207
+ Exchange.prototype.getPublicKey = getPublicKey;
208
+ if (which === 'client')
209
+ testMalformed(Exchange, 'server');
210
+ }));
211
+ }
@@ -12,8 +12,6 @@ const {
12
12
 
13
13
  const { mustCall } = require('./common.js');
14
14
 
15
- const nodeMajor = +/(?<=^v)\d+/.exec(process.version)[0];
16
-
17
15
  function tryParse(...args) {
18
16
  const ret = parseKey(...args);
19
17
  if (ret instanceof Error)
@@ -34,10 +32,6 @@ const inputs = [
34
32
  if (inputs.length === 0)
35
33
  return;
36
34
  const { args, sshType } = inputs.shift();
37
- if (args[0] === 'ed25519' && nodeMajor < 12) {
38
- console.log('Skipping ed25519 on node < 12');
39
- return;
40
- }
41
35
  const passphrase = (args[1] && args[1].passphrase);
42
36
  const comment = ((args[1] && args[1].comment) || '');
43
37
  const check = (keys) => {
@@ -58,3 +52,19 @@ const inputs = [
58
52
  next();
59
53
  }));
60
54
  })();
55
+
56
+ // An ed25519 public key that starts with zero bytes (1 key in 256) has to
57
+ // keep them: only the BIT STRING's unused-bits byte is not part of the key
58
+ {
59
+ let leadingZero = 0;
60
+ for (let i = 0; i < 20000 && leadingZero < 3; ++i) {
61
+ const keys = generateKeyPairSync('ed25519');
62
+ const parsed = tryParse(keys.private);
63
+ const pub = parsed.getPublicSSH();
64
+ assert.strictEqual(pub.length, 4 + 11 + 4 + 32, 'Wrong public key length');
65
+ assert.deepStrictEqual(tryParse(keys.public).getPublicSSH(), pub);
66
+ if (pub[4 + 11 + 4] === 0x00)
67
+ ++leadingZero;
68
+ }
69
+ assert(leadingZero > 0, 'No key with a leading zero byte was generated');
70
+ }
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ const assert = require('assert');
4
+
5
+ const {
6
+ fixture,
7
+ mustCall,
8
+ setup,
9
+ } = require('./common.js');
10
+
11
+ const debug = false;
12
+
13
+ // A connection emits 'close' before its channels are closed, so a handler
14
+ // there can still close one. That write has nowhere to go and is dropped. With
15
+ // compression it used to throw 'Invalid Zlib instance' out of the handler.
16
+ [ 'none', 'zlib@openssh.com' ].forEach((compress) => {
17
+ [ 'sftp', 'exec' ].forEach((type) => {
18
+ const algorithms = { compress: [ compress ] };
19
+ const { client, server } = setup(
20
+ `Channel (${type}) closed after the connection is gone (${compress})`,
21
+ {
22
+ client: { username: 'foo', password: 'bar', algorithms },
23
+ server: { hostKeys: [ fixture('ssh_host_rsa_key') ], algorithms },
24
+ debug,
25
+ noClientError: true,
26
+ noServerError: true,
27
+ }
28
+ );
29
+
30
+ server.on('connection', mustCall((conn) => {
31
+ let channel;
32
+ conn.on('authentication', mustCall((ctx) => {
33
+ ctx.accept();
34
+ })).on('ready', mustCall(() => {
35
+ conn.on('session', mustCall((accept, reject) => {
36
+ accept().on(type, mustCall((accept, reject) => {
37
+ channel = accept();
38
+ // The peer just vanishes
39
+ client._sock.destroy();
40
+ }));
41
+ }));
42
+ })).on('error', () => {
43
+ // ECONNRESET, depending on timing
44
+ }).on('close', mustCall(() => {
45
+ assert(channel, 'Missing channel');
46
+ assert.strictEqual(channel.outgoing.state, 'open');
47
+ if (type === 'sftp')
48
+ channel.destroy();
49
+ else
50
+ channel.close();
51
+ }));
52
+ }));
53
+
54
+ client.on('error', () => {}).on('ready', mustCall(() => {
55
+ if (type === 'sftp')
56
+ client.sftp(mustCall((err) => assert(err, 'Expected error')));
57
+ else
58
+ client.exec('uptime', mustCall((err) => assert(err, 'Expected error')));
59
+ }));
60
+ });
61
+ });