Class: HrrRbSsh::Transport

Inherits:
Object
  • Object
show all
Includes:
Loggable, Constant
Defined in:
lib/hrr_rb_ssh/transport.rb,
lib/hrr_rb_ssh/transport/sender.rb,
lib/hrr_rb_ssh/transport/constant.rb,
lib/hrr_rb_ssh/transport/receiver.rb,
lib/hrr_rb_ssh/transport/direction.rb,
lib/hrr_rb_ssh/transport/kex_algorithm.rb,
lib/hrr_rb_ssh/transport/mac_algorithm.rb,
lib/hrr_rb_ssh/transport/sequence_number.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/none.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm.rb,
lib/hrr_rb_ssh/transport/compression_algorithm.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_md5.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_sha1.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/none.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_md5_96.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm.rb,
lib/hrr_rb_ssh/transport/compression_algorithm/none.rb,
lib/hrr_rb_ssh/transport/compression_algorithm/zlib.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/functionable.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_sha1_96.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/iv_computable.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_sha2_256.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/hmac_sha2_512.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/arcfour.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman.rb,
lib/hrr_rb_ssh/transport/mac_algorithm/unfunctionable.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes128_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes128_ctr.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes192_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes192_ctr.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes256_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/aes256_ctr.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman/h0.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/cast128_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/blowfish_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/functionable.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/ssh_dss.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/ssh_rsa.rb,
lib/hrr_rb_ssh/transport/compression_algorithm/functionable.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/three_des_cbc.rb,
lib/hrr_rb_ssh/transport/encryption_algorithm/unfunctionable.rb,
lib/hrr_rb_ssh/transport/compression_algorithm/unfunctionable.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/functionable.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group1_sha1.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group14_sha1.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group14_sha256.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group15_sha512.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group16_sha512.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group17_sha512.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group18_sha512.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group_exchange.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/ecdsa_sha2_nistp256.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/ecdsa_sha2_nistp384.rb,
lib/hrr_rb_ssh/transport/server_host_key_algorithm/ecdsa_sha2_nistp521.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group_exchange/h0.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman/h0.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group_exchange_sha1.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/diffie_hellman_group_exchange_sha256.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman_sha2_nistp256.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman_sha2_nistp384.rb,
lib/hrr_rb_ssh/transport/kex_algorithm/elliptic_curve_diffie_hellman_sha2_nistp521.rb

Defined Under Namespace

Modules: Constant, Direction Classes: CompressionAlgorithm, EncryptionAlgorithm, KexAlgorithm, MacAlgorithm, Receiver, Sender, SequenceNumber, ServerHostKeyAlgorithm

Constant Summary

Constants included from Constant

Constant::CR, Constant::LF

Instance Attribute Summary collapse

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Constructor Details

#initialize(io, mode, options = {}, logger: nil) ⇒ Transport

Returns a new instance of Transport.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hrr_rb_ssh/transport.rb', line 54

def initialize io, mode, options={}, logger: nil
  self.logger = logger

  @io = io
  @mode = mode
  @options = options

  @closed = nil
  @disconnected = nil

  @in_kex = false

  @sender   = Sender.new logger: logger
  @receiver = Receiver.new logger: logger

  @sender_monitor   = Monitor.new
  @receiver_monitor = Monitor.new

  @local_version  = @options.delete('local_version') || "SSH-2.0-HrrRbSsh-#{VERSION}".force_encoding(Encoding::ASCII_8BIT)
  @remote_version = "".force_encoding(Encoding::ASCII_8BIT)

  @incoming_sequence_number = SequenceNumber.new
  @outgoing_sequence_number = SequenceNumber.new

  @acceptable_services = Array.new

  update_supported_algorithms
  update_preferred_algorithms
  initialize_local_algorithms
  initialize_algorithms
end

Instance Attribute Details

#i_cObject (readonly)

Returns the value of attribute i_c.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def i_c
  @i_c
end

#i_sObject (readonly)

Returns the value of attribute i_s.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def i_s
  @i_s
end

#incoming_compression_algorithmObject (readonly)

Returns the value of attribute incoming_compression_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def incoming_compression_algorithm
  @incoming_compression_algorithm
end

#incoming_encryption_algorithmObject (readonly)

Returns the value of attribute incoming_encryption_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def incoming_encryption_algorithm
  @incoming_encryption_algorithm
end

