Class Net::SSH::Authentication::Agent

  1. lib/net/ssh/authentication/agent.rb
Parent: Object

This class implements a simple client for the ssh-agent protocol. It does not implement any specific protocol, but instead copies the behavior of the ssh-agent functions in the OpenSSH library (3.8).

This means that although it behaves like a SSH1 client, it also has some SSH2 functionality (like signing data).

Methods

public class

  1. connect
  2. new

public instance

  1. close
  2. connect!
  3. identities
  4. negotiate!
  5. sign

Included modules

  1. Loggable

Classes and Modules

Module Net::SSH::Authentication::Agent::Comment

Constants

SSH2_AGENT_REQUEST_VERSION = 1
SSH2_AGENT_REQUEST_IDENTITIES = 11
SSH2_AGENT_IDENTITIES_ANSWER = 12
SSH2_AGENT_SIGN_REQUEST = 13
SSH2_AGENT_SIGN_RESPONSE = 14
SSH2_AGENT_FAILURE = 30
SSH2_AGENT_VERSION_RESPONSE = 103
SSH_COM_AGENT2_FAILURE = 102
SSH_AGENT_REQUEST_RSA_IDENTITIES = 1
SSH_AGENT_RSA_IDENTITIES_ANSWER1 = 2
SSH_AGENT_RSA_IDENTITIES_ANSWER2 = 5
SSH_AGENT_FAILURE = 5

Attributes

socket [R] The underlying socket being used to communicate with the SSH agent.

Public class methods

connect (logger=nil)

Instantiates a new agent object, connects to a running SSH agent, negotiates the agent protocol version, and returns the agent object.

[show source]
    # File lib/net/ssh/authentication/agent.rb, line 54
54:     def self.connect(logger=nil)
55:       agent = new(logger)
56:       agent.connect!
57:       agent.negotiate!
58:       agent
59:     end
new (logger=nil)

Creates a new Agent object, using the optional logger instance to report status.

[show source]
    # File lib/net/ssh/authentication/agent.rb, line 63
63:     def initialize(logger=nil)
64:       self.logger = logger
65:     end

Public instance methods

close ()

Closes this socket. This agent reference is no longer able to query the agent.

[show source]
     # File lib/net/ssh/authentication/agent.rb, line 115
115:     def close
116:       @socket.close
117:     end
connect! ()

Connect to the agent process using the socket factory and socket name given by the attribute writers. If the agent on the other end of the socket reports that it is an SSH2-compatible agent, this will fail (it only supports the ssh-agent distributed by OpenSSH).

[show source]
    # File lib/net/ssh/authentication/agent.rb, line 71
71:     def connect!
72:       begin
73:         debug { "connecting to ssh-agent" }
74:         @socket = agent_socket_factory.open(ENV['SSH_AUTH_SOCK'])
75:       rescue
76:         error { "could not connect to ssh-agent" }
77:         raise AgentNotAvailable, $!.message
78:       end
79:     end
identities ()

Return an array of all identities (public keys) known to the agent. Each key returned is augmented with a comment property which is set to the comment returned by the agent for that key.

[show source]
     # File lib/net/ssh/authentication/agent.rb, line 97
 97:     def identities
 98:       type, body = send_and_wait(SSH2_AGENT_REQUEST_IDENTITIES)
 99:       raise AgentError, "could not get identity count" if agent_failed(type)
100:       raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER
101: 
102:       identities = []
103:       body.read_long.times do
104:         key = Buffer.new(body.read_string).read_key
105:         key.extend(Comment)
106:         key.comment = body.read_string
107:         identities.push key
108:       end
109: 
110:       return identities
111:     end
negotiate! ()

Attempts to negotiate the SSH agent protocol version. Raises an error if the version could not be negotiated successfully.

[show source]
    # File lib/net/ssh/authentication/agent.rb, line 83
83:     def negotiate!
84:       # determine what type of agent we're communicating with
85:       type, body = send_and_wait(SSH2_AGENT_REQUEST_VERSION, :string, Transport::ServerVersion::PROTO_VERSION)
86: 
87:       if type == SSH2_AGENT_VERSION_RESPONSE
88:         raise NotImplementedError, "SSH2 agents are not yet supported"
89:       elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2
90:         raise AgentError, "unknown response from agent: #{type}, #{body.to_s.inspect}"
91:       end
92:     end
sign (key, data)

Using the agent and the given public key, sign the given data. The signature is returned in SSH2 format.

[show source]
     # File lib/net/ssh/authentication/agent.rb, line 121
121:     def sign(key, data)
122:       type, reply = send_and_wait(SSH2_AGENT_SIGN_REQUEST, :string, Buffer.from(:key, key), :string, data, :long, 0)
123: 
124:       if agent_failed(type)
125:         raise AgentError, "agent could not sign data with requested identity"
126:       elsif type != SSH2_AGENT_SIGN_RESPONSE
127:         raise AgentError, "bad authentication response #{type}"
128:       end
129: 
130:       return reply.read_string
131:     end