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); }
internal static ComInfo[] GetComInfo(string path) { XmlDocument document = GetXmlDocument(path); XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); string manifestFileName = Path.GetFileName(path); var comInfoList = new List <ComInfo>(); XmlNodeList comNodes = document.SelectNodes(XPaths.comFilesPath, nsmgr); foreach (XmlNode comNode in comNodes) { XmlNode nameNode = comNode.SelectSingleNode(XPaths.fileNameAttribute, nsmgr); string componentFileName = nameNode?.Value; XmlNodeList clsidNodes = comNode.SelectNodes(XPaths.clsidAttribute, nsmgr); foreach (XmlNode clsidNode in clsidNodes) { comInfoList.Add(new ComInfo(manifestFileName, componentFileName, clsidNode.Value, null)); } XmlNodeList tlbidNodes = comNode.SelectNodes(XPaths.tlbidAttribute, nsmgr); foreach (XmlNode tlbidNode in tlbidNodes) { comInfoList.Add(new ComInfo(manifestFileName, componentFileName, null, tlbidNode.Value)); } } return(comInfoList.ToArray()); }
private void ValidateConfig() { if (String.IsNullOrEmpty(ConfigFile)) { return; } FileReference configFile = FileReferences.FindTargetPath(ConfigFile); if (configFile == null) { return; } if (!TrustInfo.IsFullTrust) { XmlDocument document = new XmlDocument(); XmlReaderSettings xrs = new XmlReaderSettings(); xrs.DtdProcessing = DtdProcessing.Ignore; using (XmlReader xr = XmlReader.Create(configFile.ResolvedPath, xrs)) { document.Load(xr); } XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlNodeList nodes = document.SelectNodes(XPaths.configBindingRedirect, nsmgr); if (nodes.Count > 0) { OutputMessages.AddWarningMessage("GenerateManifest.ConfigBindingRedirectsWithPartialTrust"); } } }
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); }
private static AssemblyIdentity FromManifest(XmlDocument document) { XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement element = (XmlElement)document.SelectSingleNode("/asmv1:assembly/asmv1:assemblyIdentity|/asmv1:assembly/asmv2:assemblyIdentity", namespaceManager); if (element == null) { return(null); } XmlNode namedItem = element.Attributes.GetNamedItem("name"); string name = (namedItem != null) ? namedItem.Value : null; namedItem = element.Attributes.GetNamedItem("version"); string version = (namedItem != null) ? namedItem.Value : null; namedItem = element.Attributes.GetNamedItem("publicKeyToken"); string publicKeyToken = (namedItem != null) ? namedItem.Value : null; namedItem = element.Attributes.GetNamedItem("language"); string culture = (namedItem != null) ? namedItem.Value : null; namedItem = element.Attributes.GetNamedItem("processorArchitecture"); string processorArchitecture = (namedItem != null) ? namedItem.Value : null; namedItem = element.Attributes.GetNamedItem("type"); return(new AssemblyIdentity(name, version, publicKeyToken, culture, processorArchitecture, (namedItem != null) ? namedItem.Value : null)); }
private XmlElement GetInputRequestedPrivilegeElement() { if (_inputTrustInfoDocument == null) { return(null); } XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(_inputTrustInfoDocument.NameTable); XmlElement trustInfoElement = _inputTrustInfoDocument.DocumentElement; if (trustInfoElement == null) { return(null); } XmlElement securityElement = (XmlElement)trustInfoElement.SelectSingleNode(XPaths.securityElement, nsmgr); if (securityElement == null) { return(null); } XmlElement requestedPrivilegeElement = (XmlElement)securityElement.SelectSingleNode(XPaths.requestedPrivilegeElement, nsmgr); if (requestedPrivilegeElement == null) { return(null); } return(requestedPrivilegeElement); }
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); }
private XmlElement GetInputRequestedPrivilegeElement() { if (this._inputTrustInfoDocument == null) { return(null); } XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(this._inputTrustInfoDocument.NameTable); XmlElement documentElement = this._inputTrustInfoDocument.DocumentElement; if (documentElement == null) { return(null); } XmlElement element2 = (XmlElement)documentElement.SelectSingleNode("asmv2:security", namespaceManager); if (element2 == null) { return(null); } XmlElement element3 = (XmlElement)element2.SelectSingleNode("asmv3:requestedPrivileges", namespaceManager); if (element3 == null) { return(null); } return(element3); }
private static AssemblyIdentity FromManifest(XmlDocument document) { XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement element = (XmlElement)document.SelectSingleNode(XPaths.assemblyIdentityPath, nsmgr); if (element == null) { return(null); } XmlNode node = element.Attributes.GetNamedItem("name"); string name = node != null ? node.Value : null; node = element.Attributes.GetNamedItem("version"); string version = node != null ? node.Value : null; node = element.Attributes.GetNamedItem("publicKeyToken"); string publicKeyToken = node != null ? node.Value : null; node = element.Attributes.GetNamedItem("language"); string culture = node != null ? node.Value : null; node = element.Attributes.GetNamedItem("processorArchitecture"); string processorArchitecture = node != null ? node.Value : null; node = element.Attributes.GetNamedItem("type"); string type = node != null ? node.Value : null; return(new AssemblyIdentity(name, version, publicKeyToken, culture, processorArchitecture, type)); }
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); }
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 })); }
private XmlElement GetInputPermissionSetElement() { if (_inputTrustInfoDocument == null) { _inputTrustInfoDocument = new XmlDocument(); XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(_inputTrustInfoDocument.NameTable); XmlElement trustInfoElement = _inputTrustInfoDocument.CreateElement(XmlUtil.TrimPrefix(XPaths.trustInfoElement), XmlNamespaces.asmv2); _inputTrustInfoDocument.AppendChild(trustInfoElement); } return(GetPermissionSetElement(_inputTrustInfoDocument)); }
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)); }
private void Read(Stream s, string xpath) { this.Clear(); XmlDocument document = new XmlDocument(); document.Load(s); XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement element = (XmlElement)document.SelectSingleNode(xpath, namespaceManager); if (element != null) { this.ReadTrustInfo(element.OuterXml); } }
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); }
private void ValidateConfig() { if (!string.IsNullOrEmpty(this.ConfigFile)) { FileReference reference = base.FileReferences.FindTargetPath(this.ConfigFile); if ((reference != null) && !this.TrustInfo.IsFullTrust) { XmlDocument document = new XmlDocument(); document.Load(reference.ResolvedPath); XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(document.NameTable); if (document.SelectNodes("configuration/runtime/asmv1:assemblyBinding/asmv1:dependentAssembly/asmv1:bindingRedirect", namespaceManager).Count > 0) { base.OutputMessages.AddWarningMessage("GenerateManifest.ConfigBindingRedirectsWithPartialTrust", new string[0]); } } } }
private void Read(Stream s, string xpath) { Clear(); XmlDocument document = new XmlDocument(); XmlReaderSettings xrSettings = new XmlReaderSettings(); xrSettings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader xr = XmlReader.Create(s, xrSettings)) { document.Load(xr); } XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(document.NameTable); XmlElement trustInfoElement = (XmlElement)document.SelectSingleNode(xpath, nsmgr); if (trustInfoElement == null) { return; // no trustInfo element is okay } ReadTrustInfo(trustInfoElement.OuterXml); }
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]); }
internal static ComInfo[] GetComInfo(string path) { XmlDocument xmlDocument = GetXmlDocument(path); XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(xmlDocument.NameTable); string fileName = Path.GetFileName(path); List <ComInfo> list = new List <ComInfo>(); foreach (XmlNode node in xmlDocument.SelectNodes("/asmv1:assembly/asmv1:file[asmv1:typelib or asmv1:comClass]", namespaceManager)) { XmlNode node2 = node.SelectSingleNode("@name", namespaceManager); string componentFileName = (node2 != null) ? node2.Value : null; foreach (XmlNode node3 in node.SelectNodes("asmv1:comClass/@clsid", namespaceManager)) { list.Add(new ComInfo(fileName, componentFileName, node3.Value, null)); } foreach (XmlNode node4 in node.SelectNodes("asmv1:typelib/@tlbid", namespaceManager)) { list.Add(new ComInfo(fileName, componentFileName, null, node4.Value)); } } return(list.ToArray()); }
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); }
/// <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)); }
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; } } }
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); }
public static Stream Format(Stream input) { int t1 = Environment.TickCount; XmlTextReader r = new XmlTextReader(input); r.DtdProcessing = DtdProcessing.Ignore; r.WhitespaceHandling = WhitespaceHandling.None; XmlNamespaceManager nsmgr = XmlNamespaces.GetNamespaceManager(r.NameTable); MemoryStream m = new MemoryStream(); XmlTextWriter w = new XmlTextWriter(m, Encoding.UTF8); w.Formatting = Formatting.Indented; w.Indentation = 2; w.WriteStartDocument(); while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: w.WriteStartElement(r.Prefix, r.LocalName, r.NamespaceURI); if (r.HasAttributes) { string elementQName = XmlUtil.GetQName(r, nsmgr); for (int i = 0; i < r.AttributeCount; ++i) { r.MoveToAttribute(i); string attributeQName = XmlUtil.GetQName(r, nsmgr); string xpath = elementQName + "/@" + attributeQName; // Filter out language="*" if ((xpath.Equals(XPaths.languageAttribute1, StringComparison.Ordinal) || xpath.Equals(XPaths.languageAttribute2, StringComparison.Ordinal)) && String.Equals(r.Value, "*", StringComparison.Ordinal)) { continue; } // Filter out attributes with empty values if attribute is on the list... if (String.IsNullOrEmpty(r.Value) && Array.BinarySearch(XPaths.emptyAttributeList, xpath) >= 0) { continue; } w.WriteAttributeString(r.Prefix, r.LocalName, r.NamespaceURI, r.Value); } r.MoveToElement(); //Moves the reader back to the element node. } if (r.IsEmptyElement) { w.WriteEndElement(); } break; case XmlNodeType.EndElement: w.WriteEndElement(); break; case XmlNodeType.Comment: w.WriteComment(r.Value); break; case XmlNodeType.CDATA: w.WriteCData(r.Value); break; case XmlNodeType.Text: w.WriteString(r.Value); break; } } w.WriteEndDocument(); w.Flush(); m.Position = 0; Util.WriteLog(String.Format(CultureInfo.CurrentCulture, "ManifestWriter.Format t={0}", Environment.TickCount - t1)); return(m); }
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; } }
public static Stream Format(Stream input) { int tickCount = Environment.TickCount; XmlTextReader r = new XmlTextReader(input) { WhitespaceHandling = WhitespaceHandling.None }; XmlNamespaceManager namespaceManager = XmlNamespaces.GetNamespaceManager(r.NameTable); MemoryStream w = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(w, Encoding.UTF8) { Formatting = Formatting.Indented, Indentation = 2 }; writer.WriteStartDocument(); while (r.Read()) { string str; int num2; switch (r.NodeType) { case XmlNodeType.Element: writer.WriteStartElement(r.Prefix, r.LocalName, r.NamespaceURI); if (!r.HasAttributes) { goto Label_0162; } str = XmlUtil.GetQName(r, namespaceManager); num2 = 0; goto Label_014E; case XmlNodeType.Attribute: { continue; } case XmlNodeType.Text: { writer.WriteString(r.Value); continue; } case XmlNodeType.CDATA: { writer.WriteCData(r.Value); continue; } case XmlNodeType.Comment: { writer.WriteComment(r.Value); continue; } case XmlNodeType.EndElement: { writer.WriteEndElement(); continue; } default: { continue; } } Label_00BB: r.MoveToAttribute(num2); string qName = XmlUtil.GetQName(r, namespaceManager); string str3 = str + "/@" + qName; if (((!str3.Equals("asmv1:assemblyIdentity/@language", StringComparison.Ordinal) && !str3.Equals("asmv2:assemblyIdentity/@language", StringComparison.Ordinal)) || !string.Equals(r.Value, "*", StringComparison.Ordinal)) && (!string.IsNullOrEmpty(r.Value) || (Array.BinarySearch <string>(XPaths.emptyAttributeList, str3) < 0))) { writer.WriteAttributeString(r.Prefix, r.LocalName, r.NamespaceURI, r.Value); } num2++; Label_014E: if (num2 < r.AttributeCount) { goto Label_00BB; } r.MoveToElement(); Label_0162: if (r.IsEmptyElement) { writer.WriteEndElement(); } } writer.WriteEndDocument(); writer.Flush(); w.Position = 0L; Util.WriteLog(string.Format(CultureInfo.CurrentCulture, "ManifestWriter.Format t={0}", new object[] { Environment.TickCount - tickCount })); return(w); }