#incoming_mac_algorithmObject (readonly)

Returns the value of attribute incoming_mac_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def incoming_mac_algorithm
  @incoming_mac_algorithm
end

#incoming_sequence_numberObject (readonly)

Returns the value of attribute incoming_sequence_number.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def incoming_sequence_number
  @incoming_sequence_number
end

#ioObject (readonly)

Returns the value of attribute io.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def io
  @io
end

#modeObject (readonly)

Returns the value of attribute mode.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def mode
  @mode
end

#outgoing_compression_algorithmObject (readonly)

Returns the value of attribute outgoing_compression_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def outgoing_compression_algorithm
  @outgoing_compression_algorithm
end

#outgoing_encryption_algorithmObject (readonly)

Returns the value of attribute outgoing_encryption_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def outgoing_encryption_algorithm
  @outgoing_encryption_algorithm
end

#outgoing_mac_algorithmObject (readonly)

Returns the value of attribute outgoing_mac_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def outgoing_mac_algorithm
  @outgoing_mac_algorithm
end

#outgoing_sequence_numberObject (readonly)

Returns the value of attribute outgoing_sequence_number.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def outgoing_sequence_number
  @outgoing_sequence_number
end

#preferred_compression_algorithmsObject (readonly)

Returns the value of attribute preferred_compression_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def preferred_compression_algorithms
  @preferred_compression_algorithms
end

#preferred_encryption_algorithmsObject (readonly)

Returns the value of attribute preferred_encryption_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def preferred_encryption_algorithms
  @preferred_encryption_algorithms
end

#preferred_kex_algorithmsObject (readonly)

Returns the value of attribute preferred_kex_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def preferred_kex_algorithms
  @preferred_kex_algorithms
end

#preferred_mac_algorithmsObject (readonly)

Returns the value of attribute preferred_mac_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def preferred_mac_algorithms
  @preferred_mac_algorithms
end

#preferred_server_host_key_algorithmsObject (readonly)

Returns the value of attribute preferred_server_host_key_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def preferred_server_host_key_algorithms
  @preferred_server_host_key_algorithms
end

#server_host_key_algorithmObject (readonly)

Returns the value of attribute server_host_key_algorithm.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def server_host_key_algorithm
  @server_host_key_algorithm
end

#session_idObject (readonly)

Returns the value of attribute session_id.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def session_id
  @session_id
end

#supported_compression_algorithmsObject (readonly)

Returns the value of attribute supported_compression_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def supported_compression_algorithms
  @supported_compression_algorithms
end

#supported_encryption_algorithmsObject (readonly)

Returns the value of attribute supported_encryption_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def supported_encryption_algorithms
  @supported_encryption_algorithms
end

#supported_kex_algorithmsObject (readonly)

Returns the value of attribute supported_kex_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def supported_kex_algorithms
  @supported_kex_algorithms
end

#supported_mac_algorithmsObject (readonly)

Returns the value of attribute supported_mac_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def supported_mac_algorithms
  @supported_mac_algorithms
end

#supported_server_host_key_algorithmsObject (readonly)

Returns the value of attribute supported_server_host_key_algorithms.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def supported_server_host_key_algorithms
  @supported_server_host_key_algorithms
end

#v_cObject (readonly)

Returns the value of attribute v_c.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def v_c
  @v_c
end

#v_sObject (readonly)

Returns the value of attribute v_s.



26
27
28
# File 'lib/hrr_rb_ssh/transport.rb', line 26

def v_s
  @v_s
end

Instance Method Details

#check_if_preferred_algorithms_are_supportedObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/hrr_rb_ssh/transport.rb', line 273

def check_if_preferred_algorithms_are_supported
  [
    ['kex',             @preferred_kex_algorithms,             @supported_kex_algorithms            ],
    ['server host key', @preferred_server_host_key_algorithms, @supported_server_host_key_algorithms],
    ['encryption',      @preferred_encryption_algorithms,      @supported_encryption_algorithms     ],
    ['mac',             @preferred_mac_algorithms,             @supported_mac_algorithms            ],
    ['compression',     @preferred_compression_algorithms,     @supported_compression_algorithms    ],
  ].each{ |algorithm_name, list_preferred, list_supported|
    list_preferred.each{ |a|
      unless list_supported.include? a
        raise ArgumentError, "#{algorithm_name} algorithm #{a} is not supported"
      end
    }
  }
