apply_ssl_certificates.rb 995 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Certificate::ApplySSLCertificates
  3. class << self
  4. SEMAPHORE = Thread::Mutex.new
  5. # Ensure the SSLContext for the current process has all custom SSL certificates.
  6. def ensure_fresh_ssl_context
  7. SEMAPHORE.synchronize do
  8. all_certificates = SSLCertificate.all
  9. # Only update the default store if there are changes with the stored SSL certificates.
  10. cache_key = all_certificates.cache_key_with_version
  11. return if @cache_key == cache_key
  12. @cache_key = cache_key
  13. # Build a new default store.
  14. store = OpenSSL::X509::Store.new
  15. store.set_default_paths
  16. store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
  17. all_certificates.each { |cert| store.add_cert(cert.certificate_parsed) }
  18. Kernel.silence_warnings do
  19. OpenSSL::SSL::SSLContext.const_set(:DEFAULT_CERT_STORE, store)
  20. end
  21. end
  22. end
  23. end
  24. end