Class: Net::Tofu::Pub

Inherits:
Object
  • Object
show all
Defined in:
lib/net/tofu/pub.rb

Overview

Intermediate container for an X509 certificate public key. Manages Geminispace public certificates.

Constant Summary collapse

DIR =
File.expand_path("~/.tofu")
DB =
File.expand_path("~/.tofu/known_hosts")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, data) ⇒ Pub

Constructor for the cert type.

Parameters:

  • host (String)

    Host to associate with the certificate public key.

  • data (String)

    Certificate public key to associate with the host.



23
24
25
26
27
28
# File 'lib/net/tofu/pub.rb', line 23

def initialize(host, data)
  @host = host
  @key = data
  @key = format_hosts(data) if data.start_with?("-----BEGIN CERTIFICATE-----")
  @x509 = OpenSSL::X509::Certificate.new(to_x509)
end

Instance Attribute Details

#hostString (readonly)

Returns The host associated with the certificate public key.

Returns:

  • (String)

    The host associated with the certificate public key.



12
13
14
# File 'lib/net/tofu/pub.rb', line 12

def host
  @host
end

#keyString (readonly)

Returns The certificate public key associated with the host.

Returns:

  • (String)

    The certificate public key associated with the host.



15
16
17
# File 'lib/net/tofu/pub.rb', line 15

def key
  @key
end

#x509OpenSSL::X509::Certificate (readonly)

Returns The X509 certificate.

Returns:

  • (OpenSSL::X509::Certificate)

    The X509 certificate.



18
19
20
# File 'lib/net/tofu/pub.rb', line 18

def x509
  @x509
end

Class Method Details

.db_exist?Boolean

Check if known_hosts file exists, try to create it if it doesn’t.

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/net/tofu/pub.rb', line 81

def self.db_exist?
  return true if File.exist?(DB)

  FileUtils.mkdir_p(DIR) unless File.directory?(DIR)
  FileUtils.touch(DB)
  true
end

.lookup(host) ⇒ Cert

Lookup a host in the known_hosts table.

Parameters:

  • host (String)

    the hostname to lookup

Returns:

  • (Cert)

    An instance of this class if found, or nil if not found.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/net/tofu/pub.rb', line 33

def self.lookup(host)
  db_exist?

  f = File.read(DB)
  includes = nil
  f.lines.each_with_index do |l, i|
    if l.split[0].include? host
      @line = i
      includes = l.split[1]
      break
    end
  end
  return nil if includes.nil? || includes.empty?

  new(host, includes)
end

Instance Method Details

#==(other) ⇒ Object

Compare certificate public keys



76
77
78
# File 'lib/net/tofu/pub.rb', line 76

def ==(other)
  @key == other.key
end

#lineObject



50
51
52
# File 'lib/net/tofu/pub.rb', line 50

def line
  @line ||= nil
end

#pinObject

Pin a certificate public key to the known_hosts file



55
56
57
58
59
60
61
62
# File 'lib/net/tofu/pub.rb', line 55

def pin
  Net::Tofu::Pub.db_exist?

  f = File.open(DB, "a")
  f.puts self
ensure
  f&.close
end

#to_sObject

Hosts file format.



71
72
73
# File 'lib/net/tofu/pub.rb', line 71

def to_s
  "#{@host} #{@key}"
end

#to_x509Object

Conver the certificate public key to a properly formatted X509 string.



65
66
67
68
# File 'lib/net/tofu/pub.rb', line 65

def to_x509
  lines = @key.chars.each_slice(64).map(&:join)
  "-----BEGIN CERTIFICATE-----\n#{lines.join("\n")}\n-----END CERTIFICATE-----"
end