end

#closeObject



187
188
189
190
191
192
193
194
195
# File 'lib/hrr_rb_ssh/transport.rb', line 187

def close
  return if @closed
  log_info { "close transport" }
  @closed = true
  disconnect
  @incoming_compression_algorithm.close
  @outgoing_compression_algorithm.close
  log_info { "transport closed" }
end

#closed?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/hrr_rb_ssh/transport.rb', line 197

def closed?
  @closed
end

#disconnectObject



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/hrr_rb_ssh/transport.rb', line 201

def disconnect
  return if @disconnected
  log_info { "disconnect transport" }
  @disconnected = true
  begin
    send_disconnect
  rescue IOError
    log_warn { "IO is closed" }
  rescue => e
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
  end
  log_info { "transport disconnected" }
end

#exchange_key(payload = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hrr_rb_ssh/transport.rb', line 221

def exchange_key payload=nil
  @in_kex = true
  @sender_monitor.synchronize do
    @receiver_monitor.synchronize do
      send_kexinit
      if payload
        receive_kexinit payload
      else
        receive_kexinit receive
      end
      update_kex_and_server_host_key_algorithms
      start_kex_algorithm
      send_newkeys
      receive_newkeys receive
      update_encryption_mac_compression_algorithms
    end
  end
  @in_kex = false
end

#exchange_versionObject



215
216
217
218
219
# File 'lib/hrr_rb_ssh/transport.rb', line 215

def exchange_version
  send_version
  receive_version
  update_version_strings
end

#initialize_algorithmsObject



300
301
302
303
304
305
306
307
308
# File 'lib/hrr_rb_ssh/transport.rb', line 300

def initialize_algorithms
  @incoming_encryption_algorithm  = EncryptionAlgorithm['none'].new
  @incoming_mac_algorithm         = MacAlgorithm['none'].new
  @incoming_compression_algorithm = CompressionAlgorithm['none'].new

  @outgoing_encryption_algorithm  = EncryptionAlgorithm['none'].new
  @outgoing_mac_algorithm         = MacAlgorithm['none'].new
  @outgoing_compression_algorithm = CompressionAlgorithm['none'].new
end

#initialize_local_algorithmsObject



289
290
291
292
293
294
295
296
297
298
# File 'lib/hrr_rb_ssh/transport.rb', line 289

def initialize_local_algorithms
  @local_kex_algorithms                          = @preferred_kex_algorithms
  @local_server_host_key_algorithms              = @preferred_server_host_key_algorithms
  @local_encryption_algorithms_client_to_server  = @preferred_encryption_algorithms
  @local_encryption_algorithms_server_to_client  = @preferred_encryption_algorithms
  @local_mac_algorithms_client_to_server         = @preferred_mac_algorithms
  @local_mac_algorithms_server_to_client         = @preferred_mac_algorithms
  @local_compression_algorithms_client_to_server = @preferred_compression_algorithms
  @local_compression_algorithms_server_to_client = @preferred_compression_algorithms
end

#receiveObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hrr_rb_ssh/transport.rb', line 106

def receive
  raise Error::ClosedTransport if @closed
  @receiver_monitor.synchronize do
    begin
      payload = @receiver.receive self
      case payload[0,1].unpack("C")[0]
      when Message::SSH_MSG_DISCONNECT::VALUE
        message = Message::SSH_MSG_DISCONNECT.new(logger: logger).decode payload
        log_debug { "received disconnect message: #{message.inspect}" }
        @disconnected = true
        close
        raise Error::ClosedTransport
      when Message::SSH_MSG_IGNORE::VALUE
        message = Message::SSH_MSG_IGNORE.new(logger: logger).decode payload
        log_debug { "received ignore message: #{message.inspect}" }
        receive
      when Message::SSH_MSG_UNIMPLEMENTED::VALUE
        message = Message::SSH_MSG_UNIMPLEMENTED.new(logger: logger).decode payload
        log_debug { "received unimplemented message: #{message.inspect}" }
        receive
      when Message::SSH_MSG_DEBUG::VALUE
        message = Message::SSH_MSG_DEBUG.new(logger: logger).decode payload
        log_debug { "received debug message: #{message.inspect}" }
        receive
      when Message::SSH_MSG_KEXINIT::VALUE
        log_debug { "received kexinit message" }
        if @in_kex
          payload
        else
          exchange_key payload
          receive
        end
      else
        payload
      end
    rescue Error::ClosedTransport
      raise Error::ClosedTransport
    rescue EOFError => e
      close
      raise Error::ClosedTransport
    rescue IOError => e
      log_warn { "IO is closed" }
      close
      raise Error::ClosedTransport
    rescue Errno::ECONNRESET => e
      log_warn { "IO is RESET" }
      close
      raise Error::ClosedTransport
    rescue => e
      log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
      close
      raise Error::ClosedTransport
    end
  end
