net-ssh 7.3.6 → 8.1.2

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 (80) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGES.txt +106 -5
  4. data/README.md +71 -57
  5. data/lib/net/ssh/authentication/agent.rb +35 -11
  6. data/lib/net/ssh/authentication/certificate.rb +2 -2
  7. data/lib/net/ssh/authentication/ed25519.rb +137 -44
  8. data/lib/net/ssh/authentication/ed25519_loader.rb +14 -9
  9. data/lib/net/ssh/authentication/key_manager.rb +22 -11
  10. data/lib/net/ssh/authentication/methods/abstract.rb +6 -8
  11. data/lib/net/ssh/authentication/methods/hostbased.rb +3 -3
  12. data/lib/net/ssh/authentication/methods/publickey.rb +52 -38
  13. data/lib/net/ssh/authentication/security_key.rb +71 -0
  14. data/lib/net/ssh/authentication/session.rb +30 -17
  15. data/lib/net/ssh/buffer.rb +8 -3
  16. data/lib/net/ssh/buffered_io.rb +40 -3
  17. data/lib/net/ssh/config.rb +123 -50
  18. data/lib/net/ssh/connection/channel.rb +49 -2
  19. data/lib/net/ssh/connection/session.rb +151 -13
  20. data/lib/net/ssh/errors.rb +34 -6
  21. data/lib/net/ssh/key_factory.rb +7 -1
  22. data/lib/net/ssh/known_hosts.rb +103 -38
  23. data/lib/net/ssh/multiplex/client.rb +162 -0
  24. data/lib/net/ssh/multiplex/control_path.rb +120 -0
  25. data/lib/net/ssh/multiplex/proxy_transport.rb +218 -0
  26. data/lib/net/ssh/multiplex.rb +127 -0
  27. data/lib/net/ssh/prompt.rb +43 -5
  28. data/lib/net/ssh/proxy/https.rb +28 -4
  29. data/lib/net/ssh/proxy/jump.rb +61 -8
  30. data/lib/net/ssh/service/forward.rb +35 -24
  31. data/lib/net/ssh/test.rb +2 -1
  32. data/lib/net/ssh/transport/algorithms.rb +92 -34
  33. data/lib/net/ssh/transport/chacha20_poly1305_cipher.rb +89 -16
  34. data/lib/net/ssh/transport/chacha20_poly1305_cipher_loader.rb +20 -1
  35. data/lib/net/ssh/transport/cipher_factory.rb +23 -1
  36. data/lib/net/ssh/transport/constants.rb +1 -0
  37. data/lib/net/ssh/transport/gcm_cipher.rb +27 -6
  38. data/lib/net/ssh/transport/hmac/sha1_etm.rb +12 -0
  39. data/lib/net/ssh/transport/hmac.rb +2 -0
  40. data/lib/net/ssh/transport/kex/curve25519_sha256.rb +41 -9
  41. data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +14 -6
  42. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +8 -0
  43. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +45 -15
  44. data/lib/net/ssh/transport/openssl.rb +2 -1
  45. data/lib/net/ssh/transport/packet_stream.rb +12 -0
  46. data/lib/net/ssh/transport/session.rb +61 -45
  47. data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +3 -1
  48. data/lib/net/ssh/verifiers/always.rb +16 -4
  49. data/lib/net/ssh/verifiers/never.rb +2 -1
  50. data/lib/net/ssh/version.rb +3 -3
  51. data/lib/net/ssh.rb +164 -63
  52. data.tar.gz.sig +0 -0
  53. metadata +33 -61
  54. metadata.gz.sig +0 -0
  55. data/.dockerignore +0 -9
  56. data/.github/FUNDING.yml +0 -1
  57. data/.github/config/rubocop_linter_action.yml +0 -4
  58. data/.github/workflows/ci-with-docker.yml +0 -58
  59. data/.github/workflows/ci.yml +0 -99
  60. data/.github/workflows/rubocop.yml +0 -16
  61. data/.gitignore +0 -15
  62. data/.rubocop.yml +0 -22
  63. data/.rubocop_todo.yml +0 -1083
  64. data/DEVELOPMENT.md +0 -23
  65. data/Dockerfile +0 -32
  66. data/Dockerfile.openssl3 +0 -17
  67. data/Dockerfile.zlib_ng +0 -29
  68. data/Gemfile +0 -13
  69. data/Gemfile.noed25519 +0 -12
  70. data/Gemfile.norbnacl +0 -12
  71. data/ISSUE_TEMPLATE.md +0 -30
  72. data/Manifest +0 -132
  73. data/Rakefile +0 -196
  74. data/SECURITY.md +0 -4
  75. data/THANKS.txt +0 -110
  76. data/appveyor.yml +0 -58
  77. data/docker-compose.yml +0 -23
  78. data/net-ssh-public_cert.pem +0 -21
  79. data/net-ssh.gemspec +0 -48
  80. data/support/ssh_tunnel_bug.rb +0 -65
