Esempio n. 1
0
        public static void Test(VipNetCrytoProvider provider)
        {
            using (StreamReader file = File.OpenText("original-signedInfo.txt"))
            {
                //"boivBh1ioIPFGrKQ7LhhVw2pGfar/+T9Cqgz6JZ35IA="
                //"Pbp8cgljASFaqSsAHwA2eMC8GGvE9hjS5giHHYS8ZXQ="
                //"FD289YMUI30ICSHDA0boH479x0G+ZjYnBx748mk6uT8="
                //"fTBlROVsvTt/fAoyBLWVuaMYCIX/z4Eu+IVwQrNTb3k="
                //"W4hHfJjhWWvFRLxQVMu16sltkr/wLN36ulR+ySbYyCE="
                //"FD289YMUI30ICSHDA0boH479x0G+ZjYnBx748mk6uT8=" 6SEf0xFQFeilZRxXLCyflb3V9oTbyU7lsB/yLhPE2c25ZBOd7vEXNhY/wQnzt2+gNZRBkW26sUtbAWi59m1h/w==

                var       hash      = provider.HashData(file.ReadToEnd());
                Signature signature = provider.SigningHash(hash);
                bool      result    = provider.VerifySignature(signature);
                Console.WriteLine(signature.Base64);
            }



            X509Certificate cert = new X509Certificate();

            using (StreamReader file = File.OpenText("CorrectCertificate.txt"))
            {
                var data      = file.ReadToEnd().Replace("\n", "");
                var certBytes = Convert.FromBase64String(data);
                cert.Import(certBytes);
                var publicKey = cert.GetPublicKey();
            }
        }
        public static bool Verify(string xml, VipNetCrytoProvider provider)
        {
            XmlDocument doc      = XDocument.Parse(xml).GetXmlDocument();
            var         body     = Canonicalizer.GetObject(doc, "#body");
            var         bodyHash = provider.HashData(body);

            XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);

            nsManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            nsManager.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

            var digestValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue", nsManager) as XmlElement).InnerText;

            if (bodyHash.Base64 != digestValue)
            {
                return(false);
            }

            var signedInfo = Canonicalizer.GetObject(doc, "#SignedInfo");

            //using (var file = File.CreateText("original-signedInfo" + ".txt"))
            //{
            //  file.WriteLine(signedInfo);
            //}

            using (var file = File.OpenText("original-signedInfo" + ".txt"))
            {
                var       signedInfoWithoutDs = file.ReadToEnd();
                Signature sig = provider.SigningHash(provider.HashData(signedInfoWithoutDs));
            }

            var signedInfoHash = provider.HashData(signedInfo);
            var signature      = provider.SigningHash(signedInfoHash);

            var signatureValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignatureValue", nsManager) as XmlElement).InnerText;

            var             certificateBase64 = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/wsse:BinarySecurityToken", nsManager) as XmlElement).InnerText;
            X509Certificate certificate       = new X509Certificate(Convert.FromBase64String(certificateBase64));


            //var signature = new Signature {SignedHash = signedInfoHash, Bytes = Convert.FromBase64String(signatureValue)};
            signature.Bytes = Convert.FromBase64String(signatureValue);

            return(provider.VerifySignature(signature));
        }
        public void HashTest()
        {
            string message = "This is message, length=32 bytes";

            Console.WriteLine("====================== Hash Test ======================");
            var crypto = new VipNetCrytoProvider();
            var hash1  = crypto.HashData(message);

            crypto = new VipNetCrytoProvider();
            var hash2 = crypto.HashData(message);

            if (hash1.Hex != hash2.Hex)
            {
                throw new Exception("Wrong hash value");
            }

            Console.WriteLine(hash1.Hex);

            Console.WriteLine("======================================================");
        }