end

#receive_kexinit(payload) ⇒ Object



382
383
384
385
386
387
388
389
390
391
# File 'lib/hrr_rb_ssh/transport.rb', line 382

def receive_kexinit payload
  case @mode
  when Mode::SERVER
    @i_c = payload
  when Mode::CLIENT
    @i_s = payload
  end
  message = Message::SSH_MSG_KEXINIT.new(logger: logger).decode payload
  update_remote_algorithms message
end

#receive_newkeys(payload) ⇒ Object



401
402
403
# File 'lib/hrr_rb_ssh/transport.rb', line 401

def receive_newkeys payload
  message = Message::SSH_MSG_NEWKEYS.new(logger: logger).decode payload
end

#receive_service_requestObject



417
418
419
420
421
422
# File 'lib/hrr_rb_ssh/transport.rb', line 417

def receive_service_request
  payload = @receiver.receive self
  message = Message::SSH_MSG_SERVICE_REQUEST.new(logger: logger).decode payload

  message
end

#receive_versionObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/hrr_rb_ssh/transport.rb', line 314

def receive_version
  str_io = StringIO.new
  loop do
    str_io.write @io.read(1)
    if str_io.string[-2..-1] == "#{CR}#{LF}"
      if str_io.string[0..3] == "SSH-"
        @remote_version = str_io.string[0..-3]
        log_info { "received remote version string: #{@remote_version}" }
        break
      else
        log_info { "received message before remote version string: #{str_io.string}" }
        str_io.rewind
        str_io.truncate(0)
      end
    end
  end
end

#register_acceptable_service(service_name) ⇒ Object



86
87
88
# File 'lib/hrr_rb_ssh/transport.rb', line 86

def register_acceptable_service service_name
  @acceptable_services.push service_name
end

#send(payload) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hrr_rb_ssh/transport.rb', line 90

def send payload
  @sender_monitor.synchronize do
    begin
      @sender.send self, payload
    rescue Errno::EPIPE => e
      log_warn { "IO is Broken PIPE" }
      close
      raise Error::ClosedTransport
    rescue => e
      log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
      close
      raise Error::ClosedTransport
    end
  end
end

#send_disconnectObject



343
344
345
346
347
348
349
350
351
352
# File 'lib/hrr_rb_ssh/transport.rb', line 343

def send_disconnect
  message = {
    :'message number' => Message::SSH_MSG_DISCONNECT::VALUE,
    :'reason code'    => Message::SSH_MSG_DISCONNECT::ReasonCode::SSH_DISCONNECT_BY_APPLICATION,
    :'description'    => "disconnected by user",
    :'language tag'   => ""
  }
  payload = Message::SSH_MSG_DISCONNECT.new(logger: logger).encode message
  send payload
end

#send_kexinitObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/hrr_rb_ssh/transport.rb', line 354

def send_kexinit
  message = {
    :'message number'                          => Message::SSH_MSG_KEXINIT::VALUE,
    :'cookie (random byte)'                    => lambda { rand(0x01_00) },
    :'kex_algorithms'                          => @local_kex_algorithms,
    :'server_host_key_algorithms'              => @local_server_host_key_algorithms,
    :'encryption_algorithms_client_to_server'  => @local_encryption_algorithms_client_to_server,
    :'encryption_algorithms_server_to_client'  => @local_encryption_algorithms_server_to_client,
    :'mac_algorithms_client_to_server'         => @local_mac_algorithms_client_to_server,
    :'mac_algorithms_server_to_client'         => @local_mac_algorithms_server_to_client,
    :'compression_algorithms_client_to_server' => @local_compression_algorithms_client_to_server,
    :'compression_algorithms_server_to_client' => @local_compression_algorithms_server_to_client,
    :'languages_client_to_server'              => [],
    :'languages_server_to_client'              => [],
    :'first_kex_packet_follows'                => false,
    :'0 (reserved for future extension)'       => 0,
  }
  payload = Message::SSH_MSG_KEXINIT.new(logger: logger).encode message
  send payload

  case @mode
  when Mode::SERVER
    @i_s = payload
  when Mode::CLIENT
    @i_c = payload
  end
