Ejemplo n.º 1
0
        private void CheckByThumbprint(X509Store store, string thumbprint, int warningThreshold)
        {
            X509Certificate2Collection certificates = store.Certificates.Find(
                X509FindType.FindByThumbprint,
                thumbprint,
                validOnly: false);

            X509Certificate2 certificate;

            if (certificates.Count == 0)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    if (!TryFindCertificate("/var/lib/sfcerts", thumbprint, out certificate) &&
                        !TryFindCertificate("/var/lib/waagent", thumbprint, out certificate))
                    {
                        NotFoundWarnings.Add(
                            $"Could not find requested certificate with thumbprint: {thumbprint} in /var/lib/sfcerts, /var/lib/waagent, and LocalMachine/Root");
                        return;
                    }
                }
                else
                {
                    NotFoundWarnings.Add(
                        $"Could not find requested certificate with thumbprint: {thumbprint} in LocalMachine/My");
                    return;
                }
            }
            else
            {
                certificate = certificates[0];
            }

            DateTime expiry          = certificate.NotAfter; // Expiration time in local time (not UTC)
            TimeSpan timeUntilExpiry = expiry.Subtract(DateTime.Now);
            var      message         = HowToUpdateCnCertsSfLinkHtml;

            if (IsSelfSignedCertificate(certificate))
            {
                message = HowToUpdateSelfSignedCertSfLinkHtml;
            }

            if (timeUntilExpiry.TotalMilliseconds < 0)
            {
                ExpiredWarnings.Add($"Certificate Expired on {expiry.ToShortDateString()}: " +
                                    $"Thumbprint: {certificate.Thumbprint} " +
                                    $"Issuer {certificate.Issuer}, " +
                                    $"Subject: {certificate.Subject}{Environment.NewLine}{message}");
            }
            else if (timeUntilExpiry.TotalDays < warningThreshold)
            {
                ExpiringWarnings.Add($"Certificate Expiring on {expiry.ToShortDateString()}: " +
                                     $"Thumbprint: {certificate.Thumbprint} " +
                                     $"Issuer {certificate.Issuer}, " +
                                     $"Subject: {certificate.Subject}{Environment.NewLine}{message}");
            }
        }
Ejemplo n.º 2
0
        private void CheckLatestBySubjectName(X509Store store, string subjectName, int warningThreshold)
        {
            var certificates = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, false);
            X509Certificate2 newestCertificate = null;
            var newestNotAfter = DateTime.MinValue;

            if (certificates.Count == 0)
            {
                NotFoundWarnings.Add($"Could not find requested certificate with common name: {subjectName} in LocalMachine/My");
                return;
            }

            var message = HowToUpdateCnCertsSfLinkHtml;

            foreach (var certificate in certificates)
            {
                if (certificate.NotAfter > newestNotAfter)
                {
                    newestCertificate = certificate;
                    newestNotAfter    = certificate.NotAfter;
                }

                if (IsSelfSignedCertificate(certificate))
                {
                    message = HowToUpdateSelfSignedCertSfLinkHtml;
                }
            }

            DateTime?expiry          = newestCertificate?.NotAfter; // Expiration time in local time (not UTC)
            TimeSpan?timeUntilExpiry = expiry?.Subtract(DateTime.Now);

            if (timeUntilExpiry?.TotalMilliseconds < 0)
            {
                ExpiredWarnings.Add(
                    $"Certificate expired on {expiry?.ToShortDateString()}: " +
                    $"[Thumbprint: {newestCertificate?.Thumbprint} " +
                    $"" +
                    $"Issuer {newestCertificate.Issuer}, " +
                    $"Subject: {newestCertificate.Subject}]{Environment.NewLine}{message}");
            }
            else if (timeUntilExpiry?.TotalDays < warningThreshold)
            {
                ExpiringWarnings.Add(
                    $"Certificate expiring in {timeUntilExpiry?.TotalDays} days, on {expiry?.ToShortDateString()}: " +
                    $"[Thumbprint: {newestCertificate.Thumbprint} " +
                    $"Issuer {newestCertificate.Issuer}, " +
                    $"Subject: {newestCertificate.Subject}]{Environment.NewLine}{message}");
            }
        }