From 0cac1cadc2f85bcf1dd68c42cd0bf71ecc724be8 Mon Sep 17 00:00:00 2001 From: Frank Wall Date: Wed, 5 Jan 2022 12:00:01 +0100 Subject: [PATCH] security/acme-client: fix calculation of renewal date, closes #2721 Now we read the validFrom information directly from the cert file in order to calculate the renewal date. This is necessary, because in ae697392293e4a7fb3e9ed0450a559adccbab2e6 we made the import feature available to the end-user. As a result, the value of lastUpdate() does not only change after issue/renewal, but also everytime the user clicks on the "import" button. --- .../OPNsense/AcmeClient/LeCertificate.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/LeCertificate.php b/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/LeCertificate.php index 72a8f1ece..54e32d960 100644 --- a/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/LeCertificate.php +++ b/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/LeCertificate.php @@ -414,8 +414,31 @@ class LeCertificate extends LeCommon { $return = false; + // Try to get issue date from certificate + if (is_file($this->cert_file)) { + // Read contents from certificate file + $cert_content = @file_get_contents($this->cert_file); + if ($cert_content != false) { + $cert_info = @openssl_x509_parse($cert_content); + if (!empty($cert_info['validFrom_time_t'])) { + $last_update = $cert_info['validFrom_time_t']; + } else { + LeUtils::log_error('unable to get expiration time from certificate for ' . (string)$this->config->name); + $last_update = 0; // Just assume the cert requires renewal. + } + } else { + LeUtils::log_error('unable to read certificate content from file for ' . (string)$this->config->name); + $last_update = 0; // Just assume the cert requires renewal. + } + } elseif (!empty((string)$this->config->lastUpdate)) { + // Fallback to lastUpdate() state, although it may not be correct + // if the cert was imported manually after issue/renewal. + $last_update = (string)$this->config->lastUpdate; + } else { + $last_update = 0; // Just assume the cert requires renewal. + } + // Collect required information - $last_update = !empty((string)$this->config->lastUpdate) ? (string)$this->config->lastUpdate : 0; $current_time = new \DateTime(); $last_update_time = new \DateTime(); $last_update_time->setTimestamp($last_update);