end

#send_newkeysObject



393
394
395
396
397
398
399
# File 'lib/hrr_rb_ssh/transport.rb', line 393

def send_newkeys
    message = {
      :'message number' => Message::SSH_MSG_NEWKEYS::VALUE,
    }
    payload = Message::SSH_MSG_NEWKEYS.new(logger: logger).encode message
    send payload
end

#send_service_accept(service_name) ⇒ Object



424
425
426
427
428
429
430
431
# File 'lib/hrr_rb_ssh/transport.rb', line 424

def send_service_accept service_name
  message = {
    :'message number' => Message::SSH_MSG_SERVICE_ACCEPT::VALUE,
    :'service name'   => service_name,
  }
  payload = Message::SSH_MSG_SERVICE_ACCEPT.new(logger: logger).encode message
  send payload
end

#send_service_requestObject



405
406
407
408
409
410
411
412
413
414
415
# File 'lib/hrr_rb_ssh/transport.rb', line 405

def send_service_request
  message = {
    :'message number' => Message::SSH_MSG_SERVICE_REQUEST::VALUE,
    :'service name' => 'ssh-userauth',
  }
  payload = Message::SSH_MSG_SERVICE_REQUEST.new(logger: logger).encode message
  send payload

  payload = @receiver.receive self
  message = Message::SSH_MSG_SERVICE_ACCEPT.new(logger: logger).decode payload
end

#send_versionObject



310
311
312
# File 'lib/hrr_rb_ssh/transport.rb', line 310

def send_version
  @io.write (@local_version + CR + LF)
end

#startObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hrr_rb_ssh/transport.rb', line 162

def start
  log_info { "start transport" }

  begin
    exchange_version
    exchange_key

    case @mode
    when Mode::SERVER
      verify_service_request
    when Mode::CLIENT
      send_service_request
    end

    @closed = false
  rescue EOFError => e
    close
  rescue => e
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    close
  else
    log_info { "transport started" }
  end
end

#start_kex_algorithmObject



241
242
243
# File 'lib/hrr_rb_ssh/transport.rb', line 241

def start_kex_algorithm
  @kex_algorithm.start self
end

#update_compression_algorithmObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/hrr_rb_ssh/transport.rb', line 512

def update_compression_algorithm
  case @mode
  when Mode::SERVER
    compression_algorithm_c_to_s_name = @remote_compression_algorithms_client_to_server.find{ |a| @local_compression_algorithms_client_to_server.include? a } or raise
    compression_algorithm_s_to_c_name = @remote_compression_algorithms_server_to_client.find{ |a| @local_compression_algorithms_server_to_client.include? a } or raise
    incoming_compression_algorithm_name = compression_algorithm_c_to_s_name
    outgoing_compression_algorithm_name = compression_algorithm_s_to_c_name
  when Mode::CLIENT
    compression_algorithm_s_to_c_name = @local_compression_algorithms_server_to_client.find{ |a| @remote_compression_algorithms_server_to_client.include? a } or raise
    compression_algorithm_c_to_s_name = @local_compression_algorithms_client_to_server.find{ |a| @remote_compression_algorithms_client_to_server.include? a } or raise
    incoming_compression_algorithm_name = compression_algorithm_s_to_c_name
    outgoing_compression_algorithm_name = compression_algorithm_c_to_s_name
  end
  @incoming_compression_algorithm.close
  @outgoing_compression_algorithm.close
  @incoming_compression_algorithm = CompressionAlgorithm[incoming_compression_algorithm_name].new Direction::INCOMING
  @outgoing_compression_algorithm = CompressionAlgorithm[outgoing_compression_algorithm_name].new Direction::OUTGOING
end

#update_encryption_algorithmObject



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/hrr_rb_ssh/transport.rb', line 466

