@electerm/ssh2 1.16.2 → 1.17.1

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.
package/lib/client.js CHANGED
@@ -84,6 +84,7 @@ class Client extends EventEmitter {
84
84
  username: undefined,
85
85
  password: undefined,
86
86
  privateKey: undefined,
87
+ certificate: undefined,
87
88
  tryKeyboard: undefined,
88
89
  agent: undefined,
89
90
  allowAgentFwd: undefined,
@@ -209,6 +210,10 @@ class Client extends EventEmitter {
209
210
  || Buffer.isBuffer(cfg.privateKey)
210
211
  ? cfg.privateKey
211
212
  : undefined);
213
+ this.config.certificate = (typeof cfg.certificate === 'string'
214
+ || Buffer.isBuffer(cfg.certificate)
215
+ ? cfg.certificate
216
+ : undefined);
212
217
  this.config.localHostname = (typeof cfg.localHostname === 'string'
213
218
  ? cfg.localHostname
214
219
  : undefined);
@@ -268,6 +273,42 @@ class Client extends EventEmitter {
268
273
  'privateKey value does not contain a (valid) private key'
269
274
  );
270
275
  }
276
+
277
+ // Wrap with certificate if provided
278
+ if (this.config.certificate) {
279
+ const { wrapKeyWithCertificate } = require('./protocol/certificateAuth.js');
280
+ let certBuffer = this.config.certificate;
281
+
282
+ // Handle string or buffer that contains OpenSSH public key format
283
+ let certStr;
284
+ if (typeof certBuffer === 'string') {
285
+ certStr = certBuffer.trim();
286
+ } else if (Buffer.isBuffer(certBuffer)) {
287
+ // Check if it's text format (starts with ssh-) or binary
288
+ const firstBytes = certBuffer.slice(0, 4).toString('utf8');
289
+ if (firstBytes.startsWith('ssh-') || firstBytes.startsWith('ecds')) {
290
+ certStr = certBuffer.toString('utf8').trim();
291
+ }
292
+ }
293
+
294
+ if (certStr) {
295
+ // Parse OpenSSH public key format: "type base64data [comment]"
296
+ const parts = certStr.split(/\s+/);
297
+ if (parts.length >= 2) {
298
+ try {
299
+ certBuffer = Buffer.from(parts[1], 'base64');
300
+ } catch (err) {
301
+ throw new Error(`Cannot parse certificate: ${err.message}`);
302
+ }
303
+ }
304
+ }
305
+
306
+ const wrappedKey = wrapKeyWithCertificate(privateKey, certBuffer);
307
+ if (wrappedKey instanceof Error) {
308
+ throw new Error(`Cannot wrap key with certificate: ${wrappedKey.message}`);
309
+ }
310
+ privateKey = wrappedKey;
311
+ }
271
312
  }
272
313
 
273
314
  let hostVerifier;
@@ -321,6 +362,10 @@ class Client extends EventEmitter {
321
362
  },
