我在调用 CryptUIWizDigitalSign 以使用我们的公共(public)代码签名证书以编程方式签署可执行文件时遇到问题,而没有显示任何 UI。该证书是 Comodo 代码签名证书。

当时间戳 URL 参数设置为 null 时,它工作正常,但每当我传入 null 以外的任何内容时,调用都会失败(返回零)。

问题是,没有时间戳,就没有副署署名,因此接下来会出现签名有效性问题。

环境是 Windows 7 x64。有一个有效的标准互联网连接。从嗅探网络流量看来,CryptUIWizDigitalSign 没有尝试联系时间戳服务器。

我通过 PInvoke 从 .NET 调用它,但我怀疑这会产生什么不同。

网上关于这个功能的并不多......

Dim cert As X509Certificate2 = New X509Certificate2("mycert.pfx", "password")
Dim pSigningCertContext As IntPtr = cert.Handle

Dim digitalSignInfo As CRYPTUI_WIZ_DIGITAL_SIGN_INFO
    = New CRYPTUI_WIZ_DIGITAL_SIGN_INFO
digitalSignInfo.dwSize = Marshal.SizeOf(digitalSignInfo)
digitalSignInfo.dwSubjectChoice = CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE
digitalSignInfo.pwszFileName = "C:\temp\installer.exe"
digitalSignInfo.dwSigningCertChoice = CRYPTUI_WIZ_DIGITAL_SIGN_CERT
digitalSignInfo.pSigningCertContext = pSigningCertContext
digitalSignInfo.pwszTimestampURL = "http://timestamp.comodoca.com/authenticode"
digitalSignInfo.dwAdditionalCertChoice = 0
digitalSignInfo.pSignExtInfo = IntPtr.Zero

If (Not CryptUIWizDigitalSign(CRYPTUI_WIZ_NO_UI, IntPtr.Zero, vbNullString,
                              digitalSignInfo, pSignContext)) Then
    Throw New Win32Exception(Marshal.GetLastWin32Error(),
        "CryptUIWizDigitalSign")
End If

CRYPTUI_WIZ_DIGITAL_SIGN_INFO 类型定义为:
<StructLayout(LayoutKind.Sequential)> _
Public Structure CRYPTUI_WIZ_DIGITAL_SIGN_INFO
    Public dwSize As Int32
    Public dwSubjectChoice As Int32
    <MarshalAs(UnmanagedType.LPWStr)> Public pwszFileName As String
    Public dwSigningCertChoice As Int32
    Public pSigningCertContext As IntPtr
    Public pwszTimestampURL As String
    Public dwAdditionalCertChoice As Int32
    Public pSignExtInfo As IntPtr
End Structure

Public Const CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE As Int32 = 1
Public Const CRYPTUI_WIZ_DIGITAL_SIGN_CERT As Int32 = 1
Public Const CRYPTUI_WIZ_NO_UI As Int32 = 1

最佳答案

您已经在 MarshalAs 上应用了 pwszFileName 属性,但没有在 pwszTimestampURL 上应用,有什么原因吗?它们在 documentation for CRYPTUI_WIZ_DIGITAL_SIGN_INFO 中的描述相同:

pwsz文件名:



pwszTimestampURL:

关于windows - 指定 pwszTimestampURL 时 CryptUIWizDigitalSign 失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11479190/

10-11 22:57