@@ -1,5 +1,6 @@
1
1
  require 'socket'
2
2
  require 'openssl'
3
+ require 'resolv'
3
4
  require 'net/ssh/proxy/errors'
4
5
  require 'net/ssh/proxy/http'
5
6
 
@@ -15,10 +16,14 @@ module Net
15
16
  # can be used to tweak this proxy connection. In addition to the options
16
17
  # taken by Net::SSH::Proxy::HTTP it supports:
17
18
  #
18
- # * :ssl_context => the SSL configuration to use for the connection
19
+ # * :ssl_context => the SSL configuration to use for the connection. The
20
+ # default verifies the proxy's certificate and hostname against the
21
+ # system trust store. For a self-signed or private CA certificate pass a
22
+ # context with it in +cert_store+, or, to skip verification and expose
23
+ # the proxy credentials to a man-in-the-middle, a context with
24
+ # +verify_mode+ set to OpenSSL::SSL::VERIFY_NONE.
19
25
  def initialize(proxy_host, proxy_port = 80, options = {})
20
- @ssl_context = options.delete(:ssl_context) ||
21
- OpenSSL::SSL::SSLContext.new
26
+ @ssl_context = options.delete(:ssl_context) || default_ssl_context
22
27
  super(proxy_host, proxy_port, options)
23
28
  end
24
29
 
@@ -41,9 +46,28 @@ module Net
41
46
  plain_socket = super(connect_timeout)
42
47
  OpenSSL::SSL::SSLSocket.new(plain_socket, @ssl_context).tap do |socket|
43
48
  socket.extend(SSLSocketCompatibility)
44
- socket.connect
49
+ # SNI must not carry an IP address (RFC 6066).
50
+ socket.hostname = proxy_host unless Resolv::AddressRegex.match?(proxy_host)
51
+ begin
52
+ socket.connect
53
+ socket.post_connection_check(proxy_host) if verify_peer?
54
+ rescue StandardError
55
+ socket.close
56
+ raise
57
+ end
45
58
  end
46
59
  end
60
+
61
+ private
62
+
63
+ def default_ssl_context
64
+ OpenSSL::SSL::SSLContext.new.tap(&:set_params)
65
+ end
66
+
67
+ # verify_mode is nil on a bare SSLContext in older openssl gems.
68
+ def verify_peer?
69
+ @ssl_context.verify_mode.to_i != OpenSSL::SSL::VERIFY_NONE
70
+ end
47
71
  end
48
72
  end
49
73
  end
@@ -15,14 +15,33 @@ module Net
15
15
  # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
16
16
  # ...
17
17
  # end
18
+ #
19
+ # ssh_config options for the jump host can be given as a Hash via
20
+ # +options:+; each is passed to the ssh command as +-o Key=value+:
21
+ #
22
+ # proxy = Net::SSH::Proxy::Jump.new('user@proxy', options: { 'StrictHostKeyChecking' => 'no' })
23
+ #
24
+ # Options that ssh_config allows more than once (+IdentityFile+,
25
+ # +CertificateFile+ and +SendEnv+) take an Array and are passed once per
26
+ # element; an empty Array passes nothing:
27
+ #
28
+ # proxy = Net::SSH::Proxy::Jump.new('user@proxy', options: { 'IdentityFile' => ['~/.ssh/jump', '~/.ssh/jump_old'] })
29
+ #
30
+ # A +nil+ value, alone or in an Array, raises ArgumentError, as it does
31
+ # for the options passed to Net::SSH.start; omit the option instead.
32
+ #
33
+ # The options apply to the first jump host only; any further hosts in
34
+ # the chain are connected to by that host's own ssh.
18
35
  class Jump < Command
19
36
  # The jump proxies
20
37
  attr_reader :jump_proxies
21
38
 
22
39
  # Create a new socket factory that tunnels via multiple jump proxes as
23
40
  # [user@]host[:port].
