Exemple #1
0
 private PermissionSet GetInputPermissionSet()
 {
     if (_inputPermissionSet == null)
     {
         XmlElement psElement = GetInputPermissionSetElement();
         if (PreserveFullTrustPermissionSet)
         {
             XmlAttribute unrestrictedAttribute = (XmlAttribute)psElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
             _isFullTrust = unrestrictedAttribute != null && Boolean.Parse(unrestrictedAttribute.Value);
             if (_isFullTrust)
             {
                 XmlDocument document = new XmlDocument();
                 document.AppendChild(document.ImportNode(psElement, true));
                 document.DocumentElement.Attributes.RemoveNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                 psElement = document.DocumentElement;
             }
             _inputPermissionSet = SecurityUtilities.XmlToPermissionSet(psElement);
         }
         else
         {
             _inputPermissionSet = SecurityUtilities.XmlToPermissionSet(psElement);
             _isFullTrust        = _inputPermissionSet.IsUnrestricted();
         }
     }
     return(_inputPermissionSet);
 }
Exemple #2
0
 private System.Security.PermissionSet GetInputPermissionSet()
 {
     if (this._inputPermissionSet == null)
     {
         XmlElement inputPermissionSetElement = this.GetInputPermissionSetElement();
         if (this._preserveFullTrustPermissionSet)
         {
             XmlAttribute namedItem = (XmlAttribute)inputPermissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
             this._isFullTrust = (namedItem != null) && bool.Parse(namedItem.Value);
             if (this._isFullTrust)
             {
                 XmlDocument document = new XmlDocument();
                 document.AppendChild(document.ImportNode(inputPermissionSetElement, true));
                 document.DocumentElement.Attributes.RemoveNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                 inputPermissionSetElement = document.DocumentElement;
             }
             this._inputPermissionSet = SecurityUtilities.XmlToPermissionSet(inputPermissionSetElement);
         }
         else
         {
             this._inputPermissionSet = SecurityUtilities.XmlToPermissionSet(inputPermissionSetElement);
             this._isFullTrust        = this._inputPermissionSet.IsUnrestricted();
         }
     }
     return(this._inputPermissionSet);
 }
Exemple #3
0
        private XmlElement GetPermissionSetElement(XmlDocument document)
        {
            XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable);
            XmlElement          documentElement  = document.DocumentElement;
            XmlElement          newChild         = (XmlElement)documentElement.SelectSingleNode("asmv2:security", namespaceManager);

            if (newChild == null)
            {
                newChild = document.CreateElement(XmlUtil.TrimPrefix("asmv2:security"), "urn:schemas-microsoft-com:asm.v2");
                documentElement.AppendChild(newChild);
            }
            XmlElement element3 = (XmlElement)newChild.SelectSingleNode("asmv2:applicationRequestMinimum", namespaceManager);

            if (element3 == null)
            {
                element3 = document.CreateElement(XmlUtil.TrimPrefix("asmv2:applicationRequestMinimum"), "urn:schemas-microsoft-com:asm.v2");
                newChild.AppendChild(element3);
            }
            XmlElement element4 = (XmlElement)element3.SelectSingleNode("asmv2:PermissionSet", namespaceManager);

            if (element4 == null)
            {
                element4 = document.CreateElement(XmlUtil.TrimPrefix("asmv2:PermissionSet"), "urn:schemas-microsoft-com:asm.v2");
                element3.AppendChild(element4);
                XmlAttribute node = document.CreateAttribute(XmlUtil.TrimPrefix("asmv2:Unrestricted"), "urn:schemas-microsoft-com:asm.v2");
                node.Value = this._isFullTrust.ToString().ToLowerInvariant();
                element4.Attributes.Append(node);
            }
            return(element4);
        }
