Package: release.debian.org Severity: normal Tags: bullseye User: release.debian....@packages.debian.org Usertags: pu
[ Reason ] OpenSSH 8.8 disables RSA signatures using the SHA-1 hash algorithm, and that breaks clients that do not support stronger algorithms, which is the case of the ruby-net-ssh version in bullseye. [ Impact ] Users of vagrant and capistrano, for example, are not able to connect to hosts running OpenSSH 8.8, which includes Debian bookworm but also other distributions where OpenSSH 8.8. is already available. [ Tests ] All the included unit tests, includind the new ones added by the included patches, pass both during the package build and autopkgtest. This updates was also tested manually on stable by Lucas Nussbaum (see #1008541), who confirmed the fix works. [ Risks ] I don't see much risk in this update. [ Checklist ] [x] *all* changes are documented in the d/changelog [x] I reviewed all changes and I approve them [x] attach debdiff against the package in (old)stable [x] the issue is verified as fixed in unstable [ Changes ] The patches are backports of the relevant upstream patches. The first adds support for client authentication with RSA + SHA-2. The second adds support for RSA+SHA-2 in host keys. [ Other info ] I'm also attaching the patches themselves, because they are easier to read than the diff-in-diff in the debdiff.
diff --git a/debian/changelog b/debian/changelog index a1f8837f..763e6086 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,11 @@ +ruby-net-ssh (1:6.1.0-2+deb11u1) bullseye; urgency=medium + + * Backport upstream patches to fix authentication against hosts using + OpenSSH 8.8, including but not limited to Debian bookworm + (Closes: #1009155, #1008541) + + -- Antonio Terceiro <terce...@debian.org> Fri, 08 Apr 2022 10:06:46 -0300 + ruby-net-ssh (1:6.1.0-2) unstable; urgency=medium [ Cédric Boutillier ] diff --git a/debian/patches/0001-Added-support-for-RSA-client-authentication-with-SHA.patch b/debian/patches/0001-Added-support-for-RSA-client-authentication-with-SHA.patch new file mode 100644 index 00000000..ae350e52 --- /dev/null +++ b/debian/patches/0001-Added-support-for-RSA-client-authentication-with-SHA.patch @@ -0,0 +1,394 @@ +From: Zdenek Zambersky <zzamb...@redhat.com> +Date: Thu, 6 May 2021 13:50:20 +0200 +Subject: Added support for RSA client authentication with SHA-2 + +Source: https://github.com/net-ssh/net-ssh/pull/838 +Backported-By: Antonio Terceiro <terce...@debian.org> +--- + lib/net/ssh/authentication/certificate.rb | 4 +- + lib/net/ssh/authentication/ed25519.rb | 2 +- + lib/net/ssh/authentication/key_manager.rb | 22 ++++++-- + lib/net/ssh/authentication/methods/abstract.rb | 10 ++++ + lib/net/ssh/authentication/methods/publickey.rb | 66 ++++++++++++++++++----- + lib/net/ssh/authentication/session.rb | 5 +- + lib/net/ssh/transport/openssl.rb | 16 ++++-- + test/authentication/methods/test_publickey.rb | 71 ++++++++++++++++++++++--- + test/authentication/test_session.rb | 6 ++- + 9 files changed, 169 insertions(+), 33 deletions(-) + +diff --git a/lib/net/ssh/authentication/certificate.rb b/lib/net/ssh/authentication/certificate.rb +index 82e37e9..95b01ff 100644 +--- a/lib/net/ssh/authentication/certificate.rb ++++ b/lib/net/ssh/authentication/certificate.rb +@@ -65,8 +65,8 @@ module Net + ).to_s + end + +- def ssh_do_sign(data) +- key.ssh_do_sign(data) ++ def ssh_do_sign(data, sig_alg = nil) ++ key.ssh_do_sign(data, sig_alg) + end + + def ssh_do_verify(sig, data) +diff --git a/lib/net/ssh/authentication/ed25519.rb b/lib/net/ssh/authentication/ed25519.rb +index 0c5530c..1989d1f 100644 +--- a/lib/net/ssh/authentication/ed25519.rb ++++ b/lib/net/ssh/authentication/ed25519.rb +@@ -167,7 +167,7 @@ module Net + PubKey.new(@pk) + end + +- def ssh_do_sign(data) ++ def ssh_do_sign(data, sig_alg = nil) + @sign_key.sign(data) + end + +diff --git a/lib/net/ssh/authentication/key_manager.rb b/lib/net/ssh/authentication/key_manager.rb +index 242d5d5..3624550 100644 +--- a/lib/net/ssh/authentication/key_manager.rb ++++ b/lib/net/ssh/authentication/key_manager.rb +@@ -159,7 +159,7 @@ module Net + # Regardless of the identity's origin or who does the signing, this + # will always return the signature in an SSH2-specified "signature + # blob" format. +- def sign(identity, data) ++ def sign(identity, data, sig_alg = nil) + info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager" + + if info[:key].nil? && info[:from] == :file +@@ -171,13 +171,27 @@ module Net + end + + if info[:key] +- return Net::SSH::Buffer.from(:string, identity.ssh_signature_type, +- :mstring, info[:key].ssh_do_sign(data.to_s)).to_s ++ if sig_alg.nil? ++ signed = info[:key].ssh_do_sign(data.to_s) ++ sig_alg = identity.ssh_signature_type ++ else ++ signed = info[:key].ssh_do_sign(data.to_s, sig_alg) ++ end ++ return Net::SSH::Buffer.from(:string, sig_alg, ++ :mstring, signed).to_s + end + + if info[:from] == :agent + raise KeyManagerError, "the agent is no longer available" unless agent +- return agent.sign(info[:identity], data.to_s) ++ ++ case sig_alg ++ when "rsa-sha2-512" ++ return agent.sign(info[:identity], data.to_s, Net::SSH::Authentication::Agent::SSH_AGENT_RSA_SHA2_512) ++ when "rsa-sha2-256" ++ return agent.sign(info[:identity], data.to_s, Net::SSH::Authentication::Agent::SSH_AGENT_RSA_SHA2_256) ++ else ++ return agent.sign(info[:identity], data.to_s) ++ end + end + + raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})" +diff --git a/lib/net/ssh/authentication/methods/abstract.rb b/lib/net/ssh/authentication/methods/abstract.rb +index bcddd4f..8ec4d22 100644 +--- a/lib/net/ssh/authentication/methods/abstract.rb ++++ b/lib/net/ssh/authentication/methods/abstract.rb +@@ -21,12 +21,22 @@ module Net + # this. + attr_reader :key_manager + ++ # So far only affects algorithms used for rsa keys, but can be ++ # extended to other keys, e.g after reading of ++ # PubkeyAcceptedAlgorithms option from ssh_config file is implemented. ++ attr_reader :pubkey_algorithms ++ + # Instantiates a new authentication method. + def initialize(session, options={}) + @session = session + @key_manager = options[:key_manager] + @options = options + @prompt = options[:password_prompt] ++ @pubkey_algorithms = options[:pubkey_algorithms] \ ++ || %w[rsa-sha2-256-cert-...@openssh.com ++ ssh-rsa-cert-...@openssh.com ++ rsa-sha2-256 ++ ssh-rsa] + self.logger = session.logger + end + +diff --git a/lib/net/ssh/authentication/methods/publickey.rb b/lib/net/ssh/authentication/methods/publickey.rb +index bff9ffd..7e9e422 100644 +--- a/lib/net/ssh/authentication/methods/publickey.rb ++++ b/lib/net/ssh/authentication/methods/publickey.rb +@@ -27,41 +27,40 @@ module Net + + # Builds a packet that contains the request formatted for sending + # a public-key request to the server. +- def build_request(pub_key, username, next_service, has_sig) ++ def build_request(pub_key, username, next_service, alg, has_sig) + blob = Net::SSH::Buffer.new + blob.write_key pub_key + + userauth_request(username, next_service, "publickey", has_sig, +- pub_key.ssh_type, blob.to_s) ++ alg, blob.to_s) + end + + # Builds and sends a request formatted for a public-key + # authentication request. +- def send_request(pub_key, username, next_service, signature=nil) +- msg = build_request(pub_key, username, next_service, !signature.nil?) ++ def send_request(pub_key, username, next_service, alg, signature = nil) ++ msg = build_request(pub_key, username, next_service, alg, ++ !signature.nil?) + msg.write_string(signature) if signature + send_message(msg) + end + +- # Attempts to perform public-key authentication for the given +- # username, with the given identity (public key). Returns +true+ if +- # successful, or +false+ otherwise. +- def authenticate_with(identity, next_service, username) ++ def authenticate_with_alg(identity, next_service, username, alg, sig_alg = nil) + debug { "trying publickey (#{identity.fingerprint})" } +- send_request(identity, username, next_service) ++ send_request(identity, username, next_service, alg) + + message = session.next_message + + case message.type + when USERAUTH_PK_OK +- buffer = build_request(identity, username, next_service, true) ++ buffer = build_request(identity, username, next_service, alg, ++ true) + sig_data = Net::SSH::Buffer.new + sig_data.write_string(session_id) + sig_data.append(buffer.to_s) + +- sig_blob = key_manager.sign(identity, sig_data) ++ sig_blob = key_manager.sign(identity, sig_data, sig_alg) + +- send_request(identity, username, next_service, sig_blob.to_s) ++ send_request(identity, username, next_service, alg, sig_blob.to_s) + message = session.next_message + + case message.type +@@ -89,6 +88,49 @@ module Net + raise Net::SSH::Exception, "unexpected reply to USERAUTH_REQUEST: #{message.type} (#{message.inspect})" + end + end ++ ++ # Attempts to perform public-key authentication for the given ++ # username, with the given identity (public key). Returns +true+ if ++ # successful, or +false+ otherwise. ++ def authenticate_with(identity, next_service, username) ++ type = identity.ssh_type ++ if type == "ssh-rsa" ++ pubkey_algorithms.each do |pk_alg| ++ case pk_alg ++ when "rsa-sha2-512", "rsa-sha2-256", "ssh-rsa" ++ if authenticate_with_alg(identity, next_service, username, pk_alg, pk_alg) ++ # success ++ return true ++ end ++ end ++ end ++ elsif type == "ssh-rsa-cert-...@openssh.com" ++ pubkey_algorithms.each do |pk_alg| ++ case pk_alg ++ when "rsa-sha2-512-cert-...@openssh.com" ++ if authenticate_with_alg(identity, next_service, username, pk_alg, "rsa-sha2-512") ++ # success ++ return true ++ end ++ when "rsa-sha2-256-cert-...@openssh.com" ++ if authenticate_with_alg(identity, next_service, username, pk_alg, "rsa-sha2-256") ++ # success ++ return true ++ end ++ when "ssh-rsa-cert-...@openssh.com" ++ if authenticate_with_alg(identity, next_service, username, pk_alg) ++ # success ++ return true ++ end ++ end ++ end ++ elsif authenticate_with_alg(identity, next_service, username, type) ++ # success ++ return true ++ end ++ # failure ++ return false ++ end + end + + end +diff --git a/lib/net/ssh/authentication/session.rb b/lib/net/ssh/authentication/session.rb +index dfc5c06..a524c52 100644 +--- a/lib/net/ssh/authentication/session.rb ++++ b/lib/net/ssh/authentication/session.rb +@@ -77,7 +77,10 @@ module Net + debug { "trying #{name}" } + begin + auth_class = Methods.const_get(name.split(/\W+/).map { |p| p.capitalize }.join) +- method = auth_class.new(self, key_manager: key_manager, password_prompt: options[:password_prompt]) ++ method = auth_class.new(self, ++ key_manager: key_manager, password_prompt: options[:password_prompt], ++ pubkey_algorithms: options[:pubkey_algorithms] || nil) ++ + rescue NameError + debug {"Mechanism #{name} was requested, but isn't a known type. Ignoring it."} + next +diff --git a/lib/net/ssh/transport/openssl.rb b/lib/net/ssh/transport/openssl.rb +index d2d7117..3f10b02 100644 +--- a/lib/net/ssh/transport/openssl.rb ++++ b/lib/net/ssh/transport/openssl.rb +@@ -68,8 +68,16 @@ module OpenSSL + end + + # Returns the signature for the given data. +- def ssh_do_sign(data) +- sign(OpenSSL::Digest::SHA1.new, data) ++ def ssh_do_sign(data, sig_alg = nil) ++ digester = ++ if sig_alg == "rsa-sha2-512" ++ OpenSSL::Digest::SHA512.new ++ elsif sig_alg == "rsa-sha2-256" ++ OpenSSL::Digest::SHA256.new ++ else ++ OpenSSL::Digest::SHA1.new ++ end ++ sign(digester, data) + end + end + +@@ -105,7 +113,7 @@ module OpenSSL + end + + # Signs the given data. +- def ssh_do_sign(data) ++ def ssh_do_sign(data, sig_alg = nil) + sig = sign(OpenSSL::Digest::SHA1.new, data) + a1sig = OpenSSL::ASN1.decode(sig) + +@@ -218,7 +226,7 @@ module OpenSSL + end + + # Returns the signature for the given data. +- def ssh_do_sign(data) ++ def ssh_do_sign(data, sig_alg = nil) + digest = digester.digest(data) + sig = dsa_sign_asn1(digest) + a1sig = OpenSSL::ASN1.decode(sig) +diff --git a/test/authentication/methods/test_publickey.rb b/test/authentication/methods/test_publickey.rb +index 8f2cc73..05510f2 100644 +--- a/test/authentication/methods/test_publickey.rb ++++ b/test/authentication/methods/test_publickey.rb +@@ -105,6 +105,50 @@ module Authentication + assert subject.authenticate("ssh-connection", "jamis") + end + ++ def test_authenticate_rsa_sha2 ++ key_manager.expects(:sign).with(&signature_parameters_with_alg(keys.first, "rsa-sha2-256")).returns("sig-one") ++ ++ transport.expect do |t, packet| ++ assert_equal USERAUTH_REQUEST, packet.type ++ assert verify_userauth_request_packet(packet, keys.first, false, "rsa-sha2-256") ++ t.return(USERAUTH_PK_OK, :string, "rsa-sha2-256", :string, Net::SSH::Buffer.from(:key, keys.first)) ++ ++ t.expect do |t2, packet2| ++ assert_equal USERAUTH_REQUEST, packet2.type ++ assert verify_userauth_request_packet(packet2, keys.first, true, "rsa-sha2-256") ++ assert_equal "sig-one", packet2.read_string ++ t2.return(USERAUTH_SUCCESS) ++ end ++ end ++ ++ assert subject(pubkey_algorithms: %w[rsa-sha2-256]).authenticate("ssh-connection", "jamis") ++ end ++ ++ def test_authenticate_rsa_sha2_fallback ++ key_manager.expects(:sign).with(&signature_parameters(keys.first)).returns("sig-one") ++ ++ transport.expect do |t, packet| ++ assert_equal USERAUTH_REQUEST, packet.type ++ assert verify_userauth_request_packet(packet, keys.first, false, "rsa-sha2-256") ++ t.return(USERAUTH_FAILURE, :string, "publickey") ++ ++ t.expect do |t2, packet2| ++ assert_equal USERAUTH_REQUEST, packet2.type ++ assert verify_userauth_request_packet(packet2, keys.first, false) ++ t2.return(USERAUTH_PK_OK, :string, keys.first.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.first)) ++ ++ t2.expect do |t3, packet3| ++ assert_equal USERAUTH_REQUEST, packet3.type ++ assert verify_userauth_request_packet(packet3, keys.first, true) ++ assert_equal "sig-one", packet3.read_string ++ t3.return(USERAUTH_SUCCESS) ++ end ++ end ++ end ++ ++ assert subject(pubkey_algorithms: %w[rsa-sha2-256 ssh-rsa]).authenticate("ssh-connection", "jamis") ++ end ++ + private + + def signature_parameters(key) +@@ -117,13 +161,25 @@ module Authentication + end + end + +- def verify_userauth_request_packet(packet, key, has_sig) +- packet.read_string == "jamis" && # user-name +- packet.read_string == "ssh-connection" && # next service +- packet.read_string == "publickey" && # auth-method +- packet.read_bool == has_sig && # whether a signature is appended +- packet.read_string == key.ssh_type && # ssh key type +- packet.read_buffer.read_key.to_blob == key.to_blob # key ++ def signature_parameters_with_alg(key, alg) ++ Proc.new do |given_key, data, given_alg| ++ next false unless given_key.to_blob == key.to_blob ++ next false unless given_alg == alg ++ ++ buffer = Net::SSH::Buffer.new(data) ++ buffer.read_string == "abcxyz123" && # session-id ++ buffer.read_byte == USERAUTH_REQUEST && # type ++ verify_userauth_request_packet(buffer, key, true, alg) ++ end ++ end ++ ++ def verify_userauth_request_packet(packet, key, has_sig, alg = nil) ++ packet.read_string == "jamis" && # user-name ++ packet.read_string == "ssh-connection" && # next service ++ packet.read_string == "publickey" && # auth-method ++ packet.read_bool == has_sig && # whether a signature is appended ++ packet.read_string == (alg || key.ssh_type) && # ssh key type ++ packet.read_buffer.read_key.to_blob == key.to_blob # key + end + + @@keys = nil +@@ -141,6 +197,7 @@ module Authentication + + def subject(options={}) + options[:key_manager] = key_manager(options) unless options.key?(:key_manager) ++ options[:pubkey_algorithms] = %w[ssh-rsa] unless options.key?(:pubkey_algorithms) + @subject ||= Net::SSH::Authentication::Methods::Publickey.new(session(options), options) + end + end +diff --git a/test/authentication/test_session.rb b/test/authentication/test_session.rb +index db37467..19b9f8a 100644 +--- a/test/authentication/test_session.rb ++++ b/test/authentication/test_session.rb +@@ -168,8 +168,10 @@ module Authentication + + private + +- def session(options={}) +- @session ||= Net::SSH::Authentication::Session.new(transport(options), options) ++ def session(options = {}) ++ session_opts = options.clone ++ session_opts[:pubkey_algorithms] = %w[ssh-rsa] ++ @session ||= Net::SSH::Authentication::Session.new(transport(options), session_opts) + end + + def transport(options={}) diff --git a/debian/patches/0002-Implemented-rsa-sha2-512-rsa-sha2-256-host_key-algs.patch b/debian/patches/0002-Implemented-rsa-sha2-512-rsa-sha2-256-host_key-algs.patch new file mode 100644 index 00000000..33eba9c5 --- /dev/null +++ b/debian/patches/0002-Implemented-rsa-sha2-512-rsa-sha2-256-host_key-algs.patch @@ -0,0 +1,294 @@ +From: Miklos Fazekas <mfaze...@szemafor.com> +Date: Wed, 10 Jun 2020 08:17:44 +0200 +Subject: Implemented rsa-sha2-512, rsa-sha2-256 host_key algs + +This patch is a combination of the 4 commits in the Pull Request +mentioned below. + +Source: https://github.com/net-ssh/net-ssh/pull/771 +Backported-By: Antonio Terceiro <terce...@debian.org> +--- + .gitignore | 2 ++ + lib/net/ssh/authentication/certificate.rb | 4 ++-- + lib/net/ssh/authentication/ed25519.rb | 2 +- + lib/net/ssh/transport/algorithms.rb | 4 +++- + lib/net/ssh/transport/kex/abstract.rb | 11 +++++++++-- + lib/net/ssh/transport/openssl.rb | 17 +++++++++++++---- + test/integration/Vagrantfile | 5 +++-- + test/integration/common.rb | 10 +++++++++- + test/integration/playbook.yml | 11 +++++++++-- + test/transport/kex/test_ecdh_sha2_nistp256.rb | 2 +- + test/transport/test_algorithms.rb | 14 +++++++------- + 11 files changed, 59 insertions(+), 23 deletions(-) + +diff --git a/.gitignore b/.gitignore +index b02057e..acb2b39 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -9,3 +9,5 @@ test/integration/.vagrant + test/integration/playbook.retry + + .byebug_history ++ ++tryout +diff --git a/lib/net/ssh/authentication/certificate.rb b/lib/net/ssh/authentication/certificate.rb +index 95b01ff..ff22eb4 100644 +--- a/lib/net/ssh/authentication/certificate.rb ++++ b/lib/net/ssh/authentication/certificate.rb +@@ -69,8 +69,8 @@ module Net + key.ssh_do_sign(data, sig_alg) + end + +- def ssh_do_verify(sig, data) +- key.ssh_do_verify(sig, data) ++ def ssh_do_verify(sig, data, options = {}) ++ key.ssh_do_verify(sig, data, options) + end + + def to_pem +diff --git a/lib/net/ssh/authentication/ed25519.rb b/lib/net/ssh/authentication/ed25519.rb +index 1989d1f..5f3b83b 100644 +--- a/lib/net/ssh/authentication/ed25519.rb ++++ b/lib/net/ssh/authentication/ed25519.rb +@@ -123,7 +123,7 @@ module Net + ssh_type + end + +- def ssh_do_verify(sig,data) ++ def ssh_do_verify(sig, data, options = {}) + @verify_key.verify(sig,data) + end + +diff --git a/lib/net/ssh/transport/algorithms.rb b/lib/net/ssh/transport/algorithms.rb +index 8126aa6..9ab87b6 100644 +--- a/lib/net/ssh/transport/algorithms.rb ++++ b/lib/net/ssh/transport/algorithms.rb +@@ -33,7 +33,9 @@ module Net + ecdsa-sha2-nistp256 + ssh-rsa-cert-...@openssh.com + ssh-rsa-cert-...@openssh.com +- ssh-rsa], ++ ssh-rsa ++ rsa-sha2-256 ++ rsa-sha2-512], + + kex: %w[ecdh-sha2-nistp521 + ecdh-sha2-nistp384 +diff --git a/lib/net/ssh/transport/kex/abstract.rb b/lib/net/ssh/transport/kex/abstract.rb +index c70eb94..3fd8c3c 100644 +--- a/lib/net/ssh/transport/kex/abstract.rb ++++ b/lib/net/ssh/transport/kex/abstract.rb +@@ -64,11 +64,16 @@ module Net + + private + ++ def matching?(key_ssh_type, host_key_alg) ++ return true if key_ssh_type == host_key_alg ++ return true if key_ssh_type == 'ssh-rsa' && ['rsa-sha2-512', 'rsa-sha2-256'].include?(host_key_alg) ++ end ++ + # Verify that the given key is of the expected type, and that it + # really is the key for the session's host. Raise Net::SSH::Exception + # if it is not. + def verify_server_key(key) #:nodoc: +- if key.ssh_type != algorithms.host_key ++ unless matching?(key.ssh_type, algorithms.host_key) + raise Net::SSH::Exception, "host key algorithm mismatch '#{key.ssh_type}' != '#{algorithms.host_key}'" + end + +@@ -97,7 +102,9 @@ module Net + + hash = digester.digest(response.to_s) + +- unless connection.host_key_verifier.verify_signature { result[:server_key].ssh_do_verify(result[:server_sig], hash) } ++ server_key = result[:server_key] ++ server_sig = result[:server_sig] ++ unless connection.host_key_verifier.verify_signature { server_key.ssh_do_verify(server_sig, hash, host_key: algorithms.host_key) } + raise Net::SSH::Exception, 'could not verify server signature' + end + +diff --git a/lib/net/ssh/transport/openssl.rb b/lib/net/ssh/transport/openssl.rb +index 3f10b02..e995ad6 100644 +--- a/lib/net/ssh/transport/openssl.rb ++++ b/lib/net/ssh/transport/openssl.rb +@@ -63,8 +63,17 @@ module OpenSSL + end + + # Verifies the given signature matches the given data. +- def ssh_do_verify(sig, data) +- verify(OpenSSL::Digest::SHA1.new, sig, data) ++ def ssh_do_verify(sig, data, options = {}) ++ digester = ++ if options[:host_key] == "rsa-sha2-512" ++ OpenSSL::Digest::SHA512.new ++ elsif options[:host_key] == "rsa-sha2-256" ++ OpenSSL::Digest::SHA256.new ++ else ++ OpenSSL::Digest::SHA1.new ++ end ++ ++ verify(digester, sig, data) + end + + # Returns the signature for the given data. +@@ -102,7 +111,7 @@ module OpenSSL + end + + # Verifies the given signature matches the given data. +- def ssh_do_verify(sig, data) ++ def ssh_do_verify(sig, data, options = {}) + sig_r = sig[0,20].unpack("H*")[0].to_i(16) + sig_s = sig[20,20].unpack("H*")[0].to_i(16) + a1sig = OpenSSL::ASN1::Sequence([ +@@ -200,7 +209,7 @@ module OpenSSL + end + + # Verifies the given signature matches the given data. +- def ssh_do_verify(sig, data) ++ def ssh_do_verify(sig, data, options = {}) + digest = digester.digest(data) + a1sig = nil + +diff --git a/test/integration/Vagrantfile b/test/integration/Vagrantfile +index 2c42d9a..2327e5d 100644 +--- a/test/integration/Vagrantfile ++++ b/test/integration/Vagrantfile +@@ -1,10 +1,11 @@ + VAGRANTFILE_API_VERSION = "2" + + Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| +- config.vm.box = "ubuntu/trusty64" ++ config.vm.box = "ubuntu/bionic64" + config.vm.provision "ansible" do |ansible| + ansible.playbook = "./playbook.yml" +- ansible.sudo = true ++ ansible.become = true ++ ansible.become_user = 'root' + ansible.verbose = 'vvvv' + ansible.compatibility_mode = "2.0" + end +diff --git a/test/integration/common.rb b/test/integration/common.rb +index 6295ada..8fe0881 100644 +--- a/test/integration/common.rb ++++ b/test/integration/common.rb +@@ -110,7 +110,15 @@ module IntegrationTestHelpers + # down sshd. + if pid + system('sudo', 'kill', '-15', pid.to_s) +- Process.wait(pid) ++ begin ++ Timeout.timeout(5) do ++ Process.wait(pid) ++ end ++ rescue Timeout::Error ++ warn "Failed to kill net-ssh process: #{pid}" ++ system('sudo', 'kill', '-9', pid) ++ raise ++ end + end + end + +diff --git a/test/integration/playbook.yml b/test/integration/playbook.yml +index 47eba34..6d79a54 100644 +--- a/test/integration/playbook.yml ++++ b/test/integration/playbook.yml +@@ -93,13 +93,20 @@ + - name: add host aliases + lineinfile: dest='/etc/hosts' owner='root' group='root' mode=0644 + regexp='^127\.0\.0\.1\s+gateway.netssh' line='127.0.0.1 gateway.netssh' +- - apt: ++ - name: Update APT Cache ++ apt: ++ update_cache: yes ++ force_apt_get: yes ++ - name: Wait for locfile removal ++ become: yes ++ shell: while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done; ++ - name: Install packages ++ apt: + pkg: + - pv + - libgmp3-dev + - git + state: present +- update_cache: yes + - copy: content='echo "cd /net-ssh ; rake integration-test"' dest=/etc/update-motd.d/99-net-ssh-tests mode=0755 + - name: add user to rvm group so they can change gem wrappers + user: +diff --git a/test/transport/kex/test_ecdh_sha2_nistp256.rb b/test/transport/kex/test_ecdh_sha2_nistp256.rb +index 932d8d7..3489230 100644 +--- a/test/transport/kex/test_ecdh_sha2_nistp256.rb ++++ b/test/transport/kex/test_ecdh_sha2_nistp256.rb +@@ -1,7 +1,7 @@ + require 'openssl' + require 'ostruct' + require_relative '../../common' +-require 'transport/kex/test_diffie_hellman_group1_sha1' ++require_relative './test_diffie_hellman_group1_sha1' + require 'net/ssh/transport/kex/ecdh_sha2_nistp256' + + module Transport +diff --git a/test/transport/test_algorithms.rb b/test/transport/test_algorithms.rb +index 105f3af..aac8b9a 100644 +--- a/test/transport/test_algorithms.rb ++++ b/test/transport/test_algorithms.rb +@@ -18,7 +18,7 @@ module Transport + end + + def test_constructor_should_build_default_list_of_preferred_algorithms +- assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms[:host_key] ++ assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms[:host_key] + assert_equal x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1], algorithms[:kex] + assert_equal %w[aes256-ctr aes192-ctr aes128-ctr], algorithms[:encryption] + assert_equal %w[hmac-sha2-512-...@openssh.com hmac-sha2-256-...@openssh.com hmac-sha2-512 hmac-sha2-256 hmac-sha1], algorithms[:hmac] +@@ -27,7 +27,7 @@ module Transport + end + + def test_constructor_should_build_complete_list_of_algorithms_with_append_all_supported_algorithms +- assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa ssh-dss], algorithms(append_all_supported_algorithms: true)[:host_key] ++ assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512 ssh-dss], algorithms(append_all_supported_algorithms: true)[:host_key] + assert_equal x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1 diffie-hellman-group-exchange-sha1 diffie-hellman-group1-sha1], algorithms(append_all_supported_algorithms: true)[:kex] + assert_equal %w[aes256-ctr aes192-ctr aes128-ctr aes256-cbc aes192-cbc aes128-cbc rijndael-...@lysator.liu.se blowfish-ctr blowfish-cbc cast128-ctr cast128-cbc 3des-ctr 3des-cbc idea-cbc none], algorithms(append_all_supported_algorithms: true)[:encryption] + assert_equal %w[hmac-sha2-512-...@openssh.com hmac-sha2-256-...@openssh.com hmac-sha2-512 hmac-sha2-256 hmac-sha1 hmac-sha2-512-96 hmac-sha2-256-96 hmac-sha1-96 hmac-ripemd160 hmac-ripemd...@openssh.com hmac-md5 hmac-md5-96 none], algorithms(append_all_supported_algorithms: true)[:hmac] +@@ -43,12 +43,12 @@ module Transport + end + + def test_constructor_with_preferred_host_key_type_should_put_preferred_host_key_type_first +- assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms(host_key: "ssh-dss", append_all_supported_algorithms: true)[:host_key] ++ assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms(host_key: "ssh-dss", append_all_supported_algorithms: true)[:host_key] + end + + def test_constructor_with_known_hosts_reporting_known_host_key_should_use_that_host_key_type + Net::SSH::KnownHosts.expects(:search_for).with("net.ssh.test,127.0.0.1", {}).returns([stub("key", ssh_type: "ssh-dss")]) +- assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms[:host_key] ++ assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms[:host_key] + end + + def ed_host_keys +@@ -73,7 +73,7 @@ module Transport + end + + def test_constructor_with_unrecognized_host_key_type_should_return_whats_supported +- assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa ssh-dss], ++ assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512 ssh-dss], + algorithms(host_key: "bogus ssh-rsa", append_all_supported_algorithms: true)[:host_key] + end + +@@ -182,7 +182,7 @@ module Transport + end + + def test_constructor_with_host_key_removals_with_wildcard +- assert_equal ed_host_keys + %w[ecdsa-sha2-nistp521-cert-...@openssh.com ecdsa-sha2-nistp384-cert-...@openssh.com ecdsa-sha2-nistp256-cert-...@openssh.com ecdsa-sha2-nistp521 ecdsa-sha2-nistp384 ecdsa-sha2-nistp256], algorithms(host_key: %w[-ssh-rsa* -ssh-dss])[:host_key] ++ assert_equal ed_host_keys + %w[ecdsa-sha2-nistp521-cert-...@openssh.com ecdsa-sha2-nistp384-cert-...@openssh.com ecdsa-sha2-nistp256-cert-...@openssh.com ecdsa-sha2-nistp521 ecdsa-sha2-nistp384 ecdsa-sha2-nistp256], algorithms(host_key: %w[-ssh-rsa* -ssh-dss -rsa-sha*])[:host_key] + end + + def test_initial_state_should_be_neither_pending_nor_initialized +@@ -407,7 +407,7 @@ module Transport + assert_equal KEXINIT, buffer.type + assert_equal 16, buffer.read(16).length + assert_equal options[:kex] || (x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1]).join(','), buffer.read_string +- assert_equal options[:host_key] || (ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa]).join(','), buffer.read_string ++ assert_equal options[:host_key] || (ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512]).join(','), buffer.read_string + assert_equal options[:encryption_client] || 'aes256-ctr,aes192-ctr,aes128-ctr', buffer.read_string + assert_equal options[:encryption_server] || 'aes256-ctr,aes192-ctr,aes128-ctr', buffer.read_string + assert_equal options[:hmac_client] || 'hmac-sha2-512-...@openssh.com,hmac-sha2-256-...@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-sha1', buffer.read_string diff --git a/debian/patches/series b/debian/patches/series new file mode 100644 index 00000000..589995b5 --- /dev/null +++ b/debian/patches/series @@ -0,0 +1,2 @@ +0001-Added-support-for-RSA-client-authentication-with-SHA.patch +0002-Implemented-rsa-sha2-512-rsa-sha2-256-host_key-algs.patch
From: Zdenek Zambersky <zzamb...@redhat.com> Date: Thu, 6 May 2021 13:50:20 +0200 Subject: Added support for RSA client authentication with SHA-2 Source: https://github.com/net-ssh/net-ssh/pull/838 Backported-By: Antonio Terceiro <terce...@debian.org> --- lib/net/ssh/authentication/certificate.rb | 4 +- lib/net/ssh/authentication/ed25519.rb | 2 +- lib/net/ssh/authentication/key_manager.rb | 22 ++++++-- lib/net/ssh/authentication/methods/abstract.rb | 10 ++++ lib/net/ssh/authentication/methods/publickey.rb | 66 ++++++++++++++++++----- lib/net/ssh/authentication/session.rb | 5 +- lib/net/ssh/transport/openssl.rb | 16 ++++-- test/authentication/methods/test_publickey.rb | 71 ++++++++++++++++++++++--- test/authentication/test_session.rb | 6 ++- 9 files changed, 169 insertions(+), 33 deletions(-) diff --git a/lib/net/ssh/authentication/certificate.rb b/lib/net/ssh/authentication/certificate.rb index 82e37e9..95b01ff 100644 --- a/lib/net/ssh/authentication/certificate.rb +++ b/lib/net/ssh/authentication/certificate.rb @@ -65,8 +65,8 @@ module Net ).to_s end - def ssh_do_sign(data) - key.ssh_do_sign(data) + def ssh_do_sign(data, sig_alg = nil) + key.ssh_do_sign(data, sig_alg) end def ssh_do_verify(sig, data) diff --git a/lib/net/ssh/authentication/ed25519.rb b/lib/net/ssh/authentication/ed25519.rb index 0c5530c..1989d1f 100644 --- a/lib/net/ssh/authentication/ed25519.rb +++ b/lib/net/ssh/authentication/ed25519.rb @@ -167,7 +167,7 @@ module Net PubKey.new(@pk) end - def ssh_do_sign(data) + def ssh_do_sign(data, sig_alg = nil) @sign_key.sign(data) end diff --git a/lib/net/ssh/authentication/key_manager.rb b/lib/net/ssh/authentication/key_manager.rb index 242d5d5..3624550 100644 --- a/lib/net/ssh/authentication/key_manager.rb +++ b/lib/net/ssh/authentication/key_manager.rb @@ -159,7 +159,7 @@ module Net # Regardless of the identity's origin or who does the signing, this # will always return the signature in an SSH2-specified "signature # blob" format. - def sign(identity, data) + def sign(identity, data, sig_alg = nil) info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager" if info[:key].nil? && info[:from] == :file @@ -171,13 +171,27 @@ module Net end if info[:key] - return Net::SSH::Buffer.from(:string, identity.ssh_signature_type, - :mstring, info[:key].ssh_do_sign(data.to_s)).to_s + if sig_alg.nil? + signed = info[:key].ssh_do_sign(data.to_s) + sig_alg = identity.ssh_signature_type + else + signed = info[:key].ssh_do_sign(data.to_s, sig_alg) + end + return Net::SSH::Buffer.from(:string, sig_alg, + :mstring, signed).to_s end if info[:from] == :agent raise KeyManagerError, "the agent is no longer available" unless agent - return agent.sign(info[:identity], data.to_s) + + case sig_alg + when "rsa-sha2-512" + return agent.sign(info[:identity], data.to_s, Net::SSH::Authentication::Agent::SSH_AGENT_RSA_SHA2_512) + when "rsa-sha2-256" + return agent.sign(info[:identity], data.to_s, Net::SSH::Authentication::Agent::SSH_AGENT_RSA_SHA2_256) + else + return agent.sign(info[:identity], data.to_s) + end end raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})" diff --git a/lib/net/ssh/authentication/methods/abstract.rb b/lib/net/ssh/authentication/methods/abstract.rb index bcddd4f..8ec4d22 100644 --- a/lib/net/ssh/authentication/methods/abstract.rb +++ b/lib/net/ssh/authentication/methods/abstract.rb @@ -21,12 +21,22 @@ module Net # this. attr_reader :key_manager + # So far only affects algorithms used for rsa keys, but can be + # extended to other keys, e.g after reading of + # PubkeyAcceptedAlgorithms option from ssh_config file is implemented. + attr_reader :pubkey_algorithms + # Instantiates a new authentication method. def initialize(session, options={}) @session = session @key_manager = options[:key_manager] @options = options @prompt = options[:password_prompt] + @pubkey_algorithms = options[:pubkey_algorithms] \ + || %w[rsa-sha2-256-cert-...@openssh.com + ssh-rsa-cert-...@openssh.com + rsa-sha2-256 + ssh-rsa] self.logger = session.logger end diff --git a/lib/net/ssh/authentication/methods/publickey.rb b/lib/net/ssh/authentication/methods/publickey.rb index bff9ffd..7e9e422 100644 --- a/lib/net/ssh/authentication/methods/publickey.rb +++ b/lib/net/ssh/authentication/methods/publickey.rb @@ -27,41 +27,40 @@ module Net # Builds a packet that contains the request formatted for sending # a public-key request to the server. - def build_request(pub_key, username, next_service, has_sig) + def build_request(pub_key, username, next_service, alg, has_sig) blob = Net::SSH::Buffer.new blob.write_key pub_key userauth_request(username, next_service, "publickey", has_sig, - pub_key.ssh_type, blob.to_s) + alg, blob.to_s) end # Builds and sends a request formatted for a public-key # authentication request. - def send_request(pub_key, username, next_service, signature=nil) - msg = build_request(pub_key, username, next_service, !signature.nil?) + def send_request(pub_key, username, next_service, alg, signature = nil) + msg = build_request(pub_key, username, next_service, alg, + !signature.nil?) msg.write_string(signature) if signature send_message(msg) end - # Attempts to perform public-key authentication for the given - # username, with the given identity (public key). Returns +true+ if - # successful, or +false+ otherwise. - def authenticate_with(identity, next_service, username) + def authenticate_with_alg(identity, next_service, username, alg, sig_alg = nil) debug { "trying publickey (#{identity.fingerprint})" } - send_request(identity, username, next_service) + send_request(identity, username, next_service, alg) message = session.next_message case message.type when USERAUTH_PK_OK - buffer = build_request(identity, username, next_service, true) + buffer = build_request(identity, username, next_service, alg, + true) sig_data = Net::SSH::Buffer.new sig_data.write_string(session_id) sig_data.append(buffer.to_s) - sig_blob = key_manager.sign(identity, sig_data) + sig_blob = key_manager.sign(identity, sig_data, sig_alg) - send_request(identity, username, next_service, sig_blob.to_s) + send_request(identity, username, next_service, alg, sig_blob.to_s) message = session.next_message case message.type @@ -89,6 +88,49 @@ module Net raise Net::SSH::Exception, "unexpected reply to USERAUTH_REQUEST: #{message.type} (#{message.inspect})" end end + + # Attempts to perform public-key authentication for the given + # username, with the given identity (public key). Returns +true+ if + # successful, or +false+ otherwise. + def authenticate_with(identity, next_service, username) + type = identity.ssh_type + if type == "ssh-rsa" + pubkey_algorithms.each do |pk_alg| + case pk_alg + when "rsa-sha2-512", "rsa-sha2-256", "ssh-rsa" + if authenticate_with_alg(identity, next_service, username, pk_alg, pk_alg) + # success + return true + end + end + end + elsif type == "ssh-rsa-cert-...@openssh.com" + pubkey_algorithms.each do |pk_alg| + case pk_alg + when "rsa-sha2-512-cert-...@openssh.com" + if authenticate_with_alg(identity, next_service, username, pk_alg, "rsa-sha2-512") + # success + return true + end + when "rsa-sha2-256-cert-...@openssh.com" + if authenticate_with_alg(identity, next_service, username, pk_alg, "rsa-sha2-256") + # success + return true + end + when "ssh-rsa-cert-...@openssh.com" + if authenticate_with_alg(identity, next_service, username, pk_alg) + # success + return true + end + end + end + elsif authenticate_with_alg(identity, next_service, username, type) + # success + return true + end + # failure + return false + end end end diff --git a/lib/net/ssh/authentication/session.rb b/lib/net/ssh/authentication/session.rb index dfc5c06..a524c52 100644 --- a/lib/net/ssh/authentication/session.rb +++ b/lib/net/ssh/authentication/session.rb @@ -77,7 +77,10 @@ module Net debug { "trying #{name}" } begin auth_class = Methods.const_get(name.split(/\W+/).map { |p| p.capitalize }.join) - method = auth_class.new(self, key_manager: key_manager, password_prompt: options[:password_prompt]) + method = auth_class.new(self, + key_manager: key_manager, password_prompt: options[:password_prompt], + pubkey_algorithms: options[:pubkey_algorithms] || nil) + rescue NameError debug {"Mechanism #{name} was requested, but isn't a known type. Ignoring it."} next diff --git a/lib/net/ssh/transport/openssl.rb b/lib/net/ssh/transport/openssl.rb index d2d7117..3f10b02 100644 --- a/lib/net/ssh/transport/openssl.rb +++ b/lib/net/ssh/transport/openssl.rb @@ -68,8 +68,16 @@ module OpenSSL end # Returns the signature for the given data. - def ssh_do_sign(data) - sign(OpenSSL::Digest::SHA1.new, data) + def ssh_do_sign(data, sig_alg = nil) + digester = + if sig_alg == "rsa-sha2-512" + OpenSSL::Digest::SHA512.new + elsif sig_alg == "rsa-sha2-256" + OpenSSL::Digest::SHA256.new + else + OpenSSL::Digest::SHA1.new + end + sign(digester, data) end end @@ -105,7 +113,7 @@ module OpenSSL end # Signs the given data. - def ssh_do_sign(data) + def ssh_do_sign(data, sig_alg = nil) sig = sign(OpenSSL::Digest::SHA1.new, data) a1sig = OpenSSL::ASN1.decode(sig) @@ -218,7 +226,7 @@ module OpenSSL end # Returns the signature for the given data. - def ssh_do_sign(data) + def ssh_do_sign(data, sig_alg = nil) digest = digester.digest(data) sig = dsa_sign_asn1(digest) a1sig = OpenSSL::ASN1.decode(sig) diff --git a/test/authentication/methods/test_publickey.rb b/test/authentication/methods/test_publickey.rb index 8f2cc73..05510f2 100644 --- a/test/authentication/methods/test_publickey.rb +++ b/test/authentication/methods/test_publickey.rb @@ -105,6 +105,50 @@ module Authentication assert subject.authenticate("ssh-connection", "jamis") end + def test_authenticate_rsa_sha2 + key_manager.expects(:sign).with(&signature_parameters_with_alg(keys.first, "rsa-sha2-256")).returns("sig-one") + + transport.expect do |t, packet| + assert_equal USERAUTH_REQUEST, packet.type + assert verify_userauth_request_packet(packet, keys.first, false, "rsa-sha2-256") + t.return(USERAUTH_PK_OK, :string, "rsa-sha2-256", :string, Net::SSH::Buffer.from(:key, keys.first)) + + t.expect do |t2, packet2| + assert_equal USERAUTH_REQUEST, packet2.type + assert verify_userauth_request_packet(packet2, keys.first, true, "rsa-sha2-256") + assert_equal "sig-one", packet2.read_string + t2.return(USERAUTH_SUCCESS) + end + end + + assert subject(pubkey_algorithms: %w[rsa-sha2-256]).authenticate("ssh-connection", "jamis") + end + + def test_authenticate_rsa_sha2_fallback + key_manager.expects(:sign).with(&signature_parameters(keys.first)).returns("sig-one") + + transport.expect do |t, packet| + assert_equal USERAUTH_REQUEST, packet.type + assert verify_userauth_request_packet(packet, keys.first, false, "rsa-sha2-256") + t.return(USERAUTH_FAILURE, :string, "publickey") + + t.expect do |t2, packet2| + assert_equal USERAUTH_REQUEST, packet2.type + assert verify_userauth_request_packet(packet2, keys.first, false) + t2.return(USERAUTH_PK_OK, :string, keys.first.ssh_type, :string, Net::SSH::Buffer.from(:key, keys.first)) + + t2.expect do |t3, packet3| + assert_equal USERAUTH_REQUEST, packet3.type + assert verify_userauth_request_packet(packet3, keys.first, true) + assert_equal "sig-one", packet3.read_string + t3.return(USERAUTH_SUCCESS) + end + end + end + + assert subject(pubkey_algorithms: %w[rsa-sha2-256 ssh-rsa]).authenticate("ssh-connection", "jamis") + end + private def signature_parameters(key) @@ -117,13 +161,25 @@ module Authentication end end - def verify_userauth_request_packet(packet, key, has_sig) - packet.read_string == "jamis" && # user-name - packet.read_string == "ssh-connection" && # next service - packet.read_string == "publickey" && # auth-method - packet.read_bool == has_sig && # whether a signature is appended - packet.read_string == key.ssh_type && # ssh key type - packet.read_buffer.read_key.to_blob == key.to_blob # key + def signature_parameters_with_alg(key, alg) + Proc.new do |given_key, data, given_alg| + next false unless given_key.to_blob == key.to_blob + next false unless given_alg == alg + + buffer = Net::SSH::Buffer.new(data) + buffer.read_string == "abcxyz123" && # session-id + buffer.read_byte == USERAUTH_REQUEST && # type + verify_userauth_request_packet(buffer, key, true, alg) + end + end + + def verify_userauth_request_packet(packet, key, has_sig, alg = nil) + packet.read_string == "jamis" && # user-name + packet.read_string == "ssh-connection" && # next service + packet.read_string == "publickey" && # auth-method + packet.read_bool == has_sig && # whether a signature is appended + packet.read_string == (alg || key.ssh_type) && # ssh key type + packet.read_buffer.read_key.to_blob == key.to_blob # key end @@keys = nil @@ -141,6 +197,7 @@ module Authentication def subject(options={}) options[:key_manager] = key_manager(options) unless options.key?(:key_manager) + options[:pubkey_algorithms] = %w[ssh-rsa] unless options.key?(:pubkey_algorithms) @subject ||= Net::SSH::Authentication::Methods::Publickey.new(session(options), options) end end diff --git a/test/authentication/test_session.rb b/test/authentication/test_session.rb index db37467..19b9f8a 100644 --- a/test/authentication/test_session.rb +++ b/test/authentication/test_session.rb @@ -168,8 +168,10 @@ module Authentication private - def session(options={}) - @session ||= Net::SSH::Authentication::Session.new(transport(options), options) + def session(options = {}) + session_opts = options.clone + session_opts[:pubkey_algorithms] = %w[ssh-rsa] + @session ||= Net::SSH::Authentication::Session.new(transport(options), session_opts) end def transport(options={})
From: Miklos Fazekas <mfaze...@szemafor.com> Date: Wed, 10 Jun 2020 08:17:44 +0200 Subject: Implemented rsa-sha2-512, rsa-sha2-256 host_key algs This patch is a combination of the 4 commits in the Pull Request mentioned below. Source: https://github.com/net-ssh/net-ssh/pull/771 Backported-By: Antonio Terceiro <terce...@debian.org> --- .gitignore | 2 ++ lib/net/ssh/authentication/certificate.rb | 4 ++-- lib/net/ssh/authentication/ed25519.rb | 2 +- lib/net/ssh/transport/algorithms.rb | 4 +++- lib/net/ssh/transport/kex/abstract.rb | 11 +++++++++-- lib/net/ssh/transport/openssl.rb | 17 +++++++++++++---- test/integration/Vagrantfile | 5 +++-- test/integration/common.rb | 10 +++++++++- test/integration/playbook.yml | 11 +++++++++-- test/transport/kex/test_ecdh_sha2_nistp256.rb | 2 +- test/transport/test_algorithms.rb | 14 +++++++------- 11 files changed, 59 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index b02057e..acb2b39 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ test/integration/.vagrant test/integration/playbook.retry .byebug_history + +tryout diff --git a/lib/net/ssh/authentication/certificate.rb b/lib/net/ssh/authentication/certificate.rb index 95b01ff..ff22eb4 100644 --- a/lib/net/ssh/authentication/certificate.rb +++ b/lib/net/ssh/authentication/certificate.rb @@ -69,8 +69,8 @@ module Net key.ssh_do_sign(data, sig_alg) end - def ssh_do_verify(sig, data) - key.ssh_do_verify(sig, data) + def ssh_do_verify(sig, data, options = {}) + key.ssh_do_verify(sig, data, options) end def to_pem diff --git a/lib/net/ssh/authentication/ed25519.rb b/lib/net/ssh/authentication/ed25519.rb index 1989d1f..5f3b83b 100644 --- a/lib/net/ssh/authentication/ed25519.rb +++ b/lib/net/ssh/authentication/ed25519.rb @@ -123,7 +123,7 @@ module Net ssh_type end - def ssh_do_verify(sig,data) + def ssh_do_verify(sig, data, options = {}) @verify_key.verify(sig,data) end diff --git a/lib/net/ssh/transport/algorithms.rb b/lib/net/ssh/transport/algorithms.rb index 8126aa6..9ab87b6 100644 --- a/lib/net/ssh/transport/algorithms.rb +++ b/lib/net/ssh/transport/algorithms.rb @@ -33,7 +33,9 @@ module Net ecdsa-sha2-nistp256 ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com - ssh-rsa], + ssh-rsa + rsa-sha2-256 + rsa-sha2-512], kex: %w[ecdh-sha2-nistp521 ecdh-sha2-nistp384 diff --git a/lib/net/ssh/transport/kex/abstract.rb b/lib/net/ssh/transport/kex/abstract.rb index c70eb94..3fd8c3c 100644 --- a/lib/net/ssh/transport/kex/abstract.rb +++ b/lib/net/ssh/transport/kex/abstract.rb @@ -64,11 +64,16 @@ module Net private + def matching?(key_ssh_type, host_key_alg) + return true if key_ssh_type == host_key_alg + return true if key_ssh_type == 'ssh-rsa' && ['rsa-sha2-512', 'rsa-sha2-256'].include?(host_key_alg) + end + # Verify that the given key is of the expected type, and that it # really is the key for the session's host. Raise Net::SSH::Exception # if it is not. def verify_server_key(key) #:nodoc: - if key.ssh_type != algorithms.host_key + unless matching?(key.ssh_type, algorithms.host_key) raise Net::SSH::Exception, "host key algorithm mismatch '#{key.ssh_type}' != '#{algorithms.host_key}'" end @@ -97,7 +102,9 @@ module Net hash = digester.digest(response.to_s) - unless connection.host_key_verifier.verify_signature { result[:server_key].ssh_do_verify(result[:server_sig], hash) } + server_key = result[:server_key] + server_sig = result[:server_sig] + unless connection.host_key_verifier.verify_signature { server_key.ssh_do_verify(server_sig, hash, host_key: algorithms.host_key) } raise Net::SSH::Exception, 'could not verify server signature' end diff --git a/lib/net/ssh/transport/openssl.rb b/lib/net/ssh/transport/openssl.rb index 3f10b02..e995ad6 100644 --- a/lib/net/ssh/transport/openssl.rb +++ b/lib/net/ssh/transport/openssl.rb @@ -63,8 +63,17 @@ module OpenSSL end # Verifies the given signature matches the given data. - def ssh_do_verify(sig, data) - verify(OpenSSL::Digest::SHA1.new, sig, data) + def ssh_do_verify(sig, data, options = {}) + digester = + if options[:host_key] == "rsa-sha2-512" + OpenSSL::Digest::SHA512.new + elsif options[:host_key] == "rsa-sha2-256" + OpenSSL::Digest::SHA256.new + else + OpenSSL::Digest::SHA1.new + end + + verify(digester, sig, data) end # Returns the signature for the given data. @@ -102,7 +111,7 @@ module OpenSSL end # Verifies the given signature matches the given data. - def ssh_do_verify(sig, data) + def ssh_do_verify(sig, data, options = {}) sig_r = sig[0,20].unpack("H*")[0].to_i(16) sig_s = sig[20,20].unpack("H*")[0].to_i(16) a1sig = OpenSSL::ASN1::Sequence([ @@ -200,7 +209,7 @@ module OpenSSL end # Verifies the given signature matches the given data. - def ssh_do_verify(sig, data) + def ssh_do_verify(sig, data, options = {}) digest = digester.digest(data) a1sig = nil diff --git a/test/integration/Vagrantfile b/test/integration/Vagrantfile index 2c42d9a..2327e5d 100644 --- a/test/integration/Vagrantfile +++ b/test/integration/Vagrantfile @@ -1,10 +1,11 @@ VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.box = "ubuntu/trusty64" + config.vm.box = "ubuntu/bionic64" config.vm.provision "ansible" do |ansible| ansible.playbook = "./playbook.yml" - ansible.sudo = true + ansible.become = true + ansible.become_user = 'root' ansible.verbose = 'vvvv' ansible.compatibility_mode = "2.0" end diff --git a/test/integration/common.rb b/test/integration/common.rb index 6295ada..8fe0881 100644 --- a/test/integration/common.rb +++ b/test/integration/common.rb @@ -110,7 +110,15 @@ module IntegrationTestHelpers # down sshd. if pid system('sudo', 'kill', '-15', pid.to_s) - Process.wait(pid) + begin + Timeout.timeout(5) do + Process.wait(pid) + end + rescue Timeout::Error + warn "Failed to kill net-ssh process: #{pid}" + system('sudo', 'kill', '-9', pid) + raise + end end end diff --git a/test/integration/playbook.yml b/test/integration/playbook.yml index 47eba34..6d79a54 100644 --- a/test/integration/playbook.yml +++ b/test/integration/playbook.yml @@ -93,13 +93,20 @@ - name: add host aliases lineinfile: dest='/etc/hosts' owner='root' group='root' mode=0644 regexp='^127\.0\.0\.1\s+gateway.netssh' line='127.0.0.1 gateway.netssh' - - apt: + - name: Update APT Cache + apt: + update_cache: yes + force_apt_get: yes + - name: Wait for locfile removal + become: yes + shell: while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done; + - name: Install packages + apt: pkg: - pv - libgmp3-dev - git state: present - update_cache: yes - copy: content='echo "cd /net-ssh ; rake integration-test"' dest=/etc/update-motd.d/99-net-ssh-tests mode=0755 - name: add user to rvm group so they can change gem wrappers user: diff --git a/test/transport/kex/test_ecdh_sha2_nistp256.rb b/test/transport/kex/test_ecdh_sha2_nistp256.rb index 932d8d7..3489230 100644 --- a/test/transport/kex/test_ecdh_sha2_nistp256.rb +++ b/test/transport/kex/test_ecdh_sha2_nistp256.rb @@ -1,7 +1,7 @@ require 'openssl' require 'ostruct' require_relative '../../common' -require 'transport/kex/test_diffie_hellman_group1_sha1' +require_relative './test_diffie_hellman_group1_sha1' require 'net/ssh/transport/kex/ecdh_sha2_nistp256' module Transport diff --git a/test/transport/test_algorithms.rb b/test/transport/test_algorithms.rb index 105f3af..aac8b9a 100644 --- a/test/transport/test_algorithms.rb +++ b/test/transport/test_algorithms.rb @@ -18,7 +18,7 @@ module Transport end def test_constructor_should_build_default_list_of_preferred_algorithms - assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms[:host_key] + assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms[:host_key] assert_equal x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1], algorithms[:kex] assert_equal %w[aes256-ctr aes192-ctr aes128-ctr], algorithms[:encryption] assert_equal %w[hmac-sha2-512-...@openssh.com hmac-sha2-256-...@openssh.com hmac-sha2-512 hmac-sha2-256 hmac-sha1], algorithms[:hmac] @@ -27,7 +27,7 @@ module Transport end def test_constructor_should_build_complete_list_of_algorithms_with_append_all_supported_algorithms - assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa ssh-dss], algorithms(append_all_supported_algorithms: true)[:host_key] + assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512 ssh-dss], algorithms(append_all_supported_algorithms: true)[:host_key] assert_equal x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1 diffie-hellman-group-exchange-sha1 diffie-hellman-group1-sha1], algorithms(append_all_supported_algorithms: true)[:kex] assert_equal %w[aes256-ctr aes192-ctr aes128-ctr aes256-cbc aes192-cbc aes128-cbc rijndael-...@lysator.liu.se blowfish-ctr blowfish-cbc cast128-ctr cast128-cbc 3des-ctr 3des-cbc idea-cbc none], algorithms(append_all_supported_algorithms: true)[:encryption] assert_equal %w[hmac-sha2-512-...@openssh.com hmac-sha2-256-...@openssh.com hmac-sha2-512 hmac-sha2-256 hmac-sha1 hmac-sha2-512-96 hmac-sha2-256-96 hmac-sha1-96 hmac-ripemd160 hmac-ripemd...@openssh.com hmac-md5 hmac-md5-96 none], algorithms(append_all_supported_algorithms: true)[:hmac] @@ -43,12 +43,12 @@ module Transport end def test_constructor_with_preferred_host_key_type_should_put_preferred_host_key_type_first - assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms(host_key: "ssh-dss", append_all_supported_algorithms: true)[:host_key] + assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms(host_key: "ssh-dss", append_all_supported_algorithms: true)[:host_key] end def test_constructor_with_known_hosts_reporting_known_host_key_should_use_that_host_key_type Net::SSH::KnownHosts.expects(:search_for).with("net.ssh.test,127.0.0.1", {}).returns([stub("key", ssh_type: "ssh-dss")]) - assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa], algorithms[:host_key] + assert_equal %w[ssh-dss] + ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512], algorithms[:host_key] end def ed_host_keys @@ -73,7 +73,7 @@ module Transport end def test_constructor_with_unrecognized_host_key_type_should_return_whats_supported - assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa ssh-dss], + assert_equal ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512 ssh-dss], algorithms(host_key: "bogus ssh-rsa", append_all_supported_algorithms: true)[:host_key] end @@ -182,7 +182,7 @@ module Transport end def test_constructor_with_host_key_removals_with_wildcard - assert_equal ed_host_keys + %w[ecdsa-sha2-nistp521-cert-...@openssh.com ecdsa-sha2-nistp384-cert-...@openssh.com ecdsa-sha2-nistp256-cert-...@openssh.com ecdsa-sha2-nistp521 ecdsa-sha2-nistp384 ecdsa-sha2-nistp256], algorithms(host_key: %w[-ssh-rsa* -ssh-dss])[:host_key] + assert_equal ed_host_keys + %w[ecdsa-sha2-nistp521-cert-...@openssh.com ecdsa-sha2-nistp384-cert-...@openssh.com ecdsa-sha2-nistp256-cert-...@openssh.com ecdsa-sha2-nistp521 ecdsa-sha2-nistp384 ecdsa-sha2-nistp256], algorithms(host_key: %w[-ssh-rsa* -ssh-dss -rsa-sha*])[:host_key] end def test_initial_state_should_be_neither_pending_nor_initialized @@ -407,7 +407,7 @@ module Transport assert_equal KEXINIT, buffer.type assert_equal 16, buffer.read(16).length assert_equal options[:kex] || (x25519_kex + ec_kex + %w[diffie-hellman-group-exchange-sha256 diffie-hellman-group14-sha1]).join(','), buffer.read_string - assert_equal options[:host_key] || (ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa]).join(','), buffer.read_string + assert_equal options[:host_key] || (ed_ec_host_keys + %w[ssh-rsa-cert-...@openssh.com ssh-rsa-cert-...@openssh.com ssh-rsa rsa-sha2-256 rsa-sha2-512]).join(','), buffer.read_string assert_equal options[:encryption_client] || 'aes256-ctr,aes192-ctr,aes128-ctr', buffer.read_string assert_equal options[:encryption_server] || 'aes256-ctr,aes192-ctr,aes128-ctr', buffer.read_string assert_equal options[:hmac_client] || 'hmac-sha2-512-...@openssh.com,hmac-sha2-256-...@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-sha1', buffer.read_string
signature.asc
Description: PGP signature