From 06694e0b169f283f2ad2b479c731a5e5ae232b0c Mon Sep 17 00:00:00 2001 From: ema Date: Tue, 23 Dec 2025 14:41:59 +0800 Subject: [PATCH] Add password support for protected certificates Introduces UI and logic to handle password-protected certificate files. The CertViewerControl now prompts for a password if needed, and attempts to reload the certificate with the provided password. Refactored certificate loading flow to support this feature. --- .../CertLoadResult.cs | 8 ++- .../QuickLook.Plugin.CertViewer/CertUtils.cs | 10 ++- .../CertViewerControl.xaml | 52 ++++++++++------ .../CertViewerControl.xaml.cs | 61 +++++++++++++++++++ .../QuickLook.Plugin.CertViewer/Plugin.cs | 12 +--- 5 files changed, 106 insertions(+), 37 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertLoadResult.cs b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertLoadResult.cs index a23a49d..1ce728d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertLoadResult.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertLoadResult.cs @@ -8,15 +8,17 @@ internal sealed class CertLoadResult public X509Certificate2 Certificate { get; } public string Message { get; } public string RawContent { get; } + public bool NeedsPassword { get; } - public CertLoadResult(bool success, X509Certificate2 certificate, string message, string rawContent) + public CertLoadResult(bool success, X509Certificate2 certificate, string message, string rawContent, bool needsPassword = false) { Success = success; Certificate = certificate; Message = message; RawContent = rawContent; + NeedsPassword = needsPassword; } - public static CertLoadResult From(bool success, X509Certificate2 certificate, string message, string rawContent) - => new CertLoadResult(success, certificate, message, rawContent); + public static CertLoadResult From(bool success, X509Certificate2 certificate, string message, string rawContent, bool needsPassword = false) + => new CertLoadResult(success, certificate, message, rawContent, needsPassword); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertUtils.cs b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertUtils.cs index 8445a69..f2ca99a 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertUtils.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertUtils.cs @@ -14,7 +14,7 @@ internal static class CertUtils /// - Message: an informational or error message /// - RawContent: original file text or hex when parsing failed /// - public static CertLoadResult TryLoadCertificate(string path) + public static CertLoadResult TryLoadCertificate(string path, string password = null) { try { @@ -24,12 +24,16 @@ internal static class CertUtils { try { - var cert = new X509Certificate2(path); + var cert = !string.IsNullOrEmpty(password) + ? new X509Certificate2(path, password) + : new X509Certificate2(path); return new CertLoadResult(true, cert, string.Empty, null); } catch (Exception ex) { - return new CertLoadResult(false, null, "Failed to load PFX/P12: " + ex.Message, null); + var isPasswordError = ex is System.Security.Cryptography.CryptographicException || + (ex.Message?.IndexOf("password", StringComparison.OrdinalIgnoreCase) >= 0); + return new CertLoadResult(false, null, "Failed to load PFX/P12: " + ex.Message, null, isPasswordError); } } diff --git a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertViewerControl.xaml b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertViewerControl.xaml index 57a0e0b..83aa460 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertViewerControl.xaml +++ b/QuickLook.Plugin/QuickLook.Plugin.CertViewer/CertViewerControl.xaml @@ -12,29 +12,41 @@ + - - - - - - + + + + +