Skip to content

Commit 5cf822e

Browse files
committed
chore: increase perfomance
Only load CRL by URL when is necessary Signed-off-by: Vitor Mattos <[email protected]>
1 parent 7435b02 commit 5cf822e

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

lib/Handler/CertificateEngine/AEngineHandler.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCA\Libresign\Helper\MagicGetterSetterTrait;
1717
use OCA\Libresign\Service\CaIdentifierService;
1818
use OCA\Libresign\Service\CertificatePolicyService;
19+
use OCA\Libresign\Service\CrlService;
1920
use OCP\Files\AppData\IAppDataFactory;
2021
use OCP\Files\IAppData;
2122
use OCP\Files\SimpleFS\ISimpleFolder;
@@ -680,9 +681,14 @@ private function validateCrlFromUrls(array $crlUrls, string $certPem): string {
680681

681682
private function downloadAndValidateCrl(string $crlUrl, string $certPem): string {
682683
try {
683-
$crlContent = $this->downloadCrlContent($crlUrl);
684+
if ($this->isLocalCrlUrl($crlUrl)) {
685+
$crlContent = $this->generateLocalCrl($crlUrl);
686+
} else {
687+
$crlContent = $this->downloadCrlContent($crlUrl);
688+
}
689+
684690
if (!$crlContent) {
685-
throw new \Exception('Failed to download CRL');
691+
throw new \Exception('Failed to get CRL content');
686692
}
687693

688694
return $this->checkCertificateInCrl($certPem, $crlContent);
@@ -692,6 +698,54 @@ private function downloadAndValidateCrl(string $crlUrl, string $certPem): string
692698
}
693699
}
694700

701+
private function isLocalCrlUrl(string $url): bool {
702+
$host = parse_url($url, PHP_URL_HOST);
703+
if (!$host) {
704+
return false;
705+
}
706+
707+
$trustedDomains = $this->config->getSystemValue('trusted_domains', []);
708+
709+
return in_array($host, $trustedDomains, true);
710+
}
711+
712+
private function generateLocalCrl(string $crlUrl): ?string {
713+
try {
714+
$templateUrl = $this->urlGenerator->linkToRouteAbsolute('libresign.crl.getRevocationList', [
715+
'instanceId' => 'INSTANCEID',
716+
'generation' => 999999,
717+
'engineType' => 'ENGINETYPE',
718+
]);
719+
720+
$patternUrl = str_replace('INSTANCEID', '([^/_]+)', $templateUrl);
721+
$patternUrl = str_replace('999999', '(\d+)', $patternUrl);
722+
$patternUrl = str_replace('ENGINETYPE', '([^/_]+)', $patternUrl);
723+
724+
$escapedPattern = str_replace([':', '/', '.'], ['\:', '\/', '\.'], $patternUrl);
725+
$pattern = '/^' . $escapedPattern . '$/';
726+
727+
if (preg_match($pattern, $crlUrl, $matches)) {
728+
$instanceId = $matches[1];
729+
$generation = (int)$matches[2];
730+
$engineType = $matches[3];
731+
732+
/** @var CrlService */
733+
$crlService = \OC::$server->get(CrlService::class);
734+
735+
$crlData = $crlService->generateCrlDer($instanceId, $generation, $engineType);
736+
737+
return $crlData;
738+
}
739+
740+
$this->logger->debug('CRL URL does not match expected pattern', ['url' => $crlUrl, 'pattern' => $pattern]);
741+
return null;
742+
743+
} catch (\Exception $e) {
744+
$this->logger->warning('Failed to generate local CRL: ' . $e->getMessage());
745+
return null;
746+
}
747+
}
748+
695749
private function downloadCrlContent(string $url): ?string {
696750
if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
697751
return null;
@@ -706,7 +760,6 @@ private function downloadCrlContent(string $url): ?string {
706760
]
707761
]);
708762

709-
$url = str_replace('localhost', 'nginx', $url);
710763
$content = @file_get_contents($url, false, $context);
711764
return $content !== false ? $content : null;
712765
}

0 commit comments

Comments
 (0)