Class: Hedra::CertificateChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/hedra/certificate_checker.rb

Overview

Check SSL/TLS certificate validity and security

Constant Summary collapse

EXPIRY_WARNING_DAYS =
30

Instance Method Summary collapse

Instance Method Details

#check(url) ⇒ Object



12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hedra/certificate_checker.rb', line 12

def check(url)
  uri = URI.parse(url)
  return nil unless uri.scheme == 'https'

  findings = []
  cert_info = fetch_certificate(uri.host, uri.port || 443)

  return findings unless cert_info

  # Check expiry
  days_until_expiry = ((cert_info[:not_after] - Time.now) / 86_400).to_i
  if days_until_expiry.negative?
    findings << {
      header: 'ssl-certificate',
      issue: 'SSL certificate has expired',
      severity: :critical,
      recommended_fix: 'Renew SSL certificate immediately'
    }
  elsif days_until_expiry < EXPIRY_WARNING_DAYS
    findings << {
      header: 'ssl-certificate',
      issue: "SSL certificate expires in #{days_until_expiry} days",
      severity: :warning,
      recommended_fix: 'Renew SSL certificate soon'
    }
  end

  # Check signature algorithm
  if weak_signature_algorithm?(cert_info[:signature_algorithm])
    findings << {
      header: 'ssl-certificate',
      issue: "Weak signature algorithm: #{cert_info[:signature_algorithm]}",
      severity: :warning,
      recommended_fix: 'Use SHA256 or stronger'
    }
  end

  # Check key size
  if cert_info[:key_size] && cert_info[:key_size] < 2048
    findings << {
      header: 'ssl-certificate',
      issue: "Weak key size: #{cert_info[:key_size]} bits",
      severity: :critical,
      recommended_fix: 'Use at least 2048-bit RSA or 256-bit ECC'
    }
  end

  findings
rescue StandardError => e
  warn "Certificate check failed: #{e.message}"
  []
end