class Net::SSH::Transport::Algorithms

Implements the higher-level logic behind an SSH key-exchange. It handles both the initial exchange, as well as subsequent re-exchanges (as needed). It also encapsulates the negotiation of the algorithms, and provides a single point of access to the negotiated algorithms.

You will never instantiate or reference this directly. It is used internally by the transport layer.

Public Class Methods

allowed_packet?(packet) click to toggle source

Returns true if the given packet can be processed during a key-exchange.

# File lib/net/ssh/transport/algorithms.rb, line 104
def self.allowed_packet?(packet)
  ( 1.. 4).include?(packet.type) ||
  ( 6..19).include?(packet.type) ||
  (21..49).include?(packet.type)
end
new(session, options={}) click to toggle source

Instantiates a new Algorithms object, and prepares the hash of preferred algorithms based on the options parameter and the ALGORITHMS constant.

# File lib/net/ssh/transport/algorithms.rb, line 112
def initialize(session, options={})
  @session = session
  @logger = session.logger
  @options = options
  @algorithms = {}
  @pending = @initialized = false
  @client_packet = @server_packet = nil
  prepare_preferred_algorithms!
end

Public Instance Methods

[](key) click to toggle source

A convenience method for accessing the list of preferred types for a specific algorithm (see algorithms).

# File lib/net/ssh/transport/algorithms.rb, line 155
def [](key)
  algorithms[key]
end
accept_kexinit(packet) click to toggle source

Called by the transport layer when a KEXINIT packet is received, indicating that the server wants to exchange keys. This can be spontaneous, or it can be in response to a client-initiated rekey request (see rekey!). Either way, this will block until the key exchange completes.

# File lib/net/ssh/transport/algorithms.rb, line 142
def accept_kexinit(packet)
  info { "got KEXINIT from server" }
  @server_data = parse_server_algorithm_packet(packet)
  @server_packet = @server_data[:raw]
  if !pending?
    send_kexinit
  else
    proceed!
  end
end
allow?(packet) click to toggle source

Returns true if no exchange is pending, and otherwise returns true or false depending on whether the given packet is of a type that is allowed during a key exchange.

# File lib/net/ssh/transport/algorithms.rb, line 171
def allow?(packet)
  !pending? || Algorithms.allowed_packet?(packet)
end
initialized?() click to toggle source

Returns true if the algorithms have been negotiated at all.

# File lib/net/ssh/transport/algorithms.rb, line 176
def initialized?
  @initialized
end
pending?() click to toggle source

Returns true if a key-exchange is pending. This will be true from the moment either the client or server requests the key exchange, until the exchange completes. While an exchange is pending, only a limited number of packets are allowed, so event processing essentially stops during this period.

# File lib/net/ssh/transport/algorithms.rb, line 164
def pending?
  @pending
end
rekey!() click to toggle source

Request a rekey operation. This will return immediately, and does not actually perform the rekey operation. It does cause the session to change state, however–until the key exchange finishes, no new packets will be processed.

# File lib/net/ssh/transport/algorithms.rb, line 132
def rekey!
  @client_packet = @server_packet = nil
  @initialized = false
  send_kexinit
end
start() click to toggle source

Start the algorithm negotation

# File lib/net/ssh/transport/algorithms.rb, line 123
def start
  raise ArgumentError, "Cannot call start if it's negotiation started or done" if @pending || @initialized
  send_kexinit
end