24
- def initialize(jump_proxies)
41
+ def initialize(jump_proxies, options: {})
25
42
  @jump_proxies = jump_proxies
43
+ @options = options
44
+ validate_options!
26
45
  end
27
46
 
28
47
  # Return a new socket connected to the given host and port via the jump
@@ -35,28 +54,62 @@ module Net
35
54
  # We cannot build the ProxyCommand template until we know if the :config
36
55
  # option was specified during `Net::SSH.start`.
37
56
  def build_proxy_command_equivalent(connection_options = nil)
38
- extra_jumps = jump_proxies.split(",", 2)[1]
57
+ raise ArgumentError, "no jump host given" if jump_proxies.empty?
58
+
39
59
  config = connection_options && connection_options[:config]
40
- uri, = jump_proxies.split(",").map { |jump| validate_jump!(jump) }
60
+ uri, *extra_jumps = jump_proxies.split(",", -1).map { |jump| validate_jump!(jump) }
41
61
 
42
62
  template = "ssh".dup
43
- template << " -l #{uri.user}" if uri.user
44
- template << " -p #{uri.port}" if uri.port
45
- template << " -J #{extra_jumps}" if extra_jumps
46
- template << " -F #{Shellwords.escape(config)}" if config != true && config
63
+ template << " -l #{escape(uri.user)}" if uri.user
64
+ template << " -p #{uri.port}" if uri.port
65
+ template << " -J #{escape(extra_jumps.map { |jump| jump_spec(jump) }.join(','))}" unless extra_jumps.empty?
66
+ template << " -F #{escape(config)}" if config != true && config
67
+ @options.each do |key, values|
68
+ Array(values).each { |value| template << " -o #{escape(key)}=#{escape(value)}" }
69
+ end
47
70
  template << " -W %h:%p "
48
- template << uri.host
71
+ template << escape(uri.host)
49
72
 
50
73
  @command_line_template = template
51
74
  end
52
75
 
53
76
  private
54
77
 
78
+ # Like Net::SSH.start, reject nil option values (alone or in an Array)
79
+ # rather than silently passing nothing or an empty value to ssh.
80
+ def validate_options!
81
+ nil_keys = @options.select { |_key, value| value.nil? || (value.is_a?(Array) && value.include?(nil)) }.keys
82
+ return if nil_keys.empty?
83
+
84
+ raise ArgumentError, "nil is not allowed for #{nil_keys.map(&:inspect).join(', ')} in Net::SSH::Proxy::Jump options; " \
85
+ "omit the option to use the default or ssh_config value"
86
+ end
87
+
88
+ # Escape for the shell, and double any % so it survives the %h/%p
89
+ # substitution done on the template in Command#open.
90
+ def escape(value)
91
+ Shellwords.escape(value).gsub("%", "%%")
92
+ end
93
+
94
+ # Only [user@]host[:port] is checked, so anything else URI accepts (a path,
95
+ # query, fragment or password) must be refused before it reaches the shell.
55
96
  def validate_jump!(jump)
56
97
  uri = URI.parse("ssh://#{jump}")
98
+ raise URI::InvalidURIError if uri.host.to_s.empty? || jump_spec(uri) != jump
99
+
57
100
  validate_user_name!(uri.user) if uri.user
58
101
  validate_host_name!(uri.host)
102
+ validate_port!(uri.port) if uri.port
59
103
  uri
104
+ rescue URI::InvalidURIError
105
+ raise ArgumentError, "jump host is not [user@]host[:port]: #{jump.inspect}"
106
+ end
107
+
108
+ def jump_spec(uri)
109
+ spec = uri.host.dup
110
+ spec.prepend("#{uri.user}@") if uri.user
111
+ spec << ":#{uri.port}" if uri.port
112
+ spec
60
113
  end
61
114
  end
62
115
  end
@@ -320,6 +320,11 @@ module Net
320
320
  session.listen_to(client)
321
321
  channel[:socket] = client
322
322
 
323
+ # Grant window only for data the local end has accepted, so a slow
324
+ # reader stalls the remote sender instead of filling our memory.
325
+ channel.grant_window_on_consume!
326
+ client.on_sent { |bytes| channel.consumed(bytes) }
327
+
323
328
  channel.on_data do |ch, data|
324
329
  debug { "data:#{data.length} on #{type} forwarded channel" }
325
330
  ch[:socket].enqueue(data)
@@ -327,36 +332,40 @@ module Net
327
332
 
