Class: BetterCap::Proxy::HTTP::SSL::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/proxy/http/ssl/authority.rb

Overview

Used as an on-disk cache of server certificates.

Constant Summary collapse

PATH =

The store path.

File.join( Dir.home, '.bettercap', 'certificates' )

Instance Method Summary collapse

Constructor Details

#initializeStore

Create an instance of this class.



49
50
51
52
53
54
55
56
57
# File 'lib/bettercap/proxy/http/ssl/authority.rb', line 49

def initialize
  unless File.directory?( Store::PATH )
    Logger.info "[#{'SSL'.green}] Initializing certificates store '#{Store::PATH}' ..."
    FileUtils.mkdir_p( Store::PATH )
  end

  @store = {}
  @lock  = Mutex.new
end

Instance Method Details

#find(hostname, port) ⇒ Object

Find the hostname:port certificate and return it.



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
85
86
# File 'lib/bettercap/proxy/http/ssl/authority.rb', line 60

def find( hostname, port )
  # make sure *.domain.tld hostnames are correctly sanitized
  hostname.gsub!( "*.", "www." )

  key = Digest::SHA256.hexdigest( "#{hostname}_#{port}" )

  @lock.synchronize {
    unless @store.has_key?(key)
      # Certificate not available in memory, search it in the store PATH.
      filename = File.join( Store::PATH, key )
      s_cert = load_from_file( filename )
      # Not available on disk too, fetch it from the server and save it.
      if s_cert.nil?
        Logger.info "[#{'SSL'.green}] Fetching certificate from #{hostname}:#{port} ..."

        s_cert = Fetcher.fetch( hostname, port )
        save_to_file( s_cert, filename )
      else
        Logger.debug "[#{'SSL'.green}] Loaded HTTPS certificate for '#{hostname}' from store."
      end

      @store[key] = s_cert
    end
  }

  @store[key]
end