2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: check-certificates
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
2023-03-07 09:57:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# check for certificate validity
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-certificates.md
|
|
|
|
|
|
|
|
:local 0 "check-certificates";
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
|
|
|
:global CertRenewTime;
|
|
|
|
:global CertRenewUrl;
|
|
|
|
:global CertWarnTime;
|
|
|
|
:global Identity;
|
|
|
|
|
|
|
|
:global CertificateAvailable
|
2023-04-06 13:19:23 +00:00
|
|
|
:global EscapeForRegEx;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global ParseKeyValueStore;
|
2023-06-12 06:19:14 +00:00
|
|
|
:global ScriptLock;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global SendNotification2;
|
|
|
|
:global SymbolForNotification;
|
2023-04-06 13:19:23 +00:00
|
|
|
:global UrlEncode;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
2023-04-06 12:56:36 +00:00
|
|
|
:local CheckCertificatesDownloadImport do={
|
|
|
|
:local Name [ :tostr $1 ];
|
|
|
|
|
|
|
|
:global CertRenewUrl;
|
|
|
|
:global CertRenewPass;
|
|
|
|
|
|
|
|
:global CertificateNameByCN;
|
2023-04-06 20:38:17 +00:00
|
|
|
:global EscapeForRegEx;
|
2023-04-06 12:56:36 +00:00
|
|
|
:global LogPrintExit2;
|
|
|
|
:global UrlEncode;
|
|
|
|
:global WaitForFile;
|
|
|
|
|
2023-04-06 13:11:28 +00:00
|
|
|
:local Return false;
|
|
|
|
|
2023-04-06 12:56:36 +00:00
|
|
|
:foreach Type in={ ".pem"; ".p12" } do={
|
|
|
|
:local CertFileName ([ $UrlEncode $Name ] . $Type);
|
|
|
|
:do {
|
|
|
|
/tool/fetch check-certificate=yes-without-crl \
|
|
|
|
($CertRenewUrl . $CertFileName) dst-path=$CertFileName as-value;
|
|
|
|
$WaitForFile $CertFileName;
|
|
|
|
|
|
|
|
:local DecryptionFailed true;
|
|
|
|
:foreach PassPhrase in=$CertRenewPass do={
|
|
|
|
:local Result [ /certificate/import file-name=$CertFileName passphrase=$PassPhrase as-value ];
|
|
|
|
:if ($Result->"decryption-failures" = 0) do={
|
|
|
|
:set DecryptionFailed false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/file/remove [ find where name=$CertFileName ];
|
|
|
|
|
|
|
|
:if ($DecryptionFailed = true) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Decryption failed for certificate file " . $CertFileName) false;
|
|
|
|
}
|
|
|
|
|
2023-04-06 20:38:17 +00:00
|
|
|
:foreach CertInChain in=[ /certificate/find where name~("^" . [ $EscapeForRegEx $CertFileName ] . "_[0-9]+\$") \
|
2023-04-06 13:19:23 +00:00
|
|
|
common-name!=$Name !(subject-alt-name~("(^|\\W)(DNS|IP):" . [ $EscapeForRegEx $Name ] . "(\\W|\$)")) !(common-name=[]) ] do={
|
2023-04-06 12:56:36 +00:00
|
|
|
$CertificateNameByCN [ /certificate/get $CertInChain common-name ];
|
|
|
|
}
|
2023-04-06 13:11:28 +00:00
|
|
|
|
|
|
|
:set Return true;
|
2023-04-06 12:56:36 +00:00
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 debug $0 ("Could not download certificate file " . $CertFileName) false;
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 13:11:28 +00:00
|
|
|
|
|
|
|
:return $Return;
|
2023-04-06 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 08:16:55 +00:00
|
|
|
:local FormatInfo do={
|
2023-12-04 10:22:05 +00:00
|
|
|
:local Cert $1;
|
2023-04-06 08:16:55 +00:00
|
|
|
|
2023-04-20 21:55:03 +00:00
|
|
|
:global FormatLine;
|
2023-09-15 20:50:32 +00:00
|
|
|
:global FormatMultiLines;
|
2023-04-06 08:16:55 +00:00
|
|
|
:global IfThenElse;
|
2023-12-04 10:22:05 +00:00
|
|
|
|
2023-04-06 08:16:55 +00:00
|
|
|
:local FormatExpire do={
|
|
|
|
:global CharacterReplace;
|
|
|
|
:return [ $CharacterReplace [ $CharacterReplace [ :tostr $1 ] "w" "w " ] "d" "d " ];
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:05 +00:00
|
|
|
:local FormatCertChain do={
|
|
|
|
:local Cert $1;
|
|
|
|
|
2023-12-04 11:35:13 +00:00
|
|
|
:global EitherOr;
|
2023-12-04 10:22:05 +00:00
|
|
|
:global ParseKeyValueStore;
|
|
|
|
|
|
|
|
:local CertVal [ /certificate/get $Cert ];
|
|
|
|
:local Return "";
|
|
|
|
|
2023-12-04 11:53:50 +00:00
|
|
|
:for I from=0 to=5 do={
|
2023-12-04 11:35:13 +00:00
|
|
|
:set Return ($Return . [ $EitherOr ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") \
|
|
|
|
([ $ParseKeyValueStore (($CertVal->"issuer")->0) ]->"CN") ]);
|
|
|
|
:set CertVal [ /certificate/get [ find where skid=($CertVal->"akid") ] ];
|
2023-12-04 10:22:05 +00:00
|
|
|
:if (($CertVal->"akid") = "" || ($CertVal->"akid") = ($CertVal->"skid")) do={
|
|
|
|
:return $Return;
|
|
|
|
}
|
|
|
|
:set Return ($Return . " -> ");
|
|
|
|
}
|
2023-12-04 11:14:52 +00:00
|
|
|
:return ($Return . "...");
|
2023-12-04 10:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:local CertVal [ /certificate/get $Cert ];
|
|
|
|
|
2023-04-06 08:16:55 +00:00
|
|
|
:return ( \
|
2023-04-20 21:55:03 +00:00
|
|
|
[ $FormatLine "Name" ($CertVal->"name") ] . "\n" . \
|
|
|
|
[ $IfThenElse ([ :len ($CertVal->"common-name") ] > 0) ([ $FormatLine "CommonName" ($CertVal->"common-name") ] . "\n") ] . \
|
2023-09-15 20:50:32 +00:00
|
|
|
[ $IfThenElse ([ :len ($CertVal->"subject-alt-name") ] > 0) ([ $FormatMultiLines "SubjectAltNames" ($CertVal->"subject-alt-name") ] . "\n") ] . \
|
2023-04-20 21:55:03 +00:00
|
|
|
[ $FormatLine "Private key" [ $IfThenElse (($CertVal->"private-key") = true) "available" "missing" ] ] . "\n" . \
|
|
|
|
[ $FormatLine "Fingerprint" ($CertVal->"fingerprint") ] . "\n" . \
|
2023-12-04 11:53:50 +00:00
|
|
|
[ $IfThenElse ([ :len ($CertVal->"ca") ] > 0) [ $FormatLine "Issuer" ($CertVal->"ca") ] [ $FormatLine "Issuer chain" [ $FormatCertChain $Cert ] ] ] . "\n" . \
|
2023-05-23 20:40:54 +00:00
|
|
|
"Validity:\n" . \
|
|
|
|
[ $FormatLine " from" ($CertVal->"invalid-before") ] . "\n" . \
|
|
|
|
[ $FormatLine " to" ($CertVal->"invalid-after") ] . "\n" . \
|
2023-04-20 21:55:03 +00:00
|
|
|
[ $FormatLine "Expires in" [ $IfThenElse (($CertVal->"expired") = true) "expired" [ $FormatExpire ($CertVal->"expires-after") ] ] ]);
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 06:19:14 +00:00
|
|
|
$ScriptLock $0;
|
2023-03-07 09:57:42 +00:00
|
|
|
$WaitFullyConnected;
|
|
|
|
|
|
|
|
:foreach Cert in=[ /certificate/find where !revoked !ca !scep-url expires-after<$CertRenewTime ] do={
|
|
|
|
:local CertVal [ /certificate/get $Cert ];
|
2023-12-04 10:22:05 +00:00
|
|
|
:local CertNew;
|
2023-04-06 13:19:23 +00:00
|
|
|
:local LastName;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:do {
|
|
|
|
:if ([ :len $CertRenewUrl ] = 0) do={
|
|
|
|
$LogPrintExit2 info $0 ("No CertRenewUrl given.") true;
|
|
|
|
}
|
|
|
|
$LogPrintExit2 info $0 ("Attempting to renew certificate " . ($CertVal->"name") . ".") false;
|
|
|
|
|
2023-04-06 13:11:28 +00:00
|
|
|
:local ImportSuccess false;
|
2023-04-06 13:19:23 +00:00
|
|
|
:set LastName ($CertVal->"common-name");
|
|
|
|
:set ImportSuccess [ $CheckCertificatesDownloadImport $LastName ];
|
|
|
|
:foreach SAN in=($CertVal->"subject-alt-name") do={
|
|
|
|
:if ($ImportSuccess = false) do={
|
|
|
|
:set LastName [ :pick $SAN ([ :find $SAN ":" ] + 1) [ :len $SAN ] ];
|
|
|
|
:set ImportSuccess [ $CheckCertificatesDownloadImport $LastName ];
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-12-04 21:10:15 +00:00
|
|
|
:if ([ :len ($CertVal->"fingerprint") ] > 0 && $CertVal->"fingerprint" != [ /certificate/get $Cert fingerprint ]) do={
|
2023-07-23 20:01:43 +00:00
|
|
|
$LogPrintExit2 debug $0 ("Certificate '" . $CertVal->"name" . "' was updated in place.") false;
|
|
|
|
:set CertVal [ /certificate/get $Cert ];
|
2023-12-04 11:01:52 +00:00
|
|
|
} else={
|
2023-07-23 20:01:43 +00:00
|
|
|
$LogPrintExit2 debug $0 ("Certificate '" . $CertVal->"name" . "' was not updated, but replaced.") false;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-12-04 10:22:05 +00:00
|
|
|
:set CertNew [ /certificate/find where name~("^" . [ $EscapeForRegEx [ $UrlEncode $LastName ] ] . "\\.(p12|pem)_[0-9]+\$") \
|
2023-07-23 20:01:43 +00:00
|
|
|
(common-name=($CertVal->"common-name") or subject-alt-name~("(^|\\W)(DNS|IP):" . [ $EscapeForRegEx $LastName ] . "(\\W|\$)")) \
|
|
|
|
fingerprint!=[ :tostr ($CertVal->"fingerprint") ] expires-after>$CertRenewTime ];
|
|
|
|
:local CertNewVal [ /certificate/get $CertNew ];
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-07-23 20:01:43 +00:00
|
|
|
:if ([ $CertificateAvailable ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") ] = false) do={
|
|
|
|
$LogPrintExit2 warning $0 ("The certificate chain is not available!") false;
|
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:if (($CertVal->"private-key") = true && ($CertVal->"private-key") != ($CertNewVal->"private-key")) do={
|
|
|
|
/certificate/remove $CertNew;
|
|
|
|
$LogPrintExit2 warning $0 ("Old certificate '" . ($CertVal->"name") . "' has a private key, new certificate does not. Aborting renew.") true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/ip/service/set certificate=($CertNewVal->"name") [ find where certificate=($CertVal->"name") ];
|
|
|
|
|
|
|
|
/ip/ipsec/identity/set certificate=($CertNewVal->"name") [ find where certificate=($CertVal->"name") ];
|
|
|
|
/ip/ipsec/identity/set remote-certificate=($CertNewVal->"name") [ find where remote-certificate=($CertVal->"name") ];
|
|
|
|
|
|
|
|
/ip/hotspot/profile/set ssl-certificate=($CertNewVal->"name") [ find where ssl-certificate=($CertVal->"name") ];
|
|
|
|
|
|
|
|
/certificate/remove $Cert;
|
|
|
|
/certificate/set $CertNew name=($CertVal->"name");
|
2023-04-06 08:16:55 +00:00
|
|
|
:set CertNewVal;
|
2023-12-04 11:01:52 +00:00
|
|
|
:set CertVal [ /certificate/get $CertNew ];
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 08:16:55 +00:00
|
|
|
$SendNotification2 ({ origin=$0; silent=true; \
|
2023-04-19 20:05:15 +00:00
|
|
|
subject=([ $SymbolForNotification "lock-with-ink-pen" ] . "Certificate renewed: " . ($CertVal->"name")); \
|
2023-12-04 10:22:05 +00:00
|
|
|
message=("A certificate on " . $Identity . " has been renewed.\n\n" . [ $FormatInfo $CertNew ]) });
|
2023-03-07 09:57:42 +00:00
|
|
|
$LogPrintExit2 info $0 ("The certificate " . ($CertVal->"name") . " has been renewed.") false;
|
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 debug $0 ("Could not renew certificate " . ($CertVal->"name") . ".") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Cert in=[ /certificate/find where !revoked !scep-url !(expires-after=[]) \
|
|
|
|
expires-after<$CertWarnTime !(fingerprint=[]) ] do={
|
|
|
|
:local CertVal [ /certificate/get $Cert ];
|
|
|
|
|
|
|
|
:if ([ :len [ /certificate/scep-server/find where ca-cert=($CertVal->"ca") ] ] > 0) do={
|
|
|
|
$LogPrintExit2 debug $0 ("Certificate \"" . ($CertVal->"name") . "\" is handled by SCEP, skipping.") false;
|
|
|
|
} else={
|
|
|
|
:local State [ $IfThenElse (($CertVal->"expired") = true) "expired" "is about to expire" ];
|
|
|
|
|
|
|
|
$SendNotification2 ({ origin=$0; \
|
2023-04-19 20:05:15 +00:00
|
|
|
subject=([ $SymbolForNotification "warning-sign" ] . "Certificate warning: " . ($CertVal->"name")); \
|
2023-12-04 10:22:05 +00:00
|
|
|
message=("A certificate on " . $Identity . " " . $State . ".\n\n" . [ $FormatInfo $Cert ]) });
|
2023-03-07 09:57:42 +00:00
|
|
|
$LogPrintExit2 info $0 ("The certificate " . ($CertVal->"name") . " " . $State . \
|
|
|
|
", it is invalid after " . ($CertVal->"invalid-after") . ".") false;
|
|
|
|
}
|
|
|
|
}
|