328
333
  channel.on_eof do |ch|
329
334
  debug { "eof #{type} on #{type} forwarded channel" }
330
- begin
331
- ch[:socket].send_pending
332
- ch[:socket].shutdown Socket::SHUT_WR
333
- rescue IOError => e
334
- if e.message =~ /closed/ then
335
- debug { "epipe in on_eof => shallowing exception:#{e}" }
336
- else
337
- raise
338
- end
339
- rescue Errno::EPIPE => e
340
- debug { "epipe in on_eof => shallowing exception:#{e}" }
341
- rescue Errno::ENOTCONN => e
342
- debug { "enotconn in on_eof => shallowing exception:#{e}" }
343
- end
335
+ ch[:socket].shutdown_write_when_drained
344
336
  end
345
337
 
346
338
  channel.on_close do |ch|
347
339
  debug { "closing #{type} forwarded channel" }
348
- ch[:socket].close if !client.closed?
349
- session.stop_listening_to(ch[:socket])
340
+ session.close_when_drained(ch[:socket])
350
341
  end
351
342
 
352
343
  channel.on_process do |ch|
353
- if ch[:socket].closed?
344
+ socket = ch[:socket]
345
+ if socket.closed?
354
346
  ch.info { "#{type} forwarded connection closed" }
347
+ session.stop_listening_to(socket)
355
348
  ch.close
356
- elsif ch[:socket].available > 0
357
- data = ch[:socket].read_available(8192)
358
- ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
359
- ch.send_data(data)
349
+ elsif ch.eof?
350
+ # Like OpenSSH's chan_shutdown_read: data already read still goes out before CHANNEL_EOF, the rest is dropped.
351
+ ch.output.append(socket.read_available) if socket.available > 0
352
+ session.pause_reading(socket)
353
+ else
354
+ if socket.available > 0
355
+ data = socket.read_available(8192)
356
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
357
+ ch.send_data(data)
358
+ end
359
+
360
+ # Like OpenSSH's channel_pre_open, only read more once the remote
361
+ # window can take what is already queued; writes to the socket keep
362
+ # flowing. ch.output is still unsent here because Channel#process
363
+ # runs this block before enqueue_pending_output.
364
+ if ch.output.length + socket.available < ch.remote_window_size.to_i
365
+ session.resume_reading(socket)
366
+ else
367
+ session.pause_reading(socket)
368
+ end
360
369
  end
361
370
  end
362
371
  end
@@ -371,6 +380,8 @@ module Net
371
380
  end
372
381
 
373
382
  channel.on_process do |ch|
383
+ next if ch.eof?
384
+
374
385
  data = ch[:socket].read(8192)
375
386
  if data
376
387
  ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
@@ -388,13 +399,13 @@ module Net
388
399
  originator_address = packet.read_string
389
400
  originator_port = packet.read_long
390
401
 
391
- puts "REMOTE 0: #{connected_port} #{connected_address} #{originator_address} #{originator_port}"
402
+ debug { "REMOTE 0: #{connected_port} #{connected_address} #{originator_address} #{originator_port}" }
392
403
  remote = @remote_forwarded_ports[[connected_port, connected_address]]
393
404
  if remote.nil?
394
405
  raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
395
406
  end
396
407
 
397
- puts "REMOTE: #{remote.host} #{remote.port}"
408
+ debug { "REMOTE: #{remote.host} #{remote.port}" }
398
409
  client = TCPSocket.new(remote.host, remote.port)
399
410
  info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
400
411
 
@@ -415,7 +426,7 @@ module Net
415
426
  channel[:invisible] = true
416
427
 
417
428
  begin
418
- agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory])
429
+ agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory], session.options[:identity_agent])
419
430
  if (agent.socket.is_a? ::IO)
420
431
  prepare_client(agent.socket, channel, :agent)
421
432
  else
data/lib/net/ssh/test.rb CHANGED
@@ -73,7 +73,8 @@ module Net
73
73
  def transport(options = {})
74
74
  @transport ||= Net::SSH::Transport::Session.new(
75
75
  options[:host] || "localhost",
76
- options.merge(kex: "test", host_key: "ssh-rsa", append_all_supported_algorithms: true, verify_host_key: :never, proxy: socket(options))
76
+ options.merge(kex: "test", host_key: "ssh-rsa", encryption: "+none", hmac: "+none",
77
+ append_all_supported_algorithms: true, verify_host_key: :never, proxy: socket(options))
77
78
  )
