Exemplo n.º 1
0
        private static void AuthenticodeSignLicenseDom(XmlDocument licenseDom, CmiManifestSigner signer, string timeStampUrl)
        {
            if (signer.Certificate.PublicKey.Key.GetType() != typeof(RSACryptoServiceProvider))
            {
                throw new NotSupportedException();
            }
            ManifestSignedXml manifestSignedXml = new ManifestSignedXml(licenseDom);

            manifestSignedXml.SigningKey = signer.Certificate.PrivateKey;
            manifestSignedXml.SignedInfo.CanonicalizationMethod = "http://www.w3.org/2001/10/xml-exc-c14n#";
            manifestSignedXml.KeyInfo.AddClause((KeyInfoClause) new RSAKeyValue(signer.Certificate.PublicKey.Key as RSA));
            manifestSignedXml.KeyInfo.AddClause((KeyInfoClause) new KeyInfoX509Data((X509Certificate)signer.Certificate, signer.IncludeOption));
            Reference reference = new Reference();

            reference.Uri = "";
            reference.AddTransform((Transform) new XmlDsigEnvelopedSignatureTransform());
            reference.AddTransform((Transform) new XmlDsigExcC14NTransform());
            manifestSignedXml.AddReference(reference);
            manifestSignedXml.ComputeSignature();
            XmlElement xml = manifestSignedXml.GetXml();

            xml.SetAttribute("Id", "AuthenticodeSignature");
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(licenseDom.NameTable);

            nsmgr.AddNamespace("r", "urn:mpeg:mpeg21:2003:01-REL-R-NS");
            (licenseDom.SelectSingleNode("r:license/r:issuer", nsmgr) as XmlElement).AppendChild(licenseDom.ImportNode((XmlNode)xml, true));
            if (timeStampUrl != null && timeStampUrl.Length != 0)
            {
                SignedCmiManifest.TimestampSignedLicenseDom(licenseDom, timeStampUrl);
            }
            licenseDom.DocumentElement.ParentNode.InnerXml = "<msrel:RelData xmlns:msrel=\"http://schemas.microsoft.com/windows/rel/2005/reldata\">" + licenseDom.OuterXml + "</msrel:RelData>";
        }
Exemplo n.º 2
0
        private static void StrongNameSignManifestDom(XmlDocument manifestDom, XmlDocument licenseDom, CmiManifestSigner signer)
        {
            RSA strongNameKey = signer.StrongNameKey as RSA;

            if (strongNameKey == null)
            {
                throw new NotSupportedException();
            }
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(manifestDom.NameTable);

            nsmgr.AddNamespace("asm", "urn:schemas-microsoft-com:asm.v1");
            XmlElement elem = manifestDom.SelectSingleNode("asm:assembly", nsmgr) as XmlElement;

            if (elem == null)
            {
                throw new CryptographicException(-2146762749);
            }
            ManifestSignedXml manifestSignedXml = new ManifestSignedXml(elem);

            manifestSignedXml.SigningKey = signer.StrongNameKey;
            manifestSignedXml.SignedInfo.CanonicalizationMethod = "http://www.w3.org/2001/10/xml-exc-c14n#";
            manifestSignedXml.KeyInfo.AddClause((KeyInfoClause) new RSAKeyValue(strongNameKey));
            if (licenseDom != null)
            {
                manifestSignedXml.KeyInfo.AddClause((KeyInfoClause) new KeyInfoNode(licenseDom.DocumentElement));
            }
            manifestSignedXml.KeyInfo.Id = "StrongNameKeyInfo";
            Reference reference = new Reference();

            reference.Uri = "";
            reference.AddTransform((Transform) new XmlDsigEnvelopedSignatureTransform());
            reference.AddTransform((Transform) new XmlDsigExcC14NTransform());
            manifestSignedXml.AddReference(reference);
            manifestSignedXml.ComputeSignature();
            XmlElement xml = manifestSignedXml.GetXml();

            xml.SetAttribute("Id", "StrongNameSignature");
            elem.AppendChild((XmlNode)xml);
        }
