@electerm/ssh2 1.16.1 → 1.17.0

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;
@@ -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
+ };
@@ -148,10 +148,10 @@ cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
148
148
  # Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
149
149
  # already.
150
150
  quiet_cmd_mac_tool = MACTOOL $(4) $<
151
- cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"
151
+ cmd_mac_tool = /usr/local/opt/python@3.14/bin/python3.14 gyp-mac-tool $(4) $< "$@"
152
152
 
153
153
  quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
154
- cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)
154
+ cmd_mac_package_framework = /usr/local/opt/python@3.14/bin/python3.14 gyp-mac-tool package-framework "$@" $(4)
155
155
 
156
156
  quiet_cmd_infoplist = INFOPLIST $@
157
157
  cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"
@@ -167,7 +167,7 @@ quiet_cmd_symlink = SYMLINK $@
167
167
  cmd_symlink = ln -sf "$<" "$@"
168
168
 
169
169
  quiet_cmd_alink = LIBTOOL-STATIC $@
170
- cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)
170
+ cmd_alink = rm -f $@ && /usr/local/opt/python@3.14/bin/python3.14 gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)
171
171
 
172
172
  quiet_cmd_link = LINK($(TOOLSET)) $@
173
173
  cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
@@ -331,8 +331,8 @@ ifeq ($(strip $(foreach prefix,$(NO_LOAD),\
331
331
  endif
332
332
 
333
333
  quiet_cmd_regen_makefile = ACTION Regenerating $@
334
- cmd_regen_makefile = cd $(srcdir); /Users/home/.nvm/versions/node/v18.17.1/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/home/Library/Caches/node-gyp/18.17.1" "-Dnode_gyp_dir=/Users/home/.nvm/versions/node/v18.17.1/lib/node_modules/npm/node_modules/node-gyp" "-Dnode_lib_file=/Users/home/Library/Caches/node-gyp/18.17.1/<(target_arch)/node.lib" "-Dmodule_root_dir=/Users/home/dev/ssh2/lib/protocol/crypto" "-Dnode_engine=v8" "--depth=." "-Goutput_dir=." "--generator-output=build" -I/Users/home/dev/ssh2/lib/protocol/crypto/build/config.gypi -I/Users/home/.nvm/versions/node/v18.17.1/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/Users/home/Library/Caches/node-gyp/18.17.1/include/node/common.gypi "--toplevel-dir=." binding.gyp
335
- Makefile: $(srcdir)/build/config.gypi $(srcdir)/binding.gyp $(srcdir)/../../../../../.nvm/versions/node/v18.17.1/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/../../../../../Library/Caches/node-gyp/18.17.1/include/node/common.gypi
334
+ cmd_regen_makefile = cd $(srcdir); /Users/zxd/.nvm/versions/node/v22.19.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/zxd/Library/Caches/node-gyp/22.19.0" "-Dnode_gyp_dir=/Users/zxd/.nvm/versions/node/v22.19.0/lib/node_modules/npm/node_modules/node-gyp" "-Dnode_lib_file=/Users/zxd/Library/Caches/node-gyp/22.19.0/<(target_arch)/node.lib" "-Dmodule_root_dir=/Users/zxd/dev/ssh2/lib/protocol/crypto" "-Dnode_engine=v8" "--depth=." "-Goutput_dir=." "--generator-output=build" -I/Users/zxd/dev/ssh2/lib/protocol/crypto/build/config.gypi -I/Users/zxd/.nvm/versions/node/v22.19.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi -I/Users/zxd/Library/Caches/node-gyp/22.19.0/include/node/common.gypi "--toplevel-dir=." binding.gyp
335
+ Makefile: $(srcdir)/binding.gyp $(srcdir)/../../../../../.nvm/versions/node/v22.19.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi $(srcdir)/../../../../../Library/Caches/node-gyp/22.19.0/include/node/common.gypi $(srcdir)/build/config.gypi
336
336
  $(call do_cmd,regen_makefile)
337
337
 
338
338
  # "all" is a concatenation of the "all" targets from all the included