78
79
  end
79
80
 
@@ -8,6 +8,7 @@ require 'net/ssh/transport/kex'
8
8
  require 'net/ssh/transport/kex/curve25519_sha256_loader'
9
9
  require 'net/ssh/transport/server_version'
10
10
  require 'net/ssh/authentication/ed25519_loader'
11
+ require 'net/ssh/authentication/security_key'
11
12
 
12
13
  module Net
13
14
  module SSH
@@ -19,7 +20,7 @@ module Net
19
20
  #
20
21
  # You will never instantiate or reference this directly. It is used
21
22
  # internally by the transport layer.
22
- class Algorithms
23
+ class Algorithms # rubocop:disable Metrics/ClassLength
23
24
  include Loggable
24
25
  include Constants
25
26
 
@@ -31,18 +32,14 @@ module Net
31
32
  ecdsa-sha2-nistp521
32
33
  ecdsa-sha2-nistp384
33
34
  ecdsa-sha2-nistp256
34
- ssh-rsa-cert-v01@openssh.com
35
- ssh-rsa-cert-v00@openssh.com
36
- ssh-rsa
37
- rsa-sha2-256
38
- rsa-sha2-512],
35
+ rsa-sha2-512
36
+ rsa-sha2-256],
39
37
 
40
38
  kex: %w[ecdh-sha2-nistp521
41
39
  ecdh-sha2-nistp384
42
40
  ecdh-sha2-nistp256
43
41
  diffie-hellman-group-exchange-sha256
44
- diffie-hellman-group14-sha256
45
- diffie-hellman-group14-sha1],
42
+ diffie-hellman-group14-sha256],
46
43
 
47
44
  encryption: %w[aes256-ctr
48
45
  aes192-ctr
@@ -51,8 +48,18 @@ module Net
51
48
  aes128-gcm@openssh.com],
52
49
 
53
50
  hmac: %w[hmac-sha2-512-etm@openssh.com hmac-sha2-256-etm@openssh.com
51
+ hmac-sha1-etm@openssh.com
54
52
  hmac-sha2-512 hmac-sha2-256
55
- hmac-sha1]
53
+ hmac-sha1],
54
+
55
+ pubkey_algorithms: %w[ssh-ed25519-cert-v01@openssh.com
56
+ ecdsa-sha2-nistp256-cert-v01@openssh.com ecdsa-sha2-nistp384-cert-v01@openssh.com
57
+ ecdsa-sha2-nistp521-cert-v01@openssh.com
58
+ rsa-sha2-512-cert-v01@openssh.com rsa-sha2-256-cert-v01@openssh.com
59
+ ssh-ed25519
60
+ ecdsa-sha2-nistp256 ecdsa-sha2-nistp384 ecdsa-sha2-nistp521] +
61
+ [Authentication::SecurityKey::SK_SSH_ED25519, Authentication::SecurityKey::SK_ECDSA_SHA2_NISTP256] +
62
+ %w[rsa-sha2-512 rsa-sha2-256]
56
63
  }.freeze
57
64
 
58
65
  if Net::SSH::Transport::ChaCha20Poly1305CipherLoader::LOADED
@@ -76,10 +83,12 @@ module Net
76
83
 
77
84
  # Define all algorithms, with the deprecated, supported by Net::SSH.
78
85
  ALGORITHMS = {
79
- host_key: DEFAULT_ALGORITHMS[:host_key] + %w[ssh-dss],
86
+ host_key: DEFAULT_ALGORITHMS[:host_key] +
87
+ %w[ssh-rsa-cert-v01@openssh.com ssh-rsa-cert-v00@openssh.com ssh-rsa ssh-dss],
80
88
 
81
89
  kex: DEFAULT_ALGORITHMS[:kex] +
82
- %w[diffie-hellman-group-exchange-sha1
90
+ %w[diffie-hellman-group14-sha1
91
+ diffie-hellman-group-exchange-sha1
83
92
  diffie-hellman-group1-sha1],
84
93
 
85
94
  encryption: DEFAULT_ALGORITHMS[:encryption] +
@@ -98,10 +107,20 @@ module Net
98
107
  hmac-md5 hmac-md5-96
99
108
  none],
100
109
 
110
+ pubkey_algorithms: DEFAULT_ALGORITHMS[:pubkey_algorithms] + %w[ssh-rsa-cert-v01@openssh.com ssh-rsa ssh-dss],
111
+
101
112
  compression: %w[none zlib@openssh.com zlib],
102
113
  language: %w[]
103
114
  }.freeze
