Class: SSH::Key::Signer

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ssh/key/signer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#add_key_file, #add_key_from_host, #add_public_key_data

Constructor Details

#initializeSigner

Returns a new instance of Signer.



17
18
19
20
21
22
23
# File 'lib/ssh/key/signer.rb', line 17

def initialize
  @agent = Net::SSH::Authentication::Agent.new
  @use_agent = true
  @logger = Logger.new(STDERR)
  @logger.level = Logger::WARN
  @keys = []
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



12
13
14
# File 'lib/ssh/key/signer.rb', line 12

def 
  @account
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/ssh/key/signer.rb', line 14

def logger
  @logger
end

#sshd_config_fileObject

Returns the value of attribute sshd_config_file.



13
14
15
# File 'lib/ssh/key/signer.rb', line 13

def sshd_config_file
  @sshd_config_file
end

#use_agentObject

Returns the value of attribute use_agent.



15
16
17
# File 'lib/ssh/key/signer.rb', line 15

def use_agent
  @use_agent
end

Instance Method Details

#ensure_connectedObject

def initialize



25
26
27
28
29
30
31
# File 'lib/ssh/key/signer.rb', line 25

def ensure_connected
  begin
    @agent.connect! if !@agent.socket
  rescue Net::SSH::Authentication::AgentNotAvailable => e
    @use_agent = false
  end
end

#sign(string) ⇒ Object

Signs a string with all available ssh keys

  • string - the value to sign

Returns an array of SSH::Key::Signature objects

‘identity’ on each object is an openssl key instance of one of these typs:

  • OpenSSL::PKey::RSA

  • OpenSSL::PKey::DSA

  • OpenSSL::PKey::DH

Net::SSH monkeypatches the above classes to add additional methods, so just be aware.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ssh/key/signer.rb', line 46

def sign(string)
  identities = signing_identities 
  signatures = []
  identities.each do |identity|
    if identity.private?
      # FYI: OpenSSL::PKey::RSA#ssh_type and #ssh_do_sign are monkeypatched
      # by Net::SSH
      signature = SSH::Key::Signature.new
      signature.type = identity.ssh_type
      signature.signature = identity.ssh_do_sign(string)
    else
      # Only public signing identities come from our agent.
      signature = SSH::Key::Signature.from_string(@agent.sign(identity, string))
    end
    signature.identity = identity
    signatures << signature
  end
  return signatures
end

#signing_identitiesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ssh/key/signer.rb', line 66

def signing_identities
  identities = []
  if @use_agent
    ensure_connected
    begin
      @agent.identities.each { |id| identities << id }
    rescue => e
      @logger.warn("Error talking to agent while asking for message signing. Disabling agent (Error: #{e})")
      @use_agent = false
    end
  end

  if @keys
    @keys.each { |id| identities << id }
  end
  return identities
end