/// <summary> /// Verifies if the digital certificate password is correct. /// </summary> /// <param name="pfxCertificateStream">PFX certificate stream</param> /// <param name="password">PFX password</param> /// <returns></returns> public static Boolean VerifyPassword(Stream pfxCertificateStream, String password) { if (pfxCertificateStream == null) { throw new ArgumentNullException(nameof(pfxCertificateStream)); } if (!pfxCertificateStream.CanRead) { throw new IOException($"Cannot read the '{nameof(pfxCertificateStream)}' parameter."); } if (password == null) { throw new ArgumentNullException(nameof(password)); } try { X509CertificateHelpers.LoadCertificate(pfxCertificateStream, password); return(true); } catch (CryptographicException ex) { if (ex.Message.Contains("senha") || ex.Message.Contains("pass")) { return(false); } else { throw; } } }
/// <summary> /// If necessary, calls the Windows dialog box for the user to enter the digital certificate PIN code. Widely used in A3 digital certificates. /// </summary> /// <param name="certificate">The certificate</param> public static void AskForPIN(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException(nameof(certificate)); } X509CertificateHelpers.SignXML(certificate, null, "<?xml version=\"1.0\" encoding=\"utf-8\"?><dummyTag/>"); }
/// <summary> /// Sign a XML with a digital certificate. /// </summary> /// <param name="certificate">The certificate</param> /// <param name="elementNameRef">XML element to sign</param> /// <param name="xml">XML to sign</param> /// <param name="attributeRef">(Optional) Reference to use in the sign process</param> /// <returns></returns> public static String SignXML(this X509Certificate2 certificate, String elementNameRef, String xml, String attributeRef = "Id") { var docXML = new XmlDocument() { PreserveWhitespace = true }; using (XmlTextReader xtr = new XmlTextReader(new MemoryStream(Encoding.UTF8.GetBytes(xml)))) { docXML.Load(xtr); } return(X509CertificateHelpers.SignXML(certificate, elementNameRef, docXML, attributeRef)); }
/// <summary> /// Get the expiration date of a certificate. /// </summary> /// <param name="pfxCertificateStream">PFX certificate stream</param> /// <param name="password">PFX password</param> /// <returns></returns> public static DateTime GetExpirationDate(Stream pfxCertificateStream, String password) { if (pfxCertificateStream == null) { throw new ArgumentNullException(nameof(pfxCertificateStream)); } if (!pfxCertificateStream.CanRead) { throw new IOException($"Cannot read the '{nameof(pfxCertificateStream)}' parameter."); } if (password == null) { throw new ArgumentNullException(nameof(password)); } var certificate = X509CertificateHelpers.LoadCertificate(pfxCertificateStream, password); return(certificate.NotAfter); }