net-ssh 4.2.0 → 7.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.dockerignore +6 -0
- data/.github/config/rubocop_linter_action.yml +4 -0
- data/.github/workflows/ci-with-docker.yml +44 -0
- data/.github/workflows/ci.yml +87 -0
- data/.github/workflows/rubocop.yml +13 -0
- data/.gitignore +7 -0
- data/.rubocop.yml +19 -2
- data/.rubocop_todo.yml +619 -667
- data/CHANGES.txt +110 -1
- data/Dockerfile +27 -0
- data/Dockerfile.openssl3 +17 -0
- data/Gemfile +3 -7
- data/{Gemfile.norbnacl → Gemfile.noed25519} +3 -1
- data/Manifest +4 -5
- data/README.md +293 -0
- data/Rakefile +45 -29
- data/appveyor.yml +8 -6
- data/docker-compose.yml +23 -0
- data/lib/net/ssh/authentication/agent.rb +248 -223
- data/lib/net/ssh/authentication/certificate.rb +178 -164
- data/lib/net/ssh/authentication/constants.rb +17 -15
- data/lib/net/ssh/authentication/ed25519.rb +141 -116
- data/lib/net/ssh/authentication/ed25519_loader.rb +28 -28
- data/lib/net/ssh/authentication/key_manager.rb +79 -36
- data/lib/net/ssh/authentication/methods/abstract.rb +62 -47
- data/lib/net/ssh/authentication/methods/hostbased.rb +34 -37
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +3 -3
- data/lib/net/ssh/authentication/methods/none.rb +16 -19
- data/lib/net/ssh/authentication/methods/password.rb +15 -16
- data/lib/net/ssh/authentication/methods/publickey.rb +96 -55
- data/lib/net/ssh/authentication/pageant.rb +468 -465
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
- data/lib/net/ssh/authentication/session.rb +131 -122
- data/lib/net/ssh/buffer.rb +385 -332
- data/lib/net/ssh/buffered_io.rb +150 -151
- data/lib/net/ssh/config.rb +316 -239
- data/lib/net/ssh/connection/channel.rb +635 -613
- data/lib/net/ssh/connection/constants.rb +29 -29
- data/lib/net/ssh/connection/event_loop.rb +104 -95
- data/lib/net/ssh/connection/keepalive.rb +55 -51
- data/lib/net/ssh/connection/session.rb +614 -611
- data/lib/net/ssh/connection/term.rb +125 -123
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +194 -108
- data/lib/net/ssh/known_hosts.rb +212 -134
- data/lib/net/ssh/loggable.rb +50 -49
- data/lib/net/ssh/packet.rb +83 -79
- data/lib/net/ssh/prompt.rb +51 -51
- data/lib/net/ssh/proxy/command.rb +105 -91
- data/lib/net/ssh/proxy/errors.rb +12 -10
- data/lib/net/ssh/proxy/http.rb +81 -81
- data/lib/net/ssh/proxy/https.rb +37 -36
- data/lib/net/ssh/proxy/jump.rb +49 -48
- data/lib/net/ssh/proxy/socks4.rb +2 -6
- data/lib/net/ssh/proxy/socks5.rb +14 -17
- data/lib/net/ssh/service/forward.rb +365 -362
- data/lib/net/ssh/test/channel.rb +145 -143
- data/lib/net/ssh/test/extensions.rb +131 -127
- data/lib/net/ssh/test/kex.rb +34 -32
- data/lib/net/ssh/test/local_packet.rb +46 -44
- data/lib/net/ssh/test/packet.rb +87 -84
- data/lib/net/ssh/test/remote_packet.rb +32 -30
- data/lib/net/ssh/test/script.rb +155 -155
- data/lib/net/ssh/test/socket.rb +49 -48
- data/lib/net/ssh/test.rb +82 -80
- data/lib/net/ssh/transport/algorithms.rb +433 -364
- data/lib/net/ssh/transport/cipher_factory.rb +95 -91
- data/lib/net/ssh/transport/constants.rb +32 -24
- data/lib/net/ssh/transport/ctr.rb +37 -15
- data/lib/net/ssh/transport/hmac/abstract.rb +81 -63
- data/lib/net/ssh/transport/hmac/md5.rb +0 -2
- data/lib/net/ssh/transport/hmac/md5_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/none.rb +0 -2
- data/lib/net/ssh/transport/hmac/ripemd160.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac.rb +14 -12
- data/lib/net/ssh/transport/identity_cipher.rb +54 -52
- data/lib/net/ssh/transport/kex/abstract.rb +130 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +33 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha256.rb +11 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +112 -217
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -63
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +36 -90
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
- data/lib/net/ssh/transport/kex.rb +15 -12
- data/lib/net/ssh/transport/key_expander.rb +24 -21
- data/lib/net/ssh/transport/openssl.rb +158 -133
- data/lib/net/ssh/transport/packet_stream.rb +223 -191
- data/lib/net/ssh/transport/server_version.rb +55 -56
- data/lib/net/ssh/transport/session.rb +306 -259
- data/lib/net/ssh/transport/state.rb +178 -176
- data/lib/net/ssh/verifiers/accept_new.rb +33 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
- data/lib/net/ssh/verifiers/always.rb +58 -0
- data/lib/net/ssh/verifiers/never.rb +19 -0
- data/lib/net/ssh/version.rb +55 -53
- data/lib/net/ssh.rb +47 -34
- data/net-ssh-public_cert.pem +18 -19
- data/net-ssh.gemspec +12 -11
- data/support/ssh_tunnel_bug.rb +5 -5
- data.tar.gz.sig +0 -0
- metadata +78 -73
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -51
- data/Gemfile.norbnacl.lock +0 -41
- data/README.rdoc +0 -169
- data/lib/net/ssh/ruby_compat.rb +0 -24
- data/lib/net/ssh/verifiers/lenient.rb +0 -30
- data/lib/net/ssh/verifiers/null.rb +0 -12
- data/lib/net/ssh/verifiers/secure.rb +0 -52
- data/lib/net/ssh/verifiers/strict.rb +0 -24
- data/support/arcfour_check.rb +0 -20
data/docker-compose.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
version: '3'
|
2
|
+
|
3
|
+
services:
|
4
|
+
ruby-3.1:
|
5
|
+
build:
|
6
|
+
context: .
|
7
|
+
args:
|
8
|
+
RUBY_VERSION: 3.1
|
9
|
+
ruby-3.0:
|
10
|
+
build:
|
11
|
+
context: .
|
12
|
+
args:
|
13
|
+
RUBY_VERSION: 3.0
|
14
|
+
ruby-2.7:
|
15
|
+
build:
|
16
|
+
context: .
|
17
|
+
args:
|
18
|
+
RUBY_VERSION: 2.7
|
19
|
+
ruby-2.6:
|
20
|
+
build:
|
21
|
+
context: .
|
22
|
+
args:
|
23
|
+
RUBY_VERSION: 2.6
|
@@ -8,252 +8,277 @@ require 'rubygems'
|
|
8
8
|
|
9
9
|
require 'net/ssh/authentication/pageant' if Gem.win_platform? && RUBY_PLATFORM != "java"
|
10
10
|
|
11
|
-
module Net
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
11
|
+
module Net
|
12
|
+
module SSH
|
13
|
+
module Authentication
|
14
|
+
# Class for representing agent-specific errors.
|
15
|
+
class AgentError < Net::SSH::Exception; end
|
16
|
+
|
17
|
+
# An exception for indicating that the SSH agent is not available.
|
18
|
+
class AgentNotAvailable < AgentError; end
|
19
|
+
|
20
|
+
# This class implements a simple client for the ssh-agent protocol. It
|
21
|
+
# does not implement any specific protocol, but instead copies the
|
22
|
+
# behavior of the ssh-agent functions in the OpenSSH library (3.8).
|
23
|
+
#
|
24
|
+
# This means that although it behaves like a SSH1 client, it also has
|
25
|
+
# some SSH2 functionality (like signing data).
|
26
|
+
class Agent
|
27
|
+
include Loggable
|
28
|
+
|
29
|
+
# A simple module for extending keys, to allow comments to be specified
|
30
|
+
# for them.
|
31
|
+
module Comment
|
32
|
+
attr_accessor :comment
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
35
|
+
SSH2_AGENT_REQUEST_VERSION = 1
|
36
|
+
SSH2_AGENT_REQUEST_IDENTITIES = 11
|
37
|
+
SSH2_AGENT_IDENTITIES_ANSWER = 12
|
38
|
+
SSH2_AGENT_SIGN_REQUEST = 13
|
39
|
+
SSH2_AGENT_SIGN_RESPONSE = 14
|
40
|
+
SSH2_AGENT_ADD_IDENTITY = 17
|
41
|
+
SSH2_AGENT_REMOVE_IDENTITY = 18
|
42
|
+
SSH2_AGENT_REMOVE_ALL_IDENTITIES = 19
|
43
|
+
SSH2_AGENT_LOCK = 22
|
44
|
+
SSH2_AGENT_UNLOCK = 23
|
45
|
+
SSH2_AGENT_ADD_ID_CONSTRAINED = 25
|
46
|
+
SSH2_AGENT_FAILURE = 30
|
47
|
+
SSH2_AGENT_VERSION_RESPONSE = 103
|
48
|
+
|
49
|
+
SSH_COM_AGENT2_FAILURE = 102
|
50
|
+
|
51
|
+
SSH_AGENT_REQUEST_RSA_IDENTITIES = 1
|
52
|
+
SSH_AGENT_RSA_IDENTITIES_ANSWER1 = 2
|
53
|
+
SSH_AGENT_RSA_IDENTITIES_ANSWER2 = 5
|
54
|
+
SSH_AGENT_FAILURE = 5
|
55
|
+
SSH_AGENT_SUCCESS = 6
|
56
|
+
|
57
|
+
SSH_AGENT_CONSTRAIN_LIFETIME = 1
|
58
|
+
SSH_AGENT_CONSTRAIN_CONFIRM = 2
|
59
|
+
|
60
|
+
SSH_AGENT_RSA_SHA2_256 = 0x02
|
61
|
+
SSH_AGENT_RSA_SHA2_512 = 0x04
|
62
|
+
|
63
|
+
# The underlying socket being used to communicate with the SSH agent.
|
64
|
+
attr_reader :socket
|
65
|
+
|
66
|
+
# Instantiates a new agent object, connects to a running SSH agent,
|
67
|
+
# negotiates the agent protocol version, and returns the agent object.
|
68
|
+
def self.connect(logger = nil, agent_socket_factory = nil, identity_agent = nil)
|
69
|
+
agent = new(logger)
|
70
|
+
agent.connect!(agent_socket_factory, identity_agent)
|
71
|
+
agent.negotiate!
|
72
|
+
agent
|
73
|
+
end
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
+
# Creates a new Agent object, using the optional logger instance to
|
76
|
+
# report status.
|
77
|
+
def initialize(logger = nil)
|
78
|
+
self.logger = logger
|
79
|
+
end
|
75
80
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
81
|
+
# Connect to the agent process using the socket factory and socket name
|
82
|
+
# given by the attribute writers. If the agent on the other end of the
|
83
|
+
# socket reports that it is an SSH2-compatible agent, this will fail
|
84
|
+
# (it only supports the ssh-agent distributed by OpenSSH).
|
85
|
+
def connect!(agent_socket_factory = nil, identity_agent = nil)
|
86
|
+
debug { "connecting to ssh-agent" }
|
87
|
+
@socket =
|
88
|
+
if agent_socket_factory
|
89
|
+
agent_socket_factory.call
|
90
|
+
elsif identity_agent
|
91
|
+
unix_socket_class.open(File.expand_path(identity_agent))
|
92
|
+
elsif ENV['SSH_AUTH_SOCK'] && unix_socket_class
|
93
|
+
unix_socket_class.open(File.expand_path(ENV['SSH_AUTH_SOCK']))
|
94
|
+
elsif Gem.win_platform? && RUBY_ENGINE != "jruby"
|
95
|
+
Pageant::Socket.open
|
96
|
+
else
|
97
|
+
raise AgentNotAvailable, "Agent not configured"
|
98
|
+
end
|
99
|
+
rescue StandardError => e
|
100
|
+
error { "could not connect to ssh-agent: #{e.message}" }
|
101
|
+
raise AgentNotAvailable, $!.message
|
91
102
|
end
|
92
|
-
rescue StandardError => e
|
93
|
-
error { "could not connect to ssh-agent: #{e.message}" }
|
94
|
-
raise AgentNotAvailable, $!.message
|
95
|
-
end
|
96
103
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
if type == SSH2_AGENT_FAILURE
|
105
|
-
debug { "Unexpected response type==#{type}, this will be ignored" }
|
106
|
-
elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2
|
107
|
-
raise AgentNotAvailable, "unknown response from agent: #{type}, #{body.to_s.inspect}"
|
108
|
-
end
|
109
|
-
end
|
104
|
+
# Attempts to negotiate the SSH agent protocol version. Raises an error
|
105
|
+
# if the version could not be negotiated successfully.
|
106
|
+
def negotiate!
|
107
|
+
# determine what type of agent we're communicating with
|
108
|
+
type, body = send_and_wait(SSH2_AGENT_REQUEST_VERSION, :string, Transport::ServerVersion::PROTO_VERSION)
|
109
|
+
|
110
|
+
raise AgentNotAvailable, "SSH2 agents are not yet supported" if type == SSH2_AGENT_VERSION_RESPONSE
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
raise AgentError, "could not get identity count" if agent_failed(type)
|
117
|
-
raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER
|
118
|
-
|
119
|
-
identities = []
|
120
|
-
body.read_long.times do
|
121
|
-
key_str = body.read_string
|
122
|
-
comment_str = body.read_string
|
123
|
-
begin
|
124
|
-
key = Buffer.new(key_str).read_key
|
125
|
-
key.extend(Comment)
|
126
|
-
key.comment = comment_str
|
127
|
-
identities.push key
|
128
|
-
rescue NotImplementedError => e
|
129
|
-
error { "ignoring unimplemented key:#{e.message} #{comment_str}" }
|
112
|
+
if type == SSH2_AGENT_FAILURE
|
113
|
+
debug { "Unexpected response type==#{type}, this will be ignored" }
|
114
|
+
elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2
|
115
|
+
raise AgentNotAvailable, "unknown response from agent: #{type}, #{body.to_s.inspect}"
|
116
|
+
end
|
130
117
|
end
|
131
|
-
end
|
132
118
|
|
133
|
-
|
134
|
-
|
119
|
+
# Return an array of all identities (public keys) known to the agent.
|
120
|
+
# Each key returned is augmented with a +comment+ property which is set
|
121
|
+
# to the comment returned by the agent for that key.
|
122
|
+
def identities
|
123
|
+
type, body = send_and_wait(SSH2_AGENT_REQUEST_IDENTITIES)
|
124
|
+
raise AgentError, "could not get identity count" if agent_failed(type)
|
125
|
+
raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER
|
126
|
+
|
127
|
+
identities = []
|
128
|
+
body.read_long.times do
|
129
|
+
key_str = body.read_string
|
130
|
+
comment_str = body.read_string
|
131
|
+
begin
|
132
|
+
key = Buffer.new(key_str).read_key
|
133
|
+
if key.nil?
|
134
|
+
error { "ignoring invalid key: #{comment_str}" }
|
135
|
+
next
|
136
|
+
end
|
137
|
+
key.extend(Comment)
|
138
|
+
key.comment = comment_str
|
139
|
+
identities.push key
|
140
|
+
rescue NotImplementedError => e
|
141
|
+
error { "ignoring unimplemented key:#{e.message} #{comment_str}" }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
return identities
|
146
|
+
end
|
135
147
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
148
|
+
# Closes this socket. This agent reference is no longer able to
|
149
|
+
# query the agent.
|
150
|
+
def close
|
151
|
+
@socket.close
|
152
|
+
end
|
141
153
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
154
|
+
# Using the agent and the given public key, sign the given data. The
|
155
|
+
# signature is returned in SSH2 format.
|
156
|
+
def sign(key, data, flags = 0)
|
157
|
+
type, reply = send_and_wait(SSH2_AGENT_SIGN_REQUEST, :string, Buffer.from(:key, key), :string, data, :long, flags)
|
146
158
|
|
147
|
-
|
148
|
-
|
159
|
+
raise AgentError, "agent could not sign data with requested identity" if agent_failed(type)
|
160
|
+
raise AgentError, "bad authentication response #{type}" if type != SSH2_AGENT_SIGN_RESPONSE
|
149
161
|
|
150
|
-
|
151
|
-
|
162
|
+
return reply.read_string
|
163
|
+
end
|
152
164
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
+
# Adds the private key with comment to the agent.
|
166
|
+
# If lifetime is given, the key will automatically be removed after lifetime
|
167
|
+
# seconds.
|
168
|
+
# If confirm is true, confirmation will be required for each agent signing
|
169
|
+
# operation.
|
170
|
+
def add_identity(priv_key, comment, lifetime: nil, confirm: false)
|
171
|
+
constraints = Buffer.new
|
172
|
+
if lifetime
|
173
|
+
constraints.write_byte(SSH_AGENT_CONSTRAIN_LIFETIME)
|
174
|
+
constraints.write_long(lifetime)
|
175
|
+
end
|
176
|
+
constraints.write_byte(SSH_AGENT_CONSTRAIN_CONFIRM) if confirm
|
177
|
+
|
178
|
+
req_type = constraints.empty? ? SSH2_AGENT_ADD_IDENTITY : SSH2_AGENT_ADD_ID_CONSTRAINED
|
179
|
+
type, = send_and_wait(req_type, :string, priv_key.ssh_type, :raw, blob_for_add(priv_key),
|
180
|
+
:string, comment, :raw, constraints)
|
181
|
+
raise AgentError, "could not add identity to agent" if type != SSH_AGENT_SUCCESS
|
182
|
+
end
|
165
183
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
184
|
+
# Removes key from the agent.
|
185
|
+
def remove_identity(key)
|
186
|
+
type, = send_and_wait(SSH2_AGENT_REMOVE_IDENTITY, :string, key.to_blob)
|
187
|
+
raise AgentError, "could not remove identity from agent" if type != SSH_AGENT_SUCCESS
|
188
|
+
end
|
171
189
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
190
|
+
# Removes all identities from the agent.
|
191
|
+
def remove_all_identities
|
192
|
+
type, = send_and_wait(SSH2_AGENT_REMOVE_ALL_IDENTITIES)
|
193
|
+
raise AgentError, "could not remove all identity from agent" if type != SSH_AGENT_SUCCESS
|
194
|
+
end
|
177
195
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
196
|
+
# lock the ssh agent with password
|
197
|
+
def lock(password)
|
198
|
+
type, = send_and_wait(SSH2_AGENT_LOCK, :string, password)
|
199
|
+
raise AgentError, "could not lock agent" if type != SSH_AGENT_SUCCESS
|
200
|
+
end
|
183
201
|
|
184
|
-
|
202
|
+
# unlock the ssh agent with password
|
203
|
+
def unlock(password)
|
204
|
+
type, = send_and_wait(SSH2_AGENT_UNLOCK, :string, password)
|
205
|
+
raise AgentError, "could not unlock agent" if type != SSH_AGENT_SUCCESS
|
206
|
+
end
|
185
207
|
|
186
|
-
|
187
|
-
defined?(UNIXSocket) && UNIXSocket
|
188
|
-
end
|
208
|
+
private
|
189
209
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
data = [buffer.length + 1, type.to_i, buffer.to_s].pack("NCA*")
|
194
|
-
debug { "sending agent request #{type} len #{buffer.length}" }
|
195
|
-
@socket.send data, 0
|
196
|
-
end
|
210
|
+
def unix_socket_class
|
211
|
+
defined?(UNIXSocket) && UNIXSocket
|
212
|
+
end
|
197
213
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
debug { "received agent packet #{type} len #{buffer.length-4}" }
|
206
|
-
return type, buffer
|
207
|
-
end
|
214
|
+
# Send a new packet of the given type, with the associated data.
|
215
|
+
def send_packet(type, *args)
|
216
|
+
buffer = Buffer.from(*args)
|
217
|
+
data = [buffer.length + 1, type.to_i, buffer.to_s].pack("NCA*")
|
218
|
+
debug { "sending agent request #{type} len #{buffer.length}" }
|
219
|
+
@socket.send data, 0
|
220
|
+
end
|
208
221
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
222
|
+
# Read the next packet from the agent. This will return a two-part
|
223
|
+
# tuple consisting of the packet type, and the packet's body (which
|
224
|
+
# is returned as a Net::SSH::Buffer).
|
225
|
+
def read_packet
|
226
|
+
buffer = Net::SSH::Buffer.new(@socket.read(4))
|
227
|
+
buffer.append(@socket.read(buffer.read_long))
|
228
|
+
type = buffer.read_byte
|
229
|
+
debug { "received agent packet #{type} len #{buffer.length - 4}" }
|
230
|
+
return type, buffer
|
231
|
+
end
|
215
232
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
end
|
233
|
+
# Send the given packet and return the subsequent reply from the agent.
|
234
|
+
# (See #send_packet and #read_packet).
|
235
|
+
def send_and_wait(type, *args)
|
236
|
+
send_packet(type, *args)
|
237
|
+
read_packet
|
238
|
+
end
|
223
239
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
240
|
+
# Returns +true+ if the parameter indicates a "failure" response from
|
241
|
+
# the agent, and +false+ otherwise.
|
242
|
+
def agent_failed(type)
|
243
|
+
type == SSH_AGENT_FAILURE ||
|
244
|
+
type == SSH2_AGENT_FAILURE ||
|
245
|
+
type == SSH_COM_AGENT2_FAILURE
|
246
|
+
end
|
247
|
+
|
248
|
+
def blob_for_add(priv_key)
|
249
|
+
# Ideally we'd have something like `to_private_blob` on the various key types, but the
|
250
|
+
# nuances with encoding (e.g. `n` and `e` are reversed for RSA keys) make this impractical.
|
251
|
+
case priv_key.ssh_type
|
252
|
+
when /^ssh-dss$/
|
253
|
+
Net::SSH::Buffer.from(:bignum, priv_key.p, :bignum, priv_key.q, :bignum, priv_key.g,
|
254
|
+
:bignum, priv_key.pub_key, :bignum, priv_key.priv_key).to_s
|
255
|
+
when /^ssh-dss-cert-v01@openssh\.com$/
|
256
|
+
Net::SSH::Buffer.from(:string, priv_key.to_blob, :bignum, priv_key.key.priv_key).to_s
|
257
|
+
when /^ecdsa\-sha2\-(\w*)$/
|
258
|
+
curve_name = OpenSSL::PKey::EC::CurveNameAliasInv[priv_key.group.curve_name]
|
259
|
+
Net::SSH::Buffer.from(:string, curve_name, :mstring, priv_key.public_key.to_bn.to_s(2),
|
260
|
+
:bignum, priv_key.private_key).to_s
|
261
|
+
when /^ecdsa\-sha2\-(\w*)-cert-v01@openssh\.com$/
|
262
|
+
Net::SSH::Buffer.from(:string, priv_key.to_blob, :bignum, priv_key.key.private_key).to_s
|
263
|
+
when /^ssh-ed25519$/
|
264
|
+
Net::SSH::Buffer.from(:string, priv_key.public_key.verify_key.to_bytes,
|
265
|
+
:string, priv_key.sign_key.keypair).to_s
|
266
|
+
when /^ssh-ed25519-cert-v01@openssh\.com$/
|
267
|
+
# Unlike the other certificate types, the public key is included after the certifiate.
|
268
|
+
Net::SSH::Buffer.from(:string, priv_key.to_blob,
|
269
|
+
:string, priv_key.key.public_key.verify_key.to_bytes,
|
270
|
+
:string, priv_key.key.sign_key.keypair).to_s
|
271
|
+
when /^ssh-rsa$/
|
272
|
+
# `n` and `e` are reversed compared to the ordering in `OpenSSL::PKey::RSA#to_blob`.
|
273
|
+
Net::SSH::Buffer.from(:bignum, priv_key.n, :bignum, priv_key.e, :bignum, priv_key.d,
|
274
|
+
:bignum, priv_key.iqmp, :bignum, priv_key.p, :bignum, priv_key.q).to_s
|
275
|
+
when /^ssh-rsa-cert-v01@openssh\.com$/
|
276
|
+
Net::SSH::Buffer.from(:string, priv_key.to_blob, :bignum, priv_key.key.d,
|
277
|
+
:bignum, priv_key.key.iqmp, :bignum, priv_key.key.p,
|
278
|
+
:bignum, priv_key.key.q).to_s
|
279
|
+
end
|
280
|
+
end
|
255
281
|
end
|
256
282
|
end
|
257
283
|
end
|
258
|
-
|
259
|
-
end; end; end
|
284
|
+
end
|