104
115
 
116
+ # Not added by append_all_supported_algorithms; only used when named explicitly.
117
+ EXPLICIT_ONLY_ALGORITHMS = {
118
+ encryption: %w[none],
119
+ hmac: %w[none]
120
+ }.freeze
121
+
122
+ DEPRECATED_HOST_KEY = 'ssh-rsa-cert-v00@openssh.com'.freeze
123
+
105
124
  # The underlying transport layer session that supports this object
106
125
  attr_reader :session
107
126
 
@@ -269,11 +288,16 @@ module Net
269
288
 
270
289
  ALGORITHMS.each do |algorithm, supported|
271
290
  algorithms[algorithm] = compose_algorithm_list(
272
- supported, options[algorithm] || DEFAULT_ALGORITHMS[algorithm],
273
- options[:append_all_supported_algorithms]
291
+ supported,
292
+ DEFAULT_ALGORITHMS[algorithm],
293
+ options[algorithm] || DEFAULT_ALGORITHMS[algorithm],
294
+ options[:append_all_supported_algorithms],
295
+ supported - EXPLICIT_ONLY_ALGORITHMS.fetch(algorithm, [])
274
296
  )
275
297
  end
276
298
 
299
+ warn_deprecated_host_key if host_key_explicitly_named?(DEPRECATED_HOST_KEY)
300
+
277
301
  # for convention, make sure our list has the same keys as the server
278
302
  # list
279
303
 
@@ -287,7 +311,11 @@ module Net
287
311
  # existing known key for the host has preference.
288
312
 
289
313
  existing_keys = session.host_keys
290
- host_keys = existing_keys.flat_map { |key| key.respond_to?(:ssh_types) ? key.ssh_types : [key.ssh_type] }.uniq
314
+ host_keys = existing_keys.flat_map { |key| key.respond_to?(:ssh_types) ? key.ssh_types : [key.ssh_type] }
315
+ # A known ssh-rsa key also verifies with rsa-sha2-*, and must not re-enable disabled SHA-1 algorithms.
316
+ disabled_sha1 = %w[ssh-rsa ssh-rsa-cert-v01@openssh.com ssh-rsa-cert-v00@openssh.com] - algorithms[:host_key]
317
+ host_keys = host_keys.flat_map { |type| type == 'ssh-rsa' ? %w[rsa-sha2-512 rsa-sha2-256 ssh-rsa] : [type] }
318
+ host_keys = (host_keys - disabled_sha1).uniq
291
319
  algorithms[:host_key].each do |name|
292
320
  host_keys << name unless host_keys.include?(name)
293
321
  end
@@ -296,21 +324,27 @@ module Net
296
324
  end
297
325
 
298
326
  # Composes the list of algorithms by taking supported algorithms and matching with supplied options.
299
- def compose_algorithm_list(supported, option, append_all_supported_algorithms = false)
327
+ def compose_algorithm_list(supported, defaults, option, append_all_supported_algorithms = false, appendable = supported)
300
328
  return supported.dup unless option
301
329
 
302
330
  list = []
303
331
  option = Array(option).compact.uniq
304
332
 
305
- if option.first && option.first.start_with?('+', '-')
306
- list = supported.dup
307
-
308
- appends = option.select { |opt| opt.start_with?('+') }.map { |opt| opt[1..-1] }
309
- deletions = option.select { |opt| opt.start_with?('-') }.map { |opt| opt[1..-1] }
333
+ if option.first && option.first.start_with?('+', '-', '^')
334
+ ops = { '^' => [], '+' => [], '-' => [] }
335
+ cur_list = nil
336
+ option.each do |cur_opt|
337
+ if ops.key?(cur_opt[0])
338
+ cur_list = ops[cur_opt[0]]
339
+ cur_opt = cur_opt[1..-1]
340
+ end
341
+ cur_list << cur_opt
342
+ end
310
343
 
311
- list.concat(appends)
344
+ list = ops['^'] + defaults + ops['+']
345
+ list.concat(appendable) if append_all_supported_algorithms
312
346
 
313
- deletions.each do |opt|
347
+ ops['-'].each do |opt|
314
348
  if opt.include?('*')
315
349
  opt_escaped = Regexp.escape(opt)
