Ejemplo n.º 1
0
        private unsafe string VerifyPublicKeyToken()
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(this.m_manifestDom.NameTable);

            nsmgr.AddNamespace("asm", "urn:schemas-microsoft-com:asm.v1");
            nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            XmlElement xmlElement1 = this.m_manifestDom.SelectSingleNode("asm:assembly/ds:Signature/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Modulus", nsmgr) as XmlElement;
            XmlElement xmlElement2 = this.m_manifestDom.SelectSingleNode("asm:assembly/ds:Signature/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Exponent", nsmgr) as XmlElement;

            if (xmlElement1 == null || xmlElement2 == null)
            {
                throw new CryptographicException(-2146762749);
            }
            byte[] bytes1          = Encoding.UTF8.GetBytes(xmlElement1.InnerXml);
            byte[] bytes2          = Encoding.UTF8.GetBytes(xmlElement2.InnerXml);
            string publicKeyToken1 = SignedCmiManifest.GetPublicKeyToken(this.m_manifestDom);

            byte[] bytes3 = SignedCmiManifest.HexStringToBytes(publicKeyToken1);
            byte[] bytes4;
            fixed(byte *numPtr1 = bytes1)
            fixed(byte *numPtr2 = bytes2)
            {
                Win32.CRYPT_DATA_BLOB pModulusBlob  = new Win32.CRYPT_DATA_BLOB();
                Win32.CRYPT_DATA_BLOB pExponentBlob = new Win32.CRYPT_DATA_BLOB();
                IntPtr ppwszPublicKeyToken          = new IntPtr();

                pModulusBlob.cbData  = (uint)bytes1.Length;
                pModulusBlob.pbData  = new IntPtr((void *)numPtr1);
                pExponentBlob.cbData = (uint)bytes2.Length;
                pExponentBlob.pbData = new IntPtr((void *)numPtr2);
                int publicKeyToken2 = Win32._AxlRSAKeyValueToPublicKeyToken(ref pModulusBlob, ref pExponentBlob, out ppwszPublicKeyToken);

                if (publicKeyToken2 != 0)
                {
                    throw new CryptographicException(publicKeyToken2);
                }
                bytes4 = SignedCmiManifest.HexStringToBytes(Marshal.PtrToStringUni(ppwszPublicKeyToken));
                Win32.HeapFree(Win32.GetProcessHeap(), 0U, ppwszPublicKeyToken);
            }
            if (bytes3.Length == 0 || bytes3.Length != bytes4.Length)
            {
                throw new CryptographicException(-2146762485);
            }
            for (int index = 0; index < bytes3.Length; ++index)
            {
                if ((int)bytes3[index] != (int)bytes4[index])
                {
                    throw new CryptographicException(-2146762485);
                }
            }
            return(publicKeyToken1);
        }
Ejemplo n.º 2
0
        private void VerifyHash(XmlNamespaceManager nsm)
        {
            new XmlDocument().PreserveWhitespace = true;
            XmlDocument manifestDom = (XmlDocument)this.m_manifestDom.Clone();
            XmlElement  xmlElement1 = manifestDom.SelectSingleNode("asm:assembly/ds:Signature/ds:KeyInfo/msrel:RelData/r:license/r:grant/as:ManifestInformation", nsm) as XmlElement;

            if (xmlElement1 == null)
            {
                throw new CryptographicException(-2146762749);
            }
            if (!xmlElement1.HasAttribute("Hash"))
            {
                throw new CryptographicException(-2146762749);
            }
            string attribute = xmlElement1.GetAttribute("Hash");

            if (attribute == null || attribute.Length == 0)
            {
                throw new CryptographicException(-2146762749);
            }
            XmlElement xmlElement2 = manifestDom.SelectSingleNode("asm:assembly/ds:Signature", nsm) as XmlElement;

            if (xmlElement2 == null)
            {
                throw new CryptographicException(-2146762749);
            }
            xmlElement2.ParentNode.RemoveChild((XmlNode)xmlElement2);
            byte[] bytes             = SignedCmiManifest.HexStringToBytes(xmlElement1.GetAttribute("Hash"));
            byte[] hashFromManifest1 = SignedCmiManifest.ComputeHashFromManifest(manifestDom);
            if (bytes.Length == 0 || bytes.Length != hashFromManifest1.Length)
            {
                byte[] hashFromManifest2 = SignedCmiManifest.ComputeHashFromManifest(manifestDom, true);
                if (bytes.Length == 0 || bytes.Length != hashFromManifest2.Length)
                {
                    throw new CryptographicException(-2146869232);
                }
                for (int index = 0; index < bytes.Length; ++index)
                {
                    if ((int)bytes[index] != (int)hashFromManifest2[index])
                    {
                        throw new CryptographicException(-2146869232);
                    }
                }
            }
            for (int index = 0; index < bytes.Length; ++index)
            {
                if ((int)bytes[index] != (int)hashFromManifest1[index])
                {
                    byte[] hashFromManifest2 = SignedCmiManifest.ComputeHashFromManifest(manifestDom, true);
                    if (bytes.Length == 0 || bytes.Length != hashFromManifest2.Length)
                    {
                        throw new CryptographicException(-2146869232);
                    }
                    for (index = 0; index < bytes.Length; ++index)
                    {
                        if ((int)bytes[index] != (int)hashFromManifest2[index])
                        {
                            throw new CryptographicException(-2146869232);
                        }
                    }
                }
            }
        }