Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage of OpenGost to verify XMLDSIG. #20

Open
Degot opened this issue Jun 1, 2023 · 0 comments
Open

Example usage of OpenGost to verify XMLDSIG. #20

Degot opened this issue Jun 1, 2023 · 0 comments

Comments

@Degot
Copy link

Degot commented Jun 1, 2023

Hello, I've used this library to verify XMLDSIG created by CryptoPro, the main problem was that I needed to extract public key manually. Example:

public static bool VerifyXMLDSIG(string xmldsigFilename)
{
    if (xmldsigFilename == null)
        throw new ArgumentNullException(nameof(xmldsigFilename));

    var xmlDocument = new XmlDocument();
    xmlDocument.PreserveWhitespace = true;
    xmlDocument.Load(xmldsigFilename);
    var signedXml = new SignedXml(xmlDocument);
    var signatureElement = xmlDocument.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl)[0] as XmlElement;
    signedXml.LoadXml(signatureElement);
    
    var isValidXml = false;

    if (signedXml.KeyInfo != null)
    {
        foreach (KeyInfoClause clause in signedXml.KeyInfo)
        {
            if (isValidXml) break;

            if (clause is KeyInfoX509Data x509Data)
            {
                foreach(X509Certificate2 cert  in x509Data.Certificates)
                {
                    if (isValidXml) break;

                    var oid = cert.PublicKey.EncodedParameters.Oid;                            
                    var key = default(AsymmetricAlgorithm);

                    if (oid.Value == "1.2.643.7.1.1.1.1" || oid.Value == "1.2.643.7.1.1.1.2")
                    {
                        key = cert.GetGostECDsaPublicKey();
                        
                    } else
                    {
                        key = cert.GetECDsaPublicKey();
                    }

                    if (key  != null)
                    {
                        isValidXml = signedXml.CheckSignature(key);
                        key.Dispose();
                        key = null;
                    }                            
                }
            }
        }
    }           
    return isValidXml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant