Class: Above::Certs

Inherits:
Object
  • Object
show all
Defined in:
lib/above/certs.rb

Overview

Create and load certificates

Instance Method Summary collapse

Instance Method Details

#create(domain_arr:, dir:, duration: 60 * 60 * 24 * 365) ⇒ Object

Create key & cert for domain in directory, with a one year duration



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/above/certs.rb', line 26

def create(domain_arr:, dir:, duration: 60 * 60 * 24 * 365)
  # RSA is new & length, not generate & algo
  key = OpenSSL::PKey::EC.generate("secp384r1")
  name = OpenSSL::X509::Name.parse("/CN=#{domain_arr.first}")

  time = Time.now
  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = 0
  cert.not_before = time
  cert.not_after = time + duration
  # RSA is key.public_key not key - why are they different!?
  cert.public_key = key
  cert.subject = name

  extension_factory = OpenSSL::X509::ExtensionFactory.new(nil, cert)
  cert.add_extension \
    extension_factory.create_extension("basicConstraints", "CA:FALSE", true)
  cert.add_extension \
    extension_factory.create_extension(
      "keyUsage", "nonRepudiation, digitalSignature, keyEncipherment"
    )
  dns_str = ""
  domain_arr.each do |domain|
    dns_str += "DNS:#{domain},"
  end
  dns_str.chomp!(",")
  cert.add_extension \
    extension_factory.create_extension(
      "subjectAltName", dns_str
    )
  cert.add_extension \
    extension_factory.create_extension(
      "extendedKeyUsage", "serverAuth, clientAuth"
    )
  cert.add_extension extension_factory.create_extension("subjectKeyIdentifier", "hash")

  cert.issuer = name
  cert.sign key, OpenSSL::Digest.new("SHA512")

  File.write(File.join(dir, "#{domain_arr.first}.crt"), cert.to_pem)
  File.write(File.join(dir, "#{domain_arr.first}.key"), key.to_pem)
end

#dane(domain_arr:, dir:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/above/certs.rb', line 8

def dane(domain_arr:, dir:)
  file = File.read("#{File.join(dir, domain_arr.first)}.crt")
  cert = OpenSSL::X509::Certificate.new(file)
  checksum = OpenSSL::Digest::SHA512.new(cert.to_der)
  puts "For DANE create:\n\n"
  domain_arr.each do |domain|
    puts "DNS record: _1965._tcp.#{domain}\n"
  end
  puts <<~HELP

    Each with the text: TLSA 3 1 2 #{checksum}

    312 meaning the certificate's self signed, check the public cert, against this SHA-512 hash
    See: https://en.wikipedia.org/wiki/DNS-based_Authentication_of_Named_Entities
  HELP
end