322
363
  onHandshakeComplete: (negotiated) => {
323
364
  this.emit('handshake', negotiated);
365
+ // Start keepalive monitoring after handshake
366
+ if (this.config.keepaliveInterval > 0) {
367
+ resetKA();
368
+ }
324
369
  if (!ready) {
325
370
  ready = true;
326
371
  proto.service('ssh-userauth');
@@ -499,10 +544,14 @@ class Client extends EventEmitter {
499
544
  }
500
545
  },
501
546
  REQUEST_SUCCESS: (p, data) => {
547
+ // Reset keepalive on any protocol activity
548
+ this._resetKA();
502
549
  if (callbacks.length)
503
550
  callbacks.shift()(false, data);
504
551
  },
505
552
  REQUEST_FAILURE: (p) => {
553
+ // Reset keepalive on any protocol activity
554
+ this._resetKA();
506
555
  if (callbacks.length)
507
556
  callbacks.shift()(true);
508
557
  },
@@ -707,39 +756,71 @@ class Client extends EventEmitter {
707
756
 
708
757
  sock.pause();
709
758
 
710
- // TODO: check keepalive implementation
711
- // Keepalive-related
759
+ // Enhanced keepalive implementation with TCP-level and SSH protocol-level keepalives
712
760
  const kainterval = this.config.keepaliveInterval;
713
761
  const kacountmax = this.config.keepaliveCountMax;
714
762
  let kacount = 0;
715
763
  let katimer;
764
+ let lastActivity = Date.now();
765
+
766
+ // Enable TCP keepalive for network-level connection monitoring
767
+ // This helps detect dead connections even when SSH protocol keepalive is not used
768
+ if (typeof sock.setKeepAlive === 'function') {
769
+ sock.setKeepAlive(true, 10000); // Start probing after 10 seconds idle
770
+ }
771
+
716
772
  const sendKA = () => {
773
+ const now = Date.now();
774
+ const timeSinceLastActivity = now - lastActivity;
775
+
776
+ // If we've received any activity recently, reset counter and continue
777
+ if (timeSinceLastActivity < kainterval) {
778
+ kacount = 0;
779
+ return;
780
+ }
781
+
717
782
  if (++kacount > kacountmax) {
718
783
  clearInterval(katimer);
719
784
  if (sock.readable) {
720
- const err = new Error('Keepalive timeout');
785
+ const err = new Error(
786
+ `Keepalive timeout: no response after ${kacountmax} attempts`
787
+ );
721
788
  err.level = 'client-timeout';
722
789
  this.emit('error', err);
723
790
  sock.destroy();
724
791
  }
725
792
  return;
726
793
  }
794
+
727
795
  if (isWritable(sock)) {
728
- // Append dummy callback to keep correct callback order
729
- callbacks.push(resetKA);
796
+ if (debug) {
797
+ debug(
798
+ `Client: Sending keepalive (${kacount}/${kacountmax}), ` +
799
+ `${timeSinceLastActivity}ms since last activity`
800
+ );
801
+ }
802
+ // Send SSH protocol-level keepalive
803
+ // Use global request instead of relying on callbacks
730
804
  proto.ping();
805
+
806
+ // Don't rely on callback - we'll reset based on any activity
731
807
  } else {
732
808
  clearInterval(katimer);
733
809
  }
734
810
  };
811
+
735
812
  function resetKA() {
813
+ lastActivity = Date.now();
814
+ kacount = 0;
815
+
736
816
  if (kainterval > 0) {
737
- kacount = 0;
738
817
  clearInterval(katimer);
739
- if (isWritable(sock))
818
+ if (isWritable(sock)) {
740
819
  katimer = setInterval(sendKA, kainterval);
820
+ }
741
821
  }
742
822
  }
823
+
743
824
  this._resetKA = resetKA;
744
825
 
745
826
  const onDone = (() => {
@@ -632,31 +632,88 @@ class Protocol {
632
632
  if (this._server)
633
633
  throw new Error('Client-only method called in server mode');
634
634
 
635
- pubKey = parseKey(pubKey);
636
- if (pubKey instanceof Error)
637
- throw new Error('Invalid key');
635
+ const origPubKey = pubKey;
636
+
637
+ // Only parse if it's not already a parsed key object
638
+ // Check for common key object properties
639
+ if (typeof pubKey !== 'object' || pubKey === null ||
640
+ typeof pubKey.type !== 'string' || typeof pubKey.getPublicSSH !== 'function') {
641
+ pubKey = parseKey(pubKey);
642
+ if (pubKey instanceof Error)
643
+ throw new Error('Invalid key');
644
+ }
638
645
 
639
646
  let keyType = pubKey.type;
647
+
648
+ // Check if this is a certificate-wrapped key
649
+ let pubKeyData = pubKey.getPublicSSH();
650
+ let isCertificate = false;
651
+ if (pubKey.getCertificateBuffer) {
652
+ // This is a CertificateKey, use the certificate data instead
653
+ pubKeyData = pubKey.getCertificateBuffer();
654
+ isCertificate = true;
655
+ this._debug && this._debug('Using SSH certificate for authentication');
656
+ }
657
+
658
+ // Upgrade RSA to SHA2 variants if server supports them
659
+ // signAlgo is the actual algorithm used for signing
660
+ // NOTE: Order must match getKeyAlgos() in client.js - rsa-sha2-256 first
661
+ let signAlgo = keyType;
640
662
  if (keyType === 'ssh-rsa') {
641
- for (const algo of ['rsa-sha2-512', 'rsa-sha2-256']) {
663
+ for (const algo of ['rsa-sha2-256', 'rsa-sha2-512']) {
642
664
  if (this._remoteHostKeyAlgorithms.includes(algo)) {
643
- keyType = algo;
665
+ signAlgo = algo;
644
666
  break;
645
667
  }
646
668
  }
647
669
  }
648
- pubKey = pubKey.getPublicSSH();
670
+
671
+ // For certificates, append the cert suffix to the algorithm for the packet
672
+ // but keep signAlgo as the base signing algorithm
673
+ if (isCertificate) {
674
+ // Map the signing algorithm to certificate algorithm
675
+ // e.g., rsa-sha2-512 -> rsa-sha2-512-cert-v01@openssh.com
676
+ if (signAlgo === 'rsa-sha2-512') {
677
+ keyType = 'rsa-sha2-512-cert-v01@openssh.com';
678
+ } else if (signAlgo === 'rsa-sha2-256') {
679
+ keyType = 'rsa-sha2-256-cert-v01@openssh.com';
680
+ } else if (signAlgo === 'ssh-rsa' || keyType === 'ssh-rsa') {
681
+ keyType = 'ssh-rsa-cert-v01@openssh.com';
682
+ } else if (keyType === 'ssh-dss') {
683
+ keyType = 'ssh-dss-cert-v01@openssh.com';
684
+ } else if (keyType === 'ecdsa-sha2-nistp256') {
685
+ keyType = 'ecdsa-sha2-nistp256-cert-v01@openssh.com';
686
+ } else if (keyType === 'ecdsa-sha2-nistp384') {
687
+ keyType = 'ecdsa-sha2-nistp384-cert-v01@openssh.com';
688
+ } else if (keyType === 'ecdsa-sha2-nistp521') {
689
+ keyType = 'ecdsa-sha2-nistp521-cert-v01@openssh.com';
690
+ } else if (keyType === 'ssh-ed25519') {
691
+ keyType = 'ssh-ed25519-cert-v01@openssh.com';
692
+ }
693
+ this._debug && this._debug(`Certificate key algorithm: ${keyType}`);
694
+ } else {
695
+ // For non-certificates, keyType should be signAlgo
696
+ keyType = signAlgo;
697
+ }
649
698
 
650
699
  if (typeof keyAlgo === 'function') {
651
700
  cbSign = keyAlgo;
652
701
  keyAlgo = undefined;
653
702
  }
654
- if (!keyAlgo)
703
+
704
+ // For certificates, we must use the certificate algorithm regardless of what was passed
705
+ if (isCertificate) {
706
+ keyAlgo = keyType;
707
+ } else if (!keyAlgo) {
655
708
  keyAlgo = keyType;
709
+ }
656
710
 
657
711
  const userLen = Buffer.byteLength(username);
658
712
  const algoLen = Buffer.byteLength(keyAlgo);
659
- const pubKeyLen = pubKey.length;
713
+ // For certificates, signAlgo is the base signing algorithm (e.g., rsa-sha2-512)
714
+ // For non-certificates, signAlgo equals keyAlgo
715
+ const signAlgoLen = Buffer.byteLength(signAlgo);
716
+ const pubKeyLen = pubKeyData.length;
660
717
  const sessionID = this._kex.sessionID;
661
718
  const sesLen = sessionID.length;
662
719
  const payloadLen =
@@ -692,7 +749,7 @@ class Protocol {
692
749
  packet.utf8Write(keyAlgo, p += 4, algoLen);
693
750
 
694
751
  writeUInt32BE(packet, pubKeyLen, p += algoLen);
695
- packet.set(pubKey, p += 4);
752
+ packet.set(pubKeyData, p += 4);
696
753
 
697
754
  if (!cbSign) {
698
755
  this._authsQueue.push('publickey');
@@ -705,7 +762,7 @@ class Protocol {
705
762
  }
706
763
 
707
764
  cbSign(packet, (signature) => {
708
- signature = convertSignature(signature, keyType);
765
+ signature = convertSignature(signature, signAlgo);
709
766
  if (signature === false)
710
767
  throw new Error('Error while converting handshake signature');
711
768
 
@@ -713,7 +770,7 @@ class Protocol {
713
770
  p = this._packetRW.write.allocStart;
714
771
  packet = this._packetRW.write.alloc(
715
772
  1 + 4 + userLen + 4 + 14 + 4 + 9 + 1 + 4 + algoLen + 4 + pubKeyLen + 4
716
- + 4 + algoLen + 4 + sigLen
773
+ + 4 + signAlgoLen + 4 + sigLen
717
774
  );
718
775
 
719
776
  // TODO: simply copy from original "packet" to new `packet` to avoid
@@ -735,14 +792,17 @@ class Protocol {
735
792
  packet.utf8Write(keyAlgo, p += 4, algoLen);
736
793
 
737
794
  writeUInt32BE(packet, pubKeyLen, p += algoLen);
738
- packet.set(pubKey, p += 4);
795
+ packet.set(pubKeyData, p += 4);
739
796
 
740
- writeUInt32BE(packet, 4 + algoLen + 4 + sigLen, p += pubKeyLen);
797
+ // Signature blob: length-prefixed (algorithm string + raw signature)
798
+ // For RSA signatures, use the base signing algorithm (without cert suffix)
799
+ // The cert suffix is only used in the public key algorithm field of the main packet
800
+ writeUInt32BE(packet, 4 + signAlgoLen + 4 + sigLen, p += pubKeyLen);
741
801
 
742
- writeUInt32BE(packet, algoLen, p += 4);
743
- packet.utf8Write(keyAlgo, p += 4, algoLen);
802
+ writeUInt32BE(packet, signAlgoLen, p += 4);
803
+ packet.utf8Write(signAlgo, p += 4, signAlgoLen);
744
804
 
745
- writeUInt32BE(packet, sigLen, p += algoLen);
805
+ writeUInt32BE(packet, sigLen, p += signAlgoLen);
746
806
  packet.set(signature, p += 4);
747
807
 
748
808
  // Servers shouldn't send packet type 60 in response to signed publickey
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ parseSSHCertificate,
5
+ isCertificate,
6
+ } = require('./sshCertificate.js');
7
+
8
+ /**
9
+ * Handle SSH certificate public key authentication
10
+ * This extends regular publickey auth to support certificates
11
+ */
12
+
13
+ class CertificateKey {
14
+ constructor(baseKey, certificate, certBuffer) {
15
+ this.baseKey = baseKey; // The underlying parsed key
16
+ this.certificate = certificate; // Parsed certificate data
17
+ this.certBuffer = certBuffer; // Raw certificate buffer
18
+ this.type = baseKey.type;
19
+ this.comment = baseKey.comment;
20
+ }
21
+
22
+ isPrivateKey() {
23
+ return this.baseKey.isPrivateKey();
24
+ }
25
+
26
+ getPublicPEM() {
27
+ return this.baseKey.getPublicPEM?.();
28
+ }
29
+
30
+ getPublicSSH(algo) {
31
+ return this.baseKey.getPublicSSH?.(algo);
32
+ }
33
+
34
+ sign(data, algo) {
35
+ return this.baseKey.sign(data, algo);
36
+ }
37
+
38
+ verify(data, signature, algo) {
39
+ return this.baseKey.verify(data, signature, algo);
40
+ }
41
+
42
+ getCertificate() {
43
+ return this.certificate;
44
+ }
45
+
46
+ getCertificateBuffer() {
47
+ return this.certBuffer;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Wrap a parsed key with certificate information
53
+ */
54
+ function wrapKeyWithCertificate(key, certBuffer) {
55
+ if (!Buffer.isBuffer(certBuffer)) {
56
+ return new Error('Certificate must be a Buffer');
57
+ }
58
+
59
+ try {
60
+ // Verify this is a certificate
61
+ if (!isCertificate(certBuffer)) {
62
+ return new Error('Buffer does not appear to be a certificate');
63
+ }
64
+
65
+ // Parse the certificate - it expects the complete buffer including type string
66
+ const certificate = parseSSHCertificate(certBuffer);
67
+
68
+ if (certificate instanceof Error) {
69
+ return certificate;
70
+ }
71
+
72
+ return new CertificateKey(key, certificate, certBuffer);
73
+ } catch (err) {
74
+ return err;
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Extract certificate from OpenSSH public key format
80
+ * Returns { certBuffer, remainingData } or Error
81
+ */
82
+ function extractCertificateFromData(data) {
83
+ if (!Buffer.isBuffer(data)) {
84
+ return new Error('Data must be a Buffer');
85
+ }
86
+
87
+ if (!isCertificate(data)) {
88
+ return null; // Not a certificate
89
+ }
90
+
91
+ try {
92
+ // The data is in binary SSH format: [type-len][type][data-len][data]...
93
+ // For a certificate, the entire data IS the certificate
94
+ return { certBuffer: data };
95
+ } catch (err) {
96
+ return err;
97
+ }
98
+ }
99
+
100
+ module.exports = {
101
+ CertificateKey,
102
+ wrapKeyWithCertificate,
103
+ extractCertificateFromData,
104
+ };