Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aes(128|256)-gcm support #845

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

aes(128|256)-gcm support #845

wants to merge 3 commits into from

Conversation

Slokilla
Copy link

@Slokilla Slokilla commented Jan 8, 2022

I've added the support for the GCM algorithmes, whoch actually implies the support of AEAD algorithms.

Used cipher name are :

Example call:

Net::SSH.start(serverIp, username, password: pass, encryption:'aes128-gcm@openssh.com') do |ssh|
  puts ssh.exec!('hostname')
end

or

Net::SSH.start(serverIp, username, password: pass, encryption:'aes256-gcm@openssh.com') do |ssh|
  puts ssh.exec!('hostname')
end

I think the more sensitive point is the implementation of the GCM iv/counter, I really need you to pay a special intention to it during review.

Close #834

@Slokilla
Copy link
Author

Could someone review this please ? Maybe @mfazekas ?

@Slokilla Slokilla marked this pull request as draft January 18, 2022 14:56
@Slokilla
Copy link
Author

Maybe i'll need some explanations on integration tests cause i can't reproduce fails on my local setup.

@Slokilla Slokilla marked this pull request as ready for review January 21, 2022 10:04
@mfazekas
Copy link
Collaborator

Maybe i'll need some explanations on integration tests cause i can't reproduce fails on my local setup.

There are some instructions to getting integration tests running with vagrant:
https://github.com/net-ssh/net-ssh/tree/master/test/integration

@Slokilla
Copy link
Author

Slokilla commented Jan 22, 2022

Maybe i'll need some explanations on integration tests cause i can't reproduce fails on my local setup.

There are some instructions to getting integration tests running with vagrant: https://github.com/net-ssh/net-ssh/tree/master/test/integration

I'm aware about the instructions, and I've succeeded to run tests on my server. But actually, both integration and unit tests runs successfully. Or at least it looks like.

If someone have some time to checkout my branch and to try to run tests, it would be helpful. In the top of that, if the CI could be more verbose, it would help me a lot.

@mfazekas
Copy link
Collaborator

Maybe i'll need some explanations on integration tests cause i can't reproduce fails on my local setup.

There are some instructions to getting integration tests running with vagrant: https://github.com/net-ssh/net-ssh/tree/master/test/integration

I'm aware about the instructions, and I've succeeded to run tests on my server. But actually, both integration and unit tests runs successfully. Or at least it looks like.

If someone have some time to checkout my branch and to try to run tests, it would be helpful. In the top of that, if the CI could be more verbose, it would help me a lot.

@Slokilla when I'm running your branch I get a lot of errors locally as well:

vagrant@ubuntu-bionic:/net-ssh$ TESTOPS=-v NET_SSH_RUN_INTEGRATION_TESTS=1 bundle exec rake test 
Mocha deprecation warning at /net-ssh/test/integration/common.rb:4:in `require': Require 'mocha/test_unit', 'mocha/minitest' or 'mocha/api' instead of 'mocha/setup'.
Skipping packet stream test for idea-cbc
Skipping packet stream test for aes256-gcm@openssh.com
Skipping packet stream test for aes128-gcm@openssh.com
Run options: --seed 28138

# Running:

.............................................................................................................................................................................EEnter passphrase for /tmp/d20220227-8237-wr4sco/id_rsa_ed25519:
EEE............EE............................................................................................................................................/tmp/sshd_config20220227-8237-14ull6q line 84: Unsupported option UsePAM
E/tmp/sshd_config20220227-8237-2eyotw line 84: Unsupported option UsePAM
EEE........EEEEnter passphrase for /tmp/d20220227-8237-orof12/id_rsa:
EEnter passphrase for /tmp/d20220227-8237-qjqtod/id_rsa:
E.......EE.....^Crake aborted!
Interrupt: 
/usr/local/rvm/gems/ruby-2.7.1/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)
vagrant@ubuntu-bionic:/net-ssh$ Interrupted. Exiting...


Finished in 60.315224s, 6.0018 runs/s, 14.9879 assertions/s.

  1) Error:
TestED25519PKeys#test_in_file_with_password:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:55:in `block in test_in_file_with_password'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:47:in `test_in_file_with_password'

  2) Error:
TestED25519PKeys#test_ssh_agent:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:38:in `block (2 levels) in test_ssh_agent'
    /net-ssh/test/integration/common.rb:45:in `with_agent'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:29:in `block in test_ssh_agent'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:28:in `test_ssh_agent'

  3) Error:
TestED25519PKeys#test_in_file_no_password:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:20:in `block in test_in_file_no_password'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:15:in `test_in_file_no_password'

  4) Error:
TestED25519PKeys#test_with_only_ed25519_host_key:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:75:in `block (2 levels) in test_with_only_ed25519_host_key'
    /net-ssh/test/integration/common.rb:83:in `with_sshd_config'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:74:in `block in test_with_only_ed25519_host_key'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tempfile.rb:291:in `open'
    /net-ssh/test/integration/test_ed25519_pkeys.rb:72:in `test_with_only_ed25519_host_key'

  5) Error:
TestForwardOnUnixSockets#test_forward_local_unix_socket_to_remote_port:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_forward.rb:586:in `block in test_forward_local_unix_socket_to_remote_port'
    /net-ssh/test/integration/test_forward.rb:45:in `block in setup_ssh_env'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_forward.rb:40:in `setup_ssh_env'
    /net-ssh/test/integration/test_forward.rb:585:in `test_forward_local_unix_socket_to_remote_port'

  6) Error:
TestForwardOnUnixSockets#test_forward_local_unix_socket_to_remote_socket:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_forward.rb:624:in `block (3 levels) in test_forward_local_unix_socket_to_remote_socket'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:95:in `block in timeout'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `block in catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:110:in `timeout'
    /net-ssh/test/integration/test_forward.rb:623:in `block (2 levels) in test_forward_local_unix_socket_to_remote_socket'
    /net-ssh/test/integration/common.rb:128:in `block in start_sshd_7_or_later'
    /net-ssh/test/integration/common.rb:101:in `block in with_lines_as_tempfile'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tempfile.rb:291:in `open'
    /net-ssh/test/integration/common.rb:91:in `with_lines_as_tempfile'
    /net-ssh/test/integration/common.rb:125:in `start_sshd_7_or_later'
    /net-ssh/test/integration/test_forward.rb:619:in `block in test_forward_local_unix_socket_to_remote_socket'
    /net-ssh/test/integration/test_forward.rb:45:in `block in setup_ssh_env'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_forward.rb:40:in `setup_ssh_env'
    /net-ssh/test/integration/test_forward.rb:618:in `test_forward_local_unix_socket_to_remote_socket'

  7) Error:
TestHMacEtm#test_with_only_hmac_etmetm256:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_hmac_etm.rb:39:in `block (4 levels) in <class:TestHMacEtm>'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:95:in `block in timeout'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `block in catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:110:in `timeout'
    /net-ssh/test/integration/test_hmac_etm.rb:36:in `block (3 levels) in <class:TestHMacEtm>'
    /net-ssh/test/integration/common.rb:122:in `block in start_sshd_7_or_later'
    /net-ssh/test/integration/common.rb:101:in `block in with_lines_as_tempfile'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tempfile.rb:291:in `open'
    /net-ssh/test/integration/common.rb:91:in `with_lines_as_tempfile'
    /net-ssh/test/integration/common.rb:114:in `start_sshd_7_or_later'
    /net-ssh/test/integration/test_hmac_etm.rb:35:in `block (2 levels) in <class:TestHMacEtm>'

  8) Error:
TestHMacEtm#test_with_only_hmac_etmetm512:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_hmac_etm.rb:39:in `block (4 levels) in <class:TestHMacEtm>'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:95:in `block in timeout'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `block in catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:33:in `catch'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/timeout.rb:110:in `timeout'
    /net-ssh/test/integration/test_hmac_etm.rb:36:in `block (3 levels) in <class:TestHMacEtm>'
    /net-ssh/test/integration/common.rb:122:in `block in start_sshd_7_or_later'
    /net-ssh/test/integration/common.rb:101:in `block in with_lines_as_tempfile'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tempfile.rb:291:in `open'
    /net-ssh/test/integration/common.rb:91:in `with_lines_as_tempfile'
    /net-ssh/test/integration/common.rb:114:in `start_sshd_7_or_later'
    /net-ssh/test/integration/test_hmac_etm.rb:35:in `block (2 levels) in <class:TestHMacEtm>'

  9) Error:
TestHMacEtm#test_hmac_through_proxy_etm256:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_hmac_etm.rb:73:in `block (2 levels) in <class:TestHMacEtm>'

 10) Error:
TestHMacEtm#test_hmac_through_proxy_etm512:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_hmac_etm.rb:73:in `block (2 levels) in <class:TestHMacEtm>'

 11) Error:
TestIDRSAPKeys#test_in_file_no_password:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_id_rsa_keys.rb:19:in `block in test_in_file_no_password'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_id_rsa_keys.rb:14:in `test_in_file_no_password'

 12) Error:
TestIDRSAPKeys#test_asks_for_passwords_when_read_from_memory:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_id_rsa_keys.rb:86:in `block in test_asks_for_passwords_when_read_from_memory'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_id_rsa_keys.rb:74:in `test_asks_for_passwords_when_read_from_memory'

 13) Error:
TestIDRSAPKeys#test_in_file_with_password:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_id_rsa_keys.rb:65:in `block in test_in_file_with_password'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_id_rsa_keys.rb:60:in `test_in_file_with_password'

 14) Error:
TestIDRSAPKeys#test_ssh_agent:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_id_rsa_keys.rb:35:in `block (2 levels) in test_ssh_agent'
    /net-ssh/test/integration/common.rb:45:in `with_agent'
    /net-ssh/test/integration/test_id_rsa_keys.rb:29:in `block in test_ssh_agent'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_id_rsa_keys.rb:28:in `test_ssh_agent'

 15) Error:
TestIDRSAPKeys#test_ssh_agent_ignores_if_already_in_agent:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_id_rsa_keys.rb:51:in `block (2 levels) in test_ssh_agent_ignores_if_already_in_agent'
    /net-ssh/test/integration/common.rb:45:in `with_agent'
    /net-ssh/test/integration/test_id_rsa_keys.rb:45:in `block in test_ssh_agent_ignores_if_already_in_agent'
    /net-ssh/test/integration/common.rb:19:in `block in tmpdir'
    /usr/local/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/tmpdir.rb:89:in `mktmpdir'
    /net-ssh/test/integration/common.rb:18:in `tmpdir'
    /net-ssh/test/integration/test_id_rsa_keys.rb:44:in `test_ssh_agent_ignores_if_already_in_agent'

 16) Error:
TestExec#test_error_exitstatus:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_exec.rb:8:in `test_error_exitstatus'

 17) Error:
TestExec#test_ok_exitstatus:
Net::SSH::Disconnect: connection closed by remote host
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
    /net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
    /net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
    /net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
    /net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
    /net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
    /net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
    /net-ssh/lib/net/ssh.rb:254:in `start'
    /net-ssh/test/integration/test_exec.rb:16:in `test_ok_exitstatus'

362 runs, 904 assertions, 0 failures, 17 errors, 0 skips

CI seems to timeout on a test that's deadlocked that's why we don't see any logs.

while master passes just fine

vagrant@ubuntu-bionic:/net-ssh$ TESTOPS=-v NET_SSH_RUN_INTEGRATION_TESTS=1 bundle exec rake test 
...
Finished in 52.294184s, 30.1563 runs/s, 102.0381 assertions/s.

1577 runs, 5336 assertions, 0 failures, 0 errors, 0 skips

@fwininger
Copy link
Collaborator

@Slokilla can we work on this on the next couple weeks?

@flux-johnm
Copy link

@Slokilla Is our assumption correct that this change would be supported for v6.0 and above?
@fwininger Is there anything else we can help on for this pull request to get approved?

@fwininger
Copy link
Collaborator

@flux-benj @mfazekas can you take a look ?

@fwininger fwininger requested review from mfazekas, flux-benj and fwininger and removed request for flux-benj May 20, 2023 12:50
Copy link
Collaborator

@mfazekas mfazekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, and integration test would be nice. It can be very simple like this one:

https://github.com/net-ssh/net-ssh/blob/868ed01e9f64338812c1f67e10f04e4805bdbc33/test/integration/test_chacha20_poly1305_cipher.rb

lib/net/ssh/transport/cipher_factory.rb Outdated Show resolved Hide resolved
lib/net/ssh/transport/packet_stream.rb Outdated Show resolved Hide resolved
flux-johnm added a commit to flux-johnm/net-ssh that referenced this pull request May 21, 2023
A fork from net-ssh#845

Add support for AEAD-AES256-GCM

Add support for AEAD-AES128-GCM

Adding test for aead ciphers

Add aes256-gcm cipher mode

Fixing exising tests
lib/net/ssh/transport/algorithms.rb Outdated Show resolved Hide resolved
lib/net/ssh/transport/aead_aes_gcm.rb Outdated Show resolved Hide resolved
end

def padding=(pad)
# DO NOTHING (always 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: this comment could be nicer to readers and explain that the padding content is 0 and this does not refer to padding length.