def update_encryption_algorithm
  case @mode
  when Mode::SERVER
    encryption_algorithm_c_to_s_name = @remote_encryption_algorithms_client_to_server.find{ |a| @local_encryption_algorithms_client_to_server.include? a } or raise
    encryption_algorithm_s_to_c_name = @remote_encryption_algorithms_server_to_client.find{ |a| @local_encryption_algorithms_server_to_client.include? a } or raise
    incoming_encryption_algorithm_name = encryption_algorithm_c_to_s_name
    outgoing_encryption_algorithm_name = encryption_algorithm_s_to_c_name
    incoming_crpt_iv = @kex_algorithm.iv_c_to_s self, incoming_encryption_algorithm_name
    outgoing_crpt_iv = @kex_algorithm.iv_s_to_c self, outgoing_encryption_algorithm_name
    incoming_crpt_key = @kex_algorithm.key_c_to_s self, incoming_encryption_algorithm_name
    outgoing_crpt_key = @kex_algorithm.key_s_to_c self, outgoing_encryption_algorithm_name
  when Mode::CLIENT
    encryption_algorithm_s_to_c_name = @local_encryption_algorithms_server_to_client.find{ |a| @remote_encryption_algorithms_server_to_client.include? a } or raise
    encryption_algorithm_c_to_s_name = @local_encryption_algorithms_client_to_server.find{ |a| @remote_encryption_algorithms_client_to_server.include? a } or raise
    incoming_encryption_algorithm_name = encryption_algorithm_s_to_c_name
    outgoing_encryption_algorithm_name = encryption_algorithm_c_to_s_name
    incoming_crpt_iv = @kex_algorithm.iv_s_to_c self, incoming_encryption_algorithm_name
    outgoing_crpt_iv = @kex_algorithm.iv_c_to_s self, outgoing_encryption_algorithm_name
    incoming_crpt_key = @kex_algorithm.key_s_to_c self, incoming_encryption_algorithm_name
    outgoing_crpt_key = @kex_algorithm.key_c_to_s self, outgoing_encryption_algorithm_name
  end
  @incoming_encryption_algorithm = EncryptionAlgorithm[incoming_encryption_algorithm_name].new Direction::INCOMING, incoming_crpt_iv, incoming_crpt_key
  @outgoing_encryption_algorithm = EncryptionAlgorithm[outgoing_encryption_algorithm_name].new Direction::OUTGOING, outgoing_crpt_iv, outgoing_crpt_key
end

#update_encryption_mac_compression_algorithmsObject



459
460
461
462
463
464
# File 'lib/hrr_rb_ssh/transport.rb', line 459

def update_encryption_mac_compression_algorithms
  @session_id ||= @kex_algorithm.hash(self)
  update_encryption_algorithm
  update_mac_algorithm
  update_compression_algorithm
end

#update_kex_and_server_host_key_algorithmsObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/hrr_rb_ssh/transport.rb', line 444

def update_kex_and_server_host_key_algorithms
  case @mode
  when Mode::SERVER
    kex_algorithm_name             = @remote_kex_algorithms.find{ |a| @local_kex_algorithms.include? a } or raise
    server_host_key_algorithm_name = @remote_server_host_key_algorithms.find{ |a| @local_server_host_key_algorithms.include? a } or raise
    server_secret_host_key         = @options.fetch('transport_server_secret_host_keys', {}).fetch(server_host_key_algorithm_name, nil)
  when Mode::CLIENT
    kex_algorithm_name             = @local_kex_algorithms.find{ |a| @remote_kex_algorithms.include? a } or raise
    server_host_key_algorithm_name = @local_server_host_key_algorithms.find{ |a| @remote_server_host_key_algorithms.include? a } or raise
    server_secret_host_key         = nil
  end
  @server_host_key_algorithm = ServerHostKeyAlgorithm[server_host_key_algorithm_name].new server_secret_host_key
  @kex_algorithm             = KexAlgorithm[kex_algorithm_name].new
end

#update_mac_algorithmObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/hrr_rb_ssh/transport.rb', line 491