Exemple #4
0
        private XmlElement GetPermissionSetElement(XmlDocument document)
        {
            Debug.Assert(document != null, "GetPermissionSetElement was passed a null document");
            XmlNamespaceManager nsmgr            = XmlNamespaces.GetNamespaceManager(document.NameTable);
            XmlElement          trustInfoElement = document.DocumentElement;
            var securityElement = (XmlElement)trustInfoElement.SelectSingleNode(XPaths.securityElement, nsmgr);

            if (securityElement == null)
            {
                securityElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.securityElement), XmlNamespaces.asmv2);
                trustInfoElement.AppendChild(securityElement);
            }
            XmlElement applicationRequestMinimumElement = (XmlElement)securityElement.SelectSingleNode(XPaths.applicationRequestMinimumElement, nsmgr);

            if (applicationRequestMinimumElement == null)
            {
                applicationRequestMinimumElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.applicationRequestMinimumElement), XmlNamespaces.asmv2);
                securityElement.AppendChild(applicationRequestMinimumElement);
            }
            XmlElement permissionSetElement = (XmlElement)applicationRequestMinimumElement.SelectSingleNode(XPaths.permissionSetElement, nsmgr);

            if (permissionSetElement == null)
            {
                permissionSetElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.permissionSetElement), XmlNamespaces.asmv2);
                applicationRequestMinimumElement.AppendChild(permissionSetElement);
                XmlAttribute unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute), XmlNamespaces.asmv2);
                unrestrictedAttribute.Value = _isFullTrust.ToString().ToLowerInvariant();
                permissionSetElement.Attributes.Append(unrestrictedAttribute);
            }
            return(permissionSetElement);
        }
Exemple #5
0
        private static string[] XmlToIdentityList(XmlElement psElement)
        {
            XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(psElement.OwnerDocument.NameTable);
            XmlNodeList         nodes = psElement.SelectNodes(XPaths.permissionClassAttributeQuery, nsmgr);

            if (nodes == null || nodes.Count == 0)
            {
                nodes = psElement.SelectNodes(XmlUtil.TrimPrefix(XPaths.permissionClassAttributeQuery));
            }
            string[] a;
            if (nodes != null)
            {
                a = new string[nodes.Count];
                int i = 0;
                foreach (XmlNode node in nodes)
                {
                    a[i++] = node.Value;
                }
            }
            else
            {
                a = new string[0];
            }
            return(a);
        }
Exemple #6
0
        private XmlElement GetRequestedPrivilegeElement(XmlElement inputRequestedPrivilegeElement, XmlDocument document)
        {
            XmlElement newChild = document.CreateElement(XmlUtil.TrimPrefix("asmv3:requestedPrivileges"), "urn:schemas-microsoft-com:asm.v3");

            document.AppendChild(newChild);
            string str  = null;
            string str2 = null;
            string data = null;

            if (inputRequestedPrivilegeElement == null)
            {
                str  = "asInvoker";
                str2 = "false";
                data = new ResourceManager("Microsoft.Build.Tasks.Deployment.ManifestUtilities.Strings", typeof(SecurityUtilities).Module.Assembly).GetString("TrustInfo.RequestedExecutionLevelComment");
            }
            else
            {
                XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable);
                XmlElement          element2         = (XmlElement)inputRequestedPrivilegeElement.SelectSingleNode("asmv3:requestedExecutionLevel", namespaceManager);
                if (element2 != null)
                {
                    XmlNode previousSibling = element2.PreviousSibling;
                    if ((previousSibling != null) && (previousSibling.NodeType == XmlNodeType.Comment))
                    {
                        data = ((XmlComment)previousSibling).Data;
                    }
                    if (element2.HasAttribute("level"))
                    {
                        str = element2.GetAttribute("level");
                    }
                    if (element2.HasAttribute("uiAccess"))
                    {
                        str2 = element2.GetAttribute("uiAccess");
                    }
                }
            }
            if (data != null)
            {
                XmlComment comment = document.CreateComment(data);
                newChild.AppendChild(comment);
            }
            if (str != null)
            {
                XmlElement element3 = document.CreateElement(XmlUtil.TrimPrefix("asmv3:requestedExecutionLevel"), "urn:schemas-microsoft-com:asm.v3");
                newChild.AppendChild(element3);
                XmlAttribute node = document.CreateAttribute("level");
                node.Value = str;
                element3.Attributes.Append(node);
                if (str2 != null)
                {
                    XmlAttribute attribute2 = document.CreateAttribute("uiAccess");
                    attribute2.Value = str2;
                    element3.Attributes.Append(attribute2);
                }
            }
            return(newChild);
        }
Exemple #7
0
        public void WriteManifest(Stream input, Stream output)
        {
            int         tickCount = Environment.TickCount;
            XmlDocument document  = new XmlDocument();

            document.Load(input);
            XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable);
            XmlElement          element          = (XmlElement)document.SelectSingleNode("asmv1:assembly", namespaceManager);

            if (element == null)
            {
                throw new BadImageFormatException();
            }
            XmlElement newChild = (XmlElement)element.SelectSingleNode("asmv2:trustInfo", namespaceManager);

            if (newChild == null)
            {
                newChild = document.CreateElement(XmlUtil.TrimPrefix("asmv2:trustInfo"), "urn:schemas-microsoft-com:asm.v2");
                element.AppendChild(newChild);
            }
            if (((this._inputTrustInfoDocument != null) && (this._outputPermissionSet == null)) && !this.sameSiteChanged)
            {
                XmlElement element3 = (XmlElement)document.ImportNode(this._inputTrustInfoDocument.DocumentElement, true);
                newChild.ParentNode.ReplaceChild(element3, newChild);
            }
            else
            {
                XmlElement element4 = (XmlElement)newChild.SelectSingleNode("asmv2:security", namespaceManager);
                if (element4 == null)
                {
                    element4 = document.CreateElement(XmlUtil.TrimPrefix("asmv2:security"), "urn:schemas-microsoft-com:asm.v2");
                    newChild.AppendChild(element4);
                }
                XmlElement element5 = (XmlElement)element4.SelectSingleNode("asmv2:applicationRequestMinimum", namespaceManager);
                if (element5 == null)
                {
                    element5 = document.CreateElement(XmlUtil.TrimPrefix("asmv2:applicationRequestMinimum"), "urn:schemas-microsoft-com:asm.v2");
                    element4.AppendChild(element5);
                }
                foreach (XmlNode node in element5.SelectNodes("asmv2:PermissionSet", namespaceManager))
                {
                    element5.RemoveChild(node);
                }
                XmlDocument outputPermissionSetDocument = this.GetOutputPermissionSetDocument();
                XmlElement  element6 = (XmlElement)document.ImportNode(outputPermissionSetDocument.DocumentElement, true);
                element5.AppendChild(element6);
                this.FixupPermissionSetElement(element6);
            }
            if (output.Length > 0L)
            {
                output.SetLength(0L);
                output.Flush();
            }
            document.Save(output);
            Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "ManifestWriter.WriteTrustInfo t={0}", new object[] { Environment.TickCount - tickCount }));
        }
Exemple #8
0
 private XmlElement GetInputPermissionSetElement()
 {
     if (_inputTrustInfoDocument == null)
     {
         _inputTrustInfoDocument = new XmlDocument();
         XmlElement trustInfoElement = _inputTrustInfoDocument.CreateElement(XmlUtil.TrimPrefix(XPaths.trustInfoElement), XmlNamespaces.asmv2);
         _inputTrustInfoDocument.AppendChild(trustInfoElement);
     }
     return(GetPermissionSetElement(_inputTrustInfoDocument));
 }
Exemple #9
0
        private void AddSameSiteAttribute(XmlElement permissionSetElement)
        {
            XmlAttribute namedItem = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:SameSite"));

            if (namedItem == null)
            {
                namedItem = permissionSetElement.OwnerDocument.CreateAttribute(XmlUtil.TrimPrefix("asmv2:SameSite"));
                permissionSetElement.Attributes.Append(namedItem);
            }
            namedItem.Value = this.sameSiteSetting;
        }
Exemple #10
0
 private XmlElement GetInputPermissionSetElement()
 {
     if (this._inputTrustInfoDocument == null)
     {
         this._inputTrustInfoDocument = new XmlDocument();
         XmlNamespaces.GetNamespaceManager(this._inputTrustInfoDocument.NameTable);
         XmlElement newChild = this._inputTrustInfoDocument.CreateElement(XmlUtil.TrimPrefix("asmv2:trustInfo"), "urn:schemas-microsoft-com:asm.v2");
         this._inputTrustInfoDocument.AppendChild(newChild);
     }
     return(this.GetPermissionSetElement(this._inputTrustInfoDocument));
 }
        internal static void UpdateEntryPoint(string inputPath, string outputPath, string updatedApplicationPath, string applicationManifestPath)
        {
            string      str2;
            long        num;
            XmlDocument document = new XmlDocument();

            document.Load(inputPath);
            XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable);

            Microsoft.Build.Tasks.Deployment.ManifestUtilities.AssemblyIdentity identity = Microsoft.Build.Tasks.Deployment.ManifestUtilities.AssemblyIdentity.FromManifest(applicationManifestPath);
            XmlNode node = null;

            foreach (string str in XPaths.codebasePaths)
            {
                node = document.SelectSingleNode(str, namespaceManager);
                if (node != null)
                {
                    break;
                }
            }
            if (node == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "XPath not found: {0}", new object[] { XPaths.codebasePaths[0] }));
            }
            node.Value = updatedApplicationPath;
            XmlNode node2 = ((XmlAttribute)node).OwnerElement.SelectSingleNode("asmv2:assemblyIdentity/@publicKeyToken", namespaceManager);

            if (node2 == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "XPath not found: {0}", new object[] { "asmv2:assemblyIdentity/@publicKeyToken" }));
            }
            node2.Value = identity.PublicKeyToken;
            Util.GetFileInfo(applicationManifestPath, out str2, out num);
            XmlNode node3 = ((XmlAttribute)node).OwnerElement.SelectSingleNode("asmv2:hash/dsig:DigestValue", namespaceManager);

            if (node3 != null)
            {
                ((XmlElement)node3).InnerText = str2;
            }
            XmlAttribute attribute = ((XmlAttribute)node).OwnerElement.Attributes[XmlUtil.TrimPrefix("asmv2:size")];

            if (attribute == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "XPath not found: {0}", new object[] { "asmv2:size" }));
            }
            attribute.Value = num.ToString(CultureInfo.InvariantCulture);
            document.Save(outputPath);
        }
Exemple #12
0
        private static string[] XmlToIdentityList(XmlElement psElement)
        {
            XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(psElement.OwnerDocument.NameTable);
            XmlNodeList         list             = psElement.SelectNodes("asmv2:IPermission/@class", namespaceManager);

            if ((list == null) || (list.Count == 0))
            {
                list = psElement.SelectNodes(XmlUtil.TrimPrefix("asmv2:IPermission/@class"));
            }
            if (list != null)
            {
                string[] strArray = new string[list.Count];
                int      num      = 0;
                foreach (XmlNode node in list)
                {
                    strArray[num++] = node.Value;
                }
                return(strArray);
            }
            return(new string[0]);
        }
Exemple #13
0
        private void FixupPermissionSetElement(XmlElement permissionSetElement)
        {
            XmlDocument         document = permissionSetElement.OwnerDocument;
            XmlNamespaceManager nsmgr    = XmlNamespaces.GetNamespaceManager(document.NameTable);

            if (PreserveFullTrustPermissionSet)
            {
                var unrestrictedAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                if (_isFullTrust)
                {
                    if (unrestrictedAttribute == null)
                    {
                        unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                        permissionSetElement.Attributes.Append(unrestrictedAttribute);
                    }
                    unrestrictedAttribute.Value = "true";
                }
                else
                {
                    if (unrestrictedAttribute != null)
                    {
                        permissionSetElement.Attributes.RemoveNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                    }
                }
            }
            else
            {
                if (_isFullTrust)
                {
                    var unrestrictedAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                    if (unrestrictedAttribute == null)
                    {
                        unrestrictedAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));
                        permissionSetElement.Attributes.Append(unrestrictedAttribute);
                    }
                    unrestrictedAttribute.Value = "true";
                    while (permissionSetElement.FirstChild != null)
                    {
                        permissionSetElement.RemoveChild(permissionSetElement.FirstChild);
                    }
                }
            }

            // Add ID="Custom" attribute if there's not one already
            var idAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.idAttribute));

            if (idAttribute == null)
            {
                idAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.idAttribute));
                permissionSetElement.Attributes.Append(idAttribute);
            }

            if (String.IsNullOrEmpty(idAttribute.Value))
            {
                idAttribute.Value = "Custom";
            }

            AddSameSiteAttribute(permissionSetElement);

            if (permissionSetElement.ParentNode == null ||
                permissionSetElement.ParentNode.NodeType == XmlNodeType.Document)
            {
                return;
            }

            XmlElement defaultAssemblyRequestElement = (XmlElement)permissionSetElement.ParentNode.SelectSingleNode(XPaths.defaultAssemblyRequestElement, nsmgr);

            if (defaultAssemblyRequestElement == null)
            {
                defaultAssemblyRequestElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.defaultAssemblyRequestElement), XmlNamespaces.asmv2);
                permissionSetElement.ParentNode.AppendChild(defaultAssemblyRequestElement);
            }
            XmlAttribute idrefAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.permissionSetReferenceAttribute));

            if (idrefAttribute == null)
            {
                idrefAttribute = document.CreateAttribute(XmlUtil.TrimPrefix(XPaths.permissionSetReferenceAttribute));
                defaultAssemblyRequestElement.Attributes.Append(idrefAttribute);
            }

            if (!String.Equals(idAttribute.Value, idrefAttribute.Value, StringComparison.Ordinal))
            {
                idrefAttribute.Value = idAttribute.Value;
            }
        }
Exemple #14
0
        private void ReadTrustInfo(string xml)
        {
            this._inputTrustInfoDocument = new XmlDocument();
            this._inputTrustInfoDocument.LoadXml(xml);
            XmlElement   inputPermissionSetElement = this.GetInputPermissionSetElement();
            XmlAttribute namedItem = (XmlAttribute)inputPermissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));

            this._isFullTrust = (namedItem != null) && bool.Parse(namedItem.Value);
            XmlAttribute attribute2 = (XmlAttribute)inputPermissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:SameSite"));

            if (attribute2 != null)
            {
                this.sameSiteSetting = attribute2.Value;
            }
        }
Exemple #15
0
        private void AddSameSiteAttribute(XmlElement permissionSetElement)
        {
            XmlAttribute sameSiteAttribute = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.sameSiteAttribute));

            if (sameSiteAttribute == null)
            {
                sameSiteAttribute = permissionSetElement.OwnerDocument.CreateAttribute(XmlUtil.TrimPrefix(XPaths.sameSiteAttribute));
                permissionSetElement.Attributes.Append(sameSiteAttribute);
            }

            sameSiteAttribute.Value = _sameSiteSetting;
        }
Exemple #16
0
        private static XmlElement GetRequestedPrivilegeElement(XmlElement inputRequestedPrivilegeElement, XmlDocument document)
        {
            // <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            //      <!--
            //          UAC Manifest Options
            //          If you want to change the Windows User Account Control level replace the
            //          requestedExecutionLevel node with one of the following .
            //          <requestedExecutionLevel  level="asInvoker" />
            //          <requestedExecutionLevel  level="requireAdministrator" />
            //          <requestedExecutionLevel  level="highestAvailable" />
            //          If you want to utilize File and Registry Virtualization for backward compatibility
            //          delete the requestedExecutionLevel node.
            //      -->
            //      <requestedExecutionLevel level="asInvoker" />
            //  </requestedPrivileges>

            // we always create a requestedPrivilege node to put into the generated TrustInfo document
            //
            XmlElement requestedPrivilegeElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.requestedPrivilegeElement), XmlNamespaces.asmv3);

            document.AppendChild(requestedPrivilegeElement);

            // our three cases we need to handle are:
            //  (a) no requestedPrivilege node (and therefore no requestedExecutionLevel node as well) - use default values
            //  (b) requestedPrivilege node and no requestedExecutionLevel node - omit the requestedExecutionLevel node
            //  (c) requestedPrivilege node and requestedExecutionLevel node - use the incoming requestedExecutionLevel node values
            //
            // using null for both values is case (b) above -- do not output values
            //
            string executionLevelString    = null;
            string executionUIAccessString = null;
            string commentString           = null;

            // case (a) above -- load default values
            //
            if (inputRequestedPrivilegeElement == null)
            {
                // If UAC requestedPrivilege node is missing (possibly due to upgraded project) then automatically
                //  add a default UAC requestedPrivilege node with a default requestedExecutionLevel node set to
                //  the expected ClickOnce level (asInvoker) with uiAccess as false
                //
                executionLevelString    = Constants.UACAsInvoker;
                executionUIAccessString = Constants.UACUIAccess;

                // load up a default comment string that we put in front of the requestedExecutionLevel node
                //  here so we can allow the passed-in node to override it if there is a comment present
                //
                System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Microsoft.Build.Tasks.Core.Strings.ManifestUtilities", typeof(SecurityUtilities).Module.Assembly);
                commentString = resources.GetString("TrustInfo.RequestedExecutionLevelComment");
            }
            else
            {
                // we need to see if the requestedExecutionLevel node is present to decide whether or not to create one.
                //
                XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable);
                XmlElement          inputRequestedExecutionLevel = (XmlElement)inputRequestedPrivilegeElement.SelectSingleNode(XPaths.requestedExecutionLevelElement, nsmgr);

                // case (c) above -- use incoming values [note that we should do nothing for case (b) above
                //  because the default values will make us not emit the requestedExecutionLevel and comment]
                //
                if (inputRequestedExecutionLevel != null)
                {
                    XmlNode previousNode = inputRequestedExecutionLevel.PreviousSibling;

                    // fetch the current comment node if there is one (if there is not one, simply
                    //  keep the default null value which means we will not create one in the
                    //  output document)
                    //
                    if (previousNode?.NodeType == XmlNodeType.Comment)
                    {
                        commentString = ((XmlComment)previousNode).Data;
                    }

                    // fetch the current requestedExecutionLevel node's level attribute if there is one
                    //
                    if (inputRequestedExecutionLevel.HasAttribute("level"))
                    {
                        executionLevelString = inputRequestedExecutionLevel.GetAttribute("level");
                    }

                    // fetch the current requestedExecutionLevel node's uiAccess attribute if there is one
                    //
                    if (inputRequestedExecutionLevel.HasAttribute("uiAccess"))
                    {
                        executionUIAccessString = inputRequestedExecutionLevel.GetAttribute("uiAccess");
                    }
                }
            }

            if (commentString != null)
            {
                XmlComment requestedPrivilegeComment = document.CreateComment(commentString);
                requestedPrivilegeElement.AppendChild(requestedPrivilegeComment);
            }

            if (executionLevelString != null)
            {
                XmlElement requestedExecutionLevelElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.requestedExecutionLevelElement), XmlNamespaces.asmv3);
                requestedPrivilegeElement.AppendChild(requestedExecutionLevelElement);

                XmlAttribute levelAttribute = document.CreateAttribute("level");
                levelAttribute.Value = executionLevelString;
                requestedExecutionLevelElement.Attributes.Append(levelAttribute);

                if (executionUIAccessString != null)
                {
                    XmlAttribute uiAccessAttribute = document.CreateAttribute("uiAccess");
                    uiAccessAttribute.Value = executionUIAccessString;
                    requestedExecutionLevelElement.Attributes.Append(uiAccessAttribute);
                }
            }

            return(requestedPrivilegeElement);
        }
Exemple #17
0
        private void FixupPermissionSetElement(XmlElement permissionSetElement)
        {
            XmlDocument         ownerDocument    = permissionSetElement.OwnerDocument;
            XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(ownerDocument.NameTable);

            if (this._preserveFullTrustPermissionSet)
            {
                XmlAttribute node = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                if (this._isFullTrust)
                {
                    if (node == null)
                    {
                        node = ownerDocument.CreateAttribute(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                        permissionSetElement.Attributes.Append(node);
                    }
                    node.Value = "true";
                }
                else if (node != null)
                {
                    permissionSetElement.Attributes.RemoveNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                }
            }
            else if (this._isFullTrust)
            {
                XmlAttribute attribute2 = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                if (attribute2 == null)
                {
                    attribute2 = ownerDocument.CreateAttribute(XmlUtil.TrimPrefix("asmv2:Unrestricted"));
                    permissionSetElement.Attributes.Append(attribute2);
                }
                attribute2.Value = "true";
                while (permissionSetElement.FirstChild != null)
                {
                    permissionSetElement.RemoveChild(permissionSetElement.FirstChild);
                }
            }
            XmlAttribute namedItem = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:ID"));

            if (namedItem == null)
            {
                namedItem = ownerDocument.CreateAttribute(XmlUtil.TrimPrefix("asmv2:ID"));
                permissionSetElement.Attributes.Append(namedItem);
            }
            if (string.IsNullOrEmpty(namedItem.Value))
            {
                namedItem.Value = "Custom";
            }
            this.AddSameSiteAttribute(permissionSetElement);
            if ((permissionSetElement.ParentNode != null) && (permissionSetElement.ParentNode.NodeType != XmlNodeType.Document))
            {
                XmlAttribute attribute4 = null;
                XmlElement   newChild   = (XmlElement)permissionSetElement.ParentNode.SelectSingleNode("asmv2:defaultAssemblyRequest", namespaceManager);
                if (newChild == null)
                {
                    newChild = ownerDocument.CreateElement(XmlUtil.TrimPrefix("asmv2:defaultAssemblyRequest"), "urn:schemas-microsoft-com:asm.v2");
                    permissionSetElement.ParentNode.AppendChild(newChild);
                }
                attribute4 = (XmlAttribute)permissionSetElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix("asmv2:permissionSetReference"));
                if (attribute4 == null)
                {
                    attribute4 = ownerDocument.CreateAttribute(XmlUtil.TrimPrefix("asmv2:permissionSetReference"));
                    newChild.Attributes.Append(attribute4);
                }
                if (string.Compare(namedItem.Value, attribute4.Value, StringComparison.Ordinal) != 0)
                {
                    attribute4.Value = namedItem.Value;
                }
            }
        }
Exemple #18
0
        internal static void UpdateEntryPoint(string inputPath, string outputPath, string updatedApplicationPath, string applicationManifestPath, string targetFrameworkVersion)
        {
            XmlDocument       document   = new XmlDocument();
            XmlReaderSettings xrSettings = new XmlReaderSettings();

            xrSettings.DtdProcessing = DtdProcessing.Ignore;
            using (XmlReader xr = XmlReader.Create(inputPath, xrSettings))
            {
                document.Load(xr);
            }
            XmlNamespaceManager nsmgr       = XmlNamespaces.GetNamespaceManager(document.NameTable);
            AssemblyIdentity    appManifest = AssemblyIdentity.FromManifest(applicationManifestPath);

            // update path to application manifest
            XmlNode codeBaseNode = null;

            foreach (string xpath in XPaths.codebasePaths)
            {
                codeBaseNode = document.SelectSingleNode(xpath, nsmgr);
                if (codeBaseNode != null)
                {
                    break;
                }
            }
            if (codeBaseNode == null)
            {
                throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "XPath not found: {0}", XPaths.codebasePaths[0]));
            }

            codeBaseNode.Value = updatedApplicationPath;

            // update Public key token of application manifest
            XmlNode publicKeyTokenNode = ((XmlAttribute)codeBaseNode).OwnerElement.SelectSingleNode(XPaths.dependencyPublicKeyTokenAttribute, nsmgr);

            if (publicKeyTokenNode == null)
            {
                throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "XPath not found: {0}", XPaths.dependencyPublicKeyTokenAttribute));
            }

            publicKeyTokenNode.Value = appManifest.PublicKeyToken;

            // update hash of application manifest
            string hash;
            long   size;

            Util.GetFileInfo(applicationManifestPath, targetFrameworkVersion, out hash, out size);

            // Hash node may not be present with optional signing
            XmlNode hashNode = ((XmlAttribute)codeBaseNode).OwnerElement.SelectSingleNode(XPaths.hashElement, nsmgr);

            if (hashNode != null)
            {
                ((XmlElement)hashNode).InnerText = hash;
            }

            // update file size of application manifest
            XmlAttribute sizeAttribute = ((XmlAttribute)codeBaseNode).OwnerElement.Attributes[XmlUtil.TrimPrefix(XPaths.fileSizeAttribute)];

            if (sizeAttribute == null)
            {
                throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "XPath not found: {0}", XPaths.fileSizeAttribute));
            }

            sizeAttribute.Value = size.ToString(System.Globalization.CultureInfo.InvariantCulture);

            document.Save(outputPath);
        }
Exemple #19
0
        /// <summary>
        /// Updates an existing ClickOnce application manifest with the specified trust.
        /// </summary>
        /// <param name="input">Specifies an input stream.</param>
        /// <param name="output">Specifies an output stream.</param>
        public void WriteManifest(Stream input, Stream output)
        {
            int               t1         = Environment.TickCount;
            XmlDocument       document   = new XmlDocument();
            XmlReaderSettings xrSettings = new XmlReaderSettings();

            xrSettings.DtdProcessing = DtdProcessing.Ignore;
            using (XmlReader xr = XmlReader.Create(input, xrSettings))
            {
                document.Load(xr);
            }
            XmlNamespaceManager nsmgr           = XmlNamespaces.GetNamespaceManager(document.NameTable);
            XmlElement          assemblyElement = (XmlElement)document.SelectSingleNode(XPaths.assemblyElement, nsmgr);

            if (assemblyElement == null)
            {
                throw new BadImageFormatException();
            }

            XmlElement trustInfoElement = (XmlElement)assemblyElement.SelectSingleNode(XPaths.trustInfoElement, nsmgr);

            if (trustInfoElement == null)
            {
                trustInfoElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.trustInfoElement), XmlNamespaces.asmv2);
                assemblyElement.AppendChild(trustInfoElement);
            }

            // If we have an input trustinfo document and no output specified then just copy the input to the output
            if (_inputTrustInfoDocument != null && _outputPermissionSet == null && !_sameSiteChanged)
            {
                XmlElement newTrustInfoElement = (XmlElement)document.ImportNode(_inputTrustInfoDocument.DocumentElement, true);
                trustInfoElement.ParentNode.ReplaceChild(newTrustInfoElement, trustInfoElement);
            }
            else
            {
                XmlElement securityElement = (XmlElement)trustInfoElement.SelectSingleNode(XPaths.securityElement, nsmgr);
                if (securityElement == null)
                {
                    securityElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.securityElement), XmlNamespaces.asmv2);
                    trustInfoElement.AppendChild(securityElement);
                }
                XmlElement applicationRequestMinimumElement = (XmlElement)securityElement.SelectSingleNode(XPaths.applicationRequestMinimumElement, nsmgr);
                if (applicationRequestMinimumElement == null)
                {
                    applicationRequestMinimumElement = document.CreateElement(XmlUtil.TrimPrefix(XPaths.applicationRequestMinimumElement), XmlNamespaces.asmv2);
                    securityElement.AppendChild(applicationRequestMinimumElement);
                }

                XmlNodeList permissionSetNodes = applicationRequestMinimumElement.SelectNodes(XPaths.permissionSetElement, nsmgr);
                foreach (XmlNode permissionSetNode in permissionSetNodes)
                {
                    applicationRequestMinimumElement.RemoveChild(permissionSetNode);
                }

                XmlDocument permissionSetDocument = GetOutputPermissionSetDocument();
                XmlElement  permissionSetElement  = (XmlElement)document.ImportNode(permissionSetDocument.DocumentElement, true);
                applicationRequestMinimumElement.AppendChild(permissionSetElement);
                FixupPermissionSetElement(permissionSetElement);
            }

            // Truncate any contents that may be in the file
            if (output.Length > 0)
            {
                output.SetLength(0);
                output.Flush();
            }
            document.Save(output);
            Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "ManifestWriter.WriteTrustInfo t={0}", Environment.TickCount - t1));
        }
Exemple #20
0
        private void ReadTrustInfo(string xml)
        {
            _inputTrustInfoDocument = new XmlDocument();
            // CA3057: DoNotUseLoadXml. Suppressed since the suggested fix is to use XmlReader.
            // XmlReader.Create(string) requires an URI. Whereas the input parameter 'xml' is file content and not a path.
            _inputTrustInfoDocument.LoadXml(xml);
            XmlElement   psElement             = GetInputPermissionSetElement();
            XmlAttribute unrestrictedAttribute = (XmlAttribute)psElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.unrestrictedAttribute));

            _isFullTrust = unrestrictedAttribute != null && Boolean.Parse(unrestrictedAttribute.Value);
            XmlAttribute sameSiteAttribute = (XmlAttribute)psElement.Attributes.GetNamedItem(XmlUtil.TrimPrefix(XPaths.sameSiteAttribute));

            if (sameSiteAttribute != null)
            {
                _sameSiteSetting = sameSiteAttribute.Value;
            }
        }