net-ssh 7.3.4 → 8.0.0.rc1
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/workflows/ci-with-docker.yml +0 -3
- data/.github/workflows/ci.yml +2 -17
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +1 -0
- data/CHANGES.txt +20 -2
- data/Dockerfile +4 -1
- data/Dockerfile.openssl3 +1 -1
- data/Dockerfile.zlib_ng +1 -1
- data/README.md +10 -13
- data/Rakefile +18 -12
- data/appveyor.yml +4 -16
- data/docker-compose.yml +1 -7
- data/lib/net/ssh/authentication/agent.rb +4 -4
- data/lib/net/ssh/authentication/ed25519.rb +134 -36
- data/lib/net/ssh/authentication/ed25519_loader.rb +14 -9
- data/lib/net/ssh/authentication/key_manager.rb +3 -2
- data/lib/net/ssh/prompt.rb +3 -3
- data/lib/net/ssh/service/forward.rb +3 -3
- data/lib/net/ssh/transport/algorithms.rb +17 -10
- data/lib/net/ssh/transport/chacha20_poly1305_cipher.rb +89 -16
- data/lib/net/ssh/transport/chacha20_poly1305_cipher_loader.rb +20 -1
- data/lib/net/ssh/transport/cipher_factory.rb +23 -1
- data/lib/net/ssh/transport/constants.rb +1 -0
- data/lib/net/ssh/transport/gcm_cipher.rb +27 -6
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +41 -9
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +14 -6
- data/lib/net/ssh/transport/session.rb +22 -2
- data/lib/net/ssh/version.rb +4 -4
- data/lib/net/ssh.rb +7 -1
- data/net-ssh.gemspec +4 -7
- data.tar.gz.sig +0 -0
- metadata +17 -33
- metadata.gz.sig +0 -0
- data/Gemfile.noed25519 +0 -12
- data/Gemfile.norbnacl +0 -12
|
@@ -127,9 +127,10 @@ module Net
|
|
|
127
127
|
if agent
|
|
128
128
|
agent.identities.each do |key|
|
|
129
129
|
corresponding_user_identity = user_identities.detect { |identity|
|
|
130
|
-
identity[:public_key] && identity[:public_key].
|
|
130
|
+
identity[:public_key] && identity[:public_key].to_blob == key.to_blob
|
|
131
131
|
}
|
|
132
|
-
|
|
132
|
+
# By identity: EC::Point#== raises TypeError when compared with other key types.
|
|
133
|
+
user_identities.reject! { |identity| identity.equal?(corresponding_user_identity) }
|
|
133
134
|
|
|
134
135
|
if !options[:keys_only] || corresponding_user_identity
|
|
135
136
|
known_identities[key] = { from: :agent, identity: key }
|
data/lib/net/ssh/prompt.rb
CHANGED
|
@@ -41,9 +41,9 @@ module Net
|
|
|
41
41
|
def ask(prompt, echo = true)
|
|
42
42
|
$stdout.print(prompt)
|
|
43
43
|
$stdout.flush
|
|
44
|
-
ret = $stdin.noecho(&:gets)
|
|
45
|
-
$stdout.print("\n")
|
|
46
|
-
ret
|
|
44
|
+
ret = echo ? $stdin.gets : $stdin.noecho(&:gets)
|
|
45
|
+
$stdout.print("\n") unless echo
|
|
46
|
+
ret.chomp
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
# success method will be called when the password was accepted
|
|
@@ -388,13 +388,13 @@ module Net
|
|
|
388
388
|
originator_address = packet.read_string
|
|
389
389
|
originator_port = packet.read_long
|
|
390
390
|
|
|
391
|
-
|
|
391
|
+
debug { "REMOTE 0: #{connected_port} #{connected_address} #{originator_address} #{originator_port}" }
|
|
392
392
|
remote = @remote_forwarded_ports[[connected_port, connected_address]]
|
|
393
393
|
if remote.nil?
|
|
394
394
|
raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
|
|
395
395
|
end
|
|
396
396
|
|
|
397
|
-
|
|
397
|
+
debug { "REMOTE: #{remote.host} #{remote.port}" }
|
|
398
398
|
client = TCPSocket.new(remote.host, remote.port)
|
|
399
399
|
info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
|
|
400
400
|
|
|
@@ -409,7 +409,7 @@ module Net
|
|
|
409
409
|
channel[:invisible] = true
|
|
410
410
|
|
|
411
411
|
begin
|
|
412
|
-
agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory])
|
|
412
|
+
agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory], session.options[:identity_agent])
|
|
413
413
|
if (agent.socket.is_a? ::IO)
|
|
414
414
|
prepare_client(agent.socket, channel, :agent)
|
|
415
415
|
else
|
|
@@ -347,16 +347,16 @@ module Net
|
|
|
347
347
|
|
|
348
348
|
packet.read(16) # skip the cookie value
|
|
349
349
|
|
|
350
|
-
data[:kex] = packet
|
|
351
|
-
data[:host_key] = packet
|
|
352
|
-
data[:encryption_client] = packet
|
|
353
|
-
data[:encryption_server] = packet
|
|
354
|
-
data[:hmac_client] = packet
|
|
355
|
-
data[:hmac_server] = packet
|
|
356
|
-
data[:compression_client] = packet
|
|
357
|
-
data[:compression_server] = packet
|
|
358
|
-
data[:language_client] = packet
|
|
359
|
-
data[:language_server] = packet
|
|
350
|
+
data[:kex] = read_name_list(packet)
|
|
351
|
+
data[:host_key] = read_name_list(packet)
|
|
352
|
+
data[:encryption_client] = read_name_list(packet)
|
|
353
|
+
data[:encryption_server] = read_name_list(packet)
|
|
354
|
+
data[:hmac_client] = read_name_list(packet)
|
|
355
|
+
data[:hmac_server] = read_name_list(packet)
|
|
356
|
+
data[:compression_client] = read_name_list(packet)
|
|
357
|
+
data[:compression_server] = read_name_list(packet)
|
|
358
|
+
data[:language_client] = read_name_list(packet)
|
|
359
|
+
data[:language_server] = read_name_list(packet)
|
|
360
360
|
|
|
361
361
|
# TODO: if first_kex_packet_follows, we need to try to skip the
|
|
362
362
|
# actual kexinit stuff and try to guess what the server is doing...
|
|
@@ -366,6 +366,13 @@ module Net
|
|
|
366
366
|
return data
|
|
367
367
|
end
|
|
368
368
|
|
|
369
|
+
# Reads one comma-separated algorithm name-list from a KEXINIT packet.
|
|
370
|
+
def read_name_list(packet)
|
|
371
|
+
list = packet.read_string or raise Net::SSH::Exception, "malformed KEXINIT packet from server"
|
|
372
|
+
|
|
373
|
+
list.split(/,/)
|
|
374
|
+
end
|
|
375
|
+
|
|
369
376
|
# Given the #algorithms map of preferred algorithm types, this constructs
|
|
370
377
|
# a KEXINIT packet to send to the server. It does not actually send it,
|
|
371
378
|
# it simply builds the packet and returns it.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'openssl'
|
|
2
2
|
require 'net/ssh/loggable'
|
|
3
3
|
|
|
4
4
|
module Net
|
|
@@ -8,6 +8,15 @@ module Net
|
|
|
8
8
|
class ChaCha20Poly1305Cipher
|
|
9
9
|
include Net::SSH::Loggable
|
|
10
10
|
|
|
11
|
+
POLY1305_ALGORITHM = "POLY1305"
|
|
12
|
+
POLY1305_KEY_BYTES = 32
|
|
13
|
+
POLY1305_TAG_BYTES = 16
|
|
14
|
+
NAME = "chacha20-poly1305@openssh.com"
|
|
15
|
+
ZERO_BLOCK = "\x00".b * POLY1305_KEY_BYTES
|
|
16
|
+
ZERO_IV = "\x00".b * 16
|
|
17
|
+
|
|
18
|
+
class UnsupportedError < StandardError; end
|
|
19
|
+
|
|
11
20
|
# Implicit HMAC, no need to do anything
|
|
12
21
|
class ImplicitHMac
|
|
13
22
|
def etm
|
|
@@ -24,7 +33,6 @@ module Net
|
|
|
24
33
|
@chacha_hdr = OpenSSL::Cipher.new("chacha20")
|
|
25
34
|
key_len = @chacha_hdr.key_len
|
|
26
35
|
@chacha_main = OpenSSL::Cipher.new("chacha20")
|
|
27
|
-
@poly = RbNaCl::OneTimeAuths::Poly1305
|
|
28
36
|
if key.size < key_len * 2
|
|
29
37
|
error { "chacha20_poly1305: keylength doesn't match" }
|
|
30
38
|
raise "chacha20_poly1305: keylength doesn't match"
|
|
@@ -43,9 +51,9 @@ module Net
|
|
|
43
51
|
end
|
|
44
52
|
|
|
45
53
|
def update_cipher_mac(payload, sequence_number)
|
|
46
|
-
iv_data =
|
|
54
|
+
iv_data = packet_iv(sequence_number)
|
|
47
55
|
@chacha_main.iv = iv_data
|
|
48
|
-
poly_key = @chacha_main.update(
|
|
56
|
+
poly_key = @chacha_main.update(ZERO_BLOCK)
|
|
49
57
|
|
|
50
58
|
packet_length = payload.size
|
|
51
59
|
length_data = [packet_length].pack("N")
|
|
@@ -57,35 +65,36 @@ module Net
|
|
|
57
65
|
unencrypted_data = payload
|
|
58
66
|
packet += @chacha_main.update(unencrypted_data)
|
|
59
67
|
|
|
60
|
-
packet +=
|
|
68
|
+
packet += self.class.poly1305_auth(poly_key, packet)
|
|
61
69
|
return packet
|
|
62
70
|
end
|
|
63
71
|
|
|
64
72
|
def read_length(data, sequence_number)
|
|
65
|
-
iv_data =
|
|
73
|
+
iv_data = packet_iv(sequence_number)
|
|
66
74
|
@chacha_hdr.iv = iv_data
|
|
67
75
|
@chacha_hdr.update(data).unpack1("N")
|
|
68
76
|
end
|
|
69
77
|
|
|
70
78
|
def read_and_mac(data, mac, sequence_number)
|
|
71
|
-
iv_data =
|
|
79
|
+
iv_data = packet_iv(sequence_number)
|
|
72
80
|
@chacha_main.iv = iv_data
|
|
73
|
-
poly_key = @chacha_main.update(
|
|
81
|
+
poly_key = @chacha_main.update(ZERO_BLOCK)
|
|
74
82
|
|
|
75
83
|
iv_data[0] = 1.chr
|
|
76
84
|
@chacha_main.iv = iv_data
|
|
77
85
|
unencrypted_data = @chacha_main.update(data[4..])
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
|
|
87
|
+
expected_mac = self.class.poly1305_auth(poly_key, data[0..])
|
|
88
|
+
valid_mac = mac.respond_to?(:bytesize) &&
|
|
89
|
+
mac.bytesize == POLY1305_TAG_BYTES &&
|
|
90
|
+
OpenSSL.fixed_length_secure_compare(expected_mac, mac)
|
|
91
|
+
raise Net::SSH::Exception, "corrupted hmac detected #{name}" unless valid_mac
|
|
92
|
+
|
|
84
93
|
return unencrypted_data
|
|
85
94
|
end
|
|
86
95
|
|
|
87
96
|
def mac_length
|
|
88
|
-
|
|
97
|
+
POLY1305_TAG_BYTES
|
|
89
98
|
end
|
|
90
99
|
|
|
91
100
|
def block_size
|
|
@@ -93,7 +102,7 @@ module Net
|
|
|
93
102
|
end
|
|
94
103
|
|
|
95
104
|
def name
|
|
96
|
-
|
|
105
|
+
NAME
|
|
97
106
|
end
|
|
98
107
|
|
|
99
108
|
def implicit_mac?
|
|
@@ -111,6 +120,70 @@ module Net
|
|
|
111
120
|
def self.key_length
|
|
112
121
|
64
|
|
113
122
|
end
|
|
123
|
+
|
|
124
|
+
def self.iv_len
|
|
125
|
+
0
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def self.auth_length
|
|
129
|
+
POLY1305_TAG_BYTES
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def self.decrypt_private_key(ciphertext, auth_tag, key, _initialization_vector)
|
|
133
|
+
raise ArgumentError, "chacha20_poly1305: keylength doesn't match" unless
|
|
134
|
+
key.respond_to?(:bytesize) && key.bytesize == key_length
|
|
135
|
+
|
|
136
|
+
ciphertext = binary_string(ciphertext)
|
|
137
|
+
chacha = OpenSSL::Cipher.new("chacha20")
|
|
138
|
+
chacha.decrypt
|
|
139
|
+
chacha.key = binary_string(key[0...POLY1305_KEY_BYTES])
|
|
140
|
+
|
|
141
|
+
iv_data = ZERO_IV.dup
|
|
142
|
+
chacha.iv = iv_data
|
|
143
|
+
poly_key = chacha.update(ZERO_BLOCK)
|
|
144
|
+
|
|
145
|
+
valid_mac = OpenSSL.fixed_length_secure_compare(poly1305_auth(poly_key, ciphertext), auth_tag)
|
|
146
|
+
raise Net::SSH::Exception, "corrupted hmac detected #{NAME}" unless valid_mac
|
|
147
|
+
|
|
148
|
+
iv_data.setbyte(0, 1)
|
|
149
|
+
chacha.iv = iv_data
|
|
150
|
+
chacha.update(ciphertext)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def self.ensure_supported!
|
|
154
|
+
raise UnsupportedError, "OpenSSL::PKey raw private key APIs are unavailable" unless OpenSSL::PKey.respond_to?(:new_raw_private_key)
|
|
155
|
+
|
|
156
|
+
OpenSSL::Cipher.new("chacha20")
|
|
157
|
+
|
|
158
|
+
tag = poly1305_auth("\x00" * POLY1305_KEY_BYTES, "")
|
|
159
|
+
raise UnsupportedError, "OpenSSL Poly1305 authentication failed" unless tag.bytesize == POLY1305_TAG_BYTES
|
|
160
|
+
rescue OpenSSL::Cipher::CipherError, OpenSSL::PKey::PKeyError => e
|
|
161
|
+
raise UnsupportedError, e.message
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def self.poly1305_auth(poly_key, data)
|
|
165
|
+
validate_poly1305_key!(poly_key)
|
|
166
|
+
OpenSSL::PKey.new_raw_private_key(POLY1305_ALGORITHM, binary_string(poly_key)).sign(nil, binary_string(data))
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def self.validate_poly1305_key!(poly_key)
|
|
170
|
+
raise ArgumentError, "invalid Poly1305 key" unless poly_key.respond_to?(:bytesize) && poly_key.bytesize == POLY1305_KEY_BYTES
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def self.binary_string(string)
|
|
174
|
+
string.dup.force_encoding('BINARY')
|
|
175
|
+
end
|
|
176
|
+
private_class_method :binary_string
|
|
177
|
+
|
|
178
|
+
def packet_iv(sequence_number)
|
|
179
|
+
iv_data = ZERO_IV.dup
|
|
180
|
+
iv_data.setbyte(12, (sequence_number >> 24) & 0xff)
|
|
181
|
+
iv_data.setbyte(13, (sequence_number >> 16) & 0xff)
|
|
182
|
+
iv_data.setbyte(14, (sequence_number >> 8) & 0xff)
|
|
183
|
+
iv_data.setbyte(15, sequence_number & 0xff)
|
|
184
|
+
iv_data
|
|
185
|
+
end
|
|
186
|
+
private :packet_iv
|
|
114
187
|
end
|
|
115
188
|
end
|
|
116
189
|
end
|
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
module Net
|
|
2
2
|
module SSH
|
|
3
3
|
module Transport
|
|
4
|
-
# Loads chacha20
|
|
4
|
+
# Loads chacha20-poly1305 support backed by Ruby OpenSSL APIs.
|
|
5
5
|
module ChaCha20Poly1305CipherLoader
|
|
6
6
|
begin
|
|
7
7
|
require 'net/ssh/transport/chacha20_poly1305_cipher'
|
|
8
|
+
Net::SSH::Transport::ChaCha20Poly1305Cipher.ensure_supported!
|
|
8
9
|
LOADED = true
|
|
9
10
|
ERROR = nil
|
|
10
11
|
rescue LoadError => e
|
|
11
12
|
ERROR = e
|
|
12
13
|
LOADED = false
|
|
14
|
+
rescue StandardError => e
|
|
15
|
+
if defined?(Net::SSH::Transport::ChaCha20Poly1305Cipher::UnsupportedError) &&
|
|
16
|
+
e.is_a?(Net::SSH::Transport::ChaCha20Poly1305Cipher::UnsupportedError)
|
|
17
|
+
ERROR = e
|
|
18
|
+
LOADED = false
|
|
19
|
+
else
|
|
20
|
+
raise
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.raiseUnlessLoaded(message)
|
|
25
|
+
description = LOADED ? '' : chacha20_poly1305_support_required
|
|
26
|
+
description += "#{ERROR.class} : \"#{ERROR.message}\"\n" if ERROR
|
|
27
|
+
raise NotImplementedError, "#{message}\n#{description}" unless LOADED
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.chacha20_poly1305_support_required
|
|
31
|
+
"net-ssh requires openssl >= 3.2.0 and a crypto backend with ChaCha20 and Poly1305 enabled for chacha20-poly1305@openssh.com support.\n"
|
|
13
32
|
end
|
|
14
33
|
end
|
|
15
34
|
end
|
|
@@ -100,7 +100,11 @@ module Net
|
|
|
100
100
|
# if :iv_len option is supplied the third return value will be ivlen
|
|
101
101
|
def self.get_lengths(name, options = {})
|
|
102
102
|
klass = SSH_TO_CLASS[name]
|
|
103
|
-
|
|
103
|
+
unless klass.nil?
|
|
104
|
+
result = [klass.key_length, klass.block_size]
|
|
105
|
+
result << klass.iv_len if options[:iv_len]
|
|
106
|
+
return result
|
|
107
|
+
end
|
|
104
108
|
|
|
105
109
|
ossl_name = SSH_TO_OSSL[name]
|
|
106
110
|
if ossl_name.nil? || ossl_name == "none"
|
|
@@ -124,6 +128,24 @@ module Net
|
|
|
124
128
|
end
|
|
125
129
|
result
|
|
126
130
|
end
|
|
131
|
+
|
|
132
|
+
def self.auth_length(name)
|
|
133
|
+
klass = SSH_TO_CLASS[name]
|
|
134
|
+
return klass.auth_length if klass.respond_to?(:auth_length)
|
|
135
|
+
|
|
136
|
+
0
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def self.decrypt_private_key(name, ciphertext, auth_tag, key, initialization_vector)
|
|
140
|
+
klass = SSH_TO_CLASS[name]
|
|
141
|
+
raise Net::SSH::Exception, "#{name} does not support private key decryption" unless
|
|
142
|
+
klass.respond_to?(:decrypt_private_key)
|
|
143
|
+
|
|
144
|
+
raise Net::SSH::Exception, "incorrect auth_tag length" unless
|
|
145
|
+
auth_tag.respond_to?(:bytesize) && auth_tag.bytesize == klass.auth_length
|
|
146
|
+
|
|
147
|
+
klass.decrypt_private_key(ciphertext, auth_tag, key, initialization_vector)
|
|
148
|
+
end
|
|
127
149
|
end
|
|
128
150
|
end
|
|
129
151
|
end
|
|
@@ -5,7 +5,11 @@ module Net
|
|
|
5
5
|
module Transport
|
|
6
6
|
## Extension module for aes(128|256)gcm ciphers
|
|
7
7
|
module GCMCipher
|
|
8
|
-
|
|
8
|
+
BLOCK_SIZE = 16
|
|
9
|
+
IV_BYTES = 12
|
|
10
|
+
AUTH_TAG_BYTES = 16
|
|
11
|
+
|
|
12
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
9
13
|
def self.extended(orig)
|
|
10
14
|
# rubocop:disable Metrics/BlockLength
|
|
11
15
|
orig.class_eval do
|
|
@@ -100,15 +104,32 @@ module Net
|
|
|
100
104
|
end
|
|
101
105
|
|
|
102
106
|
def mac_length
|
|
103
|
-
|
|
107
|
+
GCMCipher::AUTH_TAG_BYTES
|
|
104
108
|
end
|
|
105
109
|
|
|
106
110
|
def block_size
|
|
107
|
-
|
|
111
|
+
GCMCipher::BLOCK_SIZE
|
|
108
112
|
end
|
|
109
113
|
|
|
110
114
|
def self.block_size
|
|
111
|
-
|
|
115
|
+
GCMCipher::BLOCK_SIZE
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def self.iv_len
|
|
119
|
+
GCMCipher::IV_BYTES
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.auth_length
|
|
123
|
+
GCMCipher::AUTH_TAG_BYTES
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def self.decrypt_private_key(ciphertext, auth_tag, key, initialization_vector)
|
|
127
|
+
cipher = new(encrypt: false, key: key)
|
|
128
|
+
|
|
129
|
+
cipher.nonce = initialization_vector
|
|
130
|
+
cipher.cipher.auth_tag = auth_tag.to_s
|
|
131
|
+
cipher.cipher.auth_data = ''
|
|
132
|
+
cipher.cipher.update(ciphertext.to_s) << cipher.cipher.final
|
|
112
133
|
end
|
|
113
134
|
|
|
114
135
|
#
|
|
@@ -117,7 +138,7 @@ module Net
|
|
|
117
138
|
# N_MAX maximum nonce (IV) length 12 octets
|
|
118
139
|
#
|
|
119
140
|
def iv_len
|
|
120
|
-
|
|
141
|
+
GCMCipher::IV_BYTES
|
|
121
142
|
end
|
|
122
143
|
|
|
123
144
|
#
|
|
@@ -201,7 +222,7 @@ module Net
|
|
|
201
222
|
end
|
|
202
223
|
# rubocop:enable Metrics/BlockLength
|
|
203
224
|
end
|
|
204
|
-
# rubocop:enable Metrics/AbcSize
|
|
225
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
|
205
226
|
end
|
|
206
227
|
end
|
|
207
228
|
end
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require 'x25519'
|
|
4
|
-
|
|
1
|
+
require 'openssl'
|
|
5
2
|
require 'net/ssh/transport/constants'
|
|
6
3
|
require 'net/ssh/transport/kex/abstract5656'
|
|
7
4
|
|
|
@@ -12,25 +9,60 @@ module Net
|
|
|
12
9
|
# A key-exchange service implementing the "curve25519-sha256@libssh.org"
|
|
13
10
|
# key-exchange algorithm. (defined in https://tools.ietf.org/html/draft-ietf-curdle-ssh-curves-06)
|
|
14
11
|
class Curve25519Sha256 < Abstract5656
|
|
12
|
+
ALGORITHM = "X25519"
|
|
13
|
+
KEY_BYTES = 32
|
|
14
|
+
|
|
15
|
+
class UnsupportedError < StandardError; end
|
|
16
|
+
|
|
17
|
+
def self.ensure_supported!
|
|
18
|
+
unless OpenSSL::PKey.respond_to?(:generate_key) && OpenSSL::PKey.respond_to?(:new_raw_public_key)
|
|
19
|
+
raise UnsupportedError, "OpenSSL::PKey raw public key APIs are unavailable"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
key = generate_x25519_key
|
|
23
|
+
public_key = new_raw_public_key(key.raw_public_key)
|
|
24
|
+
validate_key_bytes!(key.derive(public_key), "shared secret", KEY_BYTES)
|
|
25
|
+
rescue OpenSSL::PKey::PKeyError => e
|
|
26
|
+
raise UnsupportedError, e.message
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.generate_x25519_key
|
|
30
|
+
OpenSSL::PKey.generate_key(ALGORITHM)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.new_raw_public_key(key)
|
|
34
|
+
validate_key_bytes!(key, "public key", KEY_BYTES)
|
|
35
|
+
OpenSSL::PKey.new_raw_public_key(ALGORITHM, binary_string(key))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.validate_key_bytes!(key, label, expected_bytes)
|
|
39
|
+
raise ArgumentError, "invalid X25519 #{label}" unless key.respond_to?(:bytesize) && key.bytesize == expected_bytes
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.binary_string(string)
|
|
43
|
+
string.dup.force_encoding('BINARY')
|
|
44
|
+
end
|
|
45
|
+
private_class_method :binary_string
|
|
46
|
+
|
|
15
47
|
def digester
|
|
16
48
|
OpenSSL::Digest::SHA256
|
|
17
49
|
end
|
|
18
50
|
|
|
19
51
|
private
|
|
20
52
|
|
|
21
|
-
def generate_key
|
|
22
|
-
|
|
53
|
+
def generate_key
|
|
54
|
+
self.class.generate_x25519_key
|
|
23
55
|
end
|
|
24
56
|
|
|
25
57
|
## string Q_C, client's ephemeral public key octet string
|
|
26
58
|
def ecdh_public_key_bytes
|
|
27
|
-
ecdh.
|
|
59
|
+
ecdh.raw_public_key
|
|
28
60
|
end
|
|
29
61
|
|
|
30
62
|
# compute shared secret from server's public key and client's private key
|
|
31
63
|
def compute_shared_secret(server_ecdh_pubkey)
|
|
32
|
-
pk =
|
|
33
|
-
OpenSSL::BN.new(ecdh.
|
|
64
|
+
pk = self.class.new_raw_public_key(server_ecdh_pubkey)
|
|
65
|
+
OpenSSL::BN.new(ecdh.derive(pk), 2)
|
|
34
66
|
end
|
|
35
67
|
end
|
|
36
68
|
end
|
|
@@ -2,26 +2,34 @@ module Net
|
|
|
2
2
|
module SSH
|
|
3
3
|
module Transport
|
|
4
4
|
module Kex
|
|
5
|
-
# Loads Curve25519Sha256 support
|
|
5
|
+
# Loads Curve25519Sha256 support backed by Ruby OpenSSL X25519 APIs.
|
|
6
6
|
module Curve25519Sha256Loader
|
|
7
7
|
begin
|
|
8
8
|
require 'net/ssh/transport/kex/curve25519_sha256'
|
|
9
|
+
Net::SSH::Transport::Kex::Curve25519Sha256.ensure_supported!
|
|
9
10
|
LOADED = true
|
|
10
11
|
ERROR = nil
|
|
11
12
|
rescue LoadError => e
|
|
12
13
|
ERROR = e
|
|
13
14
|
LOADED = false
|
|
15
|
+
rescue StandardError => e
|
|
16
|
+
if defined?(Net::SSH::Transport::Kex::Curve25519Sha256::UnsupportedError) &&
|
|
17
|
+
e.is_a?(Net::SSH::Transport::Kex::Curve25519Sha256::UnsupportedError)
|
|
18
|
+
ERROR = e
|
|
19
|
+
LOADED = false
|
|
20
|
+
else
|
|
21
|
+
raise
|
|
22
|
+
end
|
|
14
23
|
end
|
|
15
24
|
|
|
16
25
|
def self.raiseUnlessLoaded(message)
|
|
17
|
-
description =
|
|
18
|
-
description
|
|
26
|
+
description = LOADED ? '' : x25519SupportRequired
|
|
27
|
+
description += "#{ERROR.class} : \"#{ERROR.message}\"\n" if ERROR
|
|
19
28
|
raise NotImplementedError, "#{message}\n#{description}" unless LOADED
|
|
20
29
|
end
|
|
21
30
|
|
|
22
|
-
def self.
|
|
23
|
-
|
|
24
|
-
result << " * x25519\n"
|
|
31
|
+
def self.x25519SupportRequired
|
|
32
|
+
"net-ssh requires openssl >= 3.2.0 and a crypto backend with X25519 enabled for curve25519-sha256 support.\n"
|
|
25
33
|
end
|
|
26
34
|
end
|
|
27
35
|
end
|
|
@@ -62,6 +62,7 @@ module Net
|
|
|
62
62
|
@port = options[:port] || DEFAULT_PORT
|
|
63
63
|
@bind_address = options[:bind_address] || nil
|
|
64
64
|
@options = options
|
|
65
|
+
@host_key_verifier = select_host_key_verifier(options[:verify_host_key])
|
|
65
66
|
|
|
66
67
|
@socket =
|
|
67
68
|
if (factory = options[:proxy])
|
|
@@ -75,20 +76,23 @@ module Net
|
|
|
75
76
|
|
|
76
77
|
@socket.extend(PacketStream)
|
|
77
78
|
@socket.logger = @logger
|
|
79
|
+
set_no_delay unless options[:no_delay] == false
|
|
78
80
|
|
|
79
81
|
debug { "connection established" }
|
|
80
82
|
|
|
81
83
|
@queue = []
|
|
82
84
|
|
|
83
|
-
@host_key_verifier = select_host_key_verifier(options[:verify_host_key])
|
|
84
|
-
|
|
85
85
|
@server_version = ServerVersion.new(socket, logger, options[:timeout])
|
|
86
86
|
|
|
87
87
|
@algorithms = Algorithms.new(self, options)
|
|
88
88
|
@algorithms.start
|
|
89
89
|
wait { algorithms.initialized? }
|
|
90
90
|
rescue Errno::ETIMEDOUT
|
|
91
|
+
@socket&.close
|
|
91
92
|
raise Net::SSH::ConnectionTimeout
|
|
93
|
+
rescue StandardError
|
|
94
|
+
@socket&.close
|
|
95
|
+
raise
|
|
92
96
|
end
|
|
93
97
|
|
|
94
98
|
def host_keys
|
|
@@ -206,6 +210,10 @@ module Net
|
|
|
206
210
|
when DEBUG
|
|
207
211
|
send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
|
|
208
212
|
|
|
213
|
+
# We don't advertise ext-info-c, but some servers send EXT_INFO anyway (#955).
|
|
214
|
+
when EXT_INFO
|
|
215
|
+
debug { "ignoring EXT_INFO packet" }
|
|
216
|
+
|
|
209
217
|
when KEXINIT
|
|
210
218
|
algorithms.accept_kexinit(packet)
|
|
211
219
|
|
|
@@ -276,6 +284,18 @@ module Net
|
|
|
276
284
|
|
|
277
285
|
private
|
|
278
286
|
|
|
287
|
+
# Turns Nagle's algorithm off on the socket, so that a packet written
|
|
288
|
+
# while the previous one is still unacknowledged goes out right away
|
|
289
|
+
# instead of waiting for the server's ACK. A ProxyCommand hands the
|
|
290
|
+
# transport a pipe rather than a socket, and the command on its other
|
|
291
|
+
# end owns the TCP connection, so there is nothing to set in that case.
|
|
292
|
+
def set_no_delay
|
|
293
|
+
return unless socket.respond_to?(:setsockopt)
|
|
294
|
+
|
|
295
|
+
debug { "setting TCP_NODELAY" }
|
|
296
|
+
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
297
|
+
end
|
|
298
|
+
|
|
279
299
|
# Compatibility verifier which allows users to keep using
|
|
280
300
|
# custom verifier code without adding new :verify_signature
|
|
281
301
|
# method.
|
data/lib/net/ssh/version.rb
CHANGED
|
@@ -46,17 +46,17 @@ module Net
|
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# The major component of this version of the Net::SSH library
|
|
49
|
-
MAJOR =
|
|
49
|
+
MAJOR = 8
|
|
50
50
|
|
|
51
51
|
# The minor component of this version of the Net::SSH library
|
|
52
|
-
MINOR =
|
|
52
|
+
MINOR = 0
|
|
53
53
|
|
|
54
54
|
# The tiny component of this version of the Net::SSH library
|
|
55
|
-
TINY =
|
|
55
|
+
TINY = 0
|
|
56
56
|
|
|
57
57
|
# The prerelease component of this version of the Net::SSH library
|
|
58
58
|
# nil allowed
|
|
59
|
-
PRE =
|
|
59
|
+
PRE = "rc1"
|
|
60
60
|
|
|
61
61
|
# The current version of the Net::SSH library as a Version instance
|
|
62
62
|
CURRENT = new(*[MAJOR, MINOR, TINY, PRE].compact)
|
data/lib/net/ssh.rb
CHANGED
|
@@ -69,7 +69,7 @@ module Net
|
|
|
69
69
|
max_win_size send_env set_env use_agent number_of_password_prompts
|
|
70
70
|
append_all_supported_algorithms non_interactive password_prompt
|
|
71
71
|
agent_socket_factory minimum_dh_bits verify_host_key
|
|
72
|
-
fingerprint_hash check_host_ip pubkey_algorithms
|
|
72
|
+
fingerprint_hash check_host_ip pubkey_algorithms no_delay
|
|
73
73
|
]
|
|
74
74
|
|
|
75
75
|
# The standard means of starting a new SSH connection. When used with a
|
|
@@ -156,6 +156,12 @@ module Net
|
|
|
156
156
|
# for better performance if your SSH server supports it (most do).
|
|
157
157
|
# * :max_win_size => maximum size we tell the other side that is supported for
|
|
158
158
|
# the window.
|
|
159
|
+
# * :no_delay => turns Nagle's algorithm off on the connection's socket
|
|
160
|
+
# (TCP_NODELAY), so a packet written while the previous one is still
|
|
161
|
+
# unacknowledged is sent right away instead of waiting for the server's
|
|
162
|
+
# ACK. OpenSSH does the same for every connection. Set to +false+ to keep
|
|
163
|
+
# Nagle's algorithm on. Has no effect when the socket comes from a
|
|
164
|
+
# ProxyCommand, whose command owns the TCP connection. Defaults to +true+.
|
|
159
165
|
# * :non_interactive => set to true if your app is non interactive and prefers
|
|
160
166
|
# authentication failure vs password prompt. Non-interactive applications
|
|
161
167
|
# should set it to true to prefer failing a password/etc auth methods vs.
|
data/net-ssh.gemspec
CHANGED
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
|
15
15
|
spec.description = %q{Net::SSH: a pure-Ruby implementation of the SSH2 client protocol. It allows you to write programs that invoke and interact with processes on remote servers, via SSH2.}
|
|
16
16
|
spec.homepage = "https://github.com/net-ssh/net-ssh"
|
|
17
17
|
spec.license = "MIT"
|
|
18
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
|
18
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
|
|
19
19
|
spec.metadata = {
|
|
20
20
|
"changelog_uri" => "https://github.com/net-ssh/net-ssh/blob/master/CHANGES.txt"
|
|
21
21
|
}
|
|
@@ -30,13 +30,10 @@ Gem::Specification.new do |spec|
|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
31
31
|
spec.require_paths = ["lib"]
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
spec.add_development_dependency("ed25519", "~> 1.2")
|
|
36
|
-
spec.add_development_dependency('x25519') unless RUBY_PLATFORM == 'java'
|
|
37
|
-
end
|
|
33
|
+
spec.add_dependency("logger")
|
|
34
|
+
spec.add_dependency("openssl", ">= 3.2.0")
|
|
38
35
|
|
|
39
|
-
spec.add_development_dependency(
|
|
36
|
+
spec.add_development_dependency("bcrypt_pbkdf", "~> 1.0") unless RUBY_PLATFORM == "java"
|
|
40
37
|
|
|
41
38
|
spec.add_development_dependency "base64"
|
|
42
39
|
spec.add_development_dependency "bundler", ">= 1.17"
|
data.tar.gz.sig
CHANGED
|
Binary file
|