def update_mac_algorithm
  case @mode
  when Mode::SERVER
    mac_algorithm_c_to_s_name = @remote_mac_algorithms_client_to_server.find{ |a| @local_mac_algorithms_client_to_server.include? a } or raise
    mac_algorithm_s_to_c_name = @remote_mac_algorithms_server_to_client.find{ |a| @local_mac_algorithms_server_to_client.include? a } or raise
    incoming_mac_algorithm_name = mac_algorithm_c_to_s_name
    outgoing_mac_algorithm_name = mac_algorithm_s_to_c_name
    incoming_mac_key = @kex_algorithm.mac_c_to_s self, incoming_mac_algorithm_name
    outgoing_mac_key = @kex_algorithm.mac_s_to_c self, outgoing_mac_algorithm_name
  when Mode::CLIENT
    mac_algorithm_s_to_c_name = @local_mac_algorithms_server_to_client.find{ |a| @remote_mac_algorithms_server_to_client.include? a } or raise
    mac_algorithm_c_to_s_name = @local_mac_algorithms_client_to_server.find{ |a| @remote_mac_algorithms_client_to_server.include? a } or raise
    incoming_mac_algorithm_name = mac_algorithm_s_to_c_name
    outgoing_mac_algorithm_name = mac_algorithm_c_to_s_name
    incoming_mac_key = @kex_algorithm.mac_s_to_c self, incoming_mac_algorithm_name
    outgoing_mac_key = @kex_algorithm.mac_c_to_s self, outgoing_mac_algorithm_name
  end
  @incoming_mac_algorithm = MacAlgorithm[incoming_mac_algorithm_name].new incoming_mac_key
  @outgoing_mac_algorithm = MacAlgorithm[outgoing_mac_algorithm_name].new outgoing_mac_key
end

#update_preferred_algorithmsObject



263
264
265
266
267
268
269
270
271
# File 'lib/hrr_rb_ssh/transport.rb', line 263

def update_preferred_algorithms
  @preferred_kex_algorithms             = @options['transport_preferred_kex_algorithms']             || KexAlgorithm.list_preferred
  @preferred_server_host_key_algorithms = @options['transport_preferred_server_host_key_algorithms'] || ServerHostKeyAlgorithm.list_preferred
  @preferred_encryption_algorithms      = @options['transport_preferred_encryption_algorithms']      || EncryptionAlgorithm.list_preferred
  @preferred_mac_algorithms             = @options['transport_preferred_mac_algorithms']             || MacAlgorithm.list_preferred
  @preferred_compression_algorithms     = @options['transport_preferred_compression_algorithms']     || CompressionAlgorithm.list_preferred

  check_if_preferred_algorithms_are_supported
end

#update_remote_algorithms(message) ⇒ Object



433
434
435
436
437
438
439
440
441
442
# File 'lib/hrr_rb_ssh/transport.rb', line 433

def update_remote_algorithms message
  @remote_kex_algorithms                          = message[:'kex_algorithms']
  @remote_server_host_key_algorithms              = message[:'server_host_key_algorithms']
  @remote_encryption_algorithms_client_to_server  = message[:'encryption_algorithms_client_to_server']
  @remote_encryption_algorithms_server_to_client  = message[:'encryption_algorithms_server_to_client']
  @remote_mac_algorithms_client_to_server         = message[:'mac_algorithms_client_to_server']
  @remote_mac_algorithms_server_to_client         = message[:'mac_algorithms_server_to_client']
  @remote_compression_algorithms_client_to_server = message[:'compression_algorithms_client_to_server']
  @remote_compression_algorithms_server_to_client = message[:'compression_algorithms_server_to_client']
end

#update_supported_algorithmsObject



255
256
257
258
259
260
261
# File 'lib/hrr_rb_ssh/transport.rb', line 255

def update_supported_algorithms
  @supported_kex_algorithms             = KexAlgorithm.list_supported
  @supported_server_host_key_algorithms = ServerHostKeyAlgorithm.list_supported
  @supported_encryption_algorithms      = EncryptionAlgorithm.list_supported
  @supported_mac_algorithms             = MacAlgorithm.list_supported
  @supported_compression_algorithms     = CompressionAlgorithm.list_supported
end

#update_version_stringsObject



332
333
334
335
336
337
338
339
340
341
# File 'lib/hrr_rb_ssh/transport.rb', line 332

def update_version_strings
  case @mode
  when Mode::SERVER
    @v_c = @remote_version
    @v_s = @local_version
  when Mode::CLIENT
    @v_c = @local_version
    @v_s = @remote_version
  end
end

#verify_service_requestObject



245
246
247
248
249
250
251
252
253
# File 'lib/hrr_rb_ssh/transport.rb', line 245

def verify_service_request
  service_request_message = receive_service_request
  service_name = service_request_message[:'service name']
  if @acceptable_services.include? service_name
    send_service_accept service_name
  else
    close
  end
end