c# - 如何从 clickonce 部署 list xml 文件中获取证书信息

标签 c# .net xml xpath xmldocument

我正在尝试解析 xml 文件以从 ClickOnce list 获取证书信息。 我需要的是 X509Certificate 信息。 示例文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
  <asmv1:assemblyIdentity name="someName.Xbap.exe" version="2.5.18.1" publicKeyToken="3i3cc7f44s0b9526" language="neutral" processorArchitecture="msil" type="win32" />
  <application />
  <entryPoint>
    <assemblyIdentity name="someName.AFW.Xbap" version="2.5.18.1" language="neutral" processorArchitecture="msil" />
    <commandLine file="someName.AFW.Xbap.exe" parameters="" />
    <hostInBrowser xmlns="urn:schemas-microsoft-com:asm.v3" />
  </entryPoint>
  <publisherIdentity name="CN=HOSTNAME" issuerKeyHash="4534734c4984227c4fa0asdd4eb114524aaed397" />
  <Signature Id="StrongNameSignature" xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
          <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>MVvHBmUFm2j7PwKjbzig0y7jdBo=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>someRandomSignatureValue</SignatureValue>
    <KeyInfo Id="StrongNameKeyInfo">
      <KeyValue>
        <RSAKeyValue>
          <Modulus>someRandomModulusValue</Modulus>
          <Exponent>AQAB</Exponent>
        </RSAKeyValue>
      </KeyValue>
      <msrel:RelData xmlns:msrel="http://schemas.microsoft.com/windows/rel/2005/reldata">
        <r:license xmlns:r="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:as="http://schemas.microsoft.com/windows/pki/2005/Authenticode">
          <r:grant>
            <as:ManifestInformation Hash="manifesthash" Description="" Url="">
              <as:assemblyIdentity name="Somename.Xbap.exe" version="1.0.18.51" publicKeyToken="3b3bc7b44b4b8810" language="neutral" processorArchitecture="msil" type="win32" />
            </as:ManifestInformation>
            <as:SignedBy />
            <as:AuthenticodePublisher>
              <as:X509SubjectName>CN=HostName</as:X509SubjectName>
            </as:AuthenticodePublisher>
          </r:grant>
          <r:issuer>
            <Signature Id="AuthenticodeSignature" xmlns="http://www.w3.org/2000/09/xmldsig#">
              <SignedInfo>
                <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                <Reference URI="">
                  <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                  </Transforms>
                  <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                  <DigestValue>asdasda+asdaasdasdad=</DigestValue>
                </Reference>
              </SignedInfo>
              <SignatureValue>someRandomSignatureValue==</SignatureValue>
              <KeyInfo>
                <KeyValue>
                  <RSAKeyValue>
                    <Modulus>someRandomSignatureValue</Modulus>
                    <Exponent>AQAB</Exponent>
                  </RSAKeyValue>
                </KeyValue>
                <X509Data>
                  <X509Certificate>!!!this is the required certificate information!!!</X509Certificate>
                </X509Data>
              </KeyInfo>
            </Signature>
          </r:issuer>
        </r:license>
      </msrel:RelData>
    </KeyInfo>
  </Signature>
</asmv1:assembly>

我试着像这样解析它:

XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(filePath);
    XmlNode securityNode = xmlDoc.SelectSingleNode("/Signature/KeyInfo/msrel:RelData/r:license/r:issuer/Signature/KeyInfo/X509Data/X509Certificate");

当我执行它时,我得到XPathException

我还尝试了一种命名空间变体。

最佳答案

这些是您错过的一些步骤。首先,您需要使用 XmlNamespaceManager 来注册 prefix-to-namespaceUri 映射:

var nsMapping = new XmlNamespaceManager(xmlDoc.NameTable);
nsMapping.AddNamespace("msrel", "http://schemas.microsoft.com/windows/rel/2005/reldata");
nsMapping.AddNamespace("r", "urn:mpeg:mpeg21:2003:01-REL-R-NS");

其次,除了上面2个命名空间,你还需要注册默认的命名空间(声明没有前缀的命名空间)用于XPath:

nsMapping.AddNamespace("d", "http://www.w3.org/2000/09/xmldsig#");

第三,除了在 XPath 中正确使用已注册的前缀之外,将命名空间管理器作为 SelectSingleNode() 的第二个参数传递:

var xpath = "//d:Signature/d:KeyInfo/msrel:RelData/r:license/r:issuer/d:Signature/d:KeyInfo/d:X509Data/d:X509Certificate";
XmlNode securityNode = xmlDoc.SelectSingleNode(xpath, nsMapping);

如您在问题中提到的,缺少上述特别步骤将触发 XPathException。顺便说一句,以下更简单的 XPath 也适用于这种情况:

var xpath = "//d:X509Certificate";

关于c# - 如何从 clickonce 部署 list xml 文件中获取证书信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29095299/

相关文章:

c# - ListView 中的项目没有行缩进

java - "no value for rss",虽然它显然在那里

java - 如何在 Android Imageview 中将 xml 中的一种颜色替换为另一种颜色(从黑到白)?

c# - 如何循环从同一个数组中删除一个值的所有实例?

c# - Asp.net MVC Ajax 调用不调用 Controller 方法

c# - 计算 MP3 帧的长度(以毫秒为单位)

c# - 本地主机永远等待 ASP.NET 5 应用程序模板项目

c# - TcpClient - 现有连接被远程主机强行关闭

c# - 尝试将对象与 EntityFramework 关联时出现无效列名错误

java - 在根元素之前序列化 XML 处理指令