Exemplo n.º 1
0
        /// <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;
                }
            }
        }
Exemplo n.º 2
0
        /// <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);
        }