server.cipher.auth_data = payload_length
payload = server.cipher.update(@packet.to_s) + server.final_cipher
padding_length = payload[0].unpack('C').first.to_i
payload = payload[1..@packet_length - padding_length - 1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This range looks correct against the RFC but might pay to double check.

@Slokilla Slokilla force-pushed the gcm branch 2 times, most recently from 1432501 to 18808a4 Compare May 22, 2023 12:31
@Slokilla
Copy link
Author

Looks good to me, and integration test would be nice. It can be very simple like this one:

https://github.com/net-ssh/net-ssh/blob/868ed01e9f64338812c1f67e10f04e4805bdbc33/test/integration/test_chacha20_poly1305_cipher.rb

Ok, I've added it. It fails, and i don't understand why. Would you please take a look @mfazekas ?

test/integration/test_gcm_cipher.rb~ Outdated Show resolved Hide resolved
line
end
end
config_lines.push("Ciphers #{cipher}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI failures suggest to me that the ssh server is failing to start which generates a pid, since the config file is derived from the tempfile which the config_lines are written to. I've not tested this but from an eyeball, looks to be due to writing the cipher as aes128-gcm where a valid cipher configuration for ssh_config is aes128-gcm@openssh.com

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep the log file shows:

Bad SSH2 cipher spec 'aes128-gcm'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask you where did you find this log line ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pass the debug: True flag to start_sshd_7_or_later then it'll print the log path of that's the sshd. I'll also add a PR to tail the log file if sshd fails to start:
20dd0f4

Copy link
Collaborator

@mfazekas mfazekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Slokilla can you run your version against an actual ssh server?
I've tried sshd in osx. And it wasn't working for me at all, server disconnected before password, that's can be a HMAC verification issue. Same issue that shown by integration server:

$:.push('./lib')
require 'net/ssh'
require 'byebug'
puts Net::SSH::Version::CURRENT
Net::SSH.start("localhost", ENV.fetch("USER", nil), encryption: "aes256-gcm@openssh.com", verbose: :debug) do |ssh|
  ssh.exec! 'echo Foo '
end
7.1.0
D, [2023-05-23T21:00:57.448434 #42854] DEBUG -- net.ssh.transport.session[21c]: establishing connection to localhost:22
D, [2023-05-23T21:00:57.450681 #42854] DEBUG -- net.ssh.transport.session[21c]: connection established
I, [2023-05-23T21:00:57.450767 #42854]  INFO -- net.ssh.transport.server_version[230]: negotiating protocol version
D, [2023-05-23T21:00:57.450791 #42854] DEBUG -- net.ssh.transport.server_version[230]: local is `SSH-2.0-Ruby/Net::SSH_7.1.0 x86_64-darwin20'
D, [2023-05-23T21:00:57.469551 #42854] DEBUG -- net.ssh.transport.server_version[230]: remote is `SSH-2.0-OpenSSH_8.6'
I, [2023-05-23T21:00:57.471082 #42854]  INFO -- net.ssh.transport.algorithms[244]: sending KEXINIT
D, [2023-05-23T21:00:57.471250 #42854] DEBUG -- socket[258]: queueing packet nr 0 type 20 len 876
D, [2023-05-23T21:00:57.471325 #42854] DEBUG -- socket[258]: sent 880 bytes
D, [2023-05-23T21:00:57.483302 #42854] DEBUG -- socket[258]: read 1056 bytes
D, [2023-05-23T21:00:57.483413 #42854] DEBUG -- socket[258]: received packet nr 0 type 20 len 1052
I, [2023-05-23T21:00:57.483456 #42854]  INFO -- net.ssh.transport.algorithms[244]: got KEXINIT from server
I, [2023-05-23T21:00:57.483529 #42854]  INFO -- net.ssh.transport.algorithms[244]: negotiating algorithms
D, [2023-05-23T21:00:57.483607 #42854] DEBUG -- net.ssh.transport.algorithms[244]: negotiated:
* kex: curve25519-sha256
* host_key: ssh-ed25519
* encryption_server: aes256-gcm@openssh.com
* encryption_client: aes256-gcm@openssh.com
* hmac_client: hmac-sha2-512-etm@openssh.com
* hmac_server: hmac-sha2-512-etm@openssh.com
* compression_client: none
* compression_server: none
* language_client: 
* language_server: 
D, [2023-05-23T21:00:57.483629 #42854] DEBUG -- net.ssh.transport.algorithms[244]: exchanging keys
D, [2023-05-23T21:00:57.483990 #42854] DEBUG -- socket[258]: queueing packet nr 1 type 30 len 44
D, [2023-05-23T21:00:57.484057 #42854] DEBUG -- socket[258]: sent 48 bytes
D, [2023-05-23T21:00:57.489188 #42854] DEBUG -- socket[258]: read 208 bytes
D, [2023-05-23T21:00:57.489272 #42854] DEBUG -- socket[258]: received packet nr 1 type 31 len 188
D, [2023-05-23T21:00:57.489886 #42854] DEBUG -- socket[258]: queueing packet nr 2 type 21 len 20
D, [2023-05-23T21:00:57.489947 #42854] DEBUG -- socket[258]: sent 24 bytes
D, [2023-05-23T21:00:57.490016 #42854] DEBUG -- socket[258]: received packet nr 2 type 21 len 12
D, [2023-05-23T21:00:57.490324 #42854] DEBUG -- net.ssh.authentication.session[26c]: beginning authentication of `boga'
D, [2023-05-23T21:00:57.490382 #42854] DEBUG -- socket[258]: using encrypt-then-mac
D, [2023-05-23T21:00:57.490484 #42854] DEBUG -- socket[258]: queueing packet nr 3 type 5 len 32
D, [2023-05-23T21:00:57.490555 #42854] DEBUG -- socket[258]: sent 100 bytes
D, [2023-05-23T21:00:57.491934 #42854] DEBUG -- socket[258]: read 0 bytes
Traceback (most recent call last):
        13: from ./tryout/test-gcm.rb:18:in `<main>'
        12: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh.rb:259:in `start'
        11: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
        10: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
         9: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
         8: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
         7: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
         6: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
         5: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
         4: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
         3: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
         2: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
         1: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
/Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet': connection closed by remote host (Net::SSH::Disconnect)

Raw ssh was working fine with the same server

ssh -c aes128-gcm@openssh.com localhost -vvv

Server shows this error:

ssh_dispatch_run_fatal: Connection from ::1 port 64341: message authentication code incorrect [preauth]

@Slokilla
Copy link
Author

@Slokilla can you run your version against an actual ssh server? I've tried sshd in osx. And it wasn't working for me at all, server disconnected before password, that's can be a HMAC verification issue. Same issue that shown by integration server:

$:.push('./lib')
require 'net/ssh'
require 'byebug'
puts Net::SSH::Version::CURRENT
Net::SSH.start("localhost", ENV.fetch("USER", nil), encryption: "aes256-gcm@openssh.com", verbose: :debug) do |ssh|
  ssh.exec! 'echo Foo '
end
7.1.0
D, [2023-05-23T21:00:57.448434 #42854] DEBUG -- net.ssh.transport.session[21c]: establishing connection to localhost:22
D, [2023-05-23T21:00:57.450681 #42854] DEBUG -- net.ssh.transport.session[21c]: connection established
I, [2023-05-23T21:00:57.450767 #42854]  INFO -- net.ssh.transport.server_version[230]: negotiating protocol version
D, [2023-05-23T21:00:57.450791 #42854] DEBUG -- net.ssh.transport.server_version[230]: local is `SSH-2.0-Ruby/Net::SSH_7.1.0 x86_64-darwin20'
D, [2023-05-23T21:00:57.469551 #42854] DEBUG -- net.ssh.transport.server_version[230]: remote is `SSH-2.0-OpenSSH_8.6'
I, [2023-05-23T21:00:57.471082 #42854]  INFO -- net.ssh.transport.algorithms[244]: sending KEXINIT
D, [2023-05-23T21:00:57.471250 #42854] DEBUG -- socket[258]: queueing packet nr 0 type 20 len 876
D, [2023-05-23T21:00:57.471325 #42854] DEBUG -- socket[258]: sent 880 bytes
D, [2023-05-23T21:00:57.483302 #42854] DEBUG -- socket[258]: read 1056 bytes
D, [2023-05-23T21:00:57.483413 #42854] DEBUG -- socket[258]: received packet nr 0 type 20 len 1052
I, [2023-05-23T21:00:57.483456 #42854]  INFO -- net.ssh.transport.algorithms[244]: got KEXINIT from server
I, [2023-05-23T21:00:57.483529 #42854]  INFO -- net.ssh.transport.algorithms[244]: negotiating algorithms
D, [2023-05-23T21:00:57.483607 #42854] DEBUG -- net.ssh.transport.algorithms[244]: negotiated:
* kex: curve25519-sha256
* host_key: ssh-ed25519
* encryption_server: aes256-gcm@openssh.com
* encryption_client: aes256-gcm@openssh.com
* hmac_client: hmac-sha2-512-etm@openssh.com
* hmac_server: hmac-sha2-512-etm@openssh.com
* compression_client: none
* compression_server: none
* language_client: 
* language_server: 
D, [2023-05-23T21:00:57.483629 #42854] DEBUG -- net.ssh.transport.algorithms[244]: exchanging keys
D, [2023-05-23T21:00:57.483990 #42854] DEBUG -- socket[258]: queueing packet nr 1 type 30 len 44
D, [2023-05-23T21:00:57.484057 #42854] DEBUG -- socket[258]: sent 48 bytes
D, [2023-05-23T21:00:57.489188 #42854] DEBUG -- socket[258]: read 208 bytes
D, [2023-05-23T21:00:57.489272 #42854] DEBUG -- socket[258]: received packet nr 1 type 31 len 188
D, [2023-05-23T21:00:57.489886 #42854] DEBUG -- socket[258]: queueing packet nr 2 type 21 len 20
D, [2023-05-23T21:00:57.489947 #42854] DEBUG -- socket[258]: sent 24 bytes
D, [2023-05-23T21:00:57.490016 #42854] DEBUG -- socket[258]: received packet nr 2 type 21 len 12
D, [2023-05-23T21:00:57.490324 #42854] DEBUG -- net.ssh.authentication.session[26c]: beginning authentication of `boga'
D, [2023-05-23T21:00:57.490382 #42854] DEBUG -- socket[258]: using encrypt-then-mac
D, [2023-05-23T21:00:57.490484 #42854] DEBUG -- socket[258]: queueing packet nr 3 type 5 len 32
D, [2023-05-23T21:00:57.490555 #42854] DEBUG -- socket[258]: sent 100 bytes
D, [2023-05-23T21:00:57.491934 #42854] DEBUG -- socket[258]: read 0 bytes
Traceback (most recent call last):
        13: from ./tryout/test-gcm.rb:18:in `<main>'
        12: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh.rb:259:in `start'
        11: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
        10: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
         9: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
         8: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
         7: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
         6: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
         5: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
         4: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
         3: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
         2: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
         1: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
/Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet': connection closed by remote host (Net::SSH::Disconnect)

Raw ssh was working fine with the same server

ssh -c aes128-gcm@openssh.com localhost -vvv

Server shows this error:

ssh_dispatch_run_fatal: Connection from ::1 port 64341: message authentication code incorrect [preauth]

Working on it, gonna ping you when ready.

@fwininger
Copy link
Collaborator

I have a docker project with a self compile openssh server via full debug options, if need : https://github.com/fwininger/docker_ssh_debug

@flux-benj
Copy link

flux-benj commented May 23, 2023

@Slokilla can you run your version against an actual ssh server? I've tried sshd in osx. And it wasn't working for me at all, server disconnected before password, that's can be a HMAC verification issue. Same issue that shown by integration server:

$:.push('./lib')
require 'net/ssh'
require 'byebug'
puts Net::SSH::Version::CURRENT
Net::SSH.start("localhost", ENV.fetch("USER", nil), encryption: "aes256-gcm@openssh.com", verbose: :debug) do |ssh|
  ssh.exec! 'echo Foo '
end
7.1.0
D, [2023-05-23T21:00:57.448434 #42854] DEBUG -- net.ssh.transport.session[21c]: establishing connection to localhost:22
D, [2023-05-23T21:00:57.450681 #42854] DEBUG -- net.ssh.transport.session[21c]: connection established
I, [2023-05-23T21:00:57.450767 #42854]  INFO -- net.ssh.transport.server_version[230]: negotiating protocol version
D, [2023-05-23T21:00:57.450791 #42854] DEBUG -- net.ssh.transport.server_version[230]: local is `SSH-2.0-Ruby/Net::SSH_7.1.0 x86_64-darwin20'
D, [2023-05-23T21:00:57.469551 #42854] DEBUG -- net.ssh.transport.server_version[230]: remote is `SSH-2.0-OpenSSH_8.6'
I, [2023-05-23T21:00:57.471082 #42854]  INFO -- net.ssh.transport.algorithms[244]: sending KEXINIT
D, [2023-05-23T21:00:57.471250 #42854] DEBUG -- socket[258]: queueing packet nr 0 type 20 len 876
D, [2023-05-23T21:00:57.471325 #42854] DEBUG -- socket[258]: sent 880 bytes
D, [2023-05-23T21:00:57.483302 #42854] DEBUG -- socket[258]: read 1056 bytes
D, [2023-05-23T21:00:57.483413 #42854] DEBUG -- socket[258]: received packet nr 0 type 20 len 1052
I, [2023-05-23T21:00:57.483456 #42854]  INFO -- net.ssh.transport.algorithms[244]: got KEXINIT from server
I, [2023-05-23T21:00:57.483529 #42854]  INFO -- net.ssh.transport.algorithms[244]: negotiating algorithms
D, [2023-05-23T21:00:57.483607 #42854] DEBUG -- net.ssh.transport.algorithms[244]: negotiated:
* kex: curve25519-sha256
* host_key: ssh-ed25519
* encryption_server: aes256-gcm@openssh.com
* encryption_client: aes256-gcm@openssh.com
* hmac_client: hmac-sha2-512-etm@openssh.com
* hmac_server: hmac-sha2-512-etm@openssh.com
* compression_client: none
* compression_server: none
* language_client: 
* language_server: 
D, [2023-05-23T21:00:57.483629 #42854] DEBUG -- net.ssh.transport.algorithms[244]: exchanging keys
D, [2023-05-23T21:00:57.483990 #42854] DEBUG -- socket[258]: queueing packet nr 1 type 30 len 44
D, [2023-05-23T21:00:57.484057 #42854] DEBUG -- socket[258]: sent 48 bytes
D, [2023-05-23T21:00:57.489188 #42854] DEBUG -- socket[258]: read 208 bytes
D, [2023-05-23T21:00:57.489272 #42854] DEBUG -- socket[258]: received packet nr 1 type 31 len 188
D, [2023-05-23T21:00:57.489886 #42854] DEBUG -- socket[258]: queueing packet nr 2 type 21 len 20
D, [2023-05-23T21:00:57.489947 #42854] DEBUG -- socket[258]: sent 24 bytes
D, [2023-05-23T21:00:57.490016 #42854] DEBUG -- socket[258]: received packet nr 2 type 21 len 12
D, [2023-05-23T21:00:57.490324 #42854] DEBUG -- net.ssh.authentication.session[26c]: beginning authentication of `boga'
D, [2023-05-23T21:00:57.490382 #42854] DEBUG -- socket[258]: using encrypt-then-mac
D, [2023-05-23T21:00:57.490484 #42854] DEBUG -- socket[258]: queueing packet nr 3 type 5 len 32
D, [2023-05-23T21:00:57.490555 #42854] DEBUG -- socket[258]: sent 100 bytes
D, [2023-05-23T21:00:57.491934 #42854] DEBUG -- socket[258]: read 0 bytes
Traceback (most recent call last):
        13: from ./tryout/test-gcm.rb:18:in `<main>'
        12: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh.rb:259:in `start'
        11: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
        10: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
         9: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
         8: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
         7: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
         6: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
         5: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
         4: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
         3: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
         2: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
         1: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
/Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet': connection closed by remote host (Net::SSH::Disconnect)

Raw ssh was working fine with the same server

ssh -c aes128-gcm@openssh.com localhost -vvv

Server shows this error:

ssh_dispatch_run_fatal: Connection from ::1 port 64341: message authentication code incorrect [preauth]

Working on it, gonna ping you when ready.

I believe this is a fallout from the changes spurred by #845 (comment) - removing the hmac ref to aes-xxx-gcm was right in the configuration but we still need to ensure the aead server/client is used instead of the etm (from combing the logs for five minutes, I may be wrong), just without the entries in the hmac section (IMO)

@Slokilla
Copy link
Author

@Slokilla can you run your version against an actual ssh server? I've tried sshd in osx. And it wasn't working for me at all, server disconnected before password, that's can be a HMAC verification issue. Same issue that shown by integration server:

$:.push('./lib')
require 'net/ssh'
require 'byebug'
puts Net::SSH::Version::CURRENT
Net::SSH.start("localhost", ENV.fetch("USER", nil), encryption: "aes256-gcm@openssh.com", verbose: :debug) do |ssh|
  ssh.exec! 'echo Foo '
end
7.1.0
D, [2023-05-23T21:00:57.448434 #42854] DEBUG -- net.ssh.transport.session[21c]: establishing connection to localhost:22
D, [2023-05-23T21:00:57.450681 #42854] DEBUG -- net.ssh.transport.session[21c]: connection established
I, [2023-05-23T21:00:57.450767 #42854]  INFO -- net.ssh.transport.server_version[230]: negotiating protocol version
D, [2023-05-23T21:00:57.450791 #42854] DEBUG -- net.ssh.transport.server_version[230]: local is `SSH-2.0-Ruby/Net::SSH_7.1.0 x86_64-darwin20'
D, [2023-05-23T21:00:57.469551 #42854] DEBUG -- net.ssh.transport.server_version[230]: remote is `SSH-2.0-OpenSSH_8.6'
I, [2023-05-23T21:00:57.471082 #42854]  INFO -- net.ssh.transport.algorithms[244]: sending KEXINIT
D, [2023-05-23T21:00:57.471250 #42854] DEBUG -- socket[258]: queueing packet nr 0 type 20 len 876
D, [2023-05-23T21:00:57.471325 #42854] DEBUG -- socket[258]: sent 880 bytes
D, [2023-05-23T21:00:57.483302 #42854] DEBUG -- socket[258]: read 1056 bytes
D, [2023-05-23T21:00:57.483413 #42854] DEBUG -- socket[258]: received packet nr 0 type 20 len 1052
I, [2023-05-23T21:00:57.483456 #42854]  INFO -- net.ssh.transport.algorithms[244]: got KEXINIT from server
I, [2023-05-23T21:00:57.483529 #42854]  INFO -- net.ssh.transport.algorithms[244]: negotiating algorithms
D, [2023-05-23T21:00:57.483607 #42854] DEBUG -- net.ssh.transport.algorithms[244]: negotiated:
* kex: curve25519-sha256
* host_key: ssh-ed25519
* encryption_server: aes256-gcm@openssh.com
* encryption_client: aes256-gcm@openssh.com
* hmac_client: hmac-sha2-512-etm@openssh.com
* hmac_server: hmac-sha2-512-etm@openssh.com
* compression_client: none
* compression_server: none
* language_client: 
* language_server: 
D, [2023-05-23T21:00:57.483629 #42854] DEBUG -- net.ssh.transport.algorithms[244]: exchanging keys
D, [2023-05-23T21:00:57.483990 #42854] DEBUG -- socket[258]: queueing packet nr 1 type 30 len 44
D, [2023-05-23T21:00:57.484057 #42854] DEBUG -- socket[258]: sent 48 bytes
D, [2023-05-23T21:00:57.489188 #42854] DEBUG -- socket[258]: read 208 bytes
D, [2023-05-23T21:00:57.489272 #42854] DEBUG -- socket[258]: received packet nr 1 type 31 len 188
D, [2023-05-23T21:00:57.489886 #42854] DEBUG -- socket[258]: queueing packet nr 2 type 21 len 20
D, [2023-05-23T21:00:57.489947 #42854] DEBUG -- socket[258]: sent 24 bytes
D, [2023-05-23T21:00:57.490016 #42854] DEBUG -- socket[258]: received packet nr 2 type 21 len 12
D, [2023-05-23T21:00:57.490324 #42854] DEBUG -- net.ssh.authentication.session[26c]: beginning authentication of `boga'
D, [2023-05-23T21:00:57.490382 #42854] DEBUG -- socket[258]: using encrypt-then-mac
D, [2023-05-23T21:00:57.490484 #42854] DEBUG -- socket[258]: queueing packet nr 3 type 5 len 32
D, [2023-05-23T21:00:57.490555 #42854] DEBUG -- socket[258]: sent 100 bytes
D, [2023-05-23T21:00:57.491934 #42854] DEBUG -- socket[258]: read 0 bytes
Traceback (most recent call last):
        13: from ./tryout/test-gcm.rb:18:in `<main>'
        12: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh.rb:259:in `start'
        11: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:61:in `authenticate'
        10: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:130:in `expect_message'
         9: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `next_message'
         8: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:101:in `loop'
         7: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/authentication/session.rb:102:in `block in next_message'
         6: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:175:in `next_message'
         5: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `poll_message'
         4: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:190:in `loop'
         3: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/session.rb:193:in `block in poll_message'
         2: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `next_packet'
         1: from /Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:102:in `loop'
/Users/boga/Work/OSS/NetSSH/net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet': connection closed by remote host (Net::SSH::Disconnect)

Raw ssh was working fine with the same server

ssh -c aes128-gcm@openssh.com localhost -vvv

Server shows this error:

ssh_dispatch_run_fatal: Connection from ::1 port 64341: message authentication code incorrect [preauth]

Working on it, gonna ping you when ready.

I believe this is a fallout from the changes spurred by #845 (comment) - removing the hmac ref to aes-xxx-gcm was right in the configuration but we still need to ensure the aead server/client is used instead of the etm (from combing the logs for five minutes, I may be wrong), just without the entries in the hmac section (IMO)

Ok, it looks good :

require 'net/ssh'

Net::SSH.start('127.0.0.1', 'slo', encryption: 'aes256-gcm@openssh.com') do |ssh|
  puts ssh.exec!('echo toto')
end

leads to :

root@91b3de6a406e:/net-ssh# bundle exec ruby test.rb
slo@127.0.0.1's password:  **typing**
toto

The error was that cipher.to_s did not return the name of the cipher.

@Slokilla
Copy link
Author

Looks like my last change fails on ruby 2.6 & 2.7, will fix it tommorow.

@Slokilla
Copy link
Author

The test were mocking Net::SSH::Transport::CipherFactory#get behavior, returning a symbol. But the real return type is an object. So i change the return to a class that responds to :name.

Add support for AEAD-AES256-GCM

Add support for AEAD-AES128-GCM

Adding test for aead ciphers

 Add aes256-gcm cipher mode

Fixing exising tests

Implementing both 128 and 256 aead-aes-gcm

Add support for AEAD-AES256-GCM

Add support for AEAD-AES128-GCM

Adding test for aead ciphers

 Add aes256-gcm cipher mode

Fixing exising tests
@mfazekas
Copy link
Collaborator

mfazekas commented Jun 10, 2023

@Slokilla it's still failing for me:


Net::SSH.start("localhost", ENV.fetch("USER", nil), encryption: 'aes128-gcm@openssh.com', port: 22, verbose: :debug) do |ssh|
  ssh.exec! 'echo Foo '
end

D, [2023-06-10T08:05:23.893095 #4610] DEBUG -- socket[258]: using encrypt-then-mac
D, [2023-06-10T08:05:23.894860 #4610] DEBUG -- socket[258]: queueing packet nr 3 type 5 len 32
D, [2023-06-10T08:05:23.898647 #4610] DEBUG -- socket[258]: sent 100 bytes
D, [2023-06-10T08:05:23.927577 #4610] DEBUG -- socket[258]: read 0 bytes
/net-ssh/lib/net/ssh/transport/packet_stream.rb:108:in `block in next_packet': connection closed by remote host (Net::SSH::Disconnect)

tail /var/log/auth.log
...
Jun 10 08:05:23 ubuntu-jammy sshd[4611]: debug1: KEX done [preauth]
Jun 10 08:05:23 ubuntu-jammy sshd[4611]: ssh_dispatch_run_fatal: Connection from 127.0.0.1 port 55402: message authentication code incorrect [preauth]
...

Same for aes256:

Jun 10 08:09:52 ubuntu-jammy sshd[4619]: debug1: kex: host key algorithm: ssh-ed25519 [preauth]
Jun 10 08:09:52 ubuntu-jammy sshd[4619]: debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none [preauth]
Jun 10 08:09:52 ubuntu-jammy sshd[4619]: debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none [preauth]
...
Jun 10 08:09:52 ubuntu-jammy sshd[4619]: ssh_dispatch_run_fatal: Connection from 127.0.0.1 port 52024: message authentication code incorrect [preauth]

Comment on lines +164 to +177
elsif client.hmac.aead
# Details of this implementation can be found in RFC 5647
unencrypted_data = [padding_length, payload, padding].pack("CA*A*")

# Despite SSH spec, when using AEAD encryption, packet_length is not ciphered. It is transported as identity,
# and added in the auth_data. See RFC 5647 7.3
client.cipher.auth_data = [packet_length].pack('N')
encrypted_data = client.update_cipher(unencrypted_data)
encrypted_data << client.final_cipher

# For SSH, GCM auth_tag acts as MAC
mac = client.auth_tag

message = [packet_length].pack("N") + encrypted_data + [mac].pack("A*")
Copy link
Collaborator

@mfazekas mfazekas Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm refactoring this part in chacha20-poly1035, and it looks like this:

if client.cipher.implicit_mac?
   unencrypted_data = [padding_length, payload, padding].pack("CA*A*")
   message = client.cipher.update_cipher_mac(unencrypted_data, client.sequence_number)

I think this would work for this use case as well, what do you think?

if client.cipher.implicit_mac?
unencrypted_data = [padding_length, payload, padding].pack("CA*A*")
message = client.cipher.update_cipher_mac(unencrypted_data, client.sequence_number)

padding_length = @packet.read_byte

payload = @packet.read(@packet_length - padding_length - 1)
if server.hmac.aead
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW This is what the chachapoly version looks like:

if server.cipher.implicit_mac?
real_hmac = read_available(server.cipher.mac_length) || ""
@packet = Net::SSH::Buffer.new(server.cipher.read_and_mac(@mac_data, real_hmac, server.sequence_number))
padding_length = @packet.read_byte
payload = @packet.read(@packet_length - padding_length - 1)

@mfazekas
Copy link
Collaborator

@Slokilla I've merged chacha20-poly1305 which creates a lot of conflicts.

As noted #845 (comment) I'm still getting incorrect MAC from SSHD server when checking out the latest version of your branch.

I've tried that on ab76e3c.

If you can fix that I can merge the your changes with the ChaCha poly changes.
Thanks much for the awesome work!

@flux-benj
Copy link

Coming back to this, we have a strong desire to get this in and released so we can get off of our internal fork which adds this cipher. This MR looks to be disbanded so was wondering if this work is going to be picked up again or we should start a new MR considering the comments above. Thoughts?

cc: @Slokilla and @mfazekas

@mfazekas
Copy link
Collaborator

mfazekas commented Dec 7, 2023

@flux-benj so my main issue with this request is that it was not working, when I've tried with openssh #845 (comment) . Probably something trivial, but debugging it and fixing is requires a bit of an effort. So if you have something working, pls open a draft PR, and we can go from there.

You should be able to use the intergration test from this PR:
https://github.com/net-ssh/net-ssh/blob/ab76e3c4e3777daeb6792afb8691a464f68ce181/test/integration/test_gcm_cipher.rb

@fwininger
Copy link
Collaborator

@mfazekas / @flux-benj : this PR gives the direction to make the support of aes-gcm, however we don't have the time to complet this features.
We use Net::SSH only to execute commands on remote servers and for this simple use case we have switch to a direct bind with the libssh library : https://github.com/Cyberwatch/libssh-ruby).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configure support for gcm algorithms?
5 participants