bdy 1.26.10 → 1.26.11-beta

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
@@ -74,7 +74,7 @@ enum ErrorType {
74
74
 
75
75
  #define POLY1305_KEYLEN 32
76
76
  #define POLY1305_TAGLEN 16
77
- class ChaChaPolyCipher : public ObjectWrap {
77
+ class ChaChaPolyCipher : public Nan::ObjectWrap {
78
78
  public:
79
79
  static NAN_MODULE_INIT(Init) {
80
80
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -85,8 +85,7 @@ class ChaChaPolyCipher : public ObjectWrap {
85
85
  SetPrototypeMethod(tpl, "free", Free);
86
86
 
87
87
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
88
- Local<Context> context = Nan::GetCurrentContext();
89
- v8::Isolate* isolate = context->GetIsolate();
88
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
90
89
 
91
90
  constructor().Set(isolate, func);
92
91
 
@@ -359,7 +358,8 @@ out:
359
358
  static NAN_METHOD(Encrypt) {
360
359
  MarkPopErrorOnReturn mark_pop_error_on_return;
361
360
 
362
- ChaChaPolyCipher* obj = ObjectWrap::Unwrap<ChaChaPolyCipher>(info.Holder());
361
+ ChaChaPolyCipher* obj =
362
+ Nan::ObjectWrap::Unwrap<ChaChaPolyCipher>(info.Holder());
363
363
 
364
364
  if (!Buffer::HasInstance(info[0]))
365
365
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -387,7 +387,8 @@ out:
387
387
  }
388
388
 
389
389
  static NAN_METHOD(Free) {
390
- ChaChaPolyCipher* obj = ObjectWrap::Unwrap<ChaChaPolyCipher>(info.Holder());
390
+ ChaChaPolyCipher* obj =
391
+ Nan::ObjectWrap::Unwrap<ChaChaPolyCipher>(info.Holder());
391
392
  obj->clear();
392
393
  }
393
394
 
@@ -408,7 +409,7 @@ out:
408
409
  #endif
409
410
  };
410
411
 
411
- class AESGCMCipher : public ObjectWrap {
412
+ class AESGCMCipher : public Nan::ObjectWrap {
412
413
  public:
413
414
  static NAN_MODULE_INIT(Init) {
414
415
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -419,8 +420,7 @@ class AESGCMCipher : public ObjectWrap {
419
420
  SetPrototypeMethod(tpl, "free", Free);
420
421
 
421
422
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
422
- Local<Context> context = Nan::GetCurrentContext();
423
- v8::Isolate* isolate = context->GetIsolate();
423
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
424
424
 
425
425
  constructor().Set(isolate, func);
426
426
 
@@ -607,7 +607,7 @@ out:
607
607
  static NAN_METHOD(Encrypt) {
608
608
  MarkPopErrorOnReturn mark_pop_error_on_return;
609
609
 
610
- AESGCMCipher* obj = ObjectWrap::Unwrap<AESGCMCipher>(info.Holder());
610
+ AESGCMCipher* obj = Nan::ObjectWrap::Unwrap<AESGCMCipher>(info.Holder());
611
611
 
612
612
  if (!Buffer::HasInstance(info[0]))
613
613
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -637,7 +637,7 @@ out:
637
637
  }
638
638
 
639
639
  static NAN_METHOD(Free) {
640
- AESGCMCipher* obj = ObjectWrap::Unwrap<AESGCMCipher>(info.Holder());
640
+ AESGCMCipher* obj = Nan::ObjectWrap::Unwrap<AESGCMCipher>(info.Holder());
641
641
  obj->clear();
642
642
  }
643
643
 
@@ -649,7 +649,7 @@ out:
649
649
  EVP_CIPHER_CTX* ctx_;
650
650
  };
651
651
 
652
- class GenericCipher : public ObjectWrap {
652
+ class GenericCipher : public Nan::ObjectWrap {
653
653
  public:
654
654
  static NAN_MODULE_INIT(Init) {
655
655
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -660,8 +660,7 @@ class GenericCipher : public ObjectWrap {
660
660
  SetPrototypeMethod(tpl, "free", Free);
661
661
 
662
662
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
663
- Local<Context> context = Nan::GetCurrentContext();
664
- v8::Isolate* isolate = context->GetIsolate();
663
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
665
664
 
666
665
  constructor().Set(isolate, func);
667
666
 
@@ -990,7 +989,7 @@ out:
990
989
  static NAN_METHOD(Encrypt) {
991
990
  MarkPopErrorOnReturn mark_pop_error_on_return;
992
991
 
993
- GenericCipher* obj = ObjectWrap::Unwrap<GenericCipher>(info.Holder());
992
+ GenericCipher* obj = Nan::ObjectWrap::Unwrap<GenericCipher>(info.Holder());
994
993
 
995
994
  if (!Buffer::HasInstance(info[0]))
996
995
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -1022,7 +1021,7 @@ out:
1022
1021
  }
1023
1022
 
1024
1023
  static NAN_METHOD(Free) {
1025
- GenericCipher* obj = ObjectWrap::Unwrap<GenericCipher>(info.Holder());
1024
+ GenericCipher* obj = Nan::ObjectWrap::Unwrap<GenericCipher>(info.Holder());
1026
1025
  obj->clear();
1027
1026
  }
1028
1027
 
@@ -1045,7 +1044,7 @@ out:
1045
1044
 
1046
1045
  // =============================================================================
1047
1046
 
1048
- class ChaChaPolyDecipher : public ObjectWrap {
1047
+ class ChaChaPolyDecipher : public Nan::ObjectWrap {
1049
1048
  public:
1050
1049
  static NAN_MODULE_INIT(Init) {
1051
1050
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -1057,8 +1056,7 @@ class ChaChaPolyDecipher : public ObjectWrap {
1057
1056
  SetPrototypeMethod(tpl, "free", Free);
1058
1057
 
1059
1058
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
1060
- Local<Context> context = Nan::GetCurrentContext();
1061
- v8::Isolate* isolate = context->GetIsolate();
1059
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
1062
1060
 
1063
1061
  constructor().Set(isolate, func);
1064
1062
 
@@ -1373,7 +1371,7 @@ out:
1373
1371
  MarkPopErrorOnReturn mark_pop_error_on_return;
1374
1372
 
1375
1373
  ChaChaPolyDecipher* obj =
1376
- ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1374
+ Nan::ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1377
1375
 
1378
1376
  if (!Buffer::HasInstance(info[0]) || Buffer::Length(info[0]) != 4)
1379
1377
  return Nan::ThrowTypeError("Missing/Invalid length bytes");
@@ -1412,7 +1410,7 @@ out:
1412
1410
  MarkPopErrorOnReturn mark_pop_error_on_return;
1413
1411
 
1414
1412
  ChaChaPolyDecipher* obj =
1415
- ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1413
+ Nan::ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1416
1414
 
1417
1415
  if (!Buffer::HasInstance(info[0]))
1418
1416
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -1452,7 +1450,7 @@ out:
1452
1450
 
1453
1451
  static NAN_METHOD(Free) {
1454
1452
  ChaChaPolyDecipher* obj =
1455
- ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1453
+ Nan::ObjectWrap::Unwrap<ChaChaPolyDecipher>(info.Holder());
1456
1454
  obj->clear();
1457
1455
  }
1458
1456
 
@@ -1474,7 +1472,7 @@ out:
1474
1472
  #endif
1475
1473
  };
1476
1474
 
1477
- class AESGCMDecipher : public ObjectWrap {
1475
+ class AESGCMDecipher : public Nan::ObjectWrap {
1478
1476
  public:
1479
1477
  static NAN_MODULE_INIT(Init) {
1480
1478
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -1485,8 +1483,7 @@ class AESGCMDecipher : public ObjectWrap {
1485
1483
  SetPrototypeMethod(tpl, "free", Free);
1486
1484
 
1487
1485
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
1488
- Local<Context> context = Nan::GetCurrentContext();
1489
- v8::Isolate* isolate = context->GetIsolate();
1486
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
1490
1487
 
1491
1488
  constructor().Set(isolate, func);
1492
1489
 
@@ -1668,7 +1665,8 @@ out:
1668
1665
  static NAN_METHOD(Decrypt) {
1669
1666
  MarkPopErrorOnReturn mark_pop_error_on_return;
1670
1667
 
1671
- AESGCMDecipher* obj = ObjectWrap::Unwrap<AESGCMDecipher>(info.Holder());
1668
+ AESGCMDecipher* obj =
1669
+ Nan::ObjectWrap::Unwrap<AESGCMDecipher>(info.Holder());
1672
1670
 
1673
1671
  if (!Buffer::HasInstance(info[0]))
1674
1672
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -1713,7 +1711,8 @@ out:
1713
1711
  }
1714
1712
 
1715
1713
  static NAN_METHOD(Free) {
1716
- AESGCMDecipher* obj = ObjectWrap::Unwrap<AESGCMDecipher>(info.Holder());
1714
+ AESGCMDecipher* obj =
1715
+ Nan::ObjectWrap::Unwrap<AESGCMDecipher>(info.Holder());
1717
1716
  obj->clear();
1718
1717
  }
1719
1718
 
@@ -1725,7 +1724,7 @@ out:
1725
1724
  EVP_CIPHER_CTX* ctx_;
1726
1725
  };
1727
1726
 
1728
- class GenericDecipher : public ObjectWrap {
1727
+ class GenericDecipher : public Nan::ObjectWrap {
1729
1728
  public:
1730
1729
  static NAN_MODULE_INIT(Init) {
1731
1730
  Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -1737,8 +1736,7 @@ class GenericDecipher : public ObjectWrap {
1737
1736
  SetPrototypeMethod(tpl, "free", Free);
1738
1737
 
1739
1738
  Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
1740
- Local<Context> context = Nan::GetCurrentContext();
1741
- v8::Isolate* isolate = context->GetIsolate();
1739
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
1742
1740
 
1743
1741
  constructor().Set(isolate, func);
1744
1742
 
@@ -2127,7 +2125,8 @@ out:
2127
2125
  static NAN_METHOD(DecryptBlock) {
2128
2126
  MarkPopErrorOnReturn mark_pop_error_on_return;
2129
2127
 
2130
- GenericDecipher* obj = ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2128
+ GenericDecipher* obj =
2129
+ Nan::ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2131
2130
 
2132
2131
  if (!Buffer::HasInstance(info[0]))
2133
2132
  return Nan::ThrowTypeError("Missing/Invalid block");
@@ -2157,7 +2156,8 @@ out:
2157
2156
  static NAN_METHOD(Decrypt) {
2158
2157
  MarkPopErrorOnReturn mark_pop_error_on_return;
2159
2158
 
2160
- GenericDecipher* obj = ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2159
+ GenericDecipher* obj =
2160
+ Nan::ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2161
2161
 
2162
2162
  if (!Buffer::HasInstance(info[0]))
2163
2163
  return Nan::ThrowTypeError("Missing/Invalid packet");
@@ -2203,7 +2203,8 @@ out:
2203
2203
  }
2204
2204
 
2205
2205
  static NAN_METHOD(Free) {
2206
- GenericDecipher* obj = ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2206
+ GenericDecipher* obj =
2207
+ Nan::ObjectWrap::Unwrap<GenericDecipher>(info.Holder());
2207
2208
  obj->clear();
2208
2209
  }
2209
2210
 
@@ -6,7 +6,9 @@ const {
6
6
  createECDH,
7
7
  createHash,
8
8
  createPublicKey,
9
+ decapsulate,
9
10
  diffieHellman,
11
+ encapsulate,
10
12
  generateKeyPairSync,
11
13
  randomFillSync,
12
14
  } = require('crypto');
@@ -16,6 +18,7 @@ const { Ber } = require('asn1');
16
18
  const {
17
19
  COMPAT,
18
20
  curve25519Supported,
21
+ mlkem768x25519Supported,
19
22
  DEFAULT_KEX,
20
23
  DEFAULT_SERVER_HOST_KEY,
21
24
  DEFAULT_CIPHER,
@@ -1351,6 +1354,120 @@ const createKeyExchange = (() => {
1351
1354
  }
1352
1355
  }
1353
1356
 
1357
+ // The mlkem768x25519-sha256 method, a hybrid of ML-KEM-768 and X25519. Unlike
1358
+ // the (EC)DH methods the two sides do not send the same kind of value:
1359
+ //
1360
+ // C_INIT = ML-KEM-768 public key (1184) || X25519 public key (32)
1361
+ // S_REPLY = ML-KEM-768 ciphertext (1088) || X25519 public key (32)
1362
+ //
1363
+ // so the server's half only exists once the client's has arrived, and it is
1364
+ // made in computeSecret(). finish() relies on that order: it computes the
1365
+ // secret before it asks for our public value.
1366
+ //
1367
+ // K = SHA-256(K_PQ || K_CL), and it is a `string`, not an `mpint`.
1368
+ const MLKEM768_PUBKEY_LEN = 1184;
1369
+ const MLKEM768_CIPHERTEXT_LEN = 1088;
1370
+ const X25519_PUBKEY_LEN = 32;
1371
+ // There is no import in node for raw public keys of either type, so the DER
1372
+ // header of a throwaway key is reused. It is the same for every key of a type
1373
+ let mlkem768SpkiHeader;
1374
+ let x25519SpkiHeader;
1375
+
1376
+ function spkiHeader(type, rawLen) {
1377
+ const der = generateKeyPairSync(type).publicKey.export({
1378
+ type: 'spki',
1379
+ format: 'der',
1380
+ });
1381
+ return der.slice(0, der.length - rawLen);
1382
+ }
1383
+
1384
+ function rawPublicKey(header, raw) {
1385
+ return createPublicKey({
1386
+ key: Buffer.concat([header, raw]),
1387
+ type: 'spki',
1388
+ format: 'der',
1389
+ });
1390
+ }
1391
+
1392
+ function rawFromKey(key, rawLen) {
1393
+ return key.export({ type: 'spki', format: 'der' }).slice(-rawLen);
1394
+ }
1395
+
1396
+ class MLKEM768X25519Exchange extends KeyExchange {
1397
+ constructor(...args) {
1398
+ super(...args);
1399
+
1400
+ this.type = 'mlkem768x25519';
1401
+ this.hashName = 'sha256';
1402
+ this._keys = null;
1403
+ this._kemKeys = null;
1404
+ this._reply = null;
1405
+ }
1406
+ generateKeys() {
1407
+ if (this._keys)
1408
+ return;
1409
+ if (!mlkem768SpkiHeader) {
1410
+ mlkem768SpkiHeader = spkiHeader('ml-kem-768', MLKEM768_PUBKEY_LEN);
1411
+ x25519SpkiHeader = spkiHeader('x25519', X25519_PUBKEY_LEN);
1412
+ }
1413
+ this._keys = generateKeyPairSync('x25519');
1414
+ // Only the client has an ML-KEM key pair, the server encapsulates to it
1415
+ if (!this._protocol._server)
1416
+ this._kemKeys = generateKeyPairSync('ml-kem-768');
1417
+ }
1418
+ getPublicKey() {
1419
+ this.generateKeys();
1420
+
1421
+ if (this._protocol._server)
1422
+ return this._reply;
1423
+
1424
+ return Buffer.concat([
1425
+ rawFromKey(this._kemKeys.publicKey, MLKEM768_PUBKEY_LEN),
1426
+ rawFromKey(this._keys.publicKey, X25519_PUBKEY_LEN),
1427
+ ]);
1428
+ }
1429
+ convertPublicKey(key) {
1430
+ return key;
1431
+ }
1432
+ computeSecret(otherPublicKey) {
1433
+ this.generateKeys();
1434
+
1435
+ try {
1436
+ const isServer = this._protocol._server;
1437
+ const pqLen = (isServer
1438
+ ? MLKEM768_PUBKEY_LEN
1439
+ : MLKEM768_CIPHERTEXT_LEN);
1440
+ if (otherPublicKey.length !== pqLen + X25519_PUBKEY_LEN)
1441
+ throw new Error('Invalid length of the remote key exchange value');
1442
+
1443
+ const pq = otherPublicKey.slice(0, pqLen);
1444
+ // Throws for a peer key that gives an all-zero shared secret
1445
+ const secretCL = diffieHellman({
1446
+ privateKey: this._keys.privateKey,
1447
+ publicKey: rawPublicKey(x25519SpkiHeader,
1448
+ otherPublicKey.slice(pqLen)),
1449
+ });
1450
+
1451
+ let secretPQ;
1452
+ if (isServer) {
1453
+ const { sharedKey, ciphertext } =
1454
+ encapsulate(rawPublicKey(mlkem768SpkiHeader, pq));
1455
+ secretPQ = sharedKey;
1456
+ this._reply = Buffer.concat([
1457
+ ciphertext,
1458
+ rawFromKey(this._keys.publicKey, X25519_PUBKEY_LEN),
1459
+ ]);
1460
+ } else {
1461
+ secretPQ = decapsulate(this._kemKeys.privateKey, pq);
1462
+ }
1463
+
1464
+ return createHash('sha256').update(secretPQ).update(secretCL).digest();
1465
+ } catch (ex) {
1466
+ return ex;
1467
+ }
1468
+ }
1469
+ }
1470
+
1354
1471
  class ECDHExchange extends KeyExchange {
1355
1472
  constructor(curveName, hashName, ...args) {
1356
1473
  super(...args);
@@ -1593,6 +1710,11 @@ const createKeyExchange = (() => {
1593
1710
  break;
1594
1711
  return new Curve25519Exchange('sha256', ...args);
1595
1712
 
1713
+ case 'mlkem768x25519-sha256':
1714
+ if (!mlkem768x25519Supported)
1715
+ break;
1716
+ return new MLKEM768X25519Exchange(...args);
1717
+
1596
1718
  case 'ecdh-sha2-nistp256':
1597
1719
  return new ECDHExchange('prime256v1', 'sha256', ...args);
1598
1720
  case 'ecdh-sha2-nistp384':
@@ -177,8 +177,10 @@ class ZlibPacketWriter {
177
177
  }
178
178
 
179
179
  cleanup() {
180
- if (this._zlib)
180
+ if (this._zlib) {
181
181
  _close(this._zlib);
182
+ this._zlib = undefined;
183
+ }
182
184
  }
183
185
 
184
186
  alloc(payloadSize, force) {
@@ -187,6 +189,14 @@ class ZlibPacketWriter {
187
189
 
188
190
  finalize(payload, force) {
189
191
  if (this._protocol._kexinit === undefined || force) {
192
+ // After cleanup() the connection is gone and the cipher drops whatever
193
+ // it is given (see free() in crypto.js), so there is nothing to
194
+ // compress. Without compression a late write, such as a channel closed
195
+ // from the connection's 'close' handler, has always been dropped
196
+ // silently, and here it threw 'Invalid Zlib instance' instead
197
+ if (this._zlib === undefined)
198
+ return this._protocol._cipher.allocPacket(payload.length);
199
+
190
200
  const output = this._zlib.writeSync(payload, true);
191
201
  const packet = this._protocol._cipher.allocPacket(output.totalLen);
192
202
  if (output.push === undefined) {
@@ -33,10 +33,10 @@ const { writeUInt32BE } = require('./protocol/utils.js');
33
33
 
34
34
  const {
35
35
  Channel,
36
- MAX_WINDOW,
37
36
  PACKET_SIZE,
37
+ parseChannelWindow,
38
38
  windowAdjust,
39
- WINDOW_THRESHOLD,
39
+ windowThreshold,
40
40
  } = require('./Channel.js');
41
41
 
42
42
  const {
@@ -131,6 +131,22 @@ class KeyboardAuthContext extends AuthContext {
131
131
  }
132
132
  }
133
133
 
134
+ // Whether the signature a client sent was made with the key it offered. The
135
+ // signature covers `blob` (the session id and the request), so a valid one is
136
+ // what proves the client has the private key, here and now.
137
+ function hasValidSignature(ctx) {
138
+ if (!Buffer.isBuffer(ctx.signature) || !Buffer.isBuffer(ctx.blob))
139
+ return false;
140
+ try {
141
+ const key = parseKey(ctx.key.data);
142
+ if (key instanceof Error || Array.isArray(key))
143
+ return false;
144
+ return (key.verify(ctx.blob, ctx.signature, ctx.hashAlgo) === true);
145
+ } catch {
146
+ return false;
147
+ }
148
+ }
149
+
134
150
  class PKAuthContext extends AuthContext {
135
151
  constructor(protocol, username, service, method, pkInfo, cb) {
136
152
  super(protocol, username, service, method, cb);
@@ -145,8 +161,13 @@ class PKAuthContext extends AuthContext {
145
161
  if (!this.signature) {
146
162
  this._initialResponse = true;
147
163
  this._protocol.authPKOK(this.key.algo, this.key.data);
148
- } else {
164
+ } else if (hasValidSignature(this)) {
149
165
  AuthContext.prototype.accept.call(this);
166
+ } else {
167
+ // An application that accepts once it has recognized the public key,
168
+ // without checking the signature itself, would otherwise log in anyone
169
+ // who knows that key. Public keys are not secret.
170
+ this.reject();
150
171
  }
151
172
  }
152
173
  }
@@ -162,6 +183,14 @@ class HostbasedAuthContext extends AuthContext {
162
183
  this.localHostname = pkInfo.localHostname;
163
184
  this.localUsername = pkInfo.localUsername;
164
185
  }
186
+
187
+ accept() {
188
+ // As for 'publickey': never without a valid signature
189
+ if (hasValidSignature(this))
190
+ AuthContext.prototype.accept.call(this);
191
+ else
192
+ this.reject();
193
+ }
165
194
  }
166
195
 
167
196
  class PwdAuthContext extends AuthContext {
@@ -198,7 +227,7 @@ class Session extends EventEmitter {
198
227
  type: 'session',
199
228
  incoming: {
200
229
  id: localChan,
201
- window: MAX_WINDOW,
230
+ window: client._maxWindow,
202
231
  packetSize: PACKET_SIZE,
203
232
  state: 'open'
204
233
  },
@@ -220,6 +249,9 @@ class Server extends EventEmitter {
220
249
  if (typeof cfg !== 'object' || cfg === null)
221
250
  throw new Error('Missing configuration object');
222
251
 
252
+ // Used per connection, checked here so a bad value fails at startup
253
+ parseChannelWindow(cfg.channelWindow);
254
+
223
255
  const hostKeys = Object.create(null);
224
256
  const hostKeyAlgoOrder = [];
225
257
 
@@ -399,6 +431,8 @@ class Client extends EventEmitter {
399
431
  constructor(socket, hostKeys, ident, offer, debug, server, srvCfg) {
400
432
  super();
401
433
 
434
+ this._maxWindow = parseChannelWindow(srvCfg.channelWindow);
435
+
402
436
  let exchanges = 0;
403
437
  let acceptedAuthSvc = false;
404
438
  let pendingAuths = [];
@@ -595,7 +629,7 @@ class Client extends EventEmitter {
595
629
 
596
630
  proto.channelOpenConfirm(info.sender,
597
631
  localChan,
598
- MAX_WINDOW,
632
+ this._maxWindow,
599
633
  PACKET_SIZE);
600
634
 
601
635
  return instance;
@@ -616,7 +650,7 @@ class Client extends EventEmitter {
616
650
  type: undefined,
617
651
  incoming: {
618
652
  id: localChan,
619
- window: MAX_WINDOW,
653
+ window: this._maxWindow,
620
654
  packetSize: PACKET_SIZE,
621
655
  state: 'open'
622
656
  },
@@ -633,7 +667,7 @@ class Client extends EventEmitter {
633
667
 
634
668
  proto.channelOpenConfirm(info.sender,
635
669
  localChan,
636
- MAX_WINDOW,
670
+ this._maxWindow,
637
671
  PACKET_SIZE);
638
672
 
639
673
  return stream;
@@ -655,7 +689,7 @@ class Client extends EventEmitter {
655
689
  type: undefined,
656
690
  incoming: {
657
691
  id: localChan,
658
- window: MAX_WINDOW,
692
+ window: this._maxWindow,
659
693
  packetSize: PACKET_SIZE,
660
694
  state: 'open'
661
695
  },
@@ -672,7 +706,7 @@ class Client extends EventEmitter {
672
706
 
673
707
  proto.channelOpenConfirm(info.sender,
674
708
  localChan,
675
- MAX_WINDOW,
709
+ this._maxWindow,
676
710
  PACKET_SIZE);
677
711
 
678
712
  return stream;
@@ -710,7 +744,7 @@ class Client extends EventEmitter {
710
744
  type: channel.type,
711
745
  incoming: {
712
746
  id: info.recipient,
713
- window: MAX_WINDOW,
747
+ window: this._maxWindow,
714
748
  packetSize: PACKET_SIZE,
715
749
  state: 'open'
716
750
  },
@@ -758,7 +792,7 @@ class Client extends EventEmitter {
758
792
  return;
759
793
  }
760
794
 
761
- if (channel.incoming.window <= WINDOW_THRESHOLD)
795
+ if (channel.incoming.window <= windowThreshold(this))
762
796
  windowAdjust(channel);
763
797
  },
764
798
  CHANNEL_EXTENDED_DATA: (p, recipient, data, type) => {
@@ -1286,6 +1320,17 @@ class Client extends EventEmitter {
1286
1320
  return this;
1287
1321
  }
1288
1322
 
1323
+ // Unlike end(), which sends a disconnect and then waits for the client to
1324
+ // close, this drops the connection at once. Also for a socket that is no
1325
+ // longer writable: after end() it is only half-closed, and it stays that way
1326
+ // for as long as a client that is gone does not answer
1327
+ destroy() {
1328
+ const sock = this._sock;
1329
+ if (sock && typeof sock.destroy === 'function' && !sock.destroyed)
1330
+ sock.destroy();
1331
+ return this;
1332
+ }
1333
+
1289
1334
  x11(originAddr, originPort, cb) {
1290
1335
  const opts = { originAddr, originPort };
1291
1336
  openChannel(this, 'x11', opts, cb);
@@ -1335,7 +1380,7 @@ class Client extends EventEmitter {
1335
1380
  function openChannel(self, type, opts, cb) {
1336
1381
  // Ask the client to open a channel for some purpose (e.g. a forwarded TCP
1337
1382
  // connection)
1338
- const initWindow = MAX_WINDOW;
1383
+ const initWindow = self._maxWindow;
1339
1384
  const maxPacket = PACKET_SIZE;
1340
1385
 
1341
1386
  if (typeof opts === 'function') {
@@ -1,11 +1,11 @@
1
1
  {
2
- "name": "ssh2",
3
- "version": "1.17.0",
2
+ "name": "@buddy-works/ssh2",
3
+ "version": "1.17.0-pq.1",
4
4
  "author": "Brian White <mscdex@mscdex.net>",
5
- "description": "SSH2 client and server modules written in pure JavaScript for node.js",
5
+ "description": "SSH2 client and server modules written in pure JavaScript for node.js (fork of ssh2 with post-quantum key exchange)",
6
6
  "main": "./lib/index.js",
7
7
  "engines": {
8
- "node": ">=10.16.0"
8
+ "node": ">=16.0.0"
9
9
  },
10
10
  "dependencies": {
11
11
  "asn1": "^0.2.6",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "optionalDependencies": {
19
19
  "cpu-features": "~0.0.10",
20
- "nan": "^2.23.0"
20
+ "nan": "^2.30.0"
21
21
  },
22
22
  "scripts": {
23
23
  "install": "node install.js",
@@ -44,6 +44,9 @@
44
44
  ],
45
45
  "repository": {
46
46
  "type": "git",
47
- "url": "http://github.com/mscdex/ssh2.git"
47
+ "url": "git+https://github.com/buddy/ssh2.git"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
48
51
  }
49
52
  }