Module: SSLShake::CommonHelpers

Included in:
SSLv2, TLS
Defined in:
lib/sslshake/common.rb

Instance Method Summary collapse

Instance Method Details

#cipher_string(ciphers, search) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sslshake/common.rb', line 42

def cipher_string(ciphers, search)
  case search
  when Proc
    ciphers.select(&search).values.join
  when String
    ciphers[search] || search
  when Regexp
    ciphers.select { |k, _| k =~ search }.values.join
  when Array
    search.map { |i| cipher_string(ciphers, i) }.join
  when nil
    ciphers.values.join
  else
    fail UserError, 'Please provide a search string, regex, or list for SSL ciphers.'
  end
end

#int_bytes(i, len = 1) ⇒ Object



11
12
13
# File 'lib/sslshake/common.rb', line 11

def int_bytes(i, len = 1)
  format("%0#{len * 2}x", i)
end

#socket_read(socket, bytes, timeout, retries) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sslshake/common.rb', line 15

def socket_read(socket, bytes, timeout, retries)
  timeout ||= 1
  retries ||= 2
  if socket.closed?
    fail Alert, 'No response, socket is closed.'
  end

  begin
    res = socket.read_nonblock(bytes)
  rescue IO::WaitReadable
    IO.select([socket], nil, nil, timeout)
    retries -= 1
    retry if retries >= 0
    raise Alert, 'Timeout while reading data.'
  rescue IO::WaitWritable
    IO.select(nil, [socket], nil, timeout)
    retries -= 1
    retry if retries >= 0
    raise Alert, 'Timeout while waiting for socket to be writable.'
  rescue EOFError
    raise Alert, 'Failed to read, EOF reached.'
  end

  fail Alert, 'No data received' if res.nil?
  res
end