Exemple #1
0
        public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnly)
        {
            if (!CheckSignature(certificate.PublicKey.Key))
            {
                return(false);
            }

            if (verifySignatureOnly)
            {
                SignedXmlDebugLog.LogVerificationResult(this, certificate, true);
                return(true);
            }

            // Check key usages to make sure it is good for signing.
            foreach (X509Extension extension in certificate.Extensions)
            {
                if (String.Compare(extension.Oid.Value, CAPI.szOID_KEY_USAGE, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    X509KeyUsageExtension keyUsage = new X509KeyUsageExtension();
                    keyUsage.CopyFrom(extension);
                    SignedXmlDebugLog.LogVerifyKeyUsage(this, certificate, keyUsage);

                    bool validKeyUsage = (keyUsage.KeyUsages & X509KeyUsageFlags.DigitalSignature) != 0 ||
                                         (keyUsage.KeyUsages & X509KeyUsageFlags.NonRepudiation) != 0;

                    if (!validKeyUsage)
                    {
                        SignedXmlDebugLog.LogVerificationFailure(this, SecurityResources.GetResourceString("Log_VerificationFailed_X509KeyUsage"));
                        return(false);
                    }
                    break;
                }
            }

            // Do the chain verification to make sure the certificate is valid.
            X509Chain chain = new X509Chain();

            chain.ChainPolicy.ExtraStore.AddRange(BuildBagOfCerts());
            bool chainVerified = chain.Build(certificate);

            SignedXmlDebugLog.LogVerifyX509Chain(this, chain, certificate);

            if (!chainVerified)
            {
                SignedXmlDebugLog.LogVerificationFailure(this, SecurityResources.GetResourceString("Log_VerificationFailed_X509Chain"));
                return(false);
            }

            SignedXmlDebugLog.LogVerificationResult(this, certificate, true);
            return(true);
        }
Exemple #2
0
        public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnly)
        {
            if (!verifySignatureOnly)
            {
                // Check key usages to make sure it is good for signing.
                foreach (X509Extension extension in certificate.Extensions)
                {
                    if (string.Equals(extension.Oid.Value, "2.5.29.15" /* szOID_KEY_USAGE */, StringComparison.OrdinalIgnoreCase))
                    {
                        X509KeyUsageExtension keyUsage = new X509KeyUsageExtension();
                        keyUsage.CopyFrom(extension);
                        SignedXmlDebugLog.LogVerifyKeyUsage(this, certificate, keyUsage);

                        bool validKeyUsage = (keyUsage.KeyUsages & X509KeyUsageFlags.DigitalSignature) != 0 ||
                                             (keyUsage.KeyUsages & X509KeyUsageFlags.NonRepudiation) != 0;

                        if (!validKeyUsage)
                        {
                            SignedXmlDebugLog.LogVerificationFailure(this, SR.Log_VerificationFailed_X509KeyUsage);
                            return(false);
                        }
                        break;
                    }
                }

                // Do the chain verification to make sure the certificate is valid.
                X509Chain chain = new X509Chain();
                chain.ChainPolicy.ExtraStore.AddRange(BuildBagOfCerts());
                bool chainVerified = chain.Build(certificate);
                SignedXmlDebugLog.LogVerifyX509Chain(this, chain, certificate);

                if (!chainVerified)
                {
                    SignedXmlDebugLog.LogVerificationFailure(this, SR.Log_VerificationFailed_X509Chain);
                    return(false);
                }
            }

            using (AsymmetricAlgorithm publicKey = Utils.GetAnyPublicKey(certificate))
            {
                if (!CheckSignature(publicKey))
                {
                    return(false);
                }
            }

            SignedXmlDebugLog.LogVerificationResult(this, certificate, true);
            return(true);
        }
        public bool CheckSignature(X509Certificate2 certificate, bool verifySignatureOnly)
        {
            if (!this.CheckSignature(certificate.PublicKey.Key))
            {
                return(false);
            }
            if (verifySignatureOnly)
            {
                SignedXmlDebugLog.LogVerificationResult(this, certificate, true);
                return(true);
            }
            X509ExtensionEnumerator enumerator = certificate.Extensions.GetEnumerator();

            while (enumerator.MoveNext())
            {
                X509Extension current = enumerator.Current;
                if (string.Compare(current.Oid.Value, "2.5.29.15", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    X509KeyUsageExtension keyUsages = new X509KeyUsageExtension();
                    keyUsages.CopyFrom(current);
                    SignedXmlDebugLog.LogVerifyKeyUsage(this, certificate, keyUsages);
                    if (((keyUsages.KeyUsages & X509KeyUsageFlags.DigitalSignature) != X509KeyUsageFlags.None) || ((keyUsages.KeyUsages & X509KeyUsageFlags.NonRepudiation) != X509KeyUsageFlags.None))
                    {
                        break;
                    }
                    SignedXmlDebugLog.LogVerificationFailure(this, SecurityResources.GetResourceString("Log_VerificationFailed_X509KeyUsage"));
                    return(false);
                }
            }
            X509Chain chain = new X509Chain();

            chain.ChainPolicy.ExtraStore.AddRange(this.BuildBagOfCerts());
            bool flag2 = chain.Build(certificate);

            SignedXmlDebugLog.LogVerifyX509Chain(this, chain, certificate);
            if (!flag2)
            {
                SignedXmlDebugLog.LogVerificationFailure(this, SecurityResources.GetResourceString("Log_VerificationFailed_X509Chain"));
                return(false);
            }
            SignedXmlDebugLog.LogVerificationResult(this, certificate, true);
            return(true);
        }