Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions lib/ssh/key/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,58 @@ def add_key_file(path, passphrase=nil)

# Add a public key from your known_hosts file
def add_key_from_host(hostname)
hostkey = %x{ssh-keygen -F "#{hostname}"}.split("\n")[1].chomp.split(" ",2)[-1] rescue nil
hostkey = SSH::Key::Helper.known_host_key('/etc/ssh/ssh_known_hosts', hostname)
if hostkey == nil
raise SSH::Key::KeyNotFound.new("Could not find host key '#{hostname}' " \
"in known_hosts (using ssh-keygen -F)")
hostkey = SSH::Key::Helper.known_host_key('~/.ssh/known_hosts', hostname)
if hostkey == nil
SSH::Key::KeyNotFound.new("Could not find host key '#{hostname}' " \
"in known_hosts file")
end
end
@keys << Net::SSH::KeyFactory.load_data_public_key(hostkey)
@keys << hostkey
end

# Add a public key from a ublic key string
def add_public_key_data(data)
@logger.info "Adding key from data #{data}"
@keys << Net::SSH::KeyFactory.load_data_public_key(data)
end # def add_key_file

def self.known_host_key(host_file, hostname)
@@known_hosts ||= {}
keys = @@known_hosts[host_file] ||= read_keys(host_file)
keys[hostname]
end

# adapted from Net::SSH::KnownHosts#keys_for(host)
def self.read_keys(host_file)
keys = {}
return keys unless File.readable?(host_file)

File.open(host_file) do |file|
scanner = StringScanner.new("")
file.each_line do |line|
scanner.string = line

scanner.skip(/\s*/)
next if scanner.match?(/$|#/)

hostlist = scanner.scan(/\S+/).split(/,/)

scanner.skip(/\s*/)
type = scanner.scan(/\S+/)

next unless Net::SSH::KnownHosts::SUPPORTED_TYPE.include?(type)

scanner.skip(/\s*/)
blob = scanner.rest.unpack("m*").first
key = Net::SSH::Buffer.new(blob).read_key

# only store first key
hostlist.each {|host| keys[host] ||= key }
end
end

keys
end
end; end; end # module SSH::Key::Helper
1 change: 1 addition & 0 deletions lib/ssh/key/verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def verify(signatures, original)

inputs.each do |signature|
identities.each do |identity|
next unless identity
key = [signature, identity]
results[key] = identity.ssh_do_verify(signature, original)
@logger.info "Trying key #{identity.to_s.split("\n")[1]}... #{results[key]}"
Expand Down