Class: CertificateTransparency::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/certificate-transparency/client.rb

Overview

Interact with a Certificate Transparency server.

Defined Under Namespace

Classes: Error, HTTPError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ CT::Client

Create thyself a new CT::Client.

Parameters:

  • url (String)

    the "base" URL to the CT log, without any /ct/v1 bits in it.

  • opts (Hash) (defaults to: {})

    any options you'd like to pass.

  • public_key (Hash)

    a customizable set of options



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/certificate-transparency/client.rb', line 34

def initialize(url, opts = {})
  unless opts.is_a? Hash
    raise ArgumentError,
          "Must pass a hash of options as second argument"
  end

  if opts[:public_key]
    begin
      @public_key = if opts[:public_key].valid_encoding? && opts[:public_key] =~ /^[A-Za-z0-9+\/]+=*$/
        OpenSSL::PKey::EC.new(opts[:public_key].unpack("m").first)
      else
        OpenSSL::PKey::EC.new(opts[:public_key])
      end
    rescue OpenSSL::PKey::ECError
      raise ArgumentError,
            "Invalid public key"
    end
  end

  @url = URI(url)
end

Instance Attribute Details

#public_keyOpenSSL::PKey (readonly)

The public key of the log, as specified in the constructor.

Returns:

  • (OpenSSL::PKey)


20
21
22
# File 'lib/certificate-transparency/client.rb', line 20

def public_key
  @public_key
end

Instance Method Details

#get_entries(first, last = nil) ⇒ Array<CT::LogEntry>

Retrieve one or more entries from the log.

Parameters:

  • first (Integer)

    the 0-based index of the first entry in the log that you wish to retrieve.

  • last (Integer) (defaults to: nil)

    the 0-base indexd of the last entry in the log that you wish to retrieve. Note that you may not get as many entries as you requested, due to limits in the response size that are imposed by many log servers.

    If last is not specified, this method will attempt to retrieve as many entries as the log is willing and able to hand over.

Returns:

  • (Array<CT::LogEntry>)

Raises:

  • (CT::Client::HTTPError)

    if something goes wrong with the HTTP request.



85
86
87
88
89
90
91
92
# File 'lib/certificate-transparency/client.rb', line 85

def get_entries(first, last = nil)
  last ||= get_sth.tree_size - 1

  entries_json = make_request("get-entries", :start => first, :end => last)
  JSON.parse(entries_json)["entries"].map do |entry|
    CT::LogEntry.from_json(entry.to_json)
  end
end

#get_sthCT::SignedTreeHead

Retrieve the current Signed Tree Head from the log.

Returns:

  • (CT::SignedTreeHead)

Raises:

  • (CT::Client::HTTPError)

    if something goes wrong with the HTTP request.



63
64
65
# File 'lib/certificate-transparency/client.rb', line 63

def get_sth
  CT::SignedTreeHead.from_json(make_request("get-sth"))
end