316
350
  algo_re = /\A#{opt_escaped.gsub('\*', '[A-Za-z\d\-@\.]*')}\z/
@@ -325,7 +359,7 @@ module Net
325
359
  list = option
326
360
 
327
361
  if append_all_supported_algorithms
328
- supported.each { |name| list << name unless list.include?(name) }
362
+ appendable.each { |name| list << name unless list.include?(name) }
329
363
  end
330
364
  end
331
365
 
@@ -341,22 +375,35 @@ module Net
341
375
  list
342
376
  end
343
377
 
378
+ def host_key_explicitly_named?(name)
379
+ algorithms[:host_key].include?(name) &&
380
+ Array(options[:host_key]).any? { |opt| opt.to_s.delete_prefix('+').delete_prefix('^') == name }
381
+ end
382
+
383
+ def warn_deprecated_host_key
384
+ return if @deprecated_host_key_warned
385
+
386
+ @deprecated_host_key_warned = true
387
+ Kernel.warn("#{DEPRECATED_HOST_KEY} is deprecated and will be removed in a future version; " \
388
+ "net-ssh cannot parse v00 certificates, use ssh-rsa-cert-v01@openssh.com or a modern host key type instead")
389
+ end
390
+
344
391
  # Parses a KEXINIT packet from the server.
345
392
  def parse_server_algorithm_packet(packet)
346
393
  data = { raw: packet.content }
347
394
 
348
395
  packet.read(16) # skip the cookie value
349
396
 
350
- data[:kex] = packet.read_string.split(/,/)
351
- data[:host_key] = packet.read_string.split(/,/)
352
- data[:encryption_client] = packet.read_string.split(/,/)
353
- data[:encryption_server] = packet.read_string.split(/,/)
354
- data[:hmac_client] = packet.read_string.split(/,/)
355
- data[:hmac_server] = packet.read_string.split(/,/)
356
- data[:compression_client] = packet.read_string.split(/,/)
357
- data[:compression_server] = packet.read_string.split(/,/)
358
- data[:language_client] = packet.read_string.split(/,/)
359
- data[:language_server] = packet.read_string.split(/,/)
397
+ data[:kex] = read_name_list(packet)
398
+ data[:host_key] = read_name_list(packet)
399
+ data[:encryption_client] = read_name_list(packet)
400
+ data[:encryption_server] = read_name_list(packet)
401
+ data[:hmac_client] = read_name_list(packet)
402
+ data[:hmac_server] = read_name_list(packet)
403
+ data[:compression_client] = read_name_list(packet)
404
+ data[:compression_server] = read_name_list(packet)
405
+ data[:language_client] = read_name_list(packet)
406
+ data[:language_server] = read_name_list(packet)
360
407
 
361
408
  # TODO: if first_kex_packet_follows, we need to try to skip the
362
409
  # actual kexinit stuff and try to guess what the server is doing...
@@ -366,11 +413,21 @@ module Net
366
413
  return data
367
414
  end
368
415
 
416
+ # Reads one comma-separated algorithm name-list from a KEXINIT packet.
417
+ def read_name_list(packet)
418
+ list = packet.read_string or raise Net::SSH::Exception, "malformed KEXINIT packet from server"
419
+
420
+ list.split(/,/)
421
+ end
422
+
369
423
  # Given the #algorithms map of preferred algorithm types, this constructs
370
424
  # a KEXINIT packet to send to the server. It does not actually send it,
371
425
  # it simply builds the packet and returns it.
372
426
  def build_client_algorithm_packet
373
- kex = algorithms[:kex].join(",")
427
+ kex_names = algorithms[:kex]
428
+ # RFC 8308: ext-info-c is only meaningful in the first key exchange.
429
+ kex_names += %w[ext-info-c] unless session_id
430
+ kex = kex_names.join(",")
374
431
  host_key = algorithms[:host_key].join(",")
375
432
  encryption = algorithms[:encryption].join(",")
376
433
  hmac = algorithms[:hmac].join(",")
@@ -391,6 +448,7 @@ module Net
391
448
  def negotiate_algorithms
392
449
  @kex = negotiate(:kex)
393
450
  @host_key = negotiate(:host_key)
451
+ warn_deprecated_host_key if @host_key == DEPRECATED_HOST_KEY
394
452
  @encryption_client = negotiate(:encryption_client)
395
453
  @encryption_server = negotiate(:encryption_server)
396
454
  @hmac_client = negotiate(:hmac_client)