Exemplo n.º 3
0
        internal void Verify(CmiManifestVerifyFlags verifyFlags)
        {
            this.m_strongNameSignerInfo   = (CmiStrongNameSignerInfo)null;
            this.m_authenticodeSignerInfo = (CmiAuthenticodeSignerInfo)null;
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(this.m_manifestDom.NameTable);

            nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            XmlElement xmlElement1 = this.m_manifestDom.SelectSingleNode("//ds:Signature", nsmgr) as XmlElement;

            if (xmlElement1 == null)
            {
                throw new CryptographicException(-2146762496);
            }
            string name = "Id";

            if (!xmlElement1.HasAttribute(name))
            {
                name = "id";
                if (!xmlElement1.HasAttribute(name))
                {
                    name = "ID";
                    if (!xmlElement1.HasAttribute(name))
                    {
                        throw new CryptographicException(-2146762749);
                    }
                }
            }
            string attribute1 = xmlElement1.GetAttribute(name);

            if (attribute1 == null || string.Compare(attribute1, "StrongNameSignature", StringComparison.Ordinal) != 0)
            {
                throw new CryptographicException(-2146762749);
            }
            bool oldFormat = false;
            bool flag1     = false;

            foreach (XmlNode selectNode in xmlElement1.SelectNodes("ds:SignedInfo/ds:Reference", nsmgr))
            {
                XmlElement xmlElement2 = selectNode as XmlElement;
                if (xmlElement2 != null && xmlElement2.HasAttribute("URI"))
                {
                    string attribute2 = xmlElement2.GetAttribute("URI");
                    if (attribute2 != null)
                    {
                        if (attribute2.Length == 0)
                        {
                            XmlNode xmlNode = xmlElement2.SelectSingleNode("ds:Transforms", nsmgr);
                            if (xmlNode == null)
                            {
                                throw new CryptographicException(-2146762749);
                            }
                            XmlNodeList xmlNodeList = xmlNode.SelectNodes("ds:Transform", nsmgr);
                            if (xmlNodeList.Count < 2)
                            {
                                throw new CryptographicException(-2146762749);
                            }
                            bool flag2 = false;
                            bool flag3 = false;
                            for (int index = 0; index < xmlNodeList.Count; ++index)
                            {
                                string attribute3 = (xmlNodeList[index] as XmlElement).GetAttribute("Algorithm");
                                if (attribute3 != null)
                                {
                                    if (string.Compare(attribute3, "http://www.w3.org/2001/10/xml-exc-c14n#", StringComparison.Ordinal) != 0)
                                    {
                                        flag2 = true;
                                        if (flag3)
                                        {
                                            flag1 = true;
                                            break;
                                        }
                                    }
                                    else if (string.Compare(attribute3, "http://www.w3.org/2000/09/xmldsig#enveloped-signature", StringComparison.Ordinal) != 0)
                                    {
                                        flag3 = true;
                                        if (flag2)
                                        {
                                            flag1 = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else if (string.Compare(attribute2, "#StrongNameKeyInfo", StringComparison.Ordinal) == 0)
                        {
                            oldFormat = true;
                            XmlNode xmlNode = selectNode.SelectSingleNode("ds:Transforms", nsmgr);
                            if (xmlNode == null)
                            {
                                throw new CryptographicException(-2146762749);
                            }
                            XmlNodeList xmlNodeList = xmlNode.SelectNodes("ds:Transform", nsmgr);
                            if (xmlNodeList.Count < 1)
                            {
                                throw new CryptographicException(-2146762749);
                            }
                            for (int index = 0; index < xmlNodeList.Count; ++index)
                            {
                                string attribute3 = (xmlNodeList[index] as XmlElement).GetAttribute("Algorithm");
                                if (attribute3 != null)
                                {
                                    if (string.Compare(attribute3, "http://www.w3.org/2001/10/xml-exc-c14n#", StringComparison.Ordinal) != 0)
                                    {
                                        flag1 = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (!flag1)
            {
                throw new CryptographicException(-2146762749);
            }
            this.m_strongNameSignerInfo = new CmiStrongNameSignerInfo(-2146762485, this.VerifyPublicKeyToken());
            ManifestSignedXml manifestSignedXml = new ManifestSignedXml(this.m_manifestDom, true);

            manifestSignedXml.LoadXml(xmlElement1);
            AsymmetricAlgorithm signingKey = (AsymmetricAlgorithm)null;
            bool flag4 = manifestSignedXml.CheckSignatureReturningKey(out signingKey);

            this.m_strongNameSignerInfo.PublicKey = signingKey;
            if (!flag4)
            {
                this.m_strongNameSignerInfo.ErrorCode = -2146869232;
                throw new CryptographicException(-2146869232);
            }
            if ((verifyFlags & CmiManifestVerifyFlags.StrongNameOnly) == CmiManifestVerifyFlags.StrongNameOnly)
            {
                return;
            }
            this.VerifyLicense(verifyFlags, oldFormat);
        }