+local global_certificates = configmanager.get("*", "certificates") or "certs";
+
+local crt_try = { "", "/%s.crt", "/%s/fullchain.pem", "/%s.pem", };
+local key_try = { "", "/%s.key", "/%s/privkey.pem", "/%s.pem", };
+
+local function find_cert(host)
+ local certs = configmanager.get(host, "certificate") or global_certificates;
+ certs = resolve_path(config_path, certs);
+ for i = 1, #crt_try do
+ local crt_path = certs .. crt_try[i]:format(host);
+ local key_path = certs .. key_try[i]:format(host);
+
+ if stat(crt_path, "mode") == "file" then
+ if stat(key_path, "mode") == "file" then
+ return { certificate = crt_path, key = key_path };
+ end
+ if key_path:sub(-4) == ".crt" then
+ key_path = key_path:sub(1, -4) .. "key";
+ if stat(key_path, "mode") == "file" then
+ return { certificate = crt_path, key = key_path };
+ end
+ end
+ end
+ end
+end
+