【问题标题】:Validate signature on signed XML验证签名 XML 上的签名
【发布时间】:2019-11-21 00:08:56
【问题描述】:

我已成功修改 this code 以在 XML 文件上签名和验证签名。但是我在没有安装证书的机器上验证签名时遇到了麻烦。我发现thisMicrosoft 参考在 C# 中执行此操作,但这似乎也需要安装 Cert。或者“密钥容器”只是一个使用相同证书签名的可执行文件?有人可以使用 PowerShell 而不是 C# 指向我的资源,因为我的 C# 技能很弱! 似乎应该可以在没有安装证书的情况下验证 XML 上的签名。手指交叉。

【问题讨论】:

  • 请更新您的问题,以准确包含您迄今为止尝试过的内容以及您遇到的具体问题?为此目的使用链接是不好的,因为当链接过期时它们会变得无用:)
  • 公钥签名验证的基础是……嗯,任何用于签名的密钥的 公钥 部分 - 证书是一种超级常用的身份验证方式并分发公钥!因此,您要么需要公开证书(没有私钥),要么将公钥材料与签名文档一起导出并发送
  • 有趣。我假设在使用 PowerShell 代码签名时,Windows 只是自动从签名的 PS1 中提取公钥,而使用 XML 我需要自己做这件事吗?我将考虑使用 Get-AuthenticodeSignature,因为在我的情况下,我确实有一个签名的可执行文件可供参考。我想问题真的是证书被捆绑到可执行文件中,而 XML 不是?

标签: xml powershell signed


【解决方案1】:

我无法在未安装证书的机器上验证签名。

有道理!

公钥签名验证的基础是……嗯,任何密钥的 公钥 部分用于签署某些东西 - 而证书是验证和分发公钥的超级常用方法!

因此,您要么需要公开证书(没有私钥),要么将公钥材料与签名文档一起导出和发送。

我们可以更新Sign-XML 例程,将证书本身复制到 xml 文档中的签名 blob 中,如下所示:

function Sign-XML {
    param(
      [xml]$xml, 
      [System.Security.Cryptography.X509Certificates.X509Certificate2]$certificate
    )

    [System.Security.Cryptography.xml.SignedXml]$signedXml = $NULL
    $signedXml = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $xml

    # Grab the PrivateKey from the cert - fail if not present
    if(-not $certificate.HasPrivateKey){
        throw 'Private Key not passed to Sign-XML, cannot sign document'
        return
    }
    $signedXml.SigningKey = $certificate.PrivateKey

    # Add a KeyInfo blob to the SignedXml element
    # KeyInfo blob will hold the certificate and therefore the public key
    $keyInfo = New-Object System.Security.Cryptography.Xml.KeyInfo
    $keyInfo.AddClause((New-Object System.Security.Cryptography.Xml.KeyInfoX509Data -ArgumentList $certificate))
    $signedXml.KeyInfo = $keyInfo

    $Reference = New-Object System.Security.Cryptography.Xml.Reference
    $Reference.Uri = ""

    $env = New-Object System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform

    $Reference.AddTransform($env);
    $signedXml.AddReference($Reference)
    $signedXml.ComputeSignature()

    [System.Xml.XmlElement]$xmlSignature = $signedXml.GetXml()
    #Add signature to end of xml file
    [void]$xml.DocumentElement.AppendChild($xml.ImportNode($xmlSignature, $true))

    if ($xml.FirstChild -is [system.xml.XmlDeclaration]) {
        [void]$xml.RemoveChild($xml.FirstChild);
    }
    $xml
}

这里需要注意两件事:

  1. 第二个参数现在采用 X509Certificate2 对象而不是原始密钥容器
  2. 我们在包含证书的文档中的签名信息中添加了KeyInfoX509Data 子句

既然证书是已签名文档的一部分,收件人可以轻松地以编程方式验证签名和公钥:

function Verify-XmlSignature {
    Param (
        [xml]$checkxml,
        [switch]$Force
    )

    # Grab signing certificate from document
    $rawCertBase64 = $signed.DocumentElement.Signature.KeyInfo.X509Data.X509Certificate

    if(-not $rawCertBase64){
        throw 'Unable to locate signing certificate in signed document'
        return
    }

    $rawCert = [convert]::FromBase64String($rawCertBase64)
    $signingCertificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @(,$rawCert)

    [System.Security.Cryptography.Xml.SignedXml]$signedXml = New-Object System.Security.Cryptography.Xml.SignedXml -ArgumentList $checkxml
    $XmlNodeList = $checkxml.GetElementsByTagName("Signature")
    $signedXml.LoadXml([System.Xml.XmlElement] ($XmlNodeList[0]))
    return $signedXml.CheckSignature($signingCertificate, $Force)
}

将$Force 传递给CheckSignature() 的第二个参数意味着如果用户指定-Force,证书本身的信任验证不会影响验证例程的结果

【讨论】:

  • 谢谢。这将是我所需要的。我很好奇,在您的代码中,您引用了“system.security.cryptography.X509Certificates.X509Certificate2”。那和没有终端2有什么区别?我看到它以任何一种方式工作,这似乎很奇怪。
  • @Gordon 查看this answer - "2" 版本只是用更大的 API 表面扩展了第一个版本(包括 Verify() 方法和 HasPrivateKey 和 PrivateKey像这样的场景)。检查文档或[System.Security.Cryptography.X509Certificates.X509Certificate2].GetMembers()|Select Name,DeclaringType 的输出,看看它如何扩展第一个
  • 有趣。起初我以为我搞砸了复制/粘贴,所以我拔出 2 并且它仍然有效。然后我回到这里,我看到你有 2。我想明天还有更多要阅读的内容。 FWIW,X509Certificate2 似乎也快了一点,如果它是更晚的并且可能更优化的代码,这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2011-06-